瀏覽代碼

Merge pull request #7279 from ThomasWaldmann/os-replace-master

use os.replace
TW 2 年之前
父節點
當前提交
48122f9b8b
共有 3 個文件被更改,包括 11 次插入10 次删除
  1. 7 6
      src/borg/cache.py
  2. 3 3
      src/borg/testsuite/archiver/checks.py
  3. 1 1
      src/borg/testsuite/repository.py

+ 7 - 6
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)

+ 3 - 3
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:

+ 1 - 1
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),
         )