buildcontext

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package buildcontext holds a build context: the files a Containerfile's COPY and ADD read, received as a tar stream (from wctr, or from a stage or image through the engine) and kept in memory. Nothing in it ever names a host path: a build reads only what was sent.

The stream is untrusted. Names are cleaned and must stay inside the context (no absolute names, no ".."); the total size, the number of entries, name length and depth are bounded; symbolic links are kept as links and only followed, when a source path goes through one, inside the context — a link that leads out of it is an error.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{MaxBytes: 256 << 20, MaxFiles: 65536, MaxPathLen: 4096, MaxDepth: 128}

DefaultLimits suit a training lab and match the engine's default container quota.

View Source
var ErrEscape = errors.New("path leaves the build context")

ErrEscape reports a path or link that leaves the context.

View Source
var ErrLimit = errors.New("build context exceeds its limits")

ErrLimit reports a context beyond its limits.

View Source
var IgnoreFiles = []string{".containerignore", ".dockerignore"}

IgnoreFiles are looked for at the context root, in this order (containerignore(5)): the first one found is used.

Functions

func CleanName

func CleanName(name string, lim Limits) (string, error)

CleanName turns a tar entry name into a context path, or fails when it would leave the context.

func HasMeta

func HasMeta(p string) bool

HasMeta reports whether a COPY source is a wildcard pattern.

func ReadIgnore

func ReadIgnore(fsys fs.FS) (*ignore.Matcher, string, error)

ReadIgnore reads the context's ignore file from fsys, if any; the name found is returned ("" for none).

Types

type Context

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

Context is an in-memory file tree. Paths are slash-separated, relative, cleaned ("." is the root).

func FromTar

func FromTar(r io.Reader, lim Limits, prefix string) (*Context, error)

FromTar reads a tar stream into a context, each name placed under prefix ("" or "." for the root). Hard links become copies of their target; devices, FIFOs and other types are skipped.

func New

func New(lim Limits) *Context

New returns an empty context.

func (*Context) Add

func (c *Context) Add(p string, e *Entry) error

Add stores an entry (creating missing parent directories) and charges it to the limits.

func (*Context) Bytes

func (c *Context) Bytes() int64

Bytes is the total file content.

func (*Context) Exclude

func (c *Context) Exclude(m *ignore.Matcher)

Exclude removes every entry the matcher excludes. Directories that are excluded but hold a re-included entry are kept (as their parents).

func (*Context) Get

func (c *Context) Get(p string) (*Entry, bool)

Get returns the entry at p, not following a final symlink.

func (*Context) Glob

func (c *Context) Glob(pattern string) ([]string, error)

Glob returns the paths matching a filepath.Match pattern (each component matched at its own depth), sorted.

func (*Context) IgnoreMatcher

func (c *Context) IgnoreMatcher() (*ignore.Matcher, error)

IgnoreMatcher reads the context's own ignore file, if it holds one.

func (*Context) Len

func (c *Context) Len() int

Len is the number of entries, the root included.

func (*Context) Paths

func (c *Context) Paths() []string

Paths returns every path in sorted order (the root excluded).

func (*Context) ReadTar

func (c *Context) ReadTar(r io.Reader, prefix string) error

ReadTar adds a tar stream's entries under prefix.

func (*Context) Resolve

func (c *Context) Resolve(p string, follow bool) (string, error)

Resolve follows symbolic links in p's components (and in its last one when follow) and returns the context path they lead to. A link that would leave the context is ErrEscape.

func (*Context) Under

func (c *Context) Under(p string) []string

Under returns p and every path below it, sorted.

type Entry

type Entry struct {
	Mode     fs.FileMode // type bits and permissions
	Data     []byte
	Link     string // symlink target
	ModTime  time.Time
	UID, GID int
	// Sum is the sha256 of Data (regular files).
	Sum [32]byte
}

Entry is one file, directory or symbolic link.

type Limits

type Limits struct {
	MaxBytes   int64 // file content, all files together
	MaxFiles   int   // entries
	MaxPathLen int
	MaxDepth   int
}

Limits bound a context.

type PackStats

type PackStats struct {
	Files int
	Bytes int64
}

PackStats says what was sent.

func Pack

func Pack(dir string, w io.Writer, lim Limits) (PackStats, error)

Pack writes the directory dir as a context tar to w, leaving out what its ignore file excludes. The directory is opened as an os.Root, so no file outside it is ever read, whatever links it holds: symbolic links are sent as links, never followed. Owners are not sent (every entry is 0:0) and neither are devices, FIFOs or sockets.

func PackFS

func PackFS(fsys fs.FS, readlink func(string) (string, error), w io.Writer, lim Limits) (PackStats, error)

PackFS is Pack over any file system; readlink reads a link's target.

Jump to

Keyboard shortcuts

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