blobs

package
v0.7.40 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 2 modules, 7 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 blobs keeps files by the SHA-256 of their contents. Each file is stored once, never changed, and removed only by a sweep that finds nothing pointing at it. A GitHub snapshot lands here, and so do an app's uploads.

A blob store differs from a cache such as wasmhost's: a cache may lose an entry and rebuild it, and a blob store may not. The design is at https://panyam.github.io/goapplib/reference/blobs/.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means the store holds no blob with that hash.
	ErrNotFound = errors.New("blobs: not found")

	// ErrInvalidHash means a hash isn't "sha256:" and 64 lowercase hex digits.
	ErrInvalidHash = errors.New("blobs: invalid hash")

	// ErrCorrupt means a blob's bytes no longer hash to its name, because something changed or
	// truncated the stored file. Open's reader returns it at the end of the blob, in place of
	// io.EOF.
	ErrCorrupt = errors.New("blobs: contents don't match the hash")

	// ErrInvalidManifest means a manifest has a path that is absolute, unclean or climbs out,
	// a duplicate path, or a malformed hash.
	ErrInvalidManifest = errors.New("blobs: invalid manifest")
)
View Source
var ErrInvalidAlias = errors.New("blobs: invalid alias")

ErrInvalidAlias means an alias's namespace or ID isn't letters, digits, '_', '.' and '-', or is "." or "..".

Functions

func Key added in v0.7.26

func Key(h Hash) (string, error)

Key returns where a store keeps h, relative to its root: "sha256/ab/cdef...", split after the first two hex digits. Dir uses this layout on disk, and a bucket store uses it under its prefix, so a tree of blobs copies between them unchanged. It returns ErrInvalidHash for a malformed h.

func ManifestRefs

func ManifestRefs(ctx context.Context, s Store, h Hash, keep func(Hash)) error

ManifestRefs calls keep for the manifest h and for every file it lists, which is what a Referrer for a snapshot needs to pass to Sweep.

func Verify added in v0.7.26

func Verify(rc io.ReadCloser, h Hash) io.ReadCloser

Verify wraps rc, the contents a store read for h, so that reading checks them against h. When the bytes don't match, the final Read returns ErrCorrupt in place of io.EOF, as Store.Open promises. A store outside this package returns it from Open.

Types

type Aliases added in v0.7.14

type Aliases interface {
	Store

	// Alias returns the hash id names in ns, and false when it names none.
	Alias(ctx context.Context, ns, id string) (Hash, bool, error)

	// SetAlias records that id in ns names h, replacing any earlier alias.
	SetAlias(ctx context.Context, ns, id string, h Hash) error
}

Aliases is a Store that also remembers which blob an outside ID names. Each ID lives in a namespace, such as "git" for git blob ids. A driver that lists a source's files with their own content IDs uses it to skip downloading a file the store already holds.

An alias is a hint. The blob it names can be swept, so a caller checks Has before trusting it.

type Cached added in v0.7.27

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

Cached is a Linker that keeps a local Dir in front of a remote Store, such as a bucket store. An app whose blobs live in a bucket can then still build folder views, since views need blobs on this server's disk.

The remote holds every blob, and the local Dir holds copies. Put writes both before it returns, so a server that dies after Put loses nothing. Open and Link serve the local copy, and fetch it from the remote on a miss, so a local folder lost on restart refills on demand. Several servers may share one remote, each with its own Dir.

func NewCached added in v0.7.27

func NewCached(remote Store, local *Dir, opts ...CachedOption) *Cached

NewCached returns a Cached that keeps every blob in remote and copies in local.

func (*Cached) Alias added in v0.7.30

func (c *Cached) Alias(ctx context.Context, ns, id string) (Hash, bool, error)

Alias implements Aliases from the local Dir, so each server keeps its own aliases. A server whose folder was lost finds none, and its next fetch records them again. Callers check Has before using a hash, and Has asks the remote, so an alias never hands out a blob that is gone.

func (*Cached) Has added in v0.7.27

