Patchwork D611: checknlink: use a random temp file name for checking

login
register
mail settings
Submitter phabricator
Date Sept. 6, 2017, 2:51 p.m.
Message ID <17949cd7a88f1d2e8b0ce4ec298d331b@localhost.localdomain>
Download mbox | patch
Permalink /patch/23693/
State Not Applicable
Headers show

Comments

phabricator - Sept. 6, 2017, 2:51 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG1104718fb090: checknlink: use a random temp file name for checking (authored by quark).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D611?vs=1616&id=1633

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

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -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