Skip to content

bpo-12800: tarfile: Restore fix from 011525ee9 #21409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,9 @@ def makelink(self, tarinfo, targetpath):
try:
# For systems that support symbolic and hard links.
if tarinfo.issym():
if os.path.lexists(targetpath):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JulienPalard, perhaps include a short comment here explaining why this is needed? Something along the lines of "tar should overwrite existing files with symlinks, but os.symlink raises an exception rather than overwriting."

# Avoid FileExistsError on following os.symlink.
os.unlink(targetpath)
os.symlink(tarinfo.linkname, targetpath)
else:
# See extract().
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,10 @@ def test_extractall_symlinks(self):
f.write('something\n')
os.symlink(source_file, target_file)
with tarfile.open(temparchive, 'w') as tar:
tar.add(source_file)
tar.add(target_file)
tar.add(source_file, arcname="source")
tar.add(target_file, arcname="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
try:
tar.extractall(path=tempdir)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.