[PATCH 1 of 1 STABLE V2] checknlink: use two testfiles (issue2543)

Adrian Buehlmann adrian at cadifra.com
Tue Dec 21 04:14:21 CST 2010


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1292276286 -3600
# Branch stable
# Node ID e82b0098b827b54544cd22e08910063246442767
# Parent  f9d29777b4eb88e87c9dca68a55e396ab63d8898
checknlink: use two testfiles (issue2543)

Preventing file loss repository corruption (e.g. vanished changelog.i) when
Mercurial pushes to repositories on Windows shares served by Samba.

This is a workaround for Samba bug 7863, which is present in current latest
stable Samba 3.5.6 and various prior versions down to 3.0.26a (the oldest one
I tested).

Of course this should be fixed in Samba, but there probably aren't that many
other applications who use hardlinks that extensively and keep files open like
Mercurial, so the pressure to fix this on Samba is probably not that high. And
even if the Samba project should be able to fix their bug within a month or
two, it will take quite some time until users upgrade their Samba installs.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -719,21 +719,37 @@ def checklink(path):
 
 def checknlink(testfile):
     '''check whether hardlink count reporting works properly'''
-    f = testfile + ".hgtmp"
 
+    # testfile may be open, so we need a separate file for checking to
+    # work around issue2543 (or testfile may get lost on Samba shares)
+    f1 = testfile + ".hgtmp1"
+    if os.path.lexists(f1):
+        return False
     try:
-        os_link(testfile, f)
-    except OSError:
+        posixfile(f1, 'w').close()
+    except IOError:
         return False
 
+    f2 = testfile + ".hgtmp2"
+    fd = None
     try:
+        try:
+            os_link(f1, f2)
+        except OSError:
+            return False
+
         # nlinks() may behave differently for files on Windows shares if
         # the file is open.
-        fd = open(f)
-        return nlinks(f) > 1
+        fd = open(f2)
+        return nlinks(f2) > 1
     finally:
-        fd.close()
-        os.unlink(f)
+        if fd is not None:
+            fd.close()
+        for f in (f1, f2):
+            try:
+                os.unlink(f)
+            except OSError:
+                pass
 
     return False
 


More information about the Mercurial-devel mailing list