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
- func Create(ctx context.Context, tarPath, srcDir string, opts ...Option) error
- func CreateFiltered(ctx context.Context, tarPath, srcDir string, skip SkipFunc, opts ...Option) error
- func CreateFilteredWithRoot(ctx context.Context, tarPath, srcDir string, skip SkipFunc, opts ...Option) error
- func CreateWithRoot(ctx context.Context, tarPath, srcDir string, opts ...Option) error
- func Extract(tarPath, dstDir string) error
- func Lchown(root *os.Root, name string, hdr *tar.Header) error
- func Mode(hdr *tar.Header) os.FileMode
- type Option
- type SkipFunc
Constants ¶
const ModeBits = os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
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 ¶
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 ¶
CreateWithRoot is Create plus a "./" entry carrying srcDir's own mode, ownership, modification time, and xattrs, which Extract applies to dstDir.
func Extract ¶
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
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.
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
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.