mounts

package
v0.7.13 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 12 Imported by: 4 modules, 0 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 mounts names the places an app's files live. A mount is a folder or a GitHub repo, and later an S3 or GCS bucket. Each is reached by a name, and a file in it by a mount:// URI. An app keeps its own records in its own store, and those records point into mounts by URI.

This package holds the contract: the Mount record, the URI and name rules, the interfaces a driver and a record store implement, and the errors they return. The design, with how agni and notation use it, is at https://panyam.github.io/goapplib/reference/mounts/.

Index

Constants

View Source
const Scheme = "mount://"

Scheme is the URI scheme for a file in a mount.

Variables

View Source
var (
	// ErrNotFound means no mount has that name, or no file has that path in the mount, or the
	// source (a repo, a ref) doesn't exist upstream.
	ErrNotFound = errors.New("mounts: not found")

	// ErrExists means a mount with that name exists already, fixed or runtime.
	ErrExists = errors.New("mounts: already exists")

	// ErrInvalidArgument means a name, a source or a ref is malformed, or a request asked for
	// something the driver doesn't do, such as a mode it doesn't support.
	ErrInvalidArgument = errors.New("mounts: invalid argument")

	// ErrInvalidPath means a path is absolute, climbs out of its mount, or names a link.
	ErrInvalidPath = errors.New("mounts: invalid path")

	// ErrFixed means the request would change a mount that comes from the server's
	// configuration, which only a restart with new configuration changes.
	ErrFixed = errors.New("mounts: mount is fixed")

	// ErrTooLarge means a snapshot or a write would pass a Limits cap.
	ErrTooLarge = errors.New("mounts: over the size or file-count limit")

	// ErrUnavailable means the source failed or refused for now: a network error, a 5xx, or a
	// rate limit. Retrying later may work.
	ErrUnavailable = errors.New("mounts: source unavailable")

	// ErrConflict means a write's IfMatch didn't match the file's current version, because
	// someone else changed or created it first. Read it again and retry.
	ErrConflict = errors.New("mounts: version conflict")

	// ErrReadOnly means the mount can't be written: it is a snapshot, or its driver has no
	// Writer.
	ErrReadOnly = errors.New("mounts: mount is read-only")

	// ErrNotLocal means the mount has no folder on this server's disk, so it has no host path.
	ErrNotLocal = errors.New("mounts: no host path")

	// ErrInUse means the app's in-use check says records still point into the mount, so it
	// can't be deleted.
	ErrInUse = errors.New("mounts: mount is in use")
)

The errors a driver, a record store and the registry return, wrapped with detail. An app maps them onto its own transport with errors.Is. The mounts design page has the table agni uses.

View Source
var DefaultLimits = Limits{MaxBytes: 256 << 20, MaxFiles: 100_000}

DefaultLimits are agni's caps for a GitHub snapshot: 256 MB and 100,000 files.

Functions

func CleanPath

func CleanPath(p string) (string, error)

CleanPath applies NewURI's path rules to p, a path inside a mount. It returns the clean, relative, slash-separated form, and "" for the mount's root. An absolute path, or one that climbs out with "..", wraps ErrInvalidPath. Drivers call it on every path they are given, so a path that skipped NewURI still can't leave the mount.

func ValidName

func ValidName(name string) error

ValidName reports whether name can name a mount. A name is 1 to 64 characters. The first is a-z or 0-9, and the rest may also be '-', '_' or '.'. The rule keeps names safe as a folder name, a URL path element and a YAML file name on any OS. It returns an error wrapping ErrInvalidArgument.

Types

type DeleteRequest

type DeleteRequest struct {
	Path    string
	IfMatch FileVersion
	Message string
}

DeleteRequest removes Path. IfMatch must be the file's current version, or AnyVersion.

type Driver

type Driver interface {
	// Kind is the Kind of mount this driver serves.
	Kind() Kind

	// Modes lists the modes the driver supports. A request for any other returns
	// ErrInvalidArgument.
	Modes() []Mode

	// Resolve checks source and turns ref into a version: a commit for GitHub, an empty string
	// for a folder. An empty ref means the source's default, such as the default branch. A
	// source or ref that doesn't exist returns ErrNotFound.
	Resolve(ctx context.Context, source, ref string, ts oauth2.TokenSource) (version string, err error)

	// Open returns the mount's files. For a snapshot mount they are the files at m.Version, and
	// the same call returns the same bytes for the life of the mount. For a live mount they are
	// the source's latest. Paths in the fs.FS are clean, relative and slash-separated, so a
	// URI's Path opens directly. Only regular files and folders appear in it, never links.
	Open(ctx context.Context, m Mount, ts oauth2.TokenSource) (fs.FS, error)
}

