bpo-12800: tarfile: Restore fix from 011525ee9 (GH-21409)

Restore fix from 011525ee92.
(cherry picked from commit 4fedd7123e)

Co-authored-by: Julien Palard <julien@palard.fr>
This commit is contained in:
Miss Islington (bot) 2020-11-25 01:52:51 -08:00 committed by GitHub
parent 543724b972
commit 9d2c2a8e3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View File

@ -2237,6 +2237,9 @@ class TarFile(object):
try: try:
# For systems that support symbolic and hard links. # For systems that support symbolic and hard links.
if tarinfo.issym(): if tarinfo.issym():
if os.path.lexists(targetpath):
# Avoid FileExistsError on following os.symlink.
os.unlink(targetpath)
os.symlink(tarinfo.linkname, targetpath) os.symlink(tarinfo.linkname, targetpath)
else: else:
# See extract(). # See extract().

View File

@ -1346,10 +1346,10 @@ class WriteTest(WriteTestBase, unittest.TestCase):
f.write('something\n') f.write('something\n')
os.symlink(source_file, target_file) os.symlink(source_file, target_file)
with tarfile.open(temparchive, 'w') as tar: with tarfile.open(temparchive, 'w') as tar:
tar.add(source_file) tar.add(source_file, arcname="source")
tar.add(target_file) tar.add(target_file, arcname="symlink")
# Let's extract it to the location which contains the symlink # Let's extract it to the location which contains the symlink
with tarfile.open(temparchive) as tar: with tarfile.open(temparchive, errorlevel=2) as tar:
# this should not raise OSError: [Errno 17] File exists # this should not raise OSError: [Errno 17] File exists
try: try:
tar.extractall(path=tempdir) tar.extractall(path=tempdir)

View File

@ -0,0 +1,4 @@
Extracting a symlink from a tarball should succeed and overwrite the symlink
if it already exists. The fix is to remove the existing file or symlink
before extraction. Based on patch by Chris AtLee, Jeffrey Kintscher, and
Senthil Kumaran.