Skip to content

Commit 4fedd71

Browse files
authored
bpo-12800: tarfile: Restore fix from 011525e (pythonGH-21409)
Restore fix from 011525e.
1 parent c9c6e9f commit 4fedd71

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Lib/tarfile.py

+3
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,9 @@ def makelink(self, tarinfo, targetpath):
22372237
try:
22382238
# For systems that support symbolic and hard links.
22392239
if tarinfo.issym():
2240+
if os.path.lexists(targetpath):
2241+
# Avoid FileExistsError on following os.symlink.
2242+
os.unlink(targetpath)
22402243
os.symlink(tarinfo.linkname, targetpath)
22412244
else:
22422245
# See extract().

Lib/test/test_tarfile.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1347,10 +1347,10 @@ def test_extractall_symlinks(self):
13471347
f.write('something\n')
13481348
os.symlink(source_file, target_file)
13491349
with tarfile.open(temparchive, 'w') as tar:
1350-
tar.add(source_file)
1351-
tar.add(target_file)
1350+
tar.add(source_file, arcname="source")
1351+
tar.add(target_file, arcname="symlink")
13521352
# Let's extract it to the location which contains the symlink
1353-
with tarfile.open(temparchive) as tar:
1353+
with tarfile.open(temparchive, errorlevel=2) as tar:
13541354
# this should not raise OSError: [Errno 17] File exists
13551355
try:
13561356
tar.extractall(path=tempdir)
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)