Construct a TarInfo object from a 512 byte bytes object.
(cls, buf, encoding, errors)
| 1224 | |
| 1225 | @classmethod |
| 1226 | def frombuf(cls, buf, encoding, errors): |
| 1227 | """Construct a TarInfo object from a 512 byte bytes object. |
| 1228 | """ |
| 1229 | if len(buf) == 0: |
| 1230 | raise EmptyHeaderError("empty header") |
| 1231 | if len(buf) != BLOCKSIZE: |
| 1232 | raise TruncatedHeaderError("truncated header") |
| 1233 | if buf.count(NUL) == BLOCKSIZE: |
| 1234 | raise EOFHeaderError("end of file header") |
| 1235 | |
| 1236 | chksum = nti(buf[148:156]) |
| 1237 | if chksum not in calc_chksums(buf): |
| 1238 | raise InvalidHeaderError("bad checksum") |
| 1239 | |
| 1240 | obj = cls() |
| 1241 | obj.name = nts(buf[0:100], encoding, errors) |
| 1242 | obj.mode = nti(buf[100:108]) |
| 1243 | obj.uid = nti(buf[108:116]) |
| 1244 | obj.gid = nti(buf[116:124]) |
| 1245 | obj.size = nti(buf[124:136]) |
| 1246 | obj.mtime = nti(buf[136:148]) |
| 1247 | obj.chksum = chksum |
| 1248 | obj.type = buf[156:157] |
| 1249 | obj.linkname = nts(buf[157:257], encoding, errors) |
| 1250 | obj.uname = nts(buf[265:297], encoding, errors) |
| 1251 | obj.gname = nts(buf[297:329], encoding, errors) |
| 1252 | obj.devmajor = nti(buf[329:337]) |
| 1253 | obj.devminor = nti(buf[337:345]) |
| 1254 | prefix = nts(buf[345:500], encoding, errors) |
| 1255 | |
| 1256 | # Old V7 tar format represents a directory as a regular |
| 1257 | # file with a trailing slash. |
| 1258 | if obj.type == AREGTYPE and obj.name.endswith("/"): |
| 1259 | obj.type = DIRTYPE |
| 1260 | |
| 1261 | # The old GNU sparse format occupies some of the unused |
| 1262 | # space in the buffer for up to 4 sparse structures. |
| 1263 | # Save them for later processing in _proc_sparse(). |
| 1264 | if obj.type == GNUTYPE_SPARSE: |
| 1265 | pos = 386 |
| 1266 | structs = [] |
| 1267 | for i in range(4): |
| 1268 | try: |
| 1269 | offset = nti(buf[pos:pos + 12]) |
| 1270 | numbytes = nti(buf[pos + 12:pos + 24]) |
| 1271 | except ValueError: |
| 1272 | break |
| 1273 | structs.append((offset, numbytes)) |
| 1274 | pos += 24 |
| 1275 | isextended = bool(buf[482]) |
| 1276 | origsize = nti(buf[483:495]) |
| 1277 | obj._sparse_structs = (structs, isextended, origsize) |
| 1278 | |
| 1279 | # Remove redundant slashes from directories. |
| 1280 | if obj.isdir(): |
| 1281 | obj.name = obj.name.rstrip("/") |
| 1282 | |
| 1283 | # Reconstruct a ustar longname. |
no test coverage detected