Driver reads one Kind of source. The registry calls it with the token source of the mount's connection, or nil for a public or local source. A driver doesn't know about connections or viewers. It sends the token it is given.

type FileVersion

type FileVersion string

FileVersion identifies one version of one file, in the driver's own format.

const AnyVersion FileVersion = "*"

AnyVersion as IfMatch lets a write replace whatever version is there, or create the file.

type Kind

type Kind string

Kind says which driver serves a mount.

const (
	// KindFolder is a folder on the server's disk. Folder mounts come from the server's own
	// configuration, never from a request, since a client may not pick a server folder.
	KindFolder Kind = "folder"

	// KindGitHub is a GitHub repository, read through GitHub's REST API.
	KindGitHub Kind = "github"
)

type Limits

type Limits struct {
	MaxBytes int64
	MaxFiles int
}

Limits caps what a snapshot or a write may hold. A driver returns ErrTooLarge past either. Zero means the default.

type Localizer

type Localizer interface {
	Driver

	// HostPath returns the host path of p inside m. It returns ErrNotLocal when m has no
	// folder here, and ErrInvalidPath when p would leave the mount.
	HostPath(m Mount, p string) (string, error)
}

Localizer is a Driver whose mounts can have a folder on this server's disk, for code that needs a host path, such as a native tool. A snapshot driver's view counts too.

type Mode

type Mode string

Mode says whether a mount's files can change after it is made.

const (
	// ModeSnapshot pins the mount at the version its ref resolved to when it was made. Its files
	// never change, so a record that points into it keeps meaning the same bytes. agni uses this
	// for designs.
	ModeSnapshot Mode = "snapshot"

	// ModeLive reads the source's latest version on each open, and a Writer can change it.
	// notation uses this for notations it edits.
	ModeLive Mode = "live"
)

type Mount

type Mount struct {
	// Name is the mount's handle in URIs: mount://<Name>/<path>. ValidName gives the rule.
	Name string

	Kind Kind

	// Source says where the files come from, in the driver's own form: "github://owner/repo"
	// for GitHub, or an absolute path for a folder.
	Source string

	// Ref is what the mount was asked for, such as a branch, a tag or a commit. Empty means the
	// source's default.
	Ref string

	// Version is what Ref resolved to when the mount was made, such as a commit sha. The driver
	// defines its format. For a snapshot mount the files are those at Version. A live mount has
	// moved on since, and its Writer returns each new version.
	Version string

	Mode Mode

	// Connection is the ID of the connection the driver reads through (package connections).
	// Empty means the source is public, or local.
	Connection string

	// Manifest is the hash of the blob listing a snapshot's files and their hashes, which is
	// what rebuilds a snapshot's view after a restart. It is empty for a live mount.
	Manifest string

	Created time.Time
}

Mount is the library's record of one mount. An app keeps any fields of its own in its own records, keyed by Name.

type Option

type Option func(*Registry)

Option configures a Registry.

func WithCredentials

func WithCredentials(f func(ctx context.Context, connection string) (oauth2.TokenSource, error)) Option

WithCredentials sets how the registry gets a mount's token source from its Connection ID. The connections service's TokenSource method has this signature. It is called with the request's context, so a connection that belongs to one person sees who is asking. A mount with an empty Connection gets a nil token source without a call.

func WithFixed

func WithFixed(ms ...Mount) Option

WithFixed adds mounts from the server's configuration, such as agni's --mount flags. They can't be deleted, and a runtime record with the same name is ignored. They are the only way to get a KindFolder mount.

func WithInUse

func WithInUse(f func(ctx context.Context, name string) (bool, error)) Option

WithInUse sets the app's check for whether its records still point into a mount. Delete calls it, and returns ErrInUse when it says true.

func WithLimits

func WithLimits(l Limits) Option

WithLimits caps writes through the registry. A zero field keeps DefaultLimits' value. Drivers that snapshot take their own Limits.

func WithRecheck

func WithRecheck(d time.Duration) Option

WithRecheck sets how often the registry asks the store whether another process changed it. The default is a second. Tests set it lower.

