Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 27369:c48ecc0b5bc9 stable
copyfile: add an optional parameter to copy other stat data
Contrary to the comment, I didn't see any evidence that we were copying
atime/mtime at all. This adds a parameter to copyfile to optionally copy it and
other stat data, with the default being to not copy it.
Many systems don't support changing the timestamp of a symlink, but we don't
need that in general anyway -- copystat is mostly useful for editors, most of
which will dereference symlinks anyway.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Sat, 12 Dec 2015 11:00:04 -0800 |
parents | 6331a0c310db |
children | 4eeef1b2d689 |
comparison
equal
deleted
inserted
replaced
27320:59d5f619e69e | 27369:c48ecc0b5bc9 |
---|---|
804 raise error.SignatureError | 804 raise error.SignatureError |
805 raise | 805 raise |
806 | 806 |
807 return check | 807 return check |
808 | 808 |
809 def copyfile(src, dest, hardlink=False): | 809 def copyfile(src, dest, hardlink=False, copystat=False): |
810 "copy a file, preserving mode and atime/mtime" | 810 '''copy a file, preserving mode and optionally other stat info like |
811 atime/mtime''' | |
811 if os.path.lexists(dest): | 812 if os.path.lexists(dest): |
812 unlink(dest) | 813 unlink(dest) |
813 # hardlinks are problematic on CIFS, quietly ignore this flag | 814 # hardlinks are problematic on CIFS, quietly ignore this flag |
814 # until we find a way to work around it cleanly (issue4546) | 815 # until we find a way to work around it cleanly (issue4546) |
815 if False and hardlink: | 816 if False and hardlink: |
818 return | 819 return |
819 except (IOError, OSError): | 820 except (IOError, OSError): |
820 pass # fall back to normal copy | 821 pass # fall back to normal copy |
821 if os.path.islink(src): | 822 if os.path.islink(src): |
822 os.symlink(os.readlink(src), dest) | 823 os.symlink(os.readlink(src), dest) |
824 # copytime is ignored for symlinks, but in general copytime isn't needed | |
825 # for them anyway | |
823 else: | 826 else: |
824 try: | 827 try: |
825 shutil.copyfile(src, dest) | 828 shutil.copyfile(src, dest) |
826 shutil.copymode(src, dest) | 829 if copystat: |
830 # copystat also copies mode | |
831 shutil.copystat(src, dest) | |
832 else: | |
833 shutil.copymode(src, dest) | |
827 except shutil.Error as inst: | 834 except shutil.Error as inst: |
828 raise Abort(str(inst)) | 835 raise Abort(str(inst)) |
829 | 836 |
830 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): | 837 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): |
831 """Copy a directory tree using hardlinks if possible.""" | 838 """Copy a directory tree using hardlinks if possible.""" |