##// END OF EJS Templates
util: add `nb_bytes` argument to `copyfile` to partially copy a file...
marmoute -
r48192:9b841267 default
parent child Browse files
Show More
@@ -1909,7 +1909,9 b' def checksignature(func, depth=1):'
1909 1909 }
1910 1910
1911 1911
1912 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1912 def copyfile(
1913 src, dest, hardlink=False, copystat=False, checkambig=False, nb_bytes=None
1914 ):
1913 1915 """copy a file, preserving mode and optionally other stat info like
1914 1916 atime/mtime
1915 1917
@@ -1918,6 +1920,8 b' def copyfile(src, dest, hardlink=False, '
1918 1920 repo.wlock).
1919 1921
1920 1922 copystat and checkambig should be exclusive.
1923
1924 nb_bytes: if set only copy the first `nb_bytes` of the source file.
1921 1925 """
1922 1926 assert not (copystat and checkambig)
1923 1927 oldstat = None
@@ -1937,6 +1941,9 b' def copyfile(src, dest, hardlink=False, '
1937 1941 if hardlink:
1938 1942 try:
1939 1943 oslink(src, dest)
1944 if nb_bytes is not None:
1945 m = "the `nb_bytes` argument is incompatible with `hardlink`"
1946 raise error.ProgrammingError(m)
1940 1947 return
1941 1948 except (IOError, OSError):
1942 1949 pass # fall back to normal copy
@@ -1944,6 +1951,9 b' def copyfile(src, dest, hardlink=False, '
1944 1951 os.symlink(os.readlink(src), dest)
1945 1952 # copytime is ignored for symlinks, but in general copytime isn't needed
1946 1953 # for them anyway
1954 if nb_bytes is not None:
1955 m = "cannot use `nb_bytes` on a symlink"
1956 raise error.ProgrammingError(m)
1947 1957 else:
1948 1958 try:
1949 1959 shutil.copyfile(src, dest)
@@ -1960,6 +1970,10 b' def copyfile(src, dest, hardlink=False, '
1960 1970 oldstat.stat[stat.ST_MTIME] + 1
1961 1971 ) & 0x7FFFFFFF
1962 1972 os.utime(dest, (advanced, advanced))
1973 # We could do something smarter using `copy_file_range` call or similar
1974 if nb_bytes is not None:
1975 with open(dest, mode='r+') as f:
1976 f.truncate(nb_bytes)
1963 1977 except shutil.Error as inst:
1964 1978 raise error.Abort(stringutil.forcebytestr(inst))
1965 1979
General Comments 0
You need to be logged in to leave comments. Login now