func (c *Cached) Has(ctx context.Context, h Hash) (bool, error)

Has implements Store. It asks the local Dir first, then the remote.

func (c *Cached) Link(h Hash, to string) error

Link implements Linker. It fetches the blob from the remote first when the local Dir lacks it, and fails as Open does when it can't.

func (*Cached) Open added in v0.7.27

func (c *Cached) Open(ctx context.Context, h Hash) (io.ReadCloser, error)

Open implements Store. On a local miss it fetches the blob from the remote and keeps it. A remote blob whose bytes don't match h fails with ErrCorrupt and is never kept locally. A blob neither holds returns ErrNotFound.

func (*Cached) Put added in v0.7.27

func (c *Cached) Put(ctx context.Context, r io.Reader) (Hash, error)

Put implements Store. It writes the local copy first, then sends that copy to the remote, and returns only after both hold the blob. A remote that stores the bytes under some other hash fails the Put with ErrCorrupt.

func (*Cached) SetAlias added in v0.7.30

func (c *Cached) SetAlias(ctx context.Context, ns, id string, h Hash) error

SetAlias implements Aliases in the local Dir.

func (*Cached) Sweep added in v0.7.27

func (c *Cached) Sweep(ctx context.Context, grace time.Duration, refs ...Referrer) (int, error)

Sweep implements Store, and returns how many blobs it removed from the remote. It calls each referrer once and sweeps the remote and then the local Dir with what they named, so a local copy goes when its blob is unreferenced and older than grace. A local copy can outlive its remote blob by one sweep, when it was fetched after the remote wrote it. With WithMaxAge, it then drops every local copy older than the max age.

type CachedOption added in v0.7.27

type CachedOption func(*Cached)

CachedOption configures a Cached.

func WithMaxAge added in v0.7.27

func WithMaxAge(d time.Duration) CachedOption

WithMaxAge lets Sweep drop any local copy fetched or put more than d ago, whether or not a referrer names it, since the remote still holds the blob. The next Open or Link fetches it again. Zero, the default, keeps a referenced local copy for as long as the remote keeps it.

type Dir

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

Dir is a Store and a Linker over a folder on this server's disk. A blob lives at <root>/sha256/ab/cdef..., which is agni's layout, so agni's existing blobs folder works as a Dir's root unchanged.

Writes go through a temp file and a rename, so a reader sees a whole blob or none, and several processes can share one Dir. Every read and removal goes through os.Root, so a link planted in the folder can't lead outside it.

func NewDir

func NewDir(root string) (*Dir, error)

NewDir returns the Dir rooted at root, creating its folder if absent.

func (*Dir) Alias added in v0.7.14

func (d *Dir) Alias(ctx context.Context, ns, id string) (Hash, bool, error)

Alias implements Aliases. An alias file that doesn't hold a valid hash counts as none.

func (*Dir) Has

func (d *Dir) Has(ctx context.Context, h Hash) (bool, error)

Has implements Store.

func (d *Dir) Link(h Hash, to string) error

Link implements Linker. The copy fallback reads through Open, so it copies only a blob that still matches its hash.

func (*Dir) Open

func (d *Dir) Open(ctx context.Context, h Hash) (io.ReadCloser, error)

Open implements Store.

func (*Dir) Path

func (d *Dir) Path(h Hash) (string, error)

Path returns where h's blob is on disk, whether or not the Dir holds it, for a caller that builds a view some other way than Link. It returns ErrInvalidHash for a malformed h.

func (*Dir) Put

func (d *Dir) Put(ctx context.Context, r io.Reader) (Hash, error)

Put implements Store.

func (*Dir) SetAlias added in v0.7.14

func (d *Dir) SetAlias(ctx context.Context, ns, id string, h Hash) error

SetAlias implements Aliases. It writes through a temp file and a rename, so a reader sees the old alias or the new one.

func (*Dir) Sweep

func (d *Dir) Sweep(ctx context.Context, grace time.Duration, refs ...Referrer) (int, error)

