xzfile

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package xzfile reads whole .xz files, as liblzma and xz -t read them, with a dictionary no larger than its block header names or the caller reads, and none above 1.5 GiB, as XZ for Java reads them, allocated as it fills, within a memory budget that the dictionaries of all decodes share.

Role

platform layer: xz decoding that no module owns. In the artifacts module, every package format that admits xz content reads it here: Nix NARs and channel tarballs, Debian package indexes and a package's control.tar.xz and data.tar.xz, CRAN .tar.xz source packages, p2 .xml.xz metadata and Terraform's remote .tar.xz and .xz modules. It decodes LZMA2 with lzma2, github.com/ulikunitz/xz's decoder copied so that its dictionary grows as it fills (decision 0045), and parses the container itself, because that package's Reader ends without an error where a block header or the index must start, and allocates whatever dictionary a block header names. It imports the standard library and lzma2, and no module. The app sets the budget from xz.dictionary_budget, and marks each HTTP request with WithRequest.

Inputs

  • The caller's context, which ends a wait for the budget, and may carry the request WithRequest marks.
  • The file, as an io.Reader read once from its first byte.
  • The limit: the most bytes the caller reads from the content, or 0 for none.
  • The budget, SetBudget: the most bytes of dictionary the process's decodes hold at once, or 0 for none. DefaultBudget is 1 GiB.

Outputs

  • An io.ReadCloser of the content of every stream in the file, in order. Its Read returns io.EOF only once the input ended after a stream's footer and its stream padding. Close gives back the dictionary it holds.
  • A refusal: io.ErrUnexpectedEOF for an input that ends anywhere but between streams, and otherwise an error whose text is the one ulikunitz/xz's Reader gives the same fault, such as "xz: checksum error for block". Errors of the LZMA2 decoder pass as it gives them.
  • MaxDict, the largest dictionary a block header may name.

