archive

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Overview

Package archive provides archive creation and manipulation functionality.

Package archive provides archive creation and manipulation functionality.

This package implements a focused subset of tar/zip handling needed by YAP:

  • Create compressed tar archives (.tar.zst / .tar.gz / .tar.xz) used as intermediate artifacts and as the on-disk format for Pacman / APK packages.
  • Extract a small set of recognized archive types to disk: tar (plain or wrapped in gzip / zstd / xz / bz2) and zip. Other formats (rar, 7z, …) return the ErrUnrecognizedArchive sentinel so callers can fall through to a system binary or treat it as a no-op.

All extraction paths defend against zip-slip path traversal via safeJoin and against malicious symlink targets via safeSymlinkTarget.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnrecognizedArchive = stderrors.New("archive format not recognized")

ErrUnrecognizedArchive is returned by Extract / ExtractFiltered when the input file's format is not recognized as an extractable archive. Callers that legitimately accept non-archive inputs (e.g. plain patch files in pkg/source.Source.Get) can detect this sentinel and treat it as a no-op; callers that expect a real archive should treat it as a hard error.

Functions

func CreateTarCompressed

func CreateTarCompressed(
	ctx context.Context,
	sourceDir,
	outputFile,
	compression string,
	formatGNU bool,
) error

CreateTarCompressed creates a compressed tar archive with the specified compression algorithm from the source directory. Supported compression algorithms are "zstd", "gzip", and "xz". If compression is empty, defaults to "zstd". Returns an error if the compression algorithm is unsupported.

formatGNU selects tar.FormatGNU when true (used for Pacman packages); the default is tar.FormatPAX (used for APK and source bundles).

func CreateTarZst

func CreateTarZst(ctx context.Context, sourceDir, outputFile string, formatGNU bool) error

CreateTarZst creates a compressed tar.zst archive from the specified source directory. This is a convenience wrapper around CreateTarCompressed that defaults to zstd compression.

func Extract

func Extract(ctx context.Context, source, destination string) error

Extract extracts an archive file to the specified destination directory. Supported formats: tar (plain or gzip / zstd / xz / bz2 wrapped) and zip. Returns ErrUnrecognizedArchive for any other input.

func ExtractDEB added in v2.1.0

func ExtractDEB(packagePath, destDir string) error

ExtractDEB extracts a Debian package (.deb) to the destination directory. DEB format: AR archive containing control.tar.gz and data.tar.{gz,xz,zst} We need to extract data.tar from the AR archive and then extract its contents.

func ExtractFiltered

func ExtractFiltered(ctx context.Context, source, destination string, patterns []string) error

ExtractFiltered extracts an archive file to the destination directory, only including entries whose name matches one of the provided glob patterns (filepath.Match semantics). If patterns is empty every entry is extracted.

func ExtractRPM added in v2.1.0

func ExtractRPM(packagePath, destDir string) error

ExtractRPM extracts an RPM package payload (cpio.{gz,xz,zst,...}) to the destination directory using github.com/sassoftware/go-rpmutils.

We iterate the cpio payload via PayloadReaderExtended rather than calling ExpandPayload because the bundled cpio.Extract refuses entries whose names begin with "/" once destDir is "/" (its containment check compares against dest+"/", which becomes "//"). YAP's rpmpack emits absolute paths like "/opt/vendor/common/bin/x264", so ExpandPayload errors out with 'invalid cpio path "/opt/..."' on every package targeted to /.

Streaming entries ourselves also lets us reuse SafeJoin for traversal protection and skip risky entry types (char/block/FIFO).

func SafeJoin added in v2.0.5

func SafeJoin(destination, name string) (string, error)

SafeJoin is the exported wrapper around safeJoin, exposed for callers in sibling packages (e.g. pkg/builders/common.extractAPK) that need the same containment guarantee when extracting custom archive formats.

func SafeSymlinkTarget added in v2.0.5

func SafeSymlinkTarget(entryName, target string) error

SafeSymlinkTarget is the exported wrapper around safeSymlinkTarget. entryName is the path of the symlink itself within the archive (e.g. "opt/foo/sbin/slapacl"); target is the symlink target as stored in the archive (e.g. "../libexec/slapd").

Types

This section is empty.

Jump to

Keyboard shortcuts

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