tar
stdlib/extra/tar.hk: ustar archives as bytes.
A tar archive is a sequence of 512-byte header blocks, each followed by its content padded to a block boundary, and terminated by one or more zero blocks. Nothing is compressed and no offset table exists: a reader walks the blocks. That makes tar the plain structure over bytes that a release bundle or a package payload wants underneath a compression layer, and it is why this module has no dependency on gzip or deflate.
Pure Hanki on both tiers, @encapsulated where a builder is used, with no effect and no seam. Reading accepts the POSIX ustar layout and the pax extended headers that record a name, a link target or a size too large for their ustar fields; writing emits ustar only, and a name that does not fit its fields is an error in place of a truncated entry.
Each direction is bounded. read takes a TarCaps: an archive is attacker-supplied input in every use this module has: a declared content size bounds nothing on its own, and a small archive can claim a million entries. A reader that allocated what the headers claim would be a memory bomb before it wrote a byte.
_at
def _at(block: bytes, i: int) -> int
Byte i of block, widened to int, or 0 past the end.
EntryKind
type EntryKind
File
Directory
Symlink(string)
HardLink(string)
end
What an entry is.
TarEntry
struct TarEntry
name: string
kind: EntryKind
mode: int
mtime: int
data: bytes
end
One entry: its metadata in the header block and its content after it.
TarCaps
struct TarCaps
max_entries: int
max_total_bytes: int
end
A reader's budget. There is no default and no unbounded form: an archive is an input, and the caller states what it may cost.
TarError
type TarError
Truncated(int)
BadChecksum(int)
BadMagic(int)
BadNumber(string)
BadTypeflag(int)
BadPax(int)
NotUtf8(int)
TooManyEntries(int)
TooLarge(int)
NameTooLong(string)
end
Why an archive could not be read or written, with the offset or field the problem was found at. The HexError idiom: one variant per failure shape, each carrying what the caller needs to name it.
Header
struct Header
name: string
prefix: string
mode: int
mtime: int
size: int
typeflag: int
linkname: string
end
The fields of one header block that this module reads.
PaxRecords
struct PaxRecords
path: Option<string>
link: Option<string>
size: Option<int>
end
The pax records that override the next entry's header fields.
NameSplit
struct NameSplit
name: string
prefix: string
end
A name split across the two ustar name fields.
zeroblock?
def _zero_block?(block: bytes) -> bool
True when every byte of block is zero, the archive terminator.
fieldtext
def _field_text(block: bytes, start: int, stop: int, at: int) -> Result<string, TarError>
The text in a fixed-width field: everything up to its first NUL.
_octal
def _octal(block: bytes, start: int, stop: int, field: string) -> Result<int, TarError>
An octal number in a fixed-width field: leading spaces are padding, a NUL ends the digits, and an empty field is 0, which is what a directory header writes for its size.
checksumunsigned
def _checksum_unsigned(block: bytes) -> int
The unsigned checksum of a header block, counting the checksum field itself as spaces.
checksumsigned
def _checksum_signed(block: bytes) -> int
The signed checksum: the same sum with each byte read as i8. Old archivers stored high bytes in names and summed them signed. A header matches when either reading agrees with the stored value.
_header
def _header(block: bytes, at: int) -> Result<Header, TarError>
Parse one header block found at at.
fullname
def _full_name(h: Header) -> string
The entry name: the prefix field and the name field joined, as ustar splits a name longer than 100 bytes.
_padded
def _padded(size: int) -> int
Round a content size up to the next block boundary.
_pax
def _pax(payload: bytes, at: int) -> Result<PaxRecords, TarError>
Parse a pax extended header's payload, which is a sequence of records: "<length> <key>=<value>\n", the length counting every byte of the record. A key this module does not read is skipped; path, linkpath and size override the next entry.
_kind
def _kind(typeflag: int, link: string) -> Result<EntryKind, TarError>
The kind an entry's type flag names. A pax or global header is consumed before this runs, and neither reaches it.
read
def read(archive: bytes, caps: TarCaps) -> Result<List<TarEntry>, TarError>
Read every entry in archive, within caps.
A zero block ends the archive, and so does a clean end at a block boundary: older writers omit the terminator, and the block walk already proves where the entries stop. No other departure from the layout is owed patience. A header whose checksum does not match its bytes, a v7 archive with no ustar marker, a type flag this module does not read and a declared size past the end of the archive are each an error, never a guess.
caps = TarCaps(max_entries=16, max_total_bytes=4096)
e = TarEntry(name="a.txt", kind=File, mode=420, mtime=0, data="hi\n".to_bytes())
archive = write([e]).unwrap_or("".to_bytes())
read(archive, caps).unwrap_or([]).length => 1
zerostext
def _zeros_text(count: int) -> string
count NUL characters, the zero fill of a short field or a padding run.
_field
def _field(text: string, width: int) -> string
text as a width-byte field, NUL-filled on the right. A name longer than its field is refused by the caller before this runs.
octaltext
def _octal_text(value: int) -> string
value in octal. int's own rendering is decimal, and a tar header's numeric fields are octal, and the conversion is explicit here.
octaldigits
def _octal_digits(value: int, digits: int, field: string) -> Result<string, TarError>
value as a digits-wide zero-padded octal number.
octalfield
def _octal_field(value: int, width: int, field: string) -> Result<string, TarError>
value as an octal field of width bytes: its digits and a NUL terminator.
headerbytes
def _header_bytes(name: string, prefix: string, mode: int, mtime: int, size: int, typeflag: int, link: string) -> Result<bytes, TarError>
The 512-byte header for one entry, with a zeroed checksum field: the checksum is over the finished bytes, and it is patched in afterwards.
USTARVERSION
_USTAR_VERSION: string = "00"
The ustar version field: the two bytes a POSIX archive writes.
typeflagchar
def _typeflag_char(typeflag: int) -> string
The one-byte type flag as its character. A byte's Display renders the number, and the four flags write emits are spelled here.
checksumfield
def _checksum_field(sum: int) -> Result<bytes, TarError>
The checksum field: six octal digits, a NUL and a space. The sum of 512 bytes never exceeds six octal digits.
splitname
def _split_name(name: string) -> Result<NameSplit, TarError>
Split name into the ustar name and prefix fields at a '/'. A name that fits the 100-byte name field remains whole; a longer one moves its leading directories into the 155-byte prefix field.
write
def write(entries: List<TarEntry>) -> Result<bytes, TarError>
Write entries as a ustar archive.
Every entry is written at its declared mode and mtime, with no user or group names, and the archive ends with two zero blocks. The output is deterministic over the entries: the same list writes the same bytes. A Directory name gains its trailing / here, and a File's content is padded to a block boundary with zeroes.
entry = TarEntry(name="d", kind=Directory, mode=493, mtime=0, data="".to_bytes())
caps = TarCaps(max_entries=16, max_total_bytes=4096)
archive = write([entry]).unwrap_or("".to_bytes())
read(archive, caps).map(|es| es.get(0).map(|e| e.name).unwrap_or("")).unwrap_or("") => "d/"
paxentry
def _pax_entry(payload: bytes) -> bytes
A pax extended header entry for payload, the one form write does not emit: a reader is tested against a header a pax writer would produce.
paxrecord
def _pax_record(key: string, value: string) -> string
One pax record: its length counts every byte of the record, the newline included.
_CAPS
_CAPS: TarCaps = TarCaps(max_entries=16, max_total_bytes=4096)
_cat
def _cat(parts: List<bytes>) -> bytes
parts joined into one buffer, the test-side spelling of bytes + bytes.
_file
def _file(name: string, text: string) -> TarEntry
_rechecksum
def _rechecksum(archive: bytes) -> Option<bytes>
Rewrite archive's first header checksum from its current bytes, which lets a test corrupt a field and still reach the check it is about.