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

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

index ed7b509eafdbbd22a0902649e423f53c9bae4c8b..9a4e515e4c6cfcaa0a9d1c50d9742923c474912a 100644 (file)
@@ -688,6 +688,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
@@ -695,7 +700,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
 
@@ -1628,10 +1633,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 7362af370a6436229166839c35adbe7b97e758aa..53dd2381d6bc7acea31ea41d53ff331aeb6e0f09 100644 (file)
@@ -134,6 +134,30 @@ 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()
+
+        # 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("/"))
+
 class ReadStreamTest(ReadTest):
     sep = "|"
 
index ab43a0ca9031378ae49b21aaf2b865945f49c524..c22d598c406d1658229599cf7535b15c5d5cf34d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Patch #1338314, Bug #1336623: fix tarfile so it can extract
+  REGTYPE directories from tarfiles written by old programs.
+
 - Patch #1309009, Fix segfault in pyexpat when the XML document is in latin_1,
   but Python incorrectly assumes it is in UTF-8 format