archive

package
v0.8.0-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 31 Imported by: 0 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 archive reads archives (RAR, 7-Zip, ZIP, TAR and single compressed files) and unpacks what's asked of them: comics' pages, today.

Archives are treated as hostile. Everything that decodes one runs in a short-lived child process (worker.go), because the decoders can be made to panic or to ask for enormous amounts of memory by a crafted header, and neither of those may take the server down. Nothing here ever writes beside the archive.

Index

Constants

View Source
const (
	// MaxEntries is far above any comic: a few hundred pages.
	MaxEntries = 10000
	// MaxUnpacked is above several dual-layer discs; the cache's own limit is usually lower.
	MaxUnpacked = int64(32) << 30
	// MaxRatio applies above ratioFloor. Blank space in a disc image compresses
	// well, but not this well. A small file may be mostly padding.
	MaxRatio = 2000
)
View Source
const ScanBudget = int64(64) << 20

ScanBudget is how much of a compressed tar a scan reads through.

Variables

View Source
var (
	ErrEncrypted   = &Error{"encrypted", "This archive is protected by a password, and this server doesn't open those. Unpack it yourself and put what it holds in the folder."}
	ErrUnsafe      = &Error{"unsafe", "This archive tries to put files outside its own folder, so it was refused."}
	ErrTooBig      = &Error{"toobig", "This archive unpacks to far more than it should, so it was refused."}
	ErrTooMany     = &Error{"toomany", "This archive holds too many files to be read, so it was refused."}
	ErrDamaged     = &Error{"damaged", "This archive is damaged or isn't the kind of file its name says."}
	ErrMethod      = &Error{"method", "This archive uses a kind of compression this server can't read. Unpack it yourself and put what it holds in the folder."}
	ErrNoSpace     = &Error{"nospace", "There isn't enough free disk space to unpack this."}
	ErrUnavailable = &Error{"unavailable", "This archive couldn't be read."}
)

Functions

func BaseName

func BaseName(name string) string

BaseName is an archive's name without what makes it an archive: "Game (USA).tar.gz", "Game (USA).part1.rar" and "Game (USA).7z.001" are all "Game (USA)". Region and dump tags stay.

func CodeOf

func CodeOf(err error) string

CodeOf says which Error an error is, or "" for any other.

func IsArchive

func IsArchive(name string) bool

IsArchive says whether a file's name is one of the kinds of archive this package reads.

func IsLaterVolume

func IsLaterVolume(name string) bool

IsLaterVolume: the second and later parts of a split RAR. They are read through the first part, and aren't archives by themselves.

func Transient

func Transient(err error) bool

Transient says an error isn't a lasting fact about the archive, and is worth trying again at the next scan.

Types

type Entry

type Entry struct {
	Name string `json:"n"`
	Size int64  `json:"s"`
	// CRC is the file's CRC-32 as the archive records it, or 0 if it doesn't. Game
	// databases are keyed on these, so a game can be recognised without unpacking it.
	CRC uint32 `json:"c,omitempty"`
}

An Entry is one regular file in an archive. Size is -1 when the archive doesn't say.

type Error

type Error struct {
	Code string
	Msg  string
}

An Error is why an archive can't be used, in words for the owner. The Code stays the same across the pipe from the worker, and into the scan's report.

func (*Error) Error

func (e *Error) Error() string

type Lister

type Lister struct {
	// contains filtered or unexported fields
}

A Lister reads archive headers one after another through one worker, so a scan of thousands costs one process and not thousands. A crash or a hang costs only the archive that caused it: that one is reported as damaged, and the next gets a new worker. Not for use by several goroutines at once.

func NewLister

func NewLister() *Lister

NewLister starts nothing until the first archive.

func (*Lister) Close

func (l *Lister) Close()

Close ends the worker.

func (*Lister) List

func (l *Lister) List(path string, budget int64) (*Listing, error)

List reads one archive's headers. budget is as for ScanBudget; 0 reads everything.

type Listing

type Listing struct {
	Entries []Entry `json:"e"`
	Partial bool    `json:"p,omitempty"`
}

A Listing is what an archive holds, read from its headers. Partial: a .tar.gz has no index, so a scan looks only so far into one.

func List

func List(path string) (*Listing, error)

List reads one archive's headers, all of them, in a worker of its own.

func (*Listing) Exts

func (l *Listing) Exts() []string

Exts are the lower-case extensions in a listing, once each, in archive order.

type Want

type Want struct {
	Name string `json:"n"`
	Out  string `json:"o"`
}

A Want is one file to unpack: Name is its name in the archive, Out its name in the folder (relative, already checked by the caller).

type Written

type Written struct {
	Out  string `json:"o"`
	Size int64  `json:"s"`
}

A Written is one file that arrived.

func Unpack

func Unpack(ctx context.Context, path, dst string, want []Want, limit int64) ([]Written, error)

Unpack takes these files out of an archive into a folder, in a worker of its own, writing no more than limit bytes.

Jump to

Keyboard shortcuts

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