localfs

package
v0.0.6 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: 15 Imported by: 0 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 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

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

func Open(dir string) (*Store, error)

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) Close

func (s *Store) Close() error

Close releases the root directory.

func (*Store) Create

func (s *Store) Create(ctx context.Context, key string, body io.Reader) (string, error)

Create implements storage.Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, key string) error

Delete implements storage.Store.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, storage.Info, error)

Get implements storage.Store.

func (*Store) GetRange

func (s *Store) GetRange(ctx context.Context, key string, offset, length int64) (io.ReadCloser, storage.Info, error)

GetRange implements storage.Store.

func (*Store) Head

func (s *Store) Head(ctx context.Context, key string) (storage.Info, error)

Head implements storage.Store.

func (*Store) List

func (s *Store) List(ctx context.Context, prefix, startAfter string, limit int) ([]storage.Entry, error)

List implements storage.Store.

func (*Store) Replace

func (s *Store) Replace(ctx context.Context, key, version string, body io.Reader) (string, error)

Replace implements storage.Store.

Jump to

Keyboard shortcuts

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