# valid, though the header is not truly POSIX conforming. */
#
#
+TAR_HDR_OFF_MAGIC = 257
TAR_FMT_OLDGNU_MAGIC = b"ustar "
def read_gnu_tar_hdr (data):
}
+def tar_hdr_check_chksum (data):
+ hdr = read_gnu_tar_hdr (data)
+ if hdr is None:
+ return False
+ s = calc_chksums (data)
+ return nti (hdr ["checksum"]) in s
+
+
def readable_tar_objects_offsets (ifd):
"""
Traverse blocks in file, trying to extract tar headers.
pos = 0
offsets = []
+ mm = mmap.mmap(ifd, 0, mmap.MAP_SHARED, mmap.PROT_READ)
+ pos = TAR_HDR_OFF_MAGIC
+
while True:
- blk = os.read (ifd, BLOCKSIZE)
- if len (blk) != BLOCKSIZE:
+ pos = mm.find (TAR_FMT_OLDGNU_MAGIC, pos)
+ if pos == -1:
break
- hdr = read_gnu_tar_hdr (blk)
- if hdr is not None:
- offsets.append (pos)
- pos += BLOCKSIZE
+ off = pos - TAR_HDR_OFF_MAGIC
+ mm.seek (off)
+ blk = mm.read (BLOCKSIZE)
+ if tar_hdr_check_chksum (blk) is True:
+ offsets.append (off)
+ pos += 1
return offsets
assert os.lseek (infd, 0, os.SEEK_SET) == 0
outfd = os.open (os.path.dirname (aname), os.O_WRONLY | os.O_TMPFILE,
stat.S_IRUSR | stat.S_IWUSR)
- junk = os.urandom (512) # tar block sized
+ junk = os.urandom (42)
# write new file with garbage prepended
done = 0