win32: remove READONLY attribute on unlink
authorAdrian Buehlmann <adrian@cadifra.com>
Sun, 27 Mar 2011 01:47:58 +0100
changeset 13776 a2f0fdb1e488
parent 13775 930efdc6bfa4
child 13777 f6e5035dc81a
win32: remove READONLY attribute on unlink
mercurial/win32.py
--- a/mercurial/win32.py	Sun Mar 27 01:17:48 2011 +0100
+++ b/mercurial/win32.py	Sun Mar 27 01:47:58 2011 +0100
@@ -56,6 +56,9 @@
 
 _OPEN_EXISTING = 3
 
+# SetFileAttributes
+_FILE_ATTRIBUTE_NORMAL = 0x80
+
 # Process Security and Access Rights
 _PROCESS_QUERY_INFORMATION = 0x0400
 
@@ -350,9 +353,15 @@
 
     try:
         os.unlink(temp)
-    except:
-        # Some very rude AV-scanners on Windows may cause this unlink to fail.
-        # Not aborting here just leaks the temp file, whereas aborting at this
-        # point may leave serious inconsistencies. Ideally, we would notify
-        # the user in this case here.
-        pass
+    except OSError:
+        # The unlink might have failed because the READONLY attribute may heave
+        # been set on the original file. Rename works fine with READONLY set,
+        # but not os.unlink. Reset all attributes and try again.
+        _kernel32.SetFileAttributesA(temp, _FILE_ATTRIBUTE_NORMAL)
+        try:
+            os.unlink(temp)
+        except OSError:
+            # The unlink might have failed due to some very rude AV-Scanners.
+            # Leaking a tempfile is the lesser evil than aborting here and
+            # leaving some potentially serious inconsistencies.
+            pass