Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 123932f

Browse files
committed
Add tests for tarfile extractall feature when with symlinks
1 parent b185a04 commit 123932f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_tarfile.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,33 @@ def exclude(name):
678678
finally:
679679
shutil.rmtree(tempdir)
680680

681+
def test_extractall_symlinks(self):
682+
# Test if extractall works properly when tarfile contains symlinks
683+
tempdir = os.path.join(TEMPDIR, "testsymlinks")
684+
temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
685+
os.mkdir(tempdir)
686+
try:
687+
source_file = os.path.join(tempdir,'source')
688+
target_file = os.path.join(tempdir,'symlink')
689+
with open(source_file,'w') as f:
690+
f.write('something\n')
691+
os.symlink(source_file, target_file)
692+
tar = tarfile.open(temparchive,'w')
693+
tar.add(source_file)
694+
tar.add(target_file)
695+
tar.close()
696+
# Let's extract it to the location which contains the symlink
697+
tar = tarfile.open(temparchive,'r')
698+
# this should not raise OSError: [Errno 17] File exists
699+
try:
700+
tar.extractall(path=tempdir)
701+
except OSError:
702+
self.fail("extractall failed with symlinked files")
703+
finally:
704+
tar.close()
705+
finally:
706+
os.unlink(temparchive)
707+
shutil.rmtree(tempdir)
681708

682709
class StreamWriteTest(WriteTestBase):
683710

0 commit comments

Comments
 (0)