type Preparer

type Preparer interface {
	Driver
	Prepare(ctx context.Context, m Mount, ts oauth2.TokenSource) (Mount, error)
}

Preparer is a Driver with work to do when a mount is made, after Resolve. A GitHub snapshot downloads the commit's files into the blob store there, and sets the mount's Manifest. The registry stores the Mount that Prepare returns. A Prepare error fails the Create, and no record is written.

type PutRequest

type PutRequest struct {
	Path    string
	Data    []byte
	IfMatch FileVersion

	// Message is the commit message, for drivers that commit. Others ignore it.
	Message string
}

PutRequest writes Data to Path. IfMatch guards against overwriting a change the caller hasn't seen. Empty means the file must not exist yet. AnyVersion means write regardless. Anything else must equal the file's current version. A mismatch returns ErrConflict.

type RecordStore

type RecordStore interface {
	// ListMounts returns every record, in name order.
	ListMounts(ctx context.Context) ([]Mount, error)

	// GetMount returns the record named name, or ErrNotFound.
	GetMount(ctx context.Context, name string) (Mount, error)

	// CreateMount adds m. A record with m.Name returns ErrExists, so of two processes creating
	// the same name, one wins.
	CreateMount(ctx context.Context, m Mount) error

	// DeleteMount removes the record named name. Deleting a missing record is not an error.
	DeleteMount(ctx context.Context, name string) error

	// Generation returns a value that changes whenever any process creates or deletes a record.
	// The registry compares it with the last one it read, and lists again only when they differ.
	// The format is the store's own.
	Generation(ctx context.Context) (string, error)
}

RecordStore keeps the runtime mounts, the ones made by requests rather than by the server's configuration. Records are created and deleted, never changed in place. A live mount's latest version comes from its driver, not from its record.

Several processes may share one store, such as a server and a worker. Each notices the others' changes through Generation.

type Registry

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

Registry is an app's one view of its mounts. It joins the fixed mounts from the server's configuration, the runtime mounts in a RecordStore, and the drivers that read them, and it asks for each mount's credentials when a driver needs them.

Several registries, in one process or several, can share a RecordStore. Each checks the store's Generation at most once per recheck interval (a second by default), and lists the store again only when it changed. A registry sees its own creates and deletes at once.

A Registry is safe for concurrent use.

func NewRegistry

func NewRegistry(store RecordStore, drivers []Driver, opts ...Option) *Registry

NewRegistry returns a registry over store, serving the kinds of drivers. Two drivers of one kind keep the later one.

func (*Registry) BlobRefs

func (r *Registry) BlobRefs(store blobs.Store) blobs.Referrer

BlobRefs returns a blobs.Referrer that names, for every snapshot mount, its manifest and every file the manifest lists, so a sweep of store keeps them. Pass it to store's Sweep beside the app's own referrers. A manifest that can't be read fails the referrer, and so the sweep, rather than letting the sweep remove a snapshot's files. The registry rereads its records at most once per recheck interval, so a sweep's grace period should be longer than that.

func (*Registry) Create

func (r *Registry) Create(ctx context.Context, m Mount) (Mount, error)

Create makes a runtime mount from m's Name, Kind, Source, Ref, Mode and Connection. An empty Mode takes the driver's first. Create resolves Ref through the driver, calls Prepare when the driver has it, sets Version and Created, and writes the record. It returns ErrInvalidArgument for a bad name, an unknown kind, a mode the driver doesn't serve, or KindFolder. A name taken by a fixed or runtime mount returns ErrExists. Driver errors pass through, such as ErrNotFound for a source that doesn't exist.

func (*Registry) Delete

func (r *Registry) Delete(ctx context.Context, name string) error

Delete removes the runtime mount named name. A fixed mount returns ErrFixed, a missing one ErrNotFound, and one the in-use check says is still referenced ErrInUse. Delete removes only the record. Blobs a snapshot used stay until a blob sweep finds nothing naming them.

func (*Registry) FS

func (r *Registry) FS(ctx context.Context) fs.FS

FS returns every mount as one fs.FS, each under its name, through goutils' mountfs. It lists the mounts once, when called, and opens each mount's driver when a path first reaches it. The context is kept for those opens, so the viewer it carries is the one whose connections are used. An app calls FS per request, or per job with that job's context.

func (*Registry) Get

func (r *Registry) Get(ctx context.Context, name string) (Mount, error)

