From 18439901722ea575db93b13b602218f82f93ec18 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 18 Jan 2023 15:14:04 +0100 Subject: [PATCH] use os.replace rather than os.rename more consistent behaviour on all OSes, incl. windows. --- src/borg/cache.py | 13 +++++++------ src/borg/testsuite/archiver/checks.py | 6 +++--- src/borg/testsuite/repository.py | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/borg/cache.py b/src/borg/cache.py index 90ded1e40..8c61d45c0 100644 --- a/src/borg/cache.py +++ b/src/borg/cache.py @@ -636,7 +636,7 @@ class LocalCache(CacheStatsMixin): except FileNotFoundError: with SaveFile(os.path.join(txn_dir, files_cache_name()), binary=True): pass # empty file - os.rename(os.path.join(self.path, "txn.tmp"), os.path.join(self.path, "txn.active")) + os.replace(txn_dir, os.path.join(self.path, "txn.active")) self.txn_active = True pi.finish() @@ -680,7 +680,7 @@ class LocalCache(CacheStatsMixin): self.cache_config.integrity["chunks"] = fd.integrity_data pi.output("Saving cache config") self.cache_config.save(self.manifest, self.key) - os.rename(os.path.join(self.path, "txn.active"), os.path.join(self.path, "txn.tmp")) + os.replace(os.path.join(self.path, "txn.active"), os.path.join(self.path, "txn.tmp")) shutil.rmtree(os.path.join(self.path, "txn.tmp")) self.txn_active = False pi.finish() @@ -696,9 +696,10 @@ class LocalCache(CacheStatsMixin): shutil.copy(os.path.join(txn_dir, "config"), self.path) shutil.copy(os.path.join(txn_dir, "chunks"), self.path) shutil.copy(os.path.join(txn_dir, discover_files_cache_name(txn_dir)), self.path) - os.rename(txn_dir, os.path.join(self.path, "txn.tmp")) - if os.path.exists(os.path.join(self.path, "txn.tmp")): - shutil.rmtree(os.path.join(self.path, "txn.tmp")) + txn_tmp = os.path.join(self.path, "txn.tmp") + os.replace(txn_dir, txn_tmp) + if os.path.exists(txn_tmp): + shutil.rmtree(txn_tmp) self.txn_active = False self._do_open() @@ -793,7 +794,7 @@ class LocalCache(CacheStatsMixin): except Exception: safe_unlink(fn_tmp) else: - os.rename(fn_tmp, fn) + os.replace(fn_tmp, fn) def read_archive_index(archive_id, archive_name): archive_chunk_idx_path = mkpath(archive_id) diff --git a/src/borg/testsuite/archiver/checks.py b/src/borg/testsuite/archiver/checks.py index 5d4a49ade..e3d297352 100644 --- a/src/borg/testsuite/archiver/checks.py +++ b/src/borg/testsuite/archiver/checks.py @@ -49,7 +49,7 @@ class ArchiverTestCase(ArchiverTestCaseBase): self.cmd(f"--repo={self.repository_location}_encrypted", "rcreate", RK_ENCRYPTION) self.cmd(f"--repo={self.repository_location}_encrypted", "create", "test", "input") shutil.rmtree(self.repository_path + "_encrypted") - os.rename(self.repository_path + "_unencrypted", self.repository_path + "_encrypted") + os.replace(self.repository_path + "_unencrypted", self.repository_path + "_encrypted") if self.FORK_DEFAULT: self.cmd(f"--repo={self.repository_location}_encrypted", "create", "test.2", "input", exit_code=EXIT_ERROR) else: @@ -82,7 +82,7 @@ class ArchiverTestCase(ArchiverTestCaseBase): self.cmd(f"--repo={self.repository_location}_unencrypted", "rdelete", "--cache-only") self.cmd(f"--repo={self.repository_location}_encrypted", "rdelete", "--cache-only") shutil.rmtree(self.repository_path + "_encrypted") - os.rename(self.repository_path + "_unencrypted", self.repository_path + "_encrypted") + os.replace(self.repository_path + "_unencrypted", self.repository_path + "_encrypted") if self.FORK_DEFAULT: self.cmd(f"--repo={self.repository_location}_encrypted", "create", "test.2", "input", exit_code=EXIT_ERROR) else: @@ -115,7 +115,7 @@ class ArchiverTestCase(ArchiverTestCaseBase): def test_repository_move(self): self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION) security_dir = self.get_security_dir() - os.rename(self.repository_path, self.repository_path + "_new") + os.replace(self.repository_path, self.repository_path + "_new") with environment_variable(BORG_RELOCATED_REPO_ACCESS_IS_OK="yes"): self.cmd(f"--repo={self.repository_location}_new", "rinfo") with open(os.path.join(security_dir, "location")) as fd: diff --git a/src/borg/testsuite/repository.py b/src/borg/testsuite/repository.py index 0e180c697..51abc9b2d 100644 --- a/src/borg/testsuite/repository.py +++ b/src/borg/testsuite/repository.py @@ -850,7 +850,7 @@ class RepositoryCheckTestCase(RepositoryTestCaseBase): os.unlink(os.path.join(self.tmppath, "repository", f"index.{self.get_head()}")) def rename_index(self, new_name): - os.rename( + os.replace( os.path.join(self.tmppath, "repository", f"index.{self.get_head()}"), os.path.join(self.tmppath, "repository", new_name), )