[PATCH 1 of 2] win32.py: let samefile and samedevice work on directories too

Adrian Buehlmann adrian at cadifra.com
Thu Jun 14 04:44:35 CDT 2012


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1339664600 -7200
# Node ID cbddd5db525031ee953f165cb902048d6a3f1292
# Parent  bdf8c6c61c9b3ff422208e62b498d61d69b12d6c
win32.py: let samefile and samedevice work on directories too

diff --git a/mercurial/win32.py b/mercurial/win32.py
--- a/mercurial/win32.py
+++ b/mercurial/win32.py
@@ -59,6 +59,8 @@
 
 _OPEN_EXISTING = 3
 
+_FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
+
 # SetFileAttributes
 _FILE_ATTRIBUTE_NORMAL = 0x80
 _FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000
@@ -192,7 +194,7 @@
 def _getfileinfo(name):
     fh = _kernel32.CreateFileA(name, 0,
             _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
-            None, _OPEN_EXISTING, 0, None)
+            None, _OPEN_EXISTING, _FILE_FLAG_BACKUP_SEMANTICS, None)
     if fh == _INVALID_HANDLE_VALUE:
         _raiseoserror(name)
     try:
@@ -214,20 +216,18 @@
     '''return number of hardlinks for the given file'''
     return _getfileinfo(name).nNumberOfLinks
 
-def samefile(fpath1, fpath2):
-    '''Returns whether fpath1 and fpath2 refer to the same file. This is only
-    guaranteed to work for files, not directories.'''
-    res1 = _getfileinfo(fpath1)
-    res2 = _getfileinfo(fpath2)
+def samefile(path1, path2):
+    '''Returns whether path1 and path2 refer to the same file or directory.'''
+    res1 = _getfileinfo(path1)
+    res2 = _getfileinfo(path2)
     return (res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
         and res1.nFileIndexHigh == res2.nFileIndexHigh
         and res1.nFileIndexLow == res2.nFileIndexLow)
 
-def samedevice(fpath1, fpath2):
-    '''Returns whether fpath1 and fpath2 are on the same device. This is only
-    guaranteed to work for files, not directories.'''
-    res1 = _getfileinfo(fpath1)
-    res2 = _getfileinfo(fpath2)
+def samedevice(path1, path2):
+    '''Returns whether path1 and path2 are on the same device.'''
+    res1 = _getfileinfo(path1)
+    res2 = _getfileinfo(path2)
     return res1.dwVolumeSerialNumber == res2.dwVolumeSerialNumber
 
 def testpid(pid):


More information about the Mercurial-devel mailing list