lockedfs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 6 Imported by: ? 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.

README

lockedfs

Serialise a go-filesystems driver behind one mutex, without losing any optional capability the driver had, and latch the first failed fsync so a retry cannot report a lost write as durable.

import "github.com/go-filesystems/lockedfs"

fsys := lockedfs.Lock(driver)              // a directory, or an image with no file of its own
fsys := lockedfs.LockImage(driver, imgFile) // an image the driver reads and writes through imgFile

lockedfs.Mutex(fsys) returns the one mutex behind a wrapped filesystem, for a caller that must run a driver setting exclusive with every call on the image.

Why

The filesystem.Filesystem interface promises nothing about concurrent path-based calls. When one image is served by several callers (SMB, NFS, SFTP, WebDAV at once) there is no common lock. lockedfs is that lock.

A struct embedding filesystem.Filesystem has that method set and nothing more, so a careless wrapper erases Opener, Truncater, LabelReader, TimesReader, MetadataSetter and a handle's WritableFile: nothing fails and everything gets slower (positional I/O measured about 70x faster than the whole-file fallback). Lock returns a value that answers exactly the capabilities of what it wraps: all 2^5 shapes are spelled out and a table test asserts each one.

fsyncgate

Sync on the wrapper is what a client flush reaches. It calls the driver's own Sync if it has one, else fsyncs the image given to LockImage. The first failure, of that fsync or of a writable handle's Sync, is latched until the wrapper is dropped (the share reopened): after an EIO the kernel may report success on the retry without the data being on the medium, so every later Sync of that image returns the first error. A Sync on an already closed handle (os.ErrClosed) does not latch.

A driver whose handle Sync is a silent no-op (it wraps a reader without Sync) is not detected here; that is the driver's defect to fix.

Cost

One uncontended mutex pair per call, no allocation per call (go test -bench .).

Tests

go test -race ./..., 100 % statement coverage enforced in CI, govulncheck. Nothing here parses input, so there is nothing to fuzz.

BSD-3-Clause.

Documentation

Overview

Package lockedfs serialises a go-filesystems driver behind one mutex while keeping exactly the optional capabilities the driver had, and latches the first failed fsync of the image so a retry cannot report success (fsyncgate).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lock

Lock wraps a driver so that every caller shares one lock. See lockFS.

func LockImage

func LockImage(fsys filesystem.Filesystem, img interface{ Sync() error }) filesystem.Filesystem

LockImage is Lock for a driver that reads and writes through img: a flush on a driver with no Sync of its own fsyncs img, and the first failure of that fsync (or of a handle's Sync) is latched for the life of the wrapper. See lockFSImage.

func Mutex added in v0.2.0

func Mutex(fsys filesystem.Filesystem) (*sync.Mutex, bool)

Mutex returns the one mutex behind a filesystem that Lock or LockImage returned, whatever capabilities it carries, and false for any other value. A caller that must run something exclusive with every call on the image (a driver setting that cannot run beside a mutator) takes it; it must not call back into the wrapper while holding it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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