]> granicus.if.org Git - python/commitdiff
Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
authorSenthil Kumaran <orsenthil@gmail.com>
Thu, 28 Apr 2011 07:30:31 +0000 (15:30 +0800)
committerSenthil Kumaran <orsenthil@gmail.com>
Thu, 28 Apr 2011 07:30:31 +0000 (15:30 +0800)
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 47be1d49df8f426894c0e474830d6dd74c043e17..105a75880dbe40ff6743b1c5c57ecd1c6ae4adb7 100644 (file)
@@ -2239,6 +2239,8 @@ class TarFile(object):
         if hasattr(os, "symlink") and hasattr(os, "link"):
             # For systems that support symbolic and hard links.
             if tarinfo.issym():
+                if os.path.exists(targetpath):
+                    os.unlink(targetpath)
                 os.symlink(tarinfo.linkname, targetpath)
             else:
                 # See extract().
index cda5262acc84bc79271ed3a8802d3540a5a62fe2..ef3bf8b4110f2eab7d87ddee9034ff0eb3bb8d29 100644 (file)
@@ -843,6 +843,33 @@ class WriteTest(WriteTestBase):
         finally:
             os.chdir(cwd)
 
+    def test_extractall_symlinks(self):
+        # Test if extractall works properly when tarfile contains symlinks
+        tempdir = os.path.join(TEMPDIR, "testsymlinks")
+        temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
+        os.mkdir(tempdir)
+        try:
+            source_file = os.path.join(tempdir,'source')
+            target_file = os.path.join(tempdir,'symlink')
+            with open(source_file,'w') as f:
+                f.write('something\n')
+            os.symlink(source_file, target_file)
+            tar = tarfile.open(temparchive,'w')
+            tar.add(source_file, arcname=os.path.basename(source_file))
+            tar.add(target_file, arcname=os.path.basename(target_file))
+            tar.close()
+            # Let's extract it to the location which contains the symlink
+            tar = tarfile.open(temparchive,'r')
+            # this should not raise OSError: [Errno 17] File exists
+            try:
+                tar.extractall(path=tempdir)
+            except OSError:
+                self.fail("extractall failed with symlinked files")
+            finally:
+                tar.close()
+        finally:
+            os.unlink(temparchive)
+            shutil.rmtree(tempdir)
 
 class StreamWriteTest(WriteTestBase):
 
index 3320e3d3e1de7652eabfd3a784a3f98f901b38ac..bb6f5ef362d724aab7b52574f3a2a31878e283af 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -65,6 +65,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10761: Fix tarfile.extractall failure  when symlinked files are
+  present. Initial patch by Scott Leerssen.
+
 - Issue #11763: don't use difflib in TestCase.assertMultiLineEqual if the
   strings are too long.