[PATCH 1 of 2] dirstate: try to use hardlink to backup dirstate

Jun Wu quark at fb.com
Thu Mar 2 03:08:51 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1488419961 28800
#      Wed Mar 01 17:59:21 2017 -0800
# Node ID 1e74c8187c9b98644ae35902c9e3faddad00f364
# Parent  0bb3089fe73527c64f1afc40b86ecb8dfe7fd7aa
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 1e74c8187c9b
dirstate: try to use hardlink to backup dirstate

This should be more efficient once util.copyfile has real hardlink support.

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -1225,6 +1225,12 @@ class dirstate(object):
             tr.registertmp(filename, location='plain')
 
-        self._opener.write(prefix + self._filename + suffix,
-                           self._opener.tryread(filename))
+        backupname = prefix + self._filename + suffix
+        assert backupname != filename
+        if self._opener.exists(backupname):
+            self._opener.unlink(backupname)
+        # hardlink backup is okay because _writedirstate is always called
+        # with an "atomictemp=True" file.
+        util.copyfile(self._opener.join(filename),
+                      self._opener.join(backupname), hardlink=True)
 
     def restorebackup(self, tr, suffix='', prefix=''):
diff --git a/tests/test-largefiles-small-disk.t b/tests/test-largefiles-small-disk.t
--- a/tests/test-largefiles-small-disk.t
+++ b/tests/test-largefiles-small-disk.t
@@ -6,5 +6,9 @@ Test how largefiles abort in case the di
   > #
   > # this makes the original largefiles code abort:
+  > _origcopyfileobj = shutil.copyfileobj
   > def copyfileobj(fsrc, fdst, length=16*1024):
+  >     # allow journal files (used by transaction) to be written
+  >     if 'journal.' in fdst.name:
+  >         return _origcopyfileobj(fsrc, fdst, length)
   >     fdst.write(fsrc.read(4))
   >     raise IOError(errno.ENOSPC, os.strerror(errno.ENOSPC))


More information about the Mercurial-devel mailing list