Invariants

  • The container is checked as the .xz file format 1.2.1 sets it out: the stream header and its CRC32, each block header and its CRC32, one LZMA2 filter, the sizes a block header declares, block padding, each block's check (none, CRC32, CRC64 or SHA-256), an index that lists exactly the blocks read, its padding and CRC32, a footer that names the index's size and the stream's check type, stream padding in groups of four null bytes, and concatenated streams.
  • The end of the input is an error anywhere but between streams. A file whose index and footer were cut off is refused, as liblzma refuses it with "Unexpected end of input" (#1297, #1331).
  • A block header whose dictionary property is above 37, a dictionary above MaxDict (1.5 GiB), is refused before anything is allocated, with ulikunitz/xz's text for a property above 40. Artifactory decodes xz with XZ for Java, which refuses it the same way; ulikunitz/xz allocated up to 4 GiB first for properties 38 to 40 (#1332).
  • Each block is decoded with the dictionary its header names, as XZ for Java and liblzma size it, lowered to the caller's limit and raised to the LZMA2 decoder's 4 KiB. A match cannot reach further back than the content decoded, so the limit changes nothing the caller reads. Data that refers further back than its header names is refused, as both refuse it. ulikunitz/xz read it within an 8 MiB dictionary, and within 64 MiB for Nix and Terraform.
  • The dictionary is allocated as it fills, where XZ for Java allocates all of it first: 64 KiB, or all of it if that is smaller, then at least twice as much each time it is full, up to its size (#1413). A file of a few bytes takes 64 KiB whatever its header names. Every answer is the one a dictionary allocated in full gives: a match past the content decoded is refused in the same words.
  • A block takes each allocation of its dictionary from the budget before it is made, as the block starts and as the dictionary grows, and gives all of it back when the block's data ends, when Read fails or returns io.EOF, and on Close; a reader dropped unclosed gives it back once the garbage collector finds it. The budget counts the dictionary only, not the 32 KiB input buffer, nor a dictionary just outgrown, which the garbage collector frees.
  • A decode that does not fit, as it starts or as it grows, waits, in order of arrival, and is never refused: XZ for Java decodes with no memory limit (Apache Commons Compress passes -1), so a refusal would change an answer (#1354). One that fits goes ahead whatever its context says, as it did without a budget; a wait its context ends fails Read with the context's error and takes nothing more.
  • A dictionary larger than the whole budget decodes alone: it goes in once nothing else holds the budget, and those behind it wait.
  • A decode that grows holds part of the budget, so it passes the decodes queued to start. When every holder inside waits for more, none of them can give any back, so the one that has waited longest grows alone past the budget, and no other goes past it until that one holds nothing: the decodes together hold at most the budget and one dictionary.
  • The readers of one request (WithRequest) are one holder. One that already holds part of the budget passes the decodes queued ahead of it, and goes in while it alone holds the budget, so a request that reads a second xz file while its first is open cannot wait on itself. Readers of no request are each a holder of their own.
  • For a fault ulikunitz/xz's Reader refused, the refusal reads as it did there, in the same order of checks, so Debian's 400 for a package (which names the fault) is unchanged. Where that Reader decoded a file whose input ended early, or, for a field that runs past its block header, ended the stream there and read on as a new stream, the refusal is io.ErrUnexpectedEOF.

References

Status

Implemented after P9d: the reader Nix's whole-stream fix added in P9c (#1297), moved here so Debian, CRAN, p2 and Terraform read xz with it too (#1331), its dictionary bounded as XZ for Java bounds it (#1332), the dictionaries of all decodes held within a shared budget (#1354), and each dictionary allocated, and charged to the budget, as it fills (#1413).

Testing

  • reader_test.go: TestUnitDecodesWholeFiles (each check type, declared sizes, several blocks, concatenated streams with padding, and ulikunitz/xz's own writer), TestUnitRefusesAnEarlyEnd (every early end of a fixture, in ulikunitz/xz's words where it refused it and as io.ErrUnexpectedEOF where it did not), TestUnitRefusesAsUlikunitzDoes (every byte of the fixture changed, against ulikunitz/xz), TestUnitRefusesBrokenParts (each part of the container broken, with ulikunitz/xz's text), TestUnitDictionarySize (the header's dictionary, the limit and the 4 KiB least), TestUnitDictionaryProperty (every property byte, and the refusal above 37), TestUnitAHugeDictionaryIsNotAllocated (headers that name more than 1.5 GiB are refused before any allocation, and one that names 1.5 GiB allocates no more than the limit) and TestUnitDataBeyondTheDictionary (data that refers past the dictionary, or past what its header names, is refused, and read whole with one that reaches).
  • grow_test.go: TestUnitAShortFileTakesLittle (a 52-byte file whose header names 1.5 GiB decodes with 64 KiB of dictionary, by the budget and runtime.MemStats, at Nix's and Debian's limits and with none), TestUnitTheBudgetHoldsWhatTheDictionaryHolds (and a short file beside it decodes at once), TestUnitAGrowingDictionaryWaitsForRoom, TestUnitStalledDecodesGrowOneAtATime, TestUnitACancelledGrowthGivesBackItsDictionary and TestUnitMatchesReachTheWholeDictionary (matches that reach back the whole dictionary decode as ulikunitz/xz's reader decodes them, and one that reaches further is refused in its words).
  • budget_test.go: TestUnitTheBudgetBoundsConcurrentDecodes (sixteen decodes at once of files whose header names 1.5 GiB and whose content fills 1 MiB hold no more than the budget and one dictionary, by the budget and runtime.MemStats, and all of theirs without it); the tests of the queue lower the dictionary below the 64 KiB a block starts with, so it is taken whole as the block starts: TestUnitADecodeThatDoesNotFitWaits, TestUnitADictionaryLargerThanTheBudgetDecodesAlone, TestUnitACancelledWaitTakesNothing, TestUnitOneRequestReadsTwoStreams (no deadlock with WithRequest, and the wait without it), TestUnitTheDictionaryIsGivenBack (block end, io.EOF, an error, Close and the garbage collector), TestUnitTurningTheBudgetOffLetsWaitersIn and TestUnitSameAnswersWithTheBudgetOnAndOff (every early end and changed byte of the fixture).
  • Each format's tests read a fixture cut by its index and footer: ecosystem/nix (xz_test.go), ecosystem/debian (xz_test.go), ecosystem/cran (xz_test.go), ecosystem/p2 (xz_test.go) and ecosystem/terraform/http (remote_archive_xz_test.go), and the server suites under test/server/artifacts send them through the routes. Each format's xz_budget_test.go reads its xz fixtures with a budget smaller than their dictionaries and with none, and gets the same answers.

Index

Constants

View Source
const DefaultBudget int64 = 1 << 30

DefaultBudget is the dictionary budget a process starts with, and the default of the xz.dictionary_budget setting: 1 GiB. It lets one decode whose dictionary fills up to 1 GiB, or a hundred and twenty-eight that fill xz -6's 8 MiB, hold their dictionaries at once; one that fills more, up to MaxDict, grows alone. A file shorter than its dictionary holds only what it fills, from 64 KiB (#1413).

View Source
const MaxDict = 3 << 29

MaxDict is the largest dictionary a block header may name: 1.5 GiB, dictionary property 37. Artifactory decodes xz with XZ for Java, whose LZMA2Decoder refuses a property above 37 before it allocates anything: https://github.com/tukaani-project/xz-java/blob/f14a6039/src/org/tukaani/xz/LZMA2Decoder.java#L15

Variables

This section is empty.

Functions

func NewReader

func NewReader(ctx context.Context, in io.Reader, limit int64) (io.ReadCloser, error)

NewReader reads the first stream header of in, as xz.NewReader does, and returns the reader of the whole file's content. limit is the most bytes the caller reads from it, or 0 for no limit: a match cannot reach further back than the content decoded, so a block is decoded with a dictionary no larger than limit and reads everything the caller does.

Each block's dictionary is allocated as it fills, from 64 KiB, and takes each allocation from the process's budget (SetBudget) before it is made, waiting while it does not fit; it gives it all back when the block ends, when Read fails or returns io.EOF, and on Close. A wait that ctx ends fails Read with ctx's error. The caller closes the reader; one dropped unclosed gives its dictionary back only once the garbage collector finds it unreachable.

func SetBudget

func SetBudget(n int64)

SetBudget sets how many bytes of LZMA2 dictionary the process's xz decodes may hold at once; 0 turns the budget off. A decode whose dictionary does not fit, as it starts or as it grows, waits for others to give theirs back, and is never refused for it: Artifactory decodes xz with XZ for Java, which has no such bound, so a refusal would change an answer (#1354).

func WithRequest

func WithRequest(ctx context.Context) context.Context

WithRequest returns ctx with a holder of the budget of its own, so the readers opened with it, or with a context made from it, count as one holder: one request. Without it each reader is a holder of its own.

A holder already holding part of the budget is let past the decodes queued ahead of it, and in once nothing but it holds the budget, even beyond the budget. Without that, a request that reads a second xz file while a first is open would wait behind a decode that waits for the first one's dictionary, which only the waiting request can give back.

Types

This section is empty.

Directories

Path Synopsis
Package lzma2 decodes LZMA2 chunk sequences, the data of an xz block, with a dictionary that grows as it fills.
Package lzma2 decodes LZMA2 chunk sequences, the data of an xz block, with a dictionary that grows as it fills.

Jump to

Keyboard shortcuts

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