Skip to content

Commit bda2e68

Browse files
bpo-12800: tarfile: Restore fix from 011525e (GH-21409)
Restore fix from 011525e. (cherry picked from commit 4fedd71) Co-authored-by: Julien Palard <julien@palard.fr>
1 parent 8388a33 commit bda2e68

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Lib/tarfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,9 @@ def makelink(self, tarinfo, targetpath):
22282228
try:
22292229
# For systems that support symbolic and hard links.
22302230
if tarinfo.issym():
2231+
if os.path.lexists(targetpath):
2232+
# Avoid FileExistsError on following os.symlink.
2233+
os.unlink(targetpath)
22312234
os.symlink(tarinfo.linkname, targetpath)
22322235
else:
22332236
# See extract().

Lib/test/test_tarfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,10 +1314,10 @@ def test_extractall_symlinks(self):
13141314
f.write('something\n')
13151315
os.symlink(source_file, target_file)
13161316
with tarfile.open(temparchive, 'w') as tar:
1317-
tar.add(source_file)
1318-
tar.add(target_file)
1317+
tar.add(source_file, arcname="source")
1318+
tar.add(target_file, arcname="symlink")
13191319
# Let's extract it to the location which contains the symlink
1320-
with tarfile.open(temparchive) as tar:
1320+
with tarfile.open(temparchive, errorlevel=2) as tar:
13211321
# this should not raise OSError: [Errno 17] File exists
13221322
try:
13231323
tar.extractall(path=tempdir)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Extracting a symlink from a tarball should succeed and overwrite the symlink
2+
if it already exists. The fix is to remove the existing file or symlink
3+
before extraction. Based on patch by Chris AtLee, Jeffrey Kintscher, and
4+
Senthil Kumaran.

0 commit comments

Comments
 (0)