diff --git a/sisi/lifecycle.py b/sisi/lifecycle.py index 1b66573..93371be 100644 --- a/sisi/lifecycle.py +++ b/sisi/lifecycle.py @@ -38,7 +38,13 @@ def _copy_into(src, bdir, rel): dst = os.path.join(bdir, rel) os.makedirs(os.path.dirname(dst), exist_ok=True) if os.path.isdir(src): - shutil.copytree(src, dst, dirs_exist_ok=True) + try: + # lock files are not data, and git's background maintenance removes them while we copy + shutil.copytree(src, dst, dirs_exist_ok=True, ignore=shutil.ignore_patterns("*.lock", "gc.pid")) + except shutil.Error as e: + # a file that vanished while being copied (git housekeeping in the personal repository) is not a failure + if not all(isinstance(x, tuple) and "No such file or directory" in str(x[2]) for x in e.args[0]): + raise elif src.endswith(".db"): _sqlite_copy(src, dst) else: diff --git a/tests/test_cycle_hardening.py b/tests/test_cycle_hardening.py index 4cc13a1..0f5bb42 100644 --- a/tests/test_cycle_hardening.py +++ b/tests/test_cycle_hardening.py @@ -949,3 +949,28 @@ class Odds(Base): text = self.sisi("cycle").stdout self.assertLessEqual(len(text), 6000) self.assertEqual(text.count("FLAGGED NOT WORKING"), 8) + + +class Snapshot(Base): + """R-16-9 (item 4's repair runs inside `sisi update`, whose backup must not fail on git housekeeping).""" + + def test_a_file_that_vanishes_while_the_backup_copies_the_data_folder_is_not_a_failure(self): + import shutil, unittest.mock + from sisi import lifecycle + d = os.path.join(self.tmp, "src") + os.makedirs(os.path.join(d, "objects")) + for n in ("a", "b"): + with open(os.path.join(d, n), "w") as f: + f.write(n) + with open(os.path.join(d, "objects", "maintenance.lock"), "w") as f: + f.write("x") + real = shutil.copy2 + + def vanishing(src, dst, **kw): + if os.path.basename(src) == "b": + os.unlink(src) # removed by another process between listing and copying + return real(src, dst, **kw) + with unittest.mock.patch("shutil.copy2", vanishing): + lifecycle._copy_into(d, os.path.join(self.tmp, "bak"), "src") + self.assertTrue(os.path.exists(os.path.join(self.tmp, "bak", "src", "a"))) + self.assertFalse(os.path.exists(os.path.join(self.tmp, "bak", "src", "objects", "maintenance.lock")))