Construct a TarInfo object from a 512 byte bytes object. If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will be normalized to ``DIRTYPE`` if the name ends in a trailing slash. ``dircheck`` must be set to ``False`` if this function is called on a follow-
(cls, buf, encoding, errors, *, dircheck=True)
| 1284 | |
| 1285 | @classmethod |
| 1286 | def _frombuf(cls, buf, encoding, errors, *, dircheck=True): |
| 1287 | """Construct a TarInfo object from a 512 byte bytes object. |
| 1288 | |
| 1289 | If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will |
| 1290 | be normalized to ``DIRTYPE`` if the name ends in a trailing slash. |
| 1291 | ``dircheck`` must be set to ``False`` if this function is called |
| 1292 | on a follow-up header such as ``GNUTYPE_LONGNAME``. |
| 1293 | """ |
| 1294 | if len(buf) == 0: |
| 1295 | raise EmptyHeaderError("empty header") |
| 1296 | if len(buf) != BLOCKSIZE: |
| 1297 | raise TruncatedHeaderError("truncated header") |
| 1298 | if buf.count(NUL) == BLOCKSIZE: |
| 1299 | raise EOFHeaderError("end of file header") |
| 1300 | |
| 1301 | chksum = nti(buf[148:156]) |
| 1302 | if chksum not in calc_chksums(buf): |
| 1303 | raise InvalidHeaderError("bad checksum") |
| 1304 | |
| 1305 | obj = cls() |
| 1306 | obj.name = nts(buf[0:100], encoding, errors) |
| 1307 | obj.mode = nti(buf[100:108]) |
| 1308 | obj.uid = nti(buf[108:116]) |
| 1309 | obj.gid = nti(buf[116:124]) |
| 1310 | obj.size = nti(buf[124:136]) |
| 1311 | obj.mtime = nti(buf[136:148]) |
| 1312 | obj.chksum = chksum |
| 1313 | obj.type = buf[156:157] |
| 1314 | obj.linkname = nts(buf[157:257], encoding, errors) |
| 1315 | obj.uname = nts(buf[265:297], encoding, errors) |
| 1316 | obj.gname = nts(buf[297:329], encoding, errors) |
| 1317 | obj.devmajor = nti(buf[329:337]) |
| 1318 | obj.devminor = nti(buf[337:345]) |
| 1319 | prefix = nts(buf[345:500], encoding, errors) |
| 1320 | |
| 1321 | # Old V7 tar format represents a directory as a regular |
| 1322 | # file with a trailing slash. |
| 1323 | if dircheck and obj.type == AREGTYPE and obj.name.endswith("/"): |
| 1324 | obj.type = DIRTYPE |
| 1325 | |
| 1326 | # The old GNU sparse format occupies some of the unused |
| 1327 | # space in the buffer for up to 4 sparse structures. |
| 1328 | # Save them for later processing in _proc_sparse(). |
| 1329 | if obj.type == GNUTYPE_SPARSE: |
| 1330 | pos = 386 |
| 1331 | structs = [] |
| 1332 | for i in range(4): |
| 1333 | try: |
| 1334 | offset = nti(buf[pos:pos + 12]) |
| 1335 | numbytes = nti(buf[pos + 12:pos + 24]) |
| 1336 | except ValueError: |
| 1337 | break |
| 1338 | structs.append((offset, numbytes)) |
| 1339 | pos += 24 |
| 1340 | isextended = bool(buf[482]) |
| 1341 | origsize = nti(buf[483:495]) |
| 1342 | obj._sparse_structs = (structs, isextended, origsize) |
| 1343 |
no test coverage detected