Sweep implements Store. It also removes temp files older than grace, which a Put that crashed left behind.

type Hash

type Hash string

Hash names a blob: "sha256:" and 64 lowercase hex digits. It is agni's format.

func HashOf

func HashOf(b []byte) Hash

HashOf returns the Hash of b.

func HashOfKey added in v0.7.26

func HashOfKey(key string) (Hash, bool)

HashOfKey is the inverse of Key. It reports false for a key that names no blob, such as a stray object a person put under the store's prefix.

func ParseHash

func ParseHash(s string) (Hash, error)

ParseHash checks that s is a Hash, and returns ErrInvalidHash if not.

func PutManifest

func PutManifest(ctx context.Context, s Store, m Manifest) (Hash, error)

PutManifest checks m and stores it in s, sorted by path, so the same files give the same hash in any order. It doesn't check that s holds the files' blobs. It returns ErrInvalidManifest for a bad path or hash.

func Spool added in v0.7.26

func Spool(r io.Reader, dir string) (*os.File, Hash, int64, error)

Spool copies r into a new temp file in dir (os.TempDir when empty), hashing as it goes, and returns the file rewound to its start with its hash and size. A store whose backend needs the key before the upload, such as a bucket, spools first. The caller closes and removes the file.

type Linker

type Linker interface {
	Store

	// Link makes the file at to hold h's contents. It hard-links where the filesystem allows,
	// so the view takes no extra space, and copies where it doesn't. A linked file keeps its
	// contents after Sweep removes the blob. It returns ErrInvalidHash or ErrNotFound for a bad
	// or missing h.
	Link(h Hash, to string) error
}

Linker is a Store on this server's disk, whose blobs can appear as files in a folder view.

type Manifest

type Manifest []ManifestEntry

Manifest lists the files of a view, such as a mount snapshot, and the blob holding each. It is stored as a blob itself, so a record names a whole snapshot with one hash however many files it has.

func GetManifest

func GetManifest(ctx context.Context, s Store, h Hash) (Manifest, error)

GetManifest reads the manifest stored under h, checking it as PutManifest does.

type ManifestEntry

type ManifestEntry struct {
	// Path is the file's place in the view: relative, clean and slash-separated.
	Path string `json:"path"`

	Hash Hash  `json:"hash"`
	Size int64 `json:"size"`
}

ManifestEntry is one file in a Manifest.

type Referrer

type Referrer func(ctx context.Context, keep func(Hash)) error

Referrer lists the hashes one owner still names, by calling keep for each. Every owner of blobs in a store passes one to Sweep, such as an app's designs and the mounts registry's snapshots, and Sweep keeps the union.

type Store

type Store interface {
	// Put stores what r yields and returns its hash. Putting contents the store already holds
	// writes nothing new, and counts as a fresh write for Sweep's grace period, so a sweep can't
	// remove the blob before the caller names it.
	Put(ctx context.Context, r io.Reader) (Hash, error)

	// Open returns the blob's contents. The reader checks the hash as it goes. If the bytes
	// don't match, the final Read returns ErrCorrupt in place of io.EOF, so only a caller who
	// reads to the end gets the check. A missing blob returns ErrNotFound.
	Open(ctx context.Context, h Hash) (io.ReadCloser, error)

	// Has reports whether the store holds h.
	Has(ctx context.Context, h Hash) (bool, error)

	// Sweep removes each blob that no referrer names and that was written more than grace ago,
	// and returns how many it removed. The grace period spares a blob that a caller has put but
	// not yet named in a record. If any referrer fails, Sweep removes nothing and returns that
	// error, since it can't know what is in use.
	Sweep(ctx context.Context, grace time.Duration, refs ...Referrer) (int, error)
}

Store keeps blobs. Several processes may share one, and Put is safe to call from all of them at once.

Directories

Path Synopsis
Package blobstest checks that a blobs.Store keeps the contract callers rely on.
Package blobstest checks that a blobs.Store keeps the contract callers rely on.

Jump to

Keyboard shortcuts

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