summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tarfile.py
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2011-05-17 02:12:18 (GMT)
committerSenthil Kumaran <senthil@uthcode.com>2011-05-17 02:12:18 (GMT)
commit4dd89ce6bfb635176d8c9185c148ca11ed207728 (patch)
treeb91a740a816a0da3c8dc0c1942d6b0e2b5299812 /Lib/test/test_tarfile.py
parent72bb99d34351a7636c9ff13e563c9e97e0f1fdac (diff)
downloadcpython-4dd89ce6bfb635176d8c9185c148ca11ed207728.zip
cpython-4dd89ce6bfb635176d8c9185c148ca11ed207728.tar.gz
cpython-4dd89ce6bfb635176d8c9185c148ca11ed207728.tar.bz2
Fix closes issue #12088 - fixes the tarfile.extractall issue when the
symlinks/hardlink was broken. It handles now in a graceful manner (No exception is raised, behavior similar GNU tar).
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r--Lib/test/test_tarfile.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index f78f9ce..6962f8e 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -872,6 +872,66 @@ class WriteTest(WriteTestBase):
os.unlink(temparchive)
shutil.rmtree(tempdir)
+ @unittest.skipUnless(hasattr(os, 'symlink'), "needs os.symlink")
+ def test_extractall_broken_symlinks(self):
+ # Test if extractall works properly when tarfile contains broken
+ # 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(target_file, arcname=os.path.basename(target_file))
+ tar.close()
+ # remove the real file
+ os.unlink(source_file)
+ # 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 broken symlinked files")
+ finally:
+ tar.close()
+ finally:
+ os.unlink(temparchive)
+ shutil.rmtree(tempdir)
+
+ @unittest.skipUnless(hasattr(os, 'link'), "needs os.link")
+ def test_extractall_hardlinks(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.link(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 linked files")
+ finally:
+ tar.close()
+ finally:
+ os.unlink(temparchive)
+ shutil.rmtree(tempdir)
+
class StreamWriteTest(WriteTestBase):
mode = "w|"