@@ -1277,6 +1277,20 @@ def _create_pax_generic_header(cls, pax_headers, type, encoding):
12771277 @classmethod
12781278 def frombuf (cls , buf , encoding , errors ):
12791279 """Construct a TarInfo object from a 512 byte bytes object.
1280+
1281+ To support the old v7 tar format AREGTYPE headers are
1282+ transformed to DIRTYPE headers if their name ends in '/'.
1283+ """
1284+ return cls ._frombuf (buf , encoding , errors )
1285+
1286+ @classmethod
1287+ def _frombuf (cls , buf , encoding , errors , * , dircheck = True ):
1288+ """Construct a TarInfo object from a 512 byte bytes object.
1289+
1290+ If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will
1291+ be normalized to ``DIRTYPE`` if the name ends in a trailing slash.
1292+ ``dircheck`` must be set to ``False`` if this function is called
1293+ on a follow-up header such as ``GNUTYPE_LONGNAME``.
12801294 """
12811295 if len (buf ) == 0 :
12821296 raise EmptyHeaderError ("empty header" )
@@ -1307,7 +1321,7 @@ def frombuf(cls, buf, encoding, errors):
13071321
13081322 # Old V7 tar format represents a directory as a regular
13091323 # file with a trailing slash.
1310- if obj .type == AREGTYPE and obj .name .endswith ("/" ):
1324+ if dircheck and obj .type == AREGTYPE and obj .name .endswith ("/" ):
13111325 obj .type = DIRTYPE
13121326
13131327 # The old GNU sparse format occupies some of the unused
@@ -1342,8 +1356,15 @@ def fromtarfile(cls, tarfile):
13421356 """Return the next TarInfo object from TarFile object
13431357 tarfile.
13441358 """
1359+ return cls ._fromtarfile (tarfile )
1360+
1361+ @classmethod
1362+ def _fromtarfile (cls , tarfile , * , dircheck = True ):
1363+ """
1364+ See dircheck documentation in _frombuf().
1365+ """
13451366 buf = tarfile .fileobj .read (BLOCKSIZE )
1346- obj = cls .frombuf (buf , tarfile .encoding , tarfile .errors )
1367+ obj = cls ._frombuf (buf , tarfile .encoding , tarfile .errors , dircheck = dircheck )
13471368 obj .offset = tarfile .fileobj .tell () - BLOCKSIZE
13481369 return obj ._proc_member (tarfile )
13491370
@@ -1401,7 +1422,7 @@ def _proc_gnulong(self, tarfile):
14011422
14021423 # Fetch the next header and process it.
14031424 try :
1404- next = self .fromtarfile (tarfile )
1425+ next = self ._fromtarfile (tarfile , dircheck = False )
14051426 except HeaderError as e :
14061427 raise SubsequentHeaderError (str (e )) from None
14071428
@@ -1536,7 +1557,7 @@ def _proc_pax(self, tarfile):
15361557
15371558 # Fetch the next header.
15381559 try :
1539- next = self .fromtarfile (tarfile )
1560+ next = self ._fromtarfile (tarfile , dircheck = False )
15401561 except HeaderError as e :
15411562 raise SubsequentHeaderError (str (e )) from None
15421563
0 commit comments