Documentation
¶
Overview ¶
Package zrecipe makes compressed files reproducible from their uncompressed content: Analyze finds the engine and parameters that re-create a gzip or zstd file exactly, and Recompress rebuilds it.
Index ¶
Constants ¶
const DefaultMaxInMemory = 64 << 20
DefaultMaxInMemory is the spool size above which content goes to a temp file.
const ParamsVersion = 1
ParamsVersion is the schema version written by this package.
Variables ¶
var ( // ErrUnsupported reports an input the library recognises but does not // handle: multi-member gzip, multi-frame zstd, skippable frames, // dictionaries. ErrUnsupported = errors.New("zrecipe: unsupported input") // ErrCorrupt reports an input that failed to decompress or verify. ErrCorrupt = errors.New("zrecipe: corrupt input") // ErrNotReproducible reports that no candidate reproduced the input. ErrNotReproducible = errors.New("zrecipe: not reproducible") // ErrInputMismatch reports uncompressed input that does not match Params. ErrInputMismatch = errors.New("zrecipe: uncompressed input does not match params") // ErrDigestMismatch reports recompressed output that does not match Params. ErrDigestMismatch = errors.New("zrecipe: recompressed output does not match params") ErrEngineUnavailable = errors.New("zrecipe: engine unavailable") // ErrEngineVersionMismatch reports an engine version different from Params. ErrEngineVersionMismatch = errors.New("zrecipe: engine version mismatch") // ErrParamsVersion reports an unknown Params schema version. ErrParamsVersion = errors.New("zrecipe: unsupported params version") // ErrInvalidParams reports Params that are internally inconsistent. ErrInvalidParams = errors.New("zrecipe: invalid params") )
Functions ¶
func DefaultEngines ¶
DefaultEngines returns every engine compiled into the binary, most likely producers of files from the wild first: the GNU gzip port, then the cgo engines zlib, pigz and libzstd (present only when built with cgo), then the remaining pure-Go engines, with the klauspost/pgzip port after the klauspost/compress engine whose current version it does not share.
func Recompress ¶
func Recompress(ctx context.Context, p *Params, uncompressed io.Reader, w io.Writer, opts *RecompressOptions) error
Recompress rebuilds the compressed file described by p from uncompressed and streams it to w. It verifies the input against p.Uncompressed and the output against p.Compressed. Bytes already written to w are not rolled back on error; write to a temporary file and rename on success.
Types ¶
type Analysis ¶ added in v0.5.0
type Analysis struct {
// contains filtered or unexported fields
}
Analysis is a decompressed input together with the candidate the elimination settled on, waiting for Confirm to reproduce the input from the content while streaming that content to the caller. Close releases the spool holding the content.
func Start ¶ added in v0.5.0
Start detects the format of r, decompresses it once into a spool while hashing both streams, validates the container, and eliminates the candidates down to one. It returns ErrNotReproducible when every candidate diverged from the input inside the elimination, and ErrUnsupported, ErrCorrupt or the context's error as Analyze does. For an uncompressed input it hashes r and there is nothing to eliminate. The caller must Close the returned Analysis.
func (*Analysis) Close ¶ added in v0.5.0
Close releases the spool. It is idempotent; Confirm fails after it.
func (*Analysis) Compressed ¶ added in v0.5.0
Compressed is the BLAKE3 digest and size of the input.
func (*Analysis) Confirm ¶ added in v0.5.0
Confirm reads the content once, writes every block to tee (nil to skip) and rebuilds the input from it through Recompress's own code, comparing the output with the input byte for byte. It returns the Params on success; ErrNotReproducible, naming the offset, when the output diverges; tee's own error, wrapped, when a tee write fails; the context's error; or an engine error. After a failure tee has received a prefix of the content. A block reaches tee before the rebuilder sees it, so tee may be a few blocks ahead of the comparison when the pass stops. With Options.VerifyLimit set the rebuild stops once that many bytes matched and the rest of the content goes to tee alone. Confirm may be called once.
func (*Analysis) Uncompressed ¶ added in v0.5.0
Uncompressed is the BLAKE3 digest and size of the decompressed content.
type DeflateParams ¶
type DeflateParams = engine.DeflateParams
Parameter types are defined in package engine and re-exported here.
type GzipParams ¶
type GzipParams = engine.GzipParams
Parameter types are defined in package engine and re-exported here.
type Options ¶
type Options struct {
// TempDir holds the spool for large inputs. Default os.TempDir().
TempDir string
// MaxInMemory is the spool size kept in memory. Default DefaultMaxInMemory.
MaxInMemory int64
// Parallelism is the number of candidates evaluated at once. Default
// runtime.NumCPU(). Takes effect only when r implements io.ReaderAt.
Parallelism int
// Uncompressed, if set, receives the decompressed content while
// Analyze confirms the parameters it found (the confirming pass, see
// Analysis.Confirm), in order and in engine.FeedSize writes. An input
// that is not reproducible writes nothing to it; one whose confirmation
// fails writes a prefix.
Uncompressed io.Writer
// Engines to search. Default DefaultEngines().
Engines []engine.Engine
// VerifyLimit, when positive, accepts a candidate once it has
// reproduced this many bytes of the compressed input instead of
// running it to the end, both in the search and in the confirming
// pass: a candidate that matches that far and diverges later is rare
// enough that recompressing the rest of a large input is not worth
// its time. The confirming pass still streams the whole content to
// its tee. Zero, the default, verifies the whole input. Recompress
// checks the output digest, so a divergence past the limit surfaces
// there.
VerifyLimit int64
}
Options configures Analyze. The zero value uses the defaults.
type Params ¶
type Params struct {
Version int `json:"version"`
Format Format `json:"format"`
Compressed Digest `json:"compressed"`
Uncompressed Digest `json:"uncompressed"`
Engine string `json:"engine,omitempty"`
EngineVersion string `json:"engine_version,omitempty"`
Gzip *GzipParams `json:"gzip,omitempty"`
Zstd *ZstdParams `json:"zstd,omitempty"`
}
Params records how to rebuild a compressed file from its content.
func Analyze ¶
Analyze detects the format of r, decompresses it once while hashing both streams, finds an engine and parameters that reproduce r exactly and confirms them through the pull path: it is Start, Confirm with Options.Uncompressed as the tee, and Close. For an uncompressed input it returns Params with FormatNone.
func ReadParams ¶
ReadParams decodes and validates a Params document.
type RecompressOptions ¶
type RecompressOptions struct {
// Engines to look the recorded engine up in. Default DefaultEngines().
Engines []engine.Engine
// AllowVersionMismatch tries the engine even when its version differs
// from the recorded one. The digest check still decides the outcome.
AllowVersionMismatch bool
}
RecompressOptions configures Recompress. The zero value uses the defaults.
type ZstdParams ¶
type ZstdParams = engine.ZstdParams
Parameter types are defined in package engine and re-exported here.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
zrecipe
command
Command zrecipe analyzes compressed files and rebuilds them from uncompressed content.
|
Command zrecipe analyzes compressed files and rebuilds them from uncompressed content. |
|
Package engine defines the compression engines that zrecipe searches over, and the parameter types recorded in Params.
|
Package engine defines the compression engines that zrecipe searches over, and the parameter types recorded in Params. |
|
gnugzip
Package gnugzip is a pure-Go port of GNU gzip's compressor, producing the raw deflate streams that the gzip program writes.
|
Package gnugzip is a pure-Go port of GNU gzip's compressor, producing the raw deflate streams that the gzip program writes. |
|
goflate
Package goflate is the Go standard library deflate engine.
|
Package goflate is the Go standard library deflate engine. |
|
kpflate
Package kpflate is the klauspost/compress deflate engine.
|
Package kpflate is the klauspost/compress deflate engine. |
|
kpzstd
Package kpzstd is the klauspost/compress zstd engine.
|
Package kpzstd is the klauspost/compress zstd engine. |
|
libzstd
Package libzstd is the cgo engine over the system libzstd.
|
Package libzstd is the cgo engine over the system libzstd. |
|
pgzip
Package pgzip reproduces the deflate streams that klauspost/pgzip, the parallel gzip used by umoci (and through it by rockcraft and every Canonical rock on Docker Hub), writes.
|
Package pgzip reproduces the deflate streams that klauspost/pgzip, the parallel gzip used by umoci (and through it by rockcraft and every Canonical rock on Docker Hub), writes. |
|
pgzip/flate
Package flate implements the DEFLATE compressed data format, described in RFC 1951.
|
Package flate implements the DEFLATE compressed data format, described in RFC 1951. |
|
pigz
Package pigz reproduces the raw deflate streams that pigz, the parallel gzip, writes.
|
Package pigz reproduces the raw deflate streams that pigz, the parallel gzip, writes. |
|
zlib
Package zlib is the cgo engine over the system zlib, producing raw deflate streams.
|
Package zlib is the cgo engine over the system zlib, producing raw deflate streams. |
|
Package enginetest holds conformance tests shared by all engines.
|
Package enginetest holds conformance tests shared by all engines. |
|
Package fixtures provides deterministic sample inputs for tests.
|
Package fixtures provides deterministic sample inputs for tests. |
|
Package format detects compression formats and parses their headers.
|
Package format detects compression formats and parses their headers. |
|
Package search evaluates candidate engine parameters against a reference compressed stream.
|
Package search evaluates candidate engine parameters against a reference compressed stream. |