# HG changeset patch # User Adrian Buehlmann # Date 2011-03-27 00:47:58 # Node ID a2f0fdb1e488da6148f8169594c12f2c0bc0ce9a # Parent 930efdc6bfa439f5a42aa3266cb59607bd76b8e2 win32: remove READONLY attribute on unlink diff --git a/mercurial/win32.py b/mercurial/win32.py --- a/mercurial/win32.py +++ b/mercurial/win32.py @@ -56,6 +56,9 @@ class _BY_HANDLE_FILE_INFORMATION(ctypes _OPEN_EXISTING = 3 +# SetFileAttributes +_FILE_ATTRIBUTE_NORMAL = 0x80 + # Process Security and Access Rights _PROCESS_QUERY_INFORMATION = 0x0400 @@ -350,9 +353,15 @@ def unlink(f): 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