diff mercurial/dirstateguard.py @ 47013:222a42ac5b2d stable

dirstateguard: use mktemp-like functionality to generate the backup filenames Previously these were generated with names like: `dirstate.backup.commit.<memory address of dirstateguard>` This could cause problems if two hg commands ran at the same time that used the same memory address, (which is apparently not uncommon if chg is involved), as memory addresses are not unique across processes. This issue was reported in the post-review comments on http://phab.mercurial-scm.org/D9952. Differential Revision: https://phab.mercurial-scm.org/D10504
author Kyle Lippincott <spectral@google.com>
date Tue, 20 Apr 2021 13:01:47 -0700
parents d4ba4d51f85f
children 6000f5b25c9b
line wrap: on
line diff
--- a/mercurial/dirstateguard.py	Sat Apr 24 16:30:05 2021 +0200
+++ b/mercurial/dirstateguard.py	Tue Apr 20 13:01:47 2021 -0700
@@ -7,11 +7,13 @@
 
 from __future__ import absolute_import
 
+import os
 from .i18n import _
 
 from . import (
     error,
     narrowspec,
+    requirements,
     util,
 )
 
@@ -34,13 +36,22 @@
         self._repo = repo
         self._active = False
         self._closed = False
-        self._backupname = b'dirstate.backup.%s.%d' % (name, id(self))
-        self._narrowspecbackupname = b'narrowspec.backup.%s.%d' % (
-            name,
-            id(self),
-        )
+
+        def getname(prefix):
+            fd, fname = repo.vfs.mkstemp(prefix=prefix)
+            os.close(fd)
+            return fname
+
+        self._backupname = getname(b'dirstate.backup.%s.' % name)
         repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
-        narrowspec.savewcbackup(repo, self._narrowspecbackupname)
+        # Don't make this the empty string, things may join it with stuff and
+        # blindly try to unlink it, which could be bad.
+        self._narrowspecbackupname = None
+        if requirements.NARROW_REQUIREMENT in repo.requirements:
+            self._narrowspecbackupname = getname(
+                b'narrowspec.backup.%s.' % name
+            )
+            narrowspec.savewcbackup(repo, self._narrowspecbackupname)
         self._active = True
 
     def __del__(self):
@@ -62,12 +73,14 @@
         self._repo.dirstate.clearbackup(
             self._repo.currenttransaction(), self._backupname
         )
-        narrowspec.clearwcbackup(self._repo, self._narrowspecbackupname)
+        if self._narrowspecbackupname:
+            narrowspec.clearwcbackup(self._repo, self._narrowspecbackupname)
         self._active = False
         self._closed = True
 
     def _abort(self):
-        narrowspec.restorewcbackup(self._repo, self._narrowspecbackupname)
+        if self._narrowspecbackupname:
+            narrowspec.restorewcbackup(self._repo, self._narrowspecbackupname)
         self._repo.dirstate.restorebackup(
             self._repo.currenttransaction(), self._backupname
         )