Patchwork [2,of,6,V2] transaction: avoid file stat ambiguity only for files in blacklist

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 4, 2017, 2:29 p.m.
Message ID <2823a6ed15e69fd8527d.1499178540@speaknoevil>
Download mbox | patch
Permalink /patch/21967/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - July 4, 2017, 2:29 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1499177626 -32400
#      Tue Jul 04 23:13:46 2017 +0900
# Node ID 2823a6ed15e69fd8527db5a833fe5c82d5a64074
# Parent  a64883be3b408c96473824274c21b0ea660e4885
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".

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1093,7 +1093,8 @@  class localrepository(object):
                                      aftertrans(renames),
                                      self.store.createmode,
                                      validator=validate,
-                                     releasefn=releasefn)
+                                     releasefn=releasefn,
+                                     checkambigfiles=_cachedfiles)
         tr.changes['revs'] = set()
 
         tr.hookargs['txnid'] = txnid
@@ -1158,7 +1159,8 @@  class localrepository(object):
                 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:
@@ -1214,7 +1216,8 @@  class localrepository(object):
         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'):
diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -44,11 +44,12 @@  def active(func):
     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 @@  def _playback(journal, report, opener, v
 
 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 @@  class transaction(object):
         * `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 @@  class transaction(object):
             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 @@  class transaction(object):
                 # 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 @@  class transaction(object):
             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 @@  def rollback(opener, vfsmap, file, repor
     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 @@  def rollback(opener, vfsmap, file, repor
                 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)
diff --git a/tests/test-mq-qpush-fail.t b/tests/test-mq-qpush-fail.t
--- a/tests/test-mq-qpush-fail.t
+++ b/tests/test-mq-qpush-fail.t
@@ -39,8 +39,9 @@  test qpush on empty series
   > 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)