changeset 34079:1104718fb090

checknlink: use a random temp file name for checking 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 D468, `~` suffix was used. Differential Revision: https://phab.mercurial-scm.org/D611
author Jun Wu <quark@fb.com>
date Fri, 01 Sep 2017 17:09:53 -0700
parents b4b196092cc3
children 4059b10d7490
files mercurial/util.py
diffstat 1 files changed, 9 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Sun Sep 03 02:34:01 2017 +0530
+++ b/mercurial/util.py	Fri Sep 01 17:09:53 2017 -0700
@@ -1457,21 +1457,14 @@
 
     # 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
+    f1, f2, fd = None, None, None
     try:
-        posixfile(f1, 'w').close()
-    except IOError:
-        try:
-            os.unlink(f1)
-        except OSError:
-            pass
-        return False
-
-    f2 = testfile + ".hgtmp2"
-    fd = None
-    try:
+        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]
+
         oslink(f1, f2)
         # nlinks() may behave differently for files on Windows shares if
         # the file is open.
@@ -1484,7 +1477,8 @@
             fd.close()
         for f in (f1, f2):
             try:
-                os.unlink(f)
+                if f is not None:
+                    os.unlink(f)
             except OSError:
                 pass