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 ¶
- Variables
- func ManifestRefs(ctx context.Context, s Store, h Hash, keep func(Hash)) error
- type Dir
- func (d *Dir) Has(ctx context.Context, h Hash) (bool, error)
- func (d *Dir) Link(h Hash, to string) error
- func (d *Dir) Open(ctx context.Context, h Hash) (io.ReadCloser, error)
- func (d *Dir) Path(h Hash) (string, error)
- func (d *Dir) Put(ctx context.Context, r io.Reader) (Hash, error)
- func (d *Dir) Sweep(ctx context.Context, grace time.Duration, refs ...Referrer) (int, error)
- type Hash
- type Linker
- type Manifest
- type ManifestEntry
- type Referrer
- type Store
Constants ¶
This section is empty.
Variables ¶
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") )
Functions ¶
Types ¶
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 (*Dir) Link ¶
Link implements Linker. The copy fallback reads through Open, so it copies only a blob that still matches its hash.
func (*Dir) Path ¶
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.
type Hash ¶
type Hash string
Hash names a blob: "sha256:" and 64 lowercase hex digits. It is agni's format.
func PutManifest ¶
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.
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 ¶
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.