tarutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 15 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 tarutil archives and restores a directory tree as a tar file, preserving the metadata a workload's data directory depends on: modes, ownership, modification times, symlinks, hardlinks, FIFOs, overlay whiteouts, and user.* / overlay extended attributes (as PAX SCHILY.xattr records).

It exists for snapshotting durable-dir volumes and rootfs overlay uppers (see cmd/ateworker-microvm): the contents are written by the sandboxed workload under arbitrary uids, shipped to object storage, and restored — possibly onto another node — where the workload must see them unchanged. The upper is why device nodes and overlay xattrs matter: the host kernel's overlayfs records a deleted file as a 0:0 character device (whiteout) and a replaced directory as a trusted.overlay.opaque xattr, and losing either silently resurrects deleted content after a resume.

Extraction is confined to the destination with os.Root, so a crafted archive cannot write outside it via "..", an absolute path, or a symlink. It also refuses device nodes other than whiteouts and restores only the xattrs keepXattr allows.

Index

Constants

ModeBits are the mode bits an extracted entry takes from its tar header: the permission bits plus setuid, setgid and sticky, which FileMode.Perm() drops.

Variables

This section is empty.

Functions

func Create

func Create(ctx context.Context, tarPath, srcDir string, opts ...Option) error

Create writes a tar archive of srcDir's contents to tarPath. Entry names are relative to srcDir, so extracting into another directory reproduces the tree. srcDir itself is not an entry.

Regular files, directories, symlinks, FIFOs, and overlay whiteouts are archived with their mode, ownership, modification time, and the xattrs keepXattr allows. A file with multiple links inside srcDir is archived once and referenced as a hardlink thereafter. Sockets and other device nodes are skipped (see writeTree).

func CreateFiltered

func CreateFiltered(ctx context.Context, tarPath, srcDir string, skip SkipFunc, opts ...Option) error

CreateFiltered is Create with entries omitted where skip returns true. A nil skip archives everything.

func CreateFilteredWithRoot

func CreateFilteredWithRoot(ctx context.Context, tarPath, srcDir string, skip SkipFunc, opts ...Option) error

CreateFilteredWithRoot is CreateWithRoot with entries omitted where skip returns true. The root entry is never skipped.

func CreateWithRoot

func CreateWithRoot(ctx context.Context, tarPath, srcDir string, opts ...Option) error

CreateWithRoot is Create plus a "./" entry carrying srcDir's own mode, ownership, modification time, and xattrs, which Extract applies to dstDir.

func Extract

func Extract(tarPath, dstDir string) error

Extract unpacks tarPath into dstDir, which must already exist. Modes, ownership, modification times, symlinks, and hardlinks are restored.

All writes go through an os.Root rooted at dstDir, so entries naming a path outside it — via "..", an absolute path, or a symlink planted earlier in the same archive — are refused rather than followed. An entry that collides with an existing path replaces it ("later entry wins", standard tar semantics), except that an existing directory is kept when the entry is also a directory.

func Lchown added in v1.0.0

func Lchown(root *os.Root, name string, hdr *tar.Header) error

Lchown applies hdr's numeric owner to name, a path relative to root, without following a final-component symlink. It is shared by every extractor that restores tar ownership: Extract here, and the image layer unpack in internal/imagecache. Unlike the rest of this package it builds on every platform, because the image cache's unit tests run off Linux.

Giving a file to another uid requires CAP_CHOWN. The production callers run as root with it (ateworker-microvm restoring snapshots, atelet unpacking image layers), so any error there means ownership was lost and is returned. An unprivileged process cannot chown at all, so rather than making extraction unusable outside a root context (unit tests, local tooling), EPERM is tolerated there and the extracted files belong to the extracting user.

func Mode added in v1.0.0

func Mode(hdr *tar.Header) os.FileMode

Mode returns the mode an extracted entry takes from hdr, as containerd and moby apply it. Without the special bits a setgid data directory would stop passing its group to new files, and /tmp would let users delete each other's files.

Types

type Option added in v1.0.0

type Option func(*options)

Option configures Create and its variants.

func WriteBehind added in v1.0.0

func WriteBehind(chunk int64) Option

WriteBehind sets how much of the archive may be dirty in the page cache before write-out starts, or 0 for no limit, e.g. when the caller has the memory to spare. The default is writeBehindChunk.

type SkipFunc

type SkipFunc func(rel string) bool

SkipFunc reports whether an archive entry should be omitted, given its slash-separated path relative to the archive root. Returning true for a directory omits its entire subtree.

Jump to

Keyboard shortcuts

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