D611: nlink: use a random temp file name for checking

quark (Jun Wu) phabricator at mercurial-scm.org
Sat Sep 2 00:29:57 UTC 2017


quark created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Previously, if `.hg/store/00manifest.d.hgtmp1` exists, hg will copy the
  entire `00manifest.d` every time when appending new manifest revisions.
  That could happen if Mercurial or the machine crashed when `.hgtmp1` was
  just created but not deleted yet.
  
  This patch changes the fixed name to a random generated name. To be
  consistent with https://phab.mercurial-scm.org/D468, `~` suffix was used.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D611

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1455,20 +1455,11 @@
 
     # 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:
-        posixfile(f1, 'w').close()
-    except IOError:
-        try:
-            os.unlink(f1)
-        except OSError:
-            pass
-        return False
-
-    f2 = testfile + ".hgtmp2"
+    fd, f1 = tempfile.mkstemp(prefix='.%s-' % os.path.basename(testfile),
+                              suffix='1~', dir=os.path.dirname(testfile))
+    os.close(fd)
     fd = None
+    f2 = '%s2~' % f1[:-2]
     try:
         oslink(f1, f2)
         # nlinks() may behave differently for files on Windows shares if



To: quark, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list