[PATCH 2 of 2] util: make atomictempfile keep win32-specific file attrs

Yuya Nishihara yuya at tcha.org
Fri May 21 06:17:11 CDT 2010


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1274372380 -32400
# Node ID 2b069d5e7a082512c7192458de4fc15a08fabc3c
# Parent  c6a03ac45bd3028e8f72a24eac118d49b0559374
util: make atomictempfile keep win32-specific file attrs

Main intention of this change is to preserve "hidden" attribute.

It copies file attributes at end, not first, because some attributes are
troublesome when editing. For example, hidden files are not truncatable
on NTFS, thus we cannot open('hidden-file', 'w') if set "hidden" before
opening.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -553,6 +553,10 @@ def hidewindow():
     """
     pass
 
+def copyfileattrs_(src, dest):
+    """copy platform-specific file attributes"""
+    pass
+
 if os.name == 'nt':
     from windows import *
 else:
@@ -785,6 +789,8 @@ class atomictempfile(object):
     def rename(self):
         if not self._fp.closed:
             self._fp.close()
+            # set attrs at end because they are troublesome on editing
+            copyfileattrs_(localpath(self.__name), self.temp)
             rename(self.temp, localpath(self.__name))
 
     def __del__(self):
diff --git a/mercurial/win32.py b/mercurial/win32.py
--- a/mercurial/win32.py
+++ b/mercurial/win32.py
@@ -202,3 +202,10 @@ def termwidth_():
             screenbuf.Detach()
     except pywintypes.error:
         return 79
+
+def copyfileattrs_(src, dest):
+    """copy windows-specific file attributes like hidden flag"""
+    try:
+        win32api.SetFileAttributes(dest, win32api.GetFileAttributes(src))
+    except pywintypes.error:
+        pass


More information about the Mercurial-devel mailing list