Documentation
¶
Overview ¶
Package gzipidx builds a random-access index for a gzip stream: restart checkpoints at deflate block boundaries plus the entry directory of the tar inside, so later commands decompress only the region they need.
Deflate cannot be restarted anywhere — a block's Huffman tables are defined at the block's start — so checkpoints are recorded exactly at block boundaries while scanning the stream once with the block-aware scanner in this package. Reading then restarts the stock (fast) decompressor at a checkpoint, using a bit-shifted view of the compressed bytes plus the saved 32 KiB window, and verifies the result against the recorder context.
Index ¶
- Constants
- Variables
- func LooksLikeTarHeader(block []byte) bool
- func ScanBlockBoundaries(r io.Reader, onBlock BlockCallback, toSink sink) (int64, error)
- func ScanMapped(data []byte, onBlock BlockCallback, toSink sink) (int64, error)
- type BlockCallback
- type BuildOptions
- type Checkpoint
- type Entry
- type Index
- type NestedOptions
- type Reader
Constants ¶
const Version = 1
Version is the index format version.
Variables ¶
var Magic = [8]byte{'u', 'd', 'f', 'g', 'z', 'i', 'd', 'x'}
Magic identifies an index blob.
Functions ¶
func LooksLikeTarHeader ¶
LooksLikeTarHeader reports whether a 512-byte block is a plausible tar member header (its checksum matches), which is how callers recognise a member of an outer tar that is itself a tar stream.
func ScanBlockBoundaries ¶
func ScanBlockBoundaries(r io.Reader, onBlock BlockCallback, toSink sink) (int64, error)
ScanBlockBoundaries decodes the deflate stream in r, invoking onBlock at every block start, and forwards decompressed bytes to toSink (which may be nil). It returns the total decompressed size.
func ScanMapped ¶
func ScanMapped(data []byte, onBlock BlockCallback, toSink sink) (int64, error)
ScanMapped is ScanBlockBoundaries for a deflate stream that is already in memory (typically a read-only mapping of the file). The scanner decodes the bytes in place, which saves a copy of the whole compressed stream.
Types ¶
type BlockCallback ¶
BlockCallback is called at every deflate block start with the compressed bit position and the decompressed offset the block starts at. window returns a copy of up to maxWindow bytes preceding that offset (only called when the caller wants to keep a restart point). Returning false stops the scan.
type BuildOptions ¶
type BuildOptions struct {
// Checkpoints is the target number of restart points (default 48).
Checkpoints int
// CandidateSpacing overrides how often a candidate checkpoint is taken
// (default 64 MiB of output).
CandidateSpacing int64
// NoDirectory skips recording tar member offsets.
NoDirectory bool
// Nested, when set, also parses selected tar members of the scanned
// stream as nested tar streams during the same pass (see NestedOptions).
// It needs the directory walk, so it is ignored together with
// NoDirectory.
Nested *NestedOptions
// Progress, when set, receives the decompressed offset.
Progress func(int64)
}
BuildOptions tunes a build.
type Checkpoint ¶
Checkpoint is a restart point at a deflate block boundary: decoding the stream from BitPos (relative to the deflate stream start) with Window as the initial history reproduces the stream from OutPos.
type Index ¶
type Index struct {
FileSize int64
DeflateStart int64
TotalOut int64
Checks []Checkpoint
Entries []Entry
SingleMember bool
}
Index is the persisted result of a scan.
func Build ¶
func Build(path string, opts BuildOptions) (*Index, error)
Build scans a gzip file once and returns its index.
func (*Index) CheckpointFor ¶
func (ix *Index) CheckpointFor(outPos int64) *Checkpoint
CheckpointFor returns the last checkpoint at or before outPos, or nil for the start of the stream.
type NestedOptions ¶
type NestedOptions struct {
// Members decides whether a tar member is parsed as a nested tar stream.
// It is called once per member with the member's name, size and the first
// 512 bytes of its payload (nil when the member is shorter than a block).
// It must be cheap: it runs inside the scan.
Members func(name string, size int64, head []byte) bool
// Headers is called once per captured member, after the scan and in member
// order, with the headers of its nested tar stream. Members that were not
// selected, or whose stream could not be replayed faithfully (PAX size
// overrides, sparse files, a broken header), are simply absent — the
// caller reads those through the index instead.
Headers func(name string, headers []*tar.Header) error
// Budget caps the header bytes kept in memory across all captured members.
// Zero means 256 MiB.
Budget int64
// Workers bounds how many captured members are parsed at once; zero means
// one per core.
Workers int
}
A docker-save or OCI archive is a tar whose members are themselves tars (the layers). Indexing such an archive already walks every member header, and the layer directories are wanted immediately afterwards — to list or extract the merged rootfs — but the only other way to them is to decompress every layer again through the index. NestedOptions captures the header bytes of selected members during the scan, so those layers are parsed from bytes that were already in hand, with file contents skipped rather than decoded a second time.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides random access into an indexed gzip file: it restarts the decompressor at the checkpoint before a requested offset and decodes only as far as needed.
func OpenReader ¶
OpenReader opens the indexed file for random access.