cpython: 0c8bc3a0130a (original) (raw)
Mercurial > cpython
changeset 69653:0c8bc3a0130a 2.7
Fix closes issue10761: tarfile.extractall failure when symlinked files are present. [#10761]
Senthil Kumaran orsenthil@gmail.com | |
---|---|
date | Thu, 28 Apr 2011 15:30:31 +0800 |
parents | ffd83aeb0b67 |
children | e19d2e3a3a58 |
files | Lib/tarfile.py Lib/test/test_tarfile.py Misc/NEWS |
diffstat | 3 files changed, 32 insertions(+), 0 deletions(-)[+] [-] Lib/tarfile.py 2 Lib/test/test_tarfile.py 27 Misc/NEWS 3 |
line wrap: on
line diff
--- 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):[](#l1.7)
os.unlink(targetpath)[](#l1.8) os.symlink(tarinfo.linkname, targetpath)[](#l1.9) else:[](#l1.10) # See extract().[](#l1.11)
--- 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[](#l2.8)
tempdir = os.path.join(TEMPDIR, "testsymlinks")[](#l2.9)
temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")[](#l2.10)
os.mkdir(tempdir)[](#l2.11)
try:[](#l2.12)
source_file = os.path.join(tempdir,'source')[](#l2.13)
target_file = os.path.join(tempdir,'symlink')[](#l2.14)
with open(source_file,'w') as f:[](#l2.15)
f.write('something\n')[](#l2.16)
os.symlink(source_file, target_file)[](#l2.17)
tar = tarfile.open(temparchive,'w')[](#l2.18)
tar.add(source_file, arcname=os.path.basename(source_file))[](#l2.19)
tar.add(target_file, arcname=os.path.basename(target_file))[](#l2.20)
tar.close()[](#l2.21)
# Let's extract it to the location which contains the symlink[](#l2.22)
tar = tarfile.open(temparchive,'r')[](#l2.23)
# this should not raise OSError: [Errno 17] File exists[](#l2.24)
try:[](#l2.25)
tar.extractall(path=tempdir)[](#l2.26)
except OSError:[](#l2.27)
self.fail("extractall failed with symlinked files")[](#l2.28)
finally:[](#l2.29)
tar.close()[](#l2.30)
finally:[](#l2.31)
os.unlink(temparchive)[](#l2.32)
shutil.rmtree(tempdir)[](#l2.33)
class StreamWriteTest(WriteTestBase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -65,6 +65,9 @@ Core and Builtins Library ------- +- Issue #10761: Fix tarfile.extractall failure when symlinked files are