Documentation
¶
Overview ¶
Package localfs is a storage.Store on a local directory.
Layout ¶
The store owns everything under its root directory:
lock empty file; flock(2) on it serialises Replace and Delete tmp/<random> writes in progress objects/<sha256(key)> committed objects
An object file starts with a header (the magic "SKYLOBJ1", a 32-character version token, the key length as a big-endian uint16, and the key) followed by the object's bytes. File names are the hex SHA-256 of the key, so a key never becomes a path: path traversal through keys is impossible, keys that differ only in case stay apart on case-insensitive filesystems such as the macOS default, and "a/b" and "a/b/c" can coexist. All file access goes through os.Root, which refuses to follow a symbolic link that leads outside the root.
Writes ¶
Every write first writes the header and body to a new file in tmp/ and fsyncs it. Nothing in tmp/ is ever visible as an object, so a write that fails, is cancelled or is killed leaves at most an orphaned temporary file.
Create commits with link(2) from the temporary file to the object's name. link fails if the name exists, atomically, so exactly one of any number of concurrent creators wins, in one process or many.
Replace takes an exclusive flock on the lock file, reads the current object's version token, compares it, and commits with rename(2), which replaces the object atomically. Delete takes the same lock, so a Delete cannot slip between a Replace's check and its rename. flock locks belong to an open file description and the lock file is opened per call, so the lock excludes goroutines of one process as well as other processes. The kernel drops the lock of a killed process.
After the commit, objects/ is fsynced, so a successful write has fsynced both the file and its directory entry. Delete fsyncs objects/ also when the key is already missing, because another caller may have unlinked it and died before its fsync. An error from that fsync, after the link, rename or unlink took effect, wraps storage.ErrOutcomeUnknown. On macOS, os.File.Sync issues F_FULLFSYNC.
A read keeps the object file open, so an open body keeps returning its version after a Replace or Delete.
Version tokens are 128 random bits from crypto/rand, written in the header and therefore replaced together with the content.
Limits ¶
- Linux and macOS only. The package does not build elsewhere: it needs flock(2), hard links and rename(2) replacing an existing file atomically, which Windows does not provide with these semantics.
- Local filesystems only, all of root on one filesystem. Network filesystems (NFS, SMB, FUSE mounts of object storage) are not supported: flock on them may be local to one client or emulated, and link and rename may not be atomic across clients. Filesystems without hard links (FAT, exFAT) are not supported.
- Orphaned files in tmp/ left by killed processes are not removed. They are harmless and may be deleted while no process has the store open.
- List reads the header of every object for every page: its cost grows with the number of objects in the store, not with the page size.
- Replace and Delete are serialised across the whole store.
- Nothing else may write under the root.
Index ¶
- type Store
- func (s *Store) Close() error
- func (s *Store) Create(ctx context.Context, key string, body io.Reader) (string, error)
- func (s *Store) Delete(ctx context.Context, key string) error
- func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, storage.Info, error)
- func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)
- func (s *Store) Head(ctx context.Context, key string) (storage.Info, error)
- func (s *Store) List(ctx context.Context, prefix, startAfter string, limit int) ([]storage.Entry, error)
- func (s *Store) Replace(ctx context.Context, key, version string, body io.Reader) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a storage.Store on a local directory. Close it when done.
func Open ¶
Open opens the store rooted at dir, which must exist. Several Stores, in one process or several, may open the same directory at once. Open fails if tmp or objects is not a directory of its own (a symbolic link, or both names for one directory), since that would make writes in progress visible as objects.
func (*Store) GetRange ¶
func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)
GetRange implements storage.Store.