blobs

package
v0.7.22 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 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 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.

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 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 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.

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.

Jump to

Keyboard shortcuts

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