appendfs

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

appendfs

The appendfs package provides an extended file system interface that goes beyond the standard functionality of io/fs. The primary goal of this interface is to ensure that the provided file system is capable of creating new files and directories.

Interface: FS

The FS interface combines several interfaces from the standard io/fs package and the external github.com/je4/filesystem/v3/pkg/writefs package. It serves as a central abstraction for file systems that can be both read from and written to.

type FS interface {
    fs.FS
    writefs.MkDirFS
    writefs.CreateFS
}

It includes the following functionalities:

  • fs.FS: Standard read access (Open(name string) (File, error)).
  • writefs.MkDirFS: Creation of directories.
  • writefs.CreateFS: Creation of files for write access.

Functions

Sub(fsys FS, path string) (FS, error)

The Sub function creates a sub-file system (Sub-FS) based on a given path. It ensures that the resulting file system also satisfies the FS interface.

This is particularly useful for restricting operations to a specific subdirectory of an OCFL storage or an object, while still maintaining write permissions.

Example:

subFS, err := appendfs.Sub(rootFS, "v1/content")
if err != nil {
    // Error handling
}

// Now you can work in subFS as if "v1/content" were the root.
f, err := subFS.Create("data.txt")
EnsureFS(fsys fs.FS) (FS, error)

The helper function EnsureFS checks if a given fs.FS implements the appendfs.FS interface (i.e., supports write access). If it does, it returns the casted interface; otherwise, an error is reported.

Project Usage

appendfs.FS is used in many places within gocfl to abstract the storage layer. It allows other packages (such as ocfl, extension, etc.) to create files and directories without having to worry about the underlying implementation (local file system, S3, etc.).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAppendFS = errors.New("provided filesystem does not implement appendfs.FS interface")

Functions

This section is empty.

Types

type FS

type FS interface {
	fs.FS
	writefs.MkDirFS
	writefs.CreateFS
}

func EnsureFS

func EnsureFS(fsys fs.FS) (FS, error)

EnsureFS checks if the given fsys implements the appendfs.FS interface. The interface requires support for Create and Mkdir in addition to the standard fs.FS methods.

func New

func New(fSys fs.FS) (FS, error)

New returns a new appendfs.FS. It wraps an existing fs.FS to ensure it implements the appendfs.FS interface. Primarily used for testing to enforce restricted functionality. If fSys does not implement appendfs.FS, appendfs.ErrNotAppendFS is returned.

func Sub

func Sub(fsys FS, path string) (FS, io.Closer, error)

Sub returns a sub-filesystem starting at the specified path. It returns the new filesystem, an io.Closer, and an error. The io.Closer is important for releasing resources if the underlying filesystem requires it. If the original fsys implements io.Closer, it is returned. Otherwise, a nopCloser is returned so the caller can always safely call Close(). An example of a filesystem that requires a closer is zipasfolder.

Jump to

Keyboard shortcuts

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