@@ -1278,6 +1278,20 @@ def _create_pax_generic_header(cls, pax_headers, type, encoding):
12781278 @classmethod
12791279 def frombuf (cls , buf , encoding , errors ):
12801280 """Construct a TarInfo object from a 512 byte bytes object.
1281+
1282+ To support the old v7 tar format AREGTYPE headers are
1283+ transformed to DIRTYPE headers if their name ends in '/'.
1284+ """
1285+ return cls ._frombuf (buf , encoding , errors )
1286+
1287+ @classmethod
1288+ def _frombuf (cls , buf , encoding , errors , * , dircheck = True ):
1289+ """Construct a TarInfo object from a 512 byte bytes object.
1290+
1291+ If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will
1292+ be normalized to ``DIRTYPE`` if the name ends in a trailing slash.
1293+ ``dircheck`` must be set to ``False`` if this function is called
1294+ on a follow-up header such as ``GNUTYPE_LONGNAME``.
12811295 """
12821296 if len (buf ) == 0 :
12831297 raise EmptyHeaderError ("empty header" )
@@ -1308,7 +1322,7 @@ def frombuf(cls, buf, encoding, errors):
13081322
13091323 # Old V7 tar format represents a directory as a regular
13101324 # file with a trailing slash.
1311- if obj .type == AREGTYPE and obj .name .endswith ("/" ):
1325+ if dircheck and obj .type == AREGTYPE and obj .name .endswith ("/" ):
13121326 obj .type = DIRTYPE
13131327
13141328 # The old GNU sparse format occupies some of the unused
@@ -1343,8 +1357,15 @@ def fromtarfile(cls, tarfile):
13431357 """Return the next TarInfo object from TarFile object
13441358 tarfile.
13451359 """
1360+ return cls ._fromtarfile (tarfile )
1361+
1362+ @classmethod
1363+ def _fromtarfile (cls , tarfile , * , dircheck = True ):
1364+ """
1365+ See dircheck documentation in _frombuf().
1366+ """
13461367 buf = tarfile .fileobj .read (BLOCKSIZE )
1347- obj = cls .frombuf (buf , tarfile .encoding , tarfile .errors )
1368+ obj = cls ._frombuf (buf , tarfile .encoding , tarfile .errors , dircheck = dircheck )
13481369 obj .offset = tarfile .fileobj .tell () - BLOCKSIZE
13491370 return obj ._proc_member (tarfile )
13501371
@@ -1402,7 +1423,7 @@ def _proc_gnulong(self, tarfile):
14021423
14031424 # Fetch the next header and process it.
14041425 try :
1405- next = self .fromtarfile (tarfile )
1426+ next = self ._fromtarfile (tarfile , dircheck = False )
14061427 except HeaderError as e :
14071428 raise SubsequentHeaderError (str (e )) from None
14081429
@@ -1537,7 +1558,7 @@ def _proc_pax(self, tarfile):
15371558
15381559 # Fetch the next header.
15391560 try :
1540- next = self .fromtarfile (tarfile )
1561+ next = self ._fromtarfile (tarfile , dircheck = False )
15411562 except HeaderError as e :
15421563 raise SubsequentHeaderError (str (e )) from None
15431564
0 commit comments