comparison mercurial/win32.py @ 12938:bf826c0b9537 stable

opener: check hardlink count reporting (issue1866) The Linux CIFS kernel driver (even in 2.6.36) suffers from a hardlink count blindness bug (lstat() returning 1 in st_nlink when it is expected to return >1), which causes repository corruption if Mercurial running on Linux pushes or commits to a hardlinked repository stored on a Windows share, if that share is mounted using the CIFS driver. This patch works around issue1866 and improves the workaround done in 50523b4407f6 to fix issue761, by teaching the opener to lazily execute a runtime check (new function checknlink) to see if the hardlink count reported by nlinks() can be trusted. Since nlinks() is also known to return varying count values (1 or >1) depending on whether the file is open or not and depending on what client and server software combination is being used for accessing and serving the Windows share, we deliberately open the file before calling nlinks() in order to have a stable precondition. Trying to depend on the precondition "file closed" would be fragile, as the file could have been opened very easily somewhere else in the program.
author Adrian Buehlmann <adrian@cadifra.com>
date Sun, 07 Nov 2010 18:21:29 +0100
parents c52c629ce19e
children 5f80f44d23c5
comparison
equal deleted inserted replaced
12937:6ff784de7c3a 12938:bf826c0b9537
41 finally: 41 finally:
42 fh.Close() 42 fh.Close()
43 43
44 def nlinks(pathname): 44 def nlinks(pathname):
45 """Return number of hardlinks for the given file.""" 45 """Return number of hardlinks for the given file."""
46 links = _getfileinfo(pathname)[7] 46 return _getfileinfo(pathname)[7]
47 if links < 2:
48 # Known to be wrong for most network drives
49 dirname = os.path.dirname(pathname)
50 if not dirname:
51 dirname = '.'
52 dt = win32file.GetDriveType(dirname + '\\')
53 if dt == 4 or dt == 1:
54 # Fake hardlink to force COW for network drives
55 links = 2
56 return links
57 47
58 def samefile(fpath1, fpath2): 48 def samefile(fpath1, fpath2):
59 """Returns whether fpath1 and fpath2 refer to the same file. This is only 49 """Returns whether fpath1 and fpath2 refer to the same file. This is only
60 guaranteed to work for files, not directories.""" 50 guaranteed to work for files, not directories."""
61 res1 = _getfileinfo(fpath1) 51 res1 = _getfileinfo(fpath1)