filer

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 2 modules, 2 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

go-filer

Go Reference Test

A filer stores the objects in a volume. Each backend implements filer.Filer, and must behave the same way, so that a volume can be stored on a filesystem or in S3 without clients seeing a difference. The semantics follow S3, and the filesystem backend emulates S3 where they differ.

Packages

Package Description
github.com/mutablelogic/go-filer The Filer interface, the WriterFS interface of a writable filesystem, and the errors
github.com/mutablelogic/go-filer/filer The registry of the volumes' backends, which are created from URLs, such as file://media/mnt/media, and watched
github.com/mutablelogic/go-filer/filer/schema The objects, requests and events
github.com/mutablelogic/go-filer/filer/fs The filesystem backend, for any fs.FS, and writable for a directory
github.com/mutablelogic/go-filer/filer/fs/fsevents Watches a directory for changes, on macOS, Linux and Windows. It has no dependencies on the other packages

Usage

fs.New returns a writable filer for a directory, and fs.NewFS returns a filer for any fs.FS, which is read-only unless it implements filer.WriterFS. Either can be made read-only with the readonly option, when creating and deleting objects is filer.ErrForbidden. The filer takes ownership of the filesystem, and closes it when it's closed.

import (
    "context"
    "fmt"
    "io"
    "os"

    gofiler "github.com/mutablelogic/go-filer"
    filerfs "github.com/mutablelogic/go-filer/filer/fs"
    schema "github.com/mutablelogic/go-filer/filer/schema"
)

