Construct a TarInfo object from a 512 byte bytes object.
(cls, buf, encoding, errors)
| 1277 | |
| 1278 | @classmethod |
| 1279 | def frombuf(cls, buf, encoding, errors): |
| 1280 | """Construct a TarInfo object from a 512 byte bytes object. |
| 1281 | """ |
| 1282 | if len(buf) == 0: |
| 1283 | raise EmptyHeaderError("empty header") |
| 1284 | if len(buf) != BLOCKSIZE: |
| 1285 | raise TruncatedHeaderError("truncated header") |
| 1286 | if buf.count(NUL) == BLOCKSIZE: |
| 1287 | raise EOFHeaderError("end of file header") |
| 1288 | |
| 1289 | chksum = nti(buf[148:156]) |
| 1290 | if chksum not in calc_chksums(buf): |
| 1291 | raise InvalidHeaderError("bad checksum") |
| 1292 | |
| 1293 | obj = cls() |
| 1294 | obj.name = nts(buf[0:100], encoding, errors) |
| 1295 | obj.mode = nti(buf[100:108]) |
| 1296 | obj.uid = nti(buf[108:116]) |
| 1297 | obj.gid = nti(buf[116:124]) |
| 1298 | obj.size = nti(buf[124:136]) |
| 1299 | obj.mtime = nti(buf[136:148]) |
| 1300 | obj.chksum = chksum |
| 1301 | obj.type = buf[156:157] |
| 1302 | obj.linkname = nts(buf[157:257], encoding, errors) |
| 1303 | obj.uname = nts(buf[265:297], encoding, errors) |
| 1304 | obj.gname = nts(buf[297:329], encoding, errors) |
| 1305 | obj.devmajor = nti(buf[329:337]) |
| 1306 | obj.devminor = nti(buf[337:345]) |
| 1307 | prefix = nts(buf[345:500], encoding, errors) |
| 1308 | |
| 1309 | # Old V7 tar format represents a directory as a regular |
| 1310 | # file with a trailing slash. |
| 1311 | if obj.type == AREGTYPE and obj.name.endswith("/"): |
| 1312 | obj.type = DIRTYPE |
| 1313 | |
| 1314 | # The old GNU sparse format occupies some of the unused |
| 1315 | # space in the buffer for up to 4 sparse structures. |
| 1316 | # Save them for later processing in _proc_sparse(). |
| 1317 | if obj.type == GNUTYPE_SPARSE: |
| 1318 | pos = 386 |
| 1319 | structs = [] |
| 1320 | for i in range(4): |
| 1321 | try: |
| 1322 | offset = nti(buf[pos:pos + 12]) |
| 1323 | numbytes = nti(buf[pos + 12:pos + 24]) |
| 1324 | except ValueError: |
| 1325 | break |
| 1326 | structs.append((offset, numbytes)) |
| 1327 | pos += 24 |
| 1328 | isextended = bool(buf[482]) |
| 1329 | origsize = nti(buf[483:495]) |
| 1330 | obj._sparse_structs = (structs, isextended, origsize) |
| 1331 | |
| 1332 | # Remove redundant slashes from directories. |
| 1333 | if obj.isdir(): |
| 1334 | obj.name = obj.name.rstrip("/") |
| 1335 | |
| 1336 | # Reconstruct a ustar longname. |