Raise an exception when src and dst refer to the same file via a hard link or a

symbolic link (bug #851123 / patch #854853, thanks Gregory Ball).
This commit is contained in:
Johannes Gijsbers 2004-08-14 13:30:02 +00:00
parent 404b06814c
commit 46f1459860
2 changed files with 32 additions and 5 deletions

View file

@ -24,16 +24,22 @@ def copyfileobj(fsrc, fdst, length=16*1024):
break
fdst.write(buf)
def _samefile(src, dst):
# Macintosh, Unix.
if hasattr(os.path,'samefile'):
return os.path.samefile(src, dst)
# All other platforms: check for same pathname.
return (os.path.normcase(os.path.abspath(src)) ==
os.path.normcase(os.path.abspath(dst)))
def copyfile(src, dst):
"""Copy data from src to dst"""
if _samefile(src, dst):
raise Error, "`%s` and `%s` are the same file" % (src, dst)
fsrc = None
fdst = None
# check for same pathname; all platforms
_src = os.path.normcase(os.path.abspath(src))
_dst = os.path.normcase(os.path.abspath(dst))
if _src == _dst:
return
try:
fsrc = open(src, 'rb')
fdst = open(dst, 'wb')