func main() {
    ctx := context.Background()

    // A writable filer for the directory, named "media"
    filer, err := filerfs.New("media", "/mnt/media", nil, gofiler.Opts{})
    if err != nil {
        panic(err)
    }
    defer filer.Close()

    // Read an object, within the function returned
    read, obj, err := filer.ReadObject(ctx, schema.GetObjectRequest{
        ObjectKey: schema.ObjectKey{Path: "/music/song.mp3"},
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(obj.ContentType, *obj.Size)
    if err := read(ctx, func(r io.Reader) error {
        _, err := io.Copy(os.Stdout, r)
        return err
    }); err != nil {
        panic(err)
    }
}

ReadObject returns the object, and a function which reads its content: the file is opened when the function is called, the content is read within the function passed to it, and the file is closed when that returns. Nothing is opened when the function isn't called. The file isn't read, and filer.ErrPreconditionFailed is returned, when it changed after the object was returned.

Closing the filer stops listings, reads, writes and watches which are in progress, which return fs.ErrClosed. The list iterator can be used from more than one goroutine.

Options

The options of the filesystem backend are passed to New and NewFS as url.Values, and are the query parameters of the filer's URL, fs://<name>, when they aren't the defaults. They're followed by the options of a backend, gofiler.Opts, whose Tracer traces each operation, such as fs.ListObjects, with the volume and path, when it's set. Each option can be repeated, and setting one replaces its defaults, so an empty value, such as skip=, sets none.

Option Description Default
skip A regular expression of the names of files and directories which aren't objects, nor is anything in a directory with the name Hidden files, starting with ., and those which operating systems and applications leave alongside content, such as Thumbs.db, desktop.ini, Icon\r, Microsoft Office owner files (~$*) and backup files (*~)
package A regular expression of the names of directories which are packages (?i)\.bundle$ and (?i)\.sparsebundle$
readonly Whether objects can only be read, and not created or deleted, even when the filesystem is writable: true, false, or empty, which is true. A filesystem which isn't writable is read-only whatever it's set to false

An unknown option, an expression which doesn't compile, or a readonly which isn't a boolean, is filer.ErrBadParameter.

Object Model

  • An object is a file, identified by its path within the volume, which starts with /. The root of the volume is /.
  • A directory exists when an object exists beneath it. Directories have no size or modification time, and their content type is inode/directory.
  • The key of a file is its path, and the key of a directory is its path followed by /. Listings are in the byte order of keys, as in S3.
  • Empty parent directories are removed by the filesystem backend when an object is deleted, as an S3 prefix with no objects beneath it does not exist.
  • Files which are not regular files, such as sockets, named pipes and devices, are not objects, nor are symbolic links which are broken, or lead outside the volume.
  • Files and directories whose names are skipped (see skip) are not objects, and can't be created.
  • A package, such as a macOS sparse disk image, is a directory which is an object, as Finder shows it as one file. It has a modification time, and the content type of its extension, but no size, and can't be read. The files in it are not objects, and are deleted with it.
  • Paths which aren't valid fs.FS paths, with //, . or .. elements, or a trailing /, are fs.ErrInvalid.
  • The content type of an object is from its extension, as sniffing the content would mean reading it.

Operations

Operation Filesystem S3
Clone with options Open the directory again, at its path Create a client again, and check the bucket with HeadBucket, with the credentials, which the URL doesn't have
GetObject of a file stat HeadObject
GetObject of a directory stat HeadObject returns 404, then ListObjectsV2 with Prefix="dir/" and MaxKeys=1 to check the directory exists
GetObject with IfModifiedSince or IfNoneMatch filer.ErrNotModified when it hasn't changed, to the second, or its ETag, or *, matches. IfNoneMatch is used in place of IfModifiedSince when it's set HeadObject with IfModifiedSince or IfNoneMatch, which returns 304
ReadObject open, checking the file hasn't changed GetObject, with IfMatch of the ETag returned
CreateObject Write a hidden temporary file, then rename it over the object, so it's replaced atomically PutObject, or a multipart upload
CreateObject with IfNotExists Link the temporary file to the object, which fails when it exists PutObject with If-None-Match: *
CreateObject with IfExists Check the object exists, then rename over it PutObject with If-Match of the ETag of the object
ListObjects Walk the directory, in key order ListObjectsV2 with Delimiter="/", or no delimiter when recursive. Directories are the CommonPrefixes
ListObjects with StartAfter Seek within each directory StartAfter, dropping a CommonPrefix equal to it
ListObjects of a missing path 404 An empty listing, so an extra request is needed to return 404
DeleteObjects of a file remove HeadObject for the metadata, and to return 404 as DeleteObject succeeds for missing keys, then DeleteObject
DeleteObjects of a directory RemoveAll. Returns the files deleted, including hidden files List the prefix, then DeleteObjects in batches of 1000. Returns the keys deleted
Remove empty parent directories After each delete Not needed, as empty prefixes do not exist
WatchObjects Watch the directory (see Watching) Subscribe to the changes to a SeaweedFS filer's metadata, with the seaweedfs-filer-grpc option, and otherwise filer.ErrNotImplemented, as S3's event notifications need a queue

Object metadata isn't stored yet, so CreateObject with metadata is filer.ErrNotImplemented.

Watching

WatchObjects sends each change to an object to a channel, until the context is cancelled or the filer is closed. A change is an object which is created, updated or deleted, or a directory which should be rescanned, as its changes may have been missed. Changes to files which aren't objects aren't sent, and a change within a package is an update to the package.

Directories are watched with the fsevents package, using the efficient mechanism of each platform:

Platform Mechanism
macOS FSEvents, which needs cgo
Linux inotify
Windows ReadDirectoryChangesW

On other platforms, and on macOS without cgo, and for a filer of a filesystem which isn't a directory, WatchObjects returns filer.ErrNotImplemented.

The registry of volumes is watched by its Run, which watches each volume when it's added, or updated, until it's removed, and sends their changes, with the names of their volumes, to the calls to its WatchObjects, until its context is cancelled, or the registry is closed, which ends them, so it should be run while the registry is used, as the server does. Without it, the volumes aren't watched. It holds a change to an object until the object is quiet for 250ms (filer.WithDebounce), or at most two seconds, and merges its changes, so an object which changes many times, such as a file which is written in parts, is one change, one which is created and deleted isn't one, and one which is deleted and created again is updated. A call to WatchObjects isn't held up by the others, so when it's slow to receive the changes, a volume's changes are dropped, and it's sent a rescan of the volume, once it can receive it. With a meter (filer.WithMeter), Run counts the changes in filer.events, a counter whose attributes are the worker, filer.worker, which is the hostname, or filer.WithWorker's name, the volume, filer.volume, and the change, filer.event.type, such as created, which the meter's provider pushes to the metrics server.

Modification Time

The modification time of an object is stored in the x-amz-meta-mtime metadata of an S3 object, which is used in preference to LastModified. LastModified is the time the object was uploaded, and cannot be set, so it is only used when the metadata is not set. Its value is seconds since the Unix epoch, with an optional fraction of a second, such as 1764569708.5, as rclone and s3fs write it, so the times they write are read too. It isn't part of an object's user metadata, so mtime can't be a key of it, outside a namespace.

ListObjectsV2 does not return metadata, so a listing from S3 has the LastModified time, which can differ from the modification time returned by GetObject.

Differences

These cannot be made identical, and are known differences between the backends:

  • Keys. S3 allows keys which cannot be represented as paths, such as those containing //, . or .. elements, \ or :, or a trailing /, and a file and a directory with the same name, such as a and a/b. The S3 backend skips these keys in listings, so that they are not objects, in the same way as hidden files.
  • Directory markers. Zero-byte objects whose keys end in / are created by the S3 console and some tools to represent directories. They are not objects, and are skipped in listings.
  • Atomicity. Deleting a directory in S3 takes several requests, and can fail part of the way through. Deleting a directory on the filesystem is not atomic either.
  • ETag. S3 returns an ETag for each object. The filesystem backend does not set it, so only IfNoneMatch: * matches.
  • Packages. S3 has no packages, so a package's files are objects in S3.

Errors

Errors are converted to HTTP errors by filer.HTTPErr. The filesystem backend returns filer errors, and fs errors, such as fs.ErrNotExist:

Error HTTP status
fs.ErrNotExist, filer.ErrNotFound 404
fs.ErrInvalid, filer.ErrBadParameter 400
fs.ErrExist, filer.ErrConflict 409
fs.ErrPermission, filer.ErrForbidden 403
fs.ErrClosed, filer.ErrServiceUnavailable 503
filer.ErrNotModified 304
filer.ErrPreconditionFailed 412
filer.ErrUnprocessableContent 422
filer.ErrNotImplemented 501

The S3 backend converts S3 errors in the same way:

S3 error Error HTTP status
NoSuchKey, NoSuchBucket fs.ErrNotExist 404
AccessDenied filer.ErrForbidden 403
SlowDown, ServiceUnavailable filer.ErrServiceUnavailable 503
PreconditionFailed filer.ErrPreconditionFailed 412

Development

The packages are vetted and tested with make, with GOWORK=off:

Target Description
make test Vet, then run the tests, and their coverage, in build/coverage.txt
make vet Vet the packages for macOS, Linux, Windows and FreeBSD
make race-test Run the tests with the race detector, which needs cgo
make tidy Tidy the module
make clean Remove build/

The test workflow vets the packages, and runs the race tests on Linux, macOS and Windows, for pull requests and pushes to main.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPErr added in v0.1.2

func HTTPErr(err error) error

HTTPErr translates an error into an error wrapping a HTTP status code

func PatchValues added in v0.3.0

func PatchValues(values, patch url.Values) url.Values

PatchValues returns a copy of the options, changed by the patch: each key of the patch replaces the option's values with its own, or, when it has none, removes the option, so it's the default

Types

type CreateMode added in v0.3.0

type CreateMode int

CreateMode is whether a file is created, replaced, or either

const (
	CreateOrReplace CreateMode = iota // a file is created, or replaces one which exists
	CreateOnly                        // a file which exists isn't replaced
	ReplaceOnly                       // a file which doesn't exist isn't created
)

type CredentialProvider added in v0.3.0

type CredentialProvider interface {
	// Expand expands the given value using the credential provider, substituting any
	// credentials $PLACEHOLDER or ${PLACEHOLDER} as needed.
	Expand(ctx context.Context, value string) (string, error)

	// Get returns the credential for the key, as JSON
	Get(ctx context.Context, key string) (json.RawMessage, error)

	// Set stores the credential for the key, which is JSON, replacing any
	// credential with the key
	Set(ctx context.Context, key string, value json.RawMessage) error
}

type Err added in v0.1.2

type Err int

Errors

const (
	ErrSuccess Err = iota
	ErrNotFound
	ErrBadParameter
	ErrNotImplemented
	ErrConflict
	ErrInternalServerError
	ErrServiceUnavailable
	ErrForbidden
	ErrNotModified
	ErrCreated              // not a failure: the object was created, rather than replaced
	ErrPreconditionFailed   // a condition of the request isn't met, such as replacing an object which doesn't exist
	ErrUnprocessableContent // the request can't be processed, such as creating an object whose name is skipped
)

func (Err) Error added in v0.1.2

func (e Err) Error() string

func (Err) HTTP added in v0.1.2

func (e Err) HTTP() httpresponse.Err

func (Err) With added in v0.1.2

func (e Err) With(args ...any) error

func (Err) Withf added in v0.1.2

func (e Err) Withf(format string, args ...any) error

Withf returns an error wrapping the error code, with a formatted message. As with fmt.Errorf, the format may use %w to wrap errors, so that they can still be matched with errors.Is and errors.As.

type Filer added in v0.3.0

type Filer interface {
	io.Closer

	// URL returns the filer URL. The scheme, host (bucket/name),
	// and path (prefix/directory) identify the storage location. Query
	// parameters carry useful non-credential details: region, endpoint, anonymous, readonly, etc.
	URL() *url.URL

	// Clone returns a new filer of the same storage, with the options of the
	// filer, changed by the patch (see PatchValues), which is nil to keep
	// them, from what the filer has, rather than its URL, which doesn't have
	// its credentials. The filer isn't changed, and the clone is checked as a
	// new filer is, such as a bucket being accessible. The registry clones a
	// volume's filer to change its options, and to open it again when
	// watching it fails.
	Clone(context.Context, url.Values) (Filer, error)

	// Create object in the filer
	CreateObject(context.Context, schema.CreateObjectRequest) (*schema.Object, error)

	// Get object metadata from the filer
	GetObject(context.Context, schema.GetObjectRequest) (*schema.Object, error)

	// Read object content from the filer, by calling the returned function,
	// with the object's metadata
	ReadObject(context.Context, schema.GetObjectRequest) (ReadObjectFunc, *schema.Object, error)

	// List objects in the filer. Call Next on the returned iterator until
	// io.EOF is returned. Caller must close the returned iterator.
	ListObjects(context.Context, schema.ListObjectsRequest) (schema.ListObjectsIterator, error)

	// Delete an object in the filer, and return the deleted object. A
	// directory is only deleted when the request is recursive, in which case
	// every object beneath it is deleted too. The root of a volume cannot be
	// deleted.
	DeleteObjects(context.Context, schema.DeleteObjectsRequest) (*schema.DeleteObjectsResponse, error)

	// Watch for changes to objects, sending each to the channel, until the
	// context is cancelled. The channel isn't closed. It will return
	// filer.ErrNotImplemented if the filer backend does not support watching objects.
	WatchObjects(context.Context, chan<- schema.Event) error
}

Filer is the interface for storing files and objects.

type Opts added in v0.3.0

type Opts struct {
	// Tracer traces the requests of the backend to other services, such as
	// S3, when it's set
	Tracer trace.Tracer

	// Credentials expand the placeholders of the credentials in the URL of a
	// backend, such as ${S3_SECRET}, when it's set
	Credentials CredentialProvider
}

Opts are the options of a backend, which the registry passes to it when it's created

type ReadObjectFunc added in v0.3.0

type ReadObjectFunc func(context.Context, func(io.Reader) error) error

ReadObjectFunc reads an object's content: it opens the object, calls the function with a reader of the content, and closes the object when the function returns. The reader returns an error once the context is cancelled, or the filer is closed. Returns the function's error, or the error opening or closing the object.

type WriterFS added in v0.3.0

type WriterFS interface {
	fs.StatFS

	// Method to create a file in the filesystem from the body, which also
	// creates any necessary parent directories. The file is replaced
	// atomically: readers see either the previous file or the complete new
	// one, and if writing fails the previous file is left unchanged. With
	// CreateOnly, an existing file is not replaced, and an error wrapping
	// fs.ErrExist is returned. With ReplaceOnly, a file which doesn't exist
	// is not created, and an error wrapping fs.ErrNotExist is returned. When
	// modtime is not zero, it is set as the modification time of the file.
	// Returns the information for the new file.
	Create(path string, body io.Reader, mode CreateMode, modtime time.Time) (fs.FileInfo, error)

	// Method to delete a file, or a directory and everything beneath it,
	// which also removes any empty parent directories. The root directory
	// cannot be deleted.
	Delete(path string) error

	// Release resources held by the filesystem
	io.Closer
}

Implement a filesystem which also allows for creating and deleting files

Directories

Path Synopsis
cmd
filer command
cmd
fs
Package fs provides the filesystem interface for managing objects in directories.
Package fs provides the filesystem interface for managing objects in directories.
fs/fsevents
Package fsevents watches a directory, and everything beneath it, for files and directories which are created, updated and deleted, using the efficient mechanism of each platform: FSEvents on macOS, ReadDirectoryChangesW on Windows, and inotify on Linux.
Package fsevents watches a directory, and everything beneath it, for files and directories which are created, updated and deleted, using the efficient mechanism of each platform: FSEvents on macOS, ReadDirectoryChangesW on Windows, and inotify on Linux.
s3
Package s3 is a filer of the objects in an S3 bucket, or one which is compatible, beneath a prefix
Package s3 is a filer of the objects in an S3 bucket, or one which is compatible, beneath a prefix
s3/seaweedfs
Package seaweedfs watches the changes to the objects of an S3 bucket of a SeaweedFS filer, by subscribing to the changes to the filer's metadata, with its gRPC service
Package seaweedfs watches the changes to the objects of an S3 bucket of a SeaweedFS filer, by subscribing to the changes to the filer's metadata, with its gRPC service
schema
Package schema is the types of the objects in a backend, which are the types of the filer's objects
Package schema is the types of the objects in a backend, which are the types of the filer's objects

Jump to

Keyboard shortcuts

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