]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-12800: tarfile: Restore fix from 011525ee9 (GH-21409)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 25 Nov 2020 10:01:01 +0000 (02:01 -0800)
committerGitHub <noreply@github.com>
Wed, 25 Nov 2020 10:01:01 +0000 (02:01 -0800)
Restore fix from 011525ee92eb1c13ad1a62d28725a840e28f8160.
(cherry picked from commit 4fedd7123eaf147edd55eabbbd72e0bcc8368e47)

Co-authored-by: Julien Palard <julien@palard.fr>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2020-07-09-11-32-28.bpo-12800.fNgWwx.rst [new file with mode: 0644]

index 39f63b9cfc1ac90baec69c64b7d1ab919808aa0a..30150ec6cca6377ac54ece6e0b62de8da12ca324 100755 (executable)
@@ -2228,6 +2228,9 @@ class TarFile(object):
         try:
             # For systems that support symbolic and hard links.
             if tarinfo.issym():
+                if os.path.lexists(targetpath):
+                    # Avoid FileExistsError on following os.symlink.
+                    os.unlink(targetpath)
                 os.symlink(tarinfo.linkname, targetpath)
             else:
                 # See extract().
index be717e315700ca639507b58fa56ff55b6161a329..bee00c56d610176913db348bc8528cc17221fad5 100644 (file)
@@ -1314,10 +1314,10 @@ class WriteTest(WriteTestBase, unittest.TestCase):
                 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)
diff --git a/Misc/NEWS.d/next/Library/2020-07-09-11-32-28.bpo-12800.fNgWwx.rst b/Misc/NEWS.d/next/Library/2020-07-09-11-32-28.bpo-12800.fNgWwx.rst
new file mode 100644 (file)
index 0000000..fdd7c5e
--- /dev/null
@@ -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.