]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1338314, Bug #1336623: fix tarfile so it can extract
authorNeal Norwitz <nnorwitz@gmail.com>
Fri, 28 Oct 2005 05:52:22 +0000 (05:52 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Fri, 28 Oct 2005 05:52:22 +0000 (05:52 +0000)
REGTYPE directories from tarfiles written by old programs.

Will backport.

Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index c86248c1a4749f4854a98d3b8c2fa58445971fd4..0b3d4771719c292b7983e68af521316dcdff3f8a 100644 (file)
@@ -739,6 +739,11 @@ class TarInfo(object):
             tarinfo.devmajor = tarinfo.devmajor = 0
         tarinfo.prefix = buf[345:500]
 
+        # Some old tar programs represent a directory as a regular
+        # file with a trailing slash.
+        if tarinfo.isreg() and tarinfo.name.endswith("/"):
+            tarinfo.type = DIRTYPE
+
         # The prefix field is used for filenames > 100 in
         # the POSIX standard.
         # name = prefix + '/' + name
@@ -746,7 +751,7 @@ class TarInfo(object):
             tarinfo.name = normpath(os.path.join(nts(tarinfo.prefix), tarinfo.name))
 
         # Directory names should have a '/' at the end.
-        if tarinfo.isdir() and tarinfo.name[-1:] != "/":
+        if tarinfo.isdir():
             tarinfo.name += "/"
         return tarinfo
 
@@ -1716,10 +1721,6 @@ class TarFile(object):
             # Skip the following data blocks.
             self.offset += self._block(tarinfo.size)
 
-        if tarinfo.isreg() and tarinfo.name[:-1] == "/":
-            # some old tar programs don't know DIRTYPE
-            tarinfo.type = DIRTYPE
-
         self.members.append(tarinfo)
         return tarinfo
 
index b202ea586873fd58385d20055c4c65c736ad1d8d..98349c4ebfd573ee850ef8c4d74266f341dd0d41 100644 (file)
@@ -144,6 +144,36 @@ class ReadTest(BaseTest):
                          "readlines() after seek failed")
             fobj.close()
 
+    def test_old_dirtype(self):
+        """Test old style dirtype member (bug #1336623).
+        """
+        # Old tars create directory members using a REGTYPE
+        # header with a "/" appended to the filename field.
+
+        # Create an old tar style directory entry.
+        filename = tmpname()
+        tarinfo = tarfile.TarInfo("directory/")
+        tarinfo.type = tarfile.REGTYPE
+
+        fobj = file(filename, "w")
+        fobj.write(tarinfo.tobuf())
+        fobj.close()
+
+        try:
+            # Test if it is still a directory entry when
+            # read back.
+            tar = tarfile.open(filename)
+            tarinfo = tar.getmembers()[0]
+            tar.close()
+
+            self.assert_(tarinfo.type == tarfile.DIRTYPE)
+            self.assert_(tarinfo.name.endswith("/"))
+        finally:
+            try:
+                os.unlink(filename)
+            except:
+                pass
+
 class ReadStreamTest(ReadTest):
     sep = "|"
 
index 8ee1e35bf99ed1bb233d4093da515c4aa1974a48..e88311f8ed82d3cddc2e4164d4f09c285ea99113 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -177,6 +177,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Patch #1338314, Bug #1336623: fix tarfile so it can extract
+  REGTYPE directories from tarfiles written by old programs.
+
 - Get bsddb module to build with BSD DB version 3.2
 
 - Patch #1309009, Fix segfault in pyexpat when the XML document is in latin_1,