gzipidx

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Version = 1

Version is the index format version.

Variables

View Source
var Magic = [8]byte{'u', 'd', 'f', 'g', 'z', 'i', 'd', 'x'}

Magic identifies an index blob.

Functions

func LooksLikeTarHeader

func LooksLikeTarHeader(block []byte) bool

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

type BlockCallback func(bitPos, outPos int64, window func() []byte) bool

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

type Checkpoint struct {
	BitPos int64
	OutPos int64
	Window []byte
	Ctx    []byte
}

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 Entry

type Entry struct {
	Name   string
	OutOff int64
	Size   int64
}

Entry is one tar member of the decompressed stream.

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 Load

func Load(path string) (*Index, error)

Load reads an index written by Save.

func ReadIndex

func ReadIndex(r io.Reader) (*Index, error)

ReadIndex decodes an index from r.

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.

func (*Index) Lookup

func (ix *Index) Lookup(name string) (Entry, bool)

Lookup finds a tar member by exact name.

func (*Index) Save

func (ix *Index) Save(path string) error

Save writes the index in a compact binary form.

func (*Index) Usable

func (ix *Index) Usable() bool

Usable reports whether the index can serve random access.

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

func OpenReader(path string, ix *Index) (*Reader, error)

OpenReader opens the indexed file for random access.

func (*Reader) Close

func (r *Reader) Close() error

func (*Reader) Index

func (r *Reader) Index() *Index

Index returns the index backing this reader.

func (*Reader) ReadAt

func (r *Reader) ReadAt(outPos int64) (io.ReadCloser, error)

ReadAt returns a reader over the decompressed stream starting at outPos. The caller must close it before the next call.

func (*Reader) ReadRange

func (r *Reader) ReadRange(outPos, n int64) ([]byte, error)

ReadRange reads exactly n bytes at outPos.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL