]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/util/fileutil.py
replace_file should allow the replaced file not to exist on Windows.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / fileutil.py
index 6449ff1ee8030afef4b531c60d0e53edac64750b..a2be63c059df16aa6e61d5f7f1c35d9f1b4500db 100644 (file)
@@ -287,7 +287,7 @@ def abspath_expanduser_unicode(path, base=None, long_path=True):
     """
     if not isinstance(path, unicode):
         raise AssertionError("paths must be Unicode strings")
-    if base is not None:
+    if base is not None and long_path:
         precondition_abspath(base)
 
     path = expanduser(path)
@@ -588,6 +588,9 @@ if sys.platform == "win32":
 
     REPLACEFILE_IGNORE_MERGE_ERRORS = 0x00000002
 
+    # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx>
+    ERROR_FILE_NOT_FOUND = 2
+
     def rename_no_overwrite(source_path, dest_path):
         os.rename(source_path, dest_path)
 
@@ -602,7 +605,13 @@ if sys.platform == "win32":
             # The UnableToUnlinkReplacementError case does not happen on Windows;
             # all errors should be treated as signalling a conflict.
             err = get_last_error()
-            raise ConflictError("WinError: %s" % (WinError(err)))
+            if err != ERROR_FILE_NOT_FOUND:
+                raise ConflictError("WinError: %s" % (WinError(err),))
+
+            try:
+                rename_no_overwrite(replacement_path, replaced_path)
+            except EnvironmentError:
+                reraise(ConflictError)
 else:
     def rename_no_overwrite(source_path, dest_path):
         # link will fail with EEXIST if there is already something at dest_path.