changeset 33278:87bca10a06ed

transaction: avoid file stat ambiguity only for files in blacklist Advancing mtime by os.utime() fails for EPERM, if the target file is owned by another. bff5ccbe5ead and related changes made some code paths give advancing mtime up in such case, to fix issue5418. This causes file stat ambiguity (again), if it is owned by another. https://www.mercurial-scm.org/wiki/ExactCacheValidationPlan To avoid file stat ambiguity in such case, especially for .hg/dirstate, ed66ec39933f made vfs.rename() copy the target file, and advance mtime of renamed one again, if EPERM (see issue5584 for detail). But straightforward "copy if EPERM" isn't reasonable for truncation of append-only files at rollbacking, because rollbacking might cost much for truncation of many filelogs, even though filelogs aren't filecache-ed. Therefore, this patch introduces blacklist "checkambigfiles", and avoids file stat ambiguity only for files specified in this blacklist. This patch consists of two parts below, which should be applied at once in order to avoid regression. - specify 'checkambig=True' at vfs.open(mode='a') in _playback() according to checkambigfiles - invoke _playback() with checkambigfiles - add transaction.__init__() checkambigfiles argument, for _abort() - make localrepo instantiate transaction with _cachedfiles - add rollback() checkambigfiles argument, for "hg rollback/recover" - make localrepo invoke rollback() with _cachedfiles After this patch, straightforward "copy if EPERM" will be reasonable at closing the file opened with checkambig=True, because this policy is applied only on files, which are listed in blacklist "checkambigfiles".
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Tue, 04 Jul 2017 23:13:46 +0900
parents 4470508eb6f2
children 7912404b70f2
files mercurial/localrepo.py mercurial/transaction.py tests/test-mq-qpush-fail.t
diffstat 3 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/localrepo.py	Tue Jul 04 23:13:46 2017 +0900
+++ b/mercurial/localrepo.py	Tue Jul 04 23:13:46 2017 +0900
@@ -1098,7 +1098,8 @@
                                      aftertrans(renames),
                                      self.store.createmode,
                                      validator=validate,
-                                     releasefn=releasefn)
+                                     releasefn=releasefn,
+                                     checkambigfiles=_cachedfiles)
         tr.changes['revs'] = set()
         tr.changes['obsmarkers'] = set()
 
@@ -1164,7 +1165,8 @@
                 vfsmap = {'': self.svfs,
                           'plain': self.vfs,}
                 transaction.rollback(self.svfs, vfsmap, "journal",
-                                     self.ui.warn)
+                                     self.ui.warn,
+                                     checkambigfiles=_cachedfiles)
                 self.invalidate()
                 return True
             else:
@@ -1220,7 +1222,8 @@
         parents = self.dirstate.parents()
         self.destroying()
         vfsmap = {'plain': self.vfs, '': self.svfs}
-        transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn)
+        transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
+                             checkambigfiles=_cachedfiles)
         if self.vfs.exists('undo.bookmarks'):
             self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
         if self.svfs.exists('undo.phaseroots'):
--- a/mercurial/transaction.py	Tue Jul 04 23:13:46 2017 +0900
+++ b/mercurial/transaction.py	Tue Jul 04 23:13:46 2017 +0900
@@ -44,11 +44,12 @@
     return _active
 
 def _playback(journal, report, opener, vfsmap, entries, backupentries,
-              unlink=True):
+              unlink=True, checkambigfiles=None):
     for f, o, _ignore in entries:
         if o or not unlink:
+            checkambig = checkambigfiles and (f, '') in checkambigfiles
             try:
-                fp = opener(f, 'a', checkambig=True)
+                fp = opener(f, 'a', checkambig=checkambig)
                 fp.truncate(o)
                 fp.close()
             except IOError:
@@ -101,7 +102,8 @@
 
 class transaction(object):
     def __init__(self, report, opener, vfsmap, journalname, undoname=None,
-                 after=None, createmode=None, validator=None, releasefn=None):
+                 after=None, createmode=None, validator=None, releasefn=None,
+                 checkambigfiles=None):
         """Begin a new transaction
 
         Begins a new transaction that allows rolling back writes in the event of
@@ -110,6 +112,10 @@
         * `after`: called after the transaction has been committed
         * `createmode`: the mode of the journal file that will be created
         * `releasefn`: called after releasing (with transaction and result)
+
+        `checkambigfiles` is a set of (path, vfs-location) tuples,
+        which determine whether file stat ambiguity should be avoided
+        for corresponded files.
         """
         self.count = 1
         self.usages = 1
@@ -137,6 +143,10 @@
             releasefn = lambda tr, success: None
         self.releasefn = releasefn
 
+        self.checkambigfiles = set()
+        if checkambigfiles:
+            self.checkambigfiles.update(checkambigfiles)
+
         # A dict dedicated to precisely tracking the changes introduced in the
         # transaction.
         self.changes = {}
@@ -564,7 +574,8 @@
                 # Prevent double usage and help clear cycles.
                 self._abortcallback = None
                 _playback(self.journal, self.report, self.opener, self._vfsmap,
-                          self.entries, self._backupentries, False)
+                          self.entries, self._backupentries, False,
+                          checkambigfiles=self.checkambigfiles)
                 self.report(_("rollback completed\n"))
             except BaseException:
                 self.report(_("rollback failed - please run hg recover\n"))
@@ -573,7 +584,7 @@
             self.releasefn(self, False) # notify failure of transaction
             self.releasefn = None # Help prevent cycles.
 
-def rollback(opener, vfsmap, file, report):
+def rollback(opener, vfsmap, file, report, checkambigfiles=None):
     """Rolls back the transaction contained in the given file
 
     Reads the entries in the specified file, and the corresponding
@@ -584,6 +595,10 @@
     file\0offset pairs, delimited by newlines. The corresponding
     '*.backupfiles' file should contain a list of file\0backupfile
     pairs, delimited by \0.
+
+    `checkambigfiles` is a set of (path, vfs-location) tuples,
+    which determine whether file stat ambiguity should be avoided at
+    restoring corresponded files.
     """
     entries = []
     backupentries = []
@@ -615,4 +630,5 @@
                 report(_("journal was created by a different version of "
                          "Mercurial\n"))
 
-    _playback(file, report, opener, vfsmap, entries, backupentries)
+    _playback(file, report, opener, vfsmap, entries, backupentries,
+              checkambigfiles=checkambigfiles)
--- a/tests/test-mq-qpush-fail.t	Tue Jul 04 23:13:46 2017 +0900
+++ b/tests/test-mq-qpush-fail.t	Tue Jul 04 23:13:46 2017 +0900
@@ -39,8 +39,9 @@
   > from mercurial import extensions, transaction
   > def wrapplayback(orig,
   >                  journal, report, opener, vfsmap, entries, backupentries,
-  >                  unlink=True):
-  >     orig(journal, report, opener, vfsmap, entries, backupentries, unlink)
+  >                  unlink=True, checkambigfiles=None):
+  >     orig(journal, report, opener, vfsmap, entries, backupentries, unlink,
+  >          checkambigfiles)
   >     # Touching files truncated at "transaction.abort" causes
   >     # forcible re-loading invalidated filecache properties
   >     # (including repo.changelog)