[Commented On] D10798: util: add `nb_bytes` argument to `copyfile` to partially copy a file

baymax (Baymax, Your Personal Patch-care Companion) phabricator at mercurial-scm.org
Mon Jun 7 09:19:38 UTC 2021


baymax added a comment.
baymax updated this revision to Diff 28479.


  ✅ refresh by Heptapod after a successful CI run (🐙 💚)

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D10798?vs=28457&id=28479

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D10798/new/

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

AFFECTED FILES
  mercurial/util.py

CHANGE DETAILS

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1909,7 +1909,9 @@
 }
 
 
-def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
+def copyfile(
+    src, dest, hardlink=False, copystat=False, checkambig=False, nb_bytes=None
+):
     """copy a file, preserving mode and optionally other stat info like
     atime/mtime
 
@@ -1918,6 +1920,8 @@
     repo.wlock).
 
     copystat and checkambig should be exclusive.
+
+    nb_bytes: if set only copy the first `nb_bytes` of the source file.
     """
     assert not (copystat and checkambig)
     oldstat = None
@@ -1937,6 +1941,9 @@
     if hardlink:
         try:
             oslink(src, dest)
+            if nb_bytes is not None:
+                m = "the `nb_bytes` argument is incompatible with `hardlink`"
+                raise error.ProgrammingError(m)
             return
         except (IOError, OSError):
             pass  # fall back to normal copy
@@ -1944,6 +1951,9 @@
         os.symlink(os.readlink(src), dest)
         # copytime is ignored for symlinks, but in general copytime isn't needed
         # for them anyway
+        if nb_bytes is not None:
+            m = "cannot use `nb_bytes` on a symlink"
+            raise error.ProgrammingError(m)
     else:
         try:
             shutil.copyfile(src, dest)
@@ -1960,6 +1970,10 @@
                             oldstat.stat[stat.ST_MTIME] + 1
                         ) & 0x7FFFFFFF
                         os.utime(dest, (advanced, advanced))
+            # We could do something smarter using `copy_file_range` call or similar
+            if nb_bytes is not None:
+                with open(dest, mode='r+') as f:
+                    f.truncate(nb_bytes)
         except shutil.Error as inst:
             raise error.Abort(stringutil.forcebytestr(inst))
 



To: marmoute, #hg-reviewers, Alphare
Cc: Alphare, mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20210607/34c1e2ee/attachment-0002.html>


More information about the Mercurial-patches mailing list