diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2011-04-28 15:30:31 +0800 |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2011-04-28 15:30:31 +0800 |
commit | 9f6162a451978702ef52089d2a1636f5ddf10ece (patch) | |
tree | c6f2e45d83c2fb294908f2dc9ac81103b8d3d58e /Lib | |
parent | 9e2e0282717296c86d67141e678afe2644a27684 (diff) | |
download | cpython-9f6162a451978702ef52089d2a1636f5ddf10ece.tar.gz |
Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tarfile.py | 2 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 27 |
2 files changed, 29 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 47be1d49df..105a75880d 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2239,6 +2239,8 @@ class TarFile(object): if hasattr(os, "symlink") and hasattr(os, "link"): # For systems that support symbolic and hard links. if tarinfo.issym(): + if os.path.exists(targetpath): + os.unlink(targetpath) os.symlink(tarinfo.linkname, targetpath) else: # See extract(). diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index cda5262acc..ef3bf8b411 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -843,6 +843,33 @@ class WriteTest(WriteTestBase): finally: os.chdir(cwd) + def test_extractall_symlinks(self): + # Test if extractall works properly when tarfile contains symlinks + tempdir = os.path.join(TEMPDIR, "testsymlinks") + temparchive = os.path.join(TEMPDIR, "testsymlinks.tar") + os.mkdir(tempdir) + try: + source_file = os.path.join(tempdir,'source') + target_file = os.path.join(tempdir,'symlink') + with open(source_file,'w') as f: + f.write('something\n') + os.symlink(source_file, target_file) + tar = tarfile.open(temparchive,'w') + tar.add(source_file, arcname=os.path.basename(source_file)) + tar.add(target_file, arcname=os.path.basename(target_file)) + tar.close() + # Let's extract it to the location which contains the symlink + tar = tarfile.open(temparchive,'r') + # this should not raise OSError: [Errno 17] File exists + try: + tar.extractall(path=tempdir) + except OSError: + self.fail("extractall failed with symlinked files") + finally: + tar.close() + finally: + os.unlink(temparchive) + shutil.rmtree(tempdir) class StreamWriteTest(WriteTestBase): |