Get returns the mount named name, or ErrNotFound.

func (*Registry) HostPath

func (r *Registry) HostPath(ctx context.Context, u URI) (string, error)

HostPath returns the host path of the file u names, for code that needs a real file. A mount whose driver isn't a Localizer returns ErrNotLocal.

func (*Registry) List

func (r *Registry) List(ctx context.Context) ([]Mount, error)

List returns every mount, fixed and runtime, in name order. When the store fails after a good read, List keeps returning the last good list rather than dropping every runtime mount.

func (*Registry) Open

func (r *Registry) Open(ctx context.Context, u URI) (fs.File, error)

Open opens the file or folder u names.

func (*Registry) Put

func (r *Registry) Put(ctx context.Context, name string, req PutRequest) (*WriteResult, error)

Put writes a file in the mount named name, with req.Path cleaned by CleanPath. A snapshot mount, or one whose driver isn't a Writer, returns ErrReadOnly. Data over the registry's MaxBytes returns ErrTooLarge. The IfMatch rules are mounts.PutRequest's.

func (*Registry) Remove

func (r *Registry) Remove(ctx context.Context, name string, req DeleteRequest) (*WriteResult, error)

Remove deletes a file in the mount named name, under the same rules as Put.

func (*Registry) Stat

func (r *Registry) Stat(ctx context.Context, u URI) (FileVersion, error)

Stat returns the current version of the file u names, for a later Put or Remove. A mount that can't be written returns ErrReadOnly.

type URI

type URI struct {
	Mount string
	Path  string
}

URI names a file or folder in a mount: mount://<Mount>/<Path>. Path is clean and relative, and never leaves the mount, so resolving it against the mount's root can't reach a file outside it. The empty Path is the mount's root.

func NewURI

func NewURI(mount, p string) (URI, error)

NewURI builds a URI from a mount name and a path inside it. The path may use '\' as a separator and may end in '/'. It may not be absolute or climb out with "..". Both failures wrap ErrInvalidPath, and a bad name wraps ErrInvalidArgument.

func ParseURI

func ParseURI(s string) (URI, error)

ParseURI parses "mount://<name>/<path>" with the rules of NewURI. Any other scheme wraps ErrInvalidPath.

func (URI) Join

func (u URI) Join(elem ...string) (URI, error)

Join returns the URI of elem under u, with the rules of NewURI.

func (URI) String

func (u URI) String() string

String formats the URI as mount://<Mount>/<Path>, which ParseURI reads back.

type WriteResult

type WriteResult struct {
	// File is the file's new version. It is empty after a Delete.
	File FileVersion

	// Mount is the mount's new version, such as the commit a GitHub write made. Drivers whose
	// sources have no version across files leave it empty.
	Mount string
}

WriteResult is what a write produced.

type Writer

type Writer interface {
	Driver

	// Stat returns the current version of the file at p, which a later Put or Delete passes as
	// IfMatch. For GitHub it is the file's blob sha. A missing file returns ErrNotFound.
	Stat(ctx context.Context, m Mount, ts oauth2.TokenSource, p string) (FileVersion, error)

	// Put creates or replaces the file at req.Path. A GitHub Put is one commit.
	Put(ctx context.Context, m Mount, ts oauth2.TokenSource, req PutRequest) (*WriteResult, error)

	// Delete removes the file at req.Path. A GitHub Delete is one commit.
	Delete(ctx context.Context, m Mount, ts oauth2.TokenSource, req DeleteRequest) (*WriteResult, error)
}

Writer is a Driver that can change a live mount. A write to a snapshot mount returns ErrReadOnly before reaching the driver.

Directories

Path Synopsis
Package folder is the mounts driver for a folder on the server's disk.
Package folder is the mounts driver for a folder on the server's disk.
Package fsstore keeps mount records in a folder, one YAML file per mount, named <name>.yaml.
Package fsstore keeps mount records in a folder, one YAML file per mount, named <name>.yaml.
Package github is the mounts driver for GitHub repositories, read and written through GitHub's REST API.
Package github is the mounts driver for GitHub repositories, read and written through GitHub's REST API.
githubtest
Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests.
Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests.
Package mountstest checks that a mounts.RecordStore keeps the contract the registry relies on.
Package mountstest checks that a mounts.RecordStore keeps the contract the registry relies on.

Jump to

Keyboard shortcuts

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