From 0a08d7a09537b2ccfc93c5a81bd36cf5f1b1e92d Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 6 Jan 2012 20:16:19 +0100 Subject: Issue #9993: When the source and destination are on different filesystems, and the source is a symlink, shutil.move() now recreates a symlink on the destination instead of copying the file contents. Patch by Jonathan Niehof and Hynek Schlawack. --- Lib/shutil.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'Lib/shutil.py') diff --git a/Lib/shutil.py b/Lib/shutil.py index 95bebb874a..5f69fb7b75 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -356,7 +356,10 @@ def move(src, dst): overwritten depending on os.rename() semantics. If the destination is on our current filesystem, then rename() is used. - Otherwise, src is copied to the destination and then removed. + Otherwise, src is copied to the destination and then removed. Symlinks are + recreated under the new name if os.rename() fails because of cross + filesystem renames. + A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over. @@ -375,7 +378,11 @@ def move(src, dst): try: os.rename(src, real_dst) except OSError: - if os.path.isdir(src): + if os.path.islink(src): + linkto = os.readlink(src) + os.symlink(linkto, real_dst) + os.unlink(src) + elif os.path.isdir(src): if _destinsrc(src, dst): raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst)) copytree(src, real_dst, symlinks=True) -- cgit v1.2.1