Documentation
¶
Overview ¶
Package oci presents an OCI/Docker image as a read-only github.com/go-filesystems/interface Filesystem by overlaying the image's tar layers with overlayfs semantics. It is pure Go (CGO_ENABLED=0) and pulls no third-party dependencies for its core: gzip and plain layers are handled by the standard library, and any other compression (e.g. zstd) is supported through an injectable Decompressor registry.
Index ¶
- Constants
- Variables
- func RegisterDecompressor(mediaType string, d Decompressor)
- type BlobSource
- type Decompressor
- type FS
- func Open(src BlobSource) (*FS, error)
- func OpenDescriptor(src BlobSource, top descriptor, sel Selector) (*FS, error)
- func OpenLayout(dir string) (*FS, error)
- func OpenSelect(src BlobSource, sel Selector) (*FS, error)
- func OpenSelectLimits(src BlobSource, sel Selector, lim Limits) (*FS, error)
- func OpenTarball(p string) (*FS, error)
- func (f *FS) Close() error
- func (f *FS) DeleteDir(path string) error
- func (f *FS) DeleteFile(path string) error
- func (f *FS) ListDir(path string) ([]filesystem.DirEntry, error)
- func (f *FS) MkDir(path string, perm os.FileMode) error
- func (f *FS) ReadFile(path string) ([]byte, error)
- func (f *FS) ReadLink(path string) (string, error)
- func (f *FS) Rename(oldPath, newPath string) error
- func (f *FS) Stat(path string) (filesystem.Stat, error)
- func (f *FS) WriteFile(path string, data []byte, perm os.FileMode) error
- type Limits
- type Selector
Constants ¶
const ( // Uncompressed tar layers. MediaTypeLayerTar = "application/vnd.oci.image.layer.v1.tar" MediaTypeDockerLayerTar = "application/vnd.docker.image.rootfs.diff.tar" // gzip-compressed tar layers. MediaTypeLayerTarGzip = "application/vnd.oci.image.layer.v1.tar+gzip" MediaTypeDockerLayerTarGzip = "application/vnd.docker.image.rootfs.diff.tar.gzip" // zstd-compressed tar layers (no built-in decompressor; register one). MediaTypeLayerTarZstd = "application/vnd.oci.image.layer.v1.tar+zstd" // Manifest / index media types we resolve. MediaTypeImageManifest = "application/vnd.oci.image.manifest.v1+json" MediaTypeImageIndex = "application/vnd.oci.image.index.v1+json" MediaTypeDockerManifest = "application/vnd.docker.distribution.manifest.v2+json" MediaTypeDockerManifestList = "application/vnd.docker.distribution.manifest.list.v2+json" MediaTypeImageConfig = "application/vnd.oci.image.config.v1+json" MediaTypeDockerImageConfig = "application/vnd.docker.container.image.v1+json" )
Built-in OCI / Docker layer media types.
const ( FileTypeUnknown uint8 = iota FileTypeRegular FileTypeDir FileTypeSymlink FileTypeHardlink FileTypeChar FileTypeBlock FileTypeFifo )
FileType constants returned by DirEntry.FileType() and node.ftype. The go-filesystems/interface package does not define these, so we define a local mapping that mirrors the POSIX d_type / tar typeflag taxonomy.
Variables ¶
var ErrReadOnly = errors.New("oci: read-only filesystem")
ErrReadOnly is returned by every mutating method of FS. An OCI image filesystem is immutable: it is a read-only overlay of the image's layers.
Functions ¶
func RegisterDecompressor ¶
func RegisterDecompressor(mediaType string, d Decompressor)
RegisterDecompressor registers d for the given layer media type, replacing any previous registration. Pass a media type such as MediaTypeLayerTarZstd to enable zstd without a core dependency.
Types ¶
type BlobSource ¶
type BlobSource interface {
// Blob returns a reader for the blob identified by digest. The caller
// must Close the returned reader. An error wrapping fs.ErrNotExist is
// returned when the digest is unknown.
Blob(digest string) (io.ReadCloser, error)
}
BlobSource provides content-addressed access to an image's blobs (manifests, configs and layers) by their digest string (e.g. "sha256:abc...").
func OCILayout ¶
func OCILayout(dir string) BlobSource
OCILayout returns a BlobSource backed by an OCI image layout directory, i.e. a directory containing an "oci-layout" marker, "index.json" and a "blobs/<alg>/<hex>" content store.
func Tarball ¶
func Tarball(p string) (BlobSource, error)
Tarball returns a BlobSource backed by a `docker save` / OCI archive tar file on disk.
func TarballFS ¶
func TarballFS(fsys fs.FS) (BlobSource, error)
TarballFS returns a BlobSource backed by an archive tar exposed through an fs.FS. The archive is expected at the path "image.tar"; if absent, the first (and only) regular file is used. Most callers should prefer Tarball.
func TarballLimits ¶ added in v0.4.0
func TarballLimits(p string, lim Limits) (BlobSource, error)
TarballLimits is Tarball with an explicit resource budget for the archive (MaxArchiveMember, MaxArchiveTotal, MaxEntries, MaxMetadata, MaxPathLen, MaxSparseHoles). A zero field takes the default.
type Decompressor ¶
Decompressor wraps a raw layer blob reader and returns a reader that yields the decompressed tar stream. Implementations must not assume ownership of r; closing the underlying blob is the caller's responsibility.
type FS ¶
type FS struct {
// contains filtered or unexported fields
}
FS is a read-only github.com/go-filesystems/interface Filesystem view over an OCI/Docker image's merged layer tree.
ReadFile strategy: file contents are read once at Open time, while the owning layer is decompressed, and kept in the merged tree. Small bodies (Limits.SpoolThreshold, 4 MiB by default, up to Limits.MaxMemory, 256 MiB by default, in total) stay on the heap; every larger body is spooled to one temporary file (mode 0600, under Limits.SpoolDir or the system temporary directory, unlinked at creation where the platform allows it and removed by Close otherwise). RAM held for file contents is therefore bounded by MaxMemory however large the image's uncompressed size is, while the disk holds at most MaxImageUncompressed bytes. Hardlink names share the target's body. Layer blobs bigger than the threshold are staged on disk the same way while their digest is verified, and removed as soon as the layer is folded. Call Close to release the spool file.
func Open ¶
func Open(src BlobSource) (*FS, error)
Open resolves the single image addressed by src (the first manifest in the index, or a multi-arch index's first matching manifest) and returns a read-only FS over its merged layers. To select a specific manifest from a multi-arch index, pass a Selector via OpenSelect.
func OpenDescriptor ¶
func OpenDescriptor(src BlobSource, top descriptor, sel Selector) (*FS, error)
OpenDescriptor opens an image given an explicit top-level descriptor, allowing use of a bare BlobSource that does not embed index discovery.
func OpenLayout ¶
OpenLayout is a convenience wrapper: OpenLayout(dir) == Open(OCILayout(dir)).
func OpenSelect ¶
func OpenSelect(src BlobSource, sel Selector) (*FS, error)
OpenSelect is like Open but selects a manifest from a multi-arch index by digest or platform.
func OpenSelectLimits ¶ added in v0.4.0
func OpenSelectLimits(src BlobSource, sel Selector, lim Limits) (*FS, error)
OpenSelectLimits is OpenSelect with an explicit resource budget for the image: use it to open an untrusted image with tighter ceilings than the defaults. A zero field of lim takes the default (see DefaultLimits). A limit that is exceeded fails with an error wrapping safeio.ErrTooLarge before the memory is spent.
func OpenTarball ¶
OpenTarball is a convenience wrapper around Tarball.
func (*FS) DeleteFile ¶
DeleteFile always returns ErrReadOnly.
func (*FS) ListDir ¶
func (f *FS) ListDir(path string) ([]filesystem.DirEntry, error)
ListDir returns the entries of the directory at path, sorted by name.
func (*FS) ReadFile ¶
ReadFile returns the contents of the regular file (or hardlink to one) at path.
type Limits ¶ added in v0.4.0
type Limits struct {
// MaxBlob bounds one raw (compressed) layer blob, in bytes.
MaxBlob int64
// MaxManifest bounds a manifest, index, index.json or manifest.json.
MaxManifest int64
// MaxLayerUncompressed bounds the regular-file bytes of one layer.
MaxLayerUncompressed int64
// MaxImageUncompressed bounds the regular-file bytes of the whole image.
MaxImageUncompressed int64
// MaxFile bounds one regular file.
MaxFile int64
// MaxLayerStream bounds the decompressed tar stream of one layer.
MaxLayerStream int64
// MaxSparseHoles bounds the bytes a layer may claim beyond what it carries.
MaxSparseHoles int64
// MaxMetadata bounds the names and link targets of the whole image.
MaxMetadata int64
// MaxEntries bounds the nodes of the merged tree.
MaxEntries int64
// MaxPathLen bounds one entry name or link target, in bytes.
MaxPathLen int
// MaxLayers bounds the number of layers.
MaxLayers int
// MaxIndexDepth bounds index-of-index nesting.
MaxIndexDepth int
// MaxArchiveMember bounds one member of a docker-save / OCI archive.
MaxArchiveMember int64
// MaxArchiveTotal bounds all the members of an archive together.
MaxArchiveTotal int64
// SpoolThreshold is the largest file body, layer blob or archive member
// (by declared size) kept on the heap; anything bigger is spooled to a
// temporary file. Zero takes the default (4 MiB); a negative value spools
// every non-empty body.
SpoolThreshold int64
// MaxMemory bounds the bytes of file bodies (and archive members) held on
// the heap in total: once reached, every further body is spooled whatever
// its size. Zero takes the default (256 MiB); a negative value spools
// every non-empty body. This, not the MaxImage/MaxLayer caps, is what an
// image can make the process hold in RAM for file contents; those caps
// bound the temporary file on disk.
MaxMemory int64
// SpoolDir is the directory of the temporary spool files (created 0600,
// unlinked at once where the platform allows it, removed by FS.Close
// otherwise). Empty means os.TempDir(). Point it at a volume with room for
// MaxImageUncompressed bytes when opening images that large.
SpoolDir string
}
Limits is the resource budget for opening one untrusted image. A zero field takes the package default (see DefaultLimits); a negative one refuses everything of that kind. Pass it to OpenSelectLimits or TarballLimits.
func DefaultLimits ¶ added in v0.4.0
func DefaultLimits() Limits
DefaultLimits returns the default budget: generous for real images, finite for hostile ones.
type Selector ¶
type Selector struct {
// Digest, if non-empty, selects the manifest descriptor whose digest
// matches exactly.
Digest string
// OS / Architecture / Variant, if set, select by platform. Empty fields
// are treated as wildcards.
OS string
Architecture string
Variant string
}
Selector picks one manifest from a multi-manifest index.
