Documentation
¶
Overview ¶
Package archive reads zip, tar and tar.gz archives: the walker the item service explodes and serves archive entries with, and a bounded walker and reader the format handlers (decision 0032) read packages with, such as an npm tarball's package.json and file count, a Go module zip's go.mod or a jar's POM. Every count an archive's headers give is the uploader's claim, so the bounded walker counts what it actually decompresses.
Index ¶
- Constants
- Variables
- func Clean(name string) (string, error)
- func ReadEntry(e Entry, lim Limits) ([]byte, error)
- func ReadFile(ra io.ReaderAt, size int64, kind Kind, lim Limits, want func(name string) bool) (string, []byte, error)
- func ReaderAt(rs io.ReadSeeker) io.ReaderAt
- func Scan(ra io.ReaderAt, size int64, kind Kind, lim Limits, fn func(Entry) error) error
- func Walk(ra io.ReaderAt, size int64, kind Kind, fn func(Entry) error) error
- type Entry
- type Kind
- type Limits
Constants ¶
const SniffLen = 512
SniffLen is how much of the content Sniff reads: a tar header's magic ends at byte 262.
Variables ¶
var DefaultLimits = Limits{Entries: 1000, Inflated: 256 << 20, File: 1 << 20}
DefaultLimits suit the descriptors formats read: a package.json, a go.mod or a POM is kilobytes, and a package whose descriptor lies past a thousand entries or 256 MiB of content is refused rather than read.
var ErrNotFound = errors.New("archive: no such entry")
ErrNotFound is an archive with no entry the caller asked for.
var ErrUnusable = errors.New("archive: unusable archive")
ErrUnusable is content that is not a usable archive: an unknown or corrupt format, an entry escaping its folder, or a limit exceeded.
Functions ¶
func Clean ¶
Clean makes an entry name a relative path, refusing names that would leave the folder the archive is read into: absolute paths, drive letters and ".." segments. Backslashes count as separators, as Windows tools write them. A name that is only the folder itself cleans to "".
func ReadFile ¶
func ReadFile( ra io.ReaderAt, size int64, kind Kind, lim Limits, want func(name string) bool, ) (string, []byte, error)
ReadFile returns the first regular file of the archive in ra whose cleaned name want accepts, with that name. ErrNotFound when no file matches; ErrUnusable when the content is no archive of kind or a limit is passed first.
func ReaderAt ¶
func ReaderAt(rs io.ReadSeeker) io.ReaderAt
ReaderAt gives random access over a reader that can only seek, such as stored content, for zip, which reads its central directory from the end. Reads are serialised because they share one seek position. A reader that already reads at offsets is returned as it is.
Types ¶
type Entry ¶
type Entry struct {
// Name is the entry's name as the archive spells it; Clean makes it a
// path.
Name string
Dir bool
// Regular is a plain file; links and devices are not.
Regular bool
// Size is what the archive's header claims.
Size int64
// Open reads the entry. A tar entry's reader is valid only until the
// walk moves on.
Open func() (io.Reader, error)
}
Entry is one entry as an archive lists it.
type Kind ¶
type Kind int
Kind is an archive container format.
func Sniff ¶
Sniff tells the kind from the content's first bytes, falling back to the name for what has no signature to read.
func (Kind) ContentType ¶
ContentType is the media type of an archive of the kind.
type Limits ¶
type Limits struct {
// Entries is how many entries the walk may pass.
Entries int
// Inflated bounds the bytes a tar.gz walk decompresses, including the
// entries it skips on the way, which a zip walk does not read.
Inflated int64
// File bounds the content of the file read.
File int64
}
Limits bound what reading one file from an archive may cost.