fs

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpUnsupported = errors.New("operation not supported by the file system")
	ErrNotFile       = errors.New("not a file")
	ErrFileType      = errors.New("invalid file type for an OCFL context")
)

Functions

func CheckFileTypes added in v0.9.0

func CheckFileTypes(ctx context.Context, files iter.Seq[*FileRef]) iter.Seq2[*FileRef, error]

CheckFileTypes checks if items in files have valid mode types for an OCFL object (see ValidFileType). If will call Stat() on any files with nil FileInfo. The resulting iterator yields all files in the input and an error. The error is an fs.PathError that wraps ErrFileType if the file has an invalid type mode.

func Copy

func Copy(ctx context.Context, dstFS FS, dst string, srcFS FS, src string) (size int64, err error)

Copy copies src in srcFS to dst in dstFS. If srcFS and dstFS are the same refererence and it implements CopyFS, then Copy uses the fs's Copy() method.

func DirEntries

func DirEntries(ctx context.Context, fsys FS, name string) iter.Seq2[fs.DirEntry, error]

DirEntries calls DirEntries if fsys implements DirEntriesFS. If fsys doesn't implement DirEntriesFS, it returns an iterator that yields an fs.PathError that wraps ErrFeatureUnsupported.

func Files

func Files(fsys FS, names ...string) iter.Seq[*FileRef]

func FilterFiles

func FilterFiles(files iter.Seq[*FileRef], filter func(*FileRef) bool) iter.Seq[*FileRef]

Filter returns a new FileSeq that yields values in files that satisfy the filter condition.

func IsNotHidden

func IsNotHidden(info *FileRef) bool

IsNotHidden is used with Filter to remove hidden files.

func ReadAll

func ReadAll(ctx context.Context, fsys FS, name string) ([]byte, error)

ReadAll returns the contents of a file.

func ReadDir

func ReadDir(ctx context.Context, fsys FS, name string) ([]fs.DirEntry, error)

ReadDir calls DirEntries and collects all yielded directory entries in a slice. If an error is encountered, the slice will included all entries read up the point of the error.

func Remove

func Remove(ctx context.Context, fsys FS, name string) error

Remove checks if fsys implements WriteFS and calls its Remove method. It returns ErrOpUnsupported if fsys is not a WriteFS

func RemoveAll

func RemoveAll(ctx context.Context, fsys FS, name string) error

RemoveAll checks if fsys implements WriteFS and calls its RemoveAll method. It returns ErrOpUnsupported if fsys is not a WriteFS. As a special case, if name == ".", RemoveAll reads the contents of the top-level directory and calls Remove/RemoveAll for all entries.

func StatFile

func StatFile(ctx context.Context, fsys FS, name string) (fs.FileInfo, error)

StatFile returns file information for the file name in fsys.

func StatFiles

func StatFiles(ctx context.Context, files iter.Seq[*FileRef]) iter.Seq2[*FileRef, error]

StatFiles returns an iterator that yields *FileRefs and an error from calling [Stat]() for values in files. Values from files are not modified.

func UntilErr

func UntilErr[T any](seq iter.Seq2[T, error]) (iter.Seq[T], func() error)

UntilErr returns an iterator that yields the values from seq until seq yields a non-nil error. The value yielded with the error is thrown out.

func ValidFileType

func ValidFileType(mode fs.FileMode) bool

ValidFileType returns true if mode is ok for a file in an OCFL object.

func WalkFiles

func WalkFiles(ctx context.Context, fsys FS, dir string) iter.Seq2[*FileRef, error]

WalkFiles checks if fsys is a FileWalker and calls its WalkFiles if it is. If fsys isn't a FileWalker, dir is walked using DirEntries.

func Write

func Write(ctx context.Context, fsys FS, name string, r io.Reader) (int64, error)

Write checks if fsys implements WriteFS and calls its Write method. It returns ErrOpUnsupported if fsys is not a WriteFS

Types

type CopyFS

type CopyFS interface {
	WriteFS
	// Copy creates or updates the file at dst with the contents of src. If dst
	// exists, it should be overwritten
	Copy(ctx context.Context, dst string, src string) (int64, error)
}

CopyFS is a storage backend that supports copying files.

type DirEntriesFS

type DirEntriesFS interface {
	FS
	// DirEntries returns an iterator that will yield an fs.DirEntry from the named
	// directory or an error (not both). The entries should be yielded in sorted
	// order. If an error is yielded, iteration terminates.
	DirEntries(ctx context.Context, name string) iter.Seq2[fs.DirEntry, error]
}

DirEntriesFS is an FS that also includes the ability to read entries in a directory.

type FS

type FS interface {
	// OpenFile opens the named file for reading. It is like [io/fs.FS.Open],
	// except it returns an error if name is a directory.
	OpenFile(ctx context.Context, name string) (fs.File, error)
}

FS is the minimal file system abstraction that includes the ability to read named files (not directories).

type FileRef

type FileRef struct {
	FS      FS          // The FS where the file is stored.
	BaseDir string      // parent directory relative to an FS.
	Path    string      // file path relative to BaseDir
	Info    fs.FileInfo // file info from StatFile (may be nil)
}

FileRef includes everything needed to access a file, including a reference to the FS where the file is stored. It may include file metadata from calling StatFile().

func (FileRef) FullPath

func (f FileRef) FullPath() string

FullPath returns the file's path relative to an FS

func (FileRef) FullPathDir

func (f FileRef) FullPathDir() string

FullPathDir returns the full path of the directory where the file is stored.

func (*FileRef) Open

func (f *FileRef) Open(ctx context.Context) (fs.File, error)

Open return an fs.File for reading the contents of the file at f.

func (*FileRef) Stat

func (f *FileRef) Stat(ctx context.Context) error

Stat() calls StatFile on the file at f and updates f.Info.

type FileWalker

type FileWalker interface {
	FS
	// WalkFiles returns an iterator that yields *FileRefs and/or an
	// error.
	WalkFiles(ctx context.Context, dir string) iter.Seq2[*FileRef, error]
}

FileWalker is an FS with an optimized implementation of WalkFiles

type WrapFS

type WrapFS struct {
	fs.FS
}

WrapFS wraps an io/fs.FS and implements DirEntriesFS.

func DirFS

func DirFS(dir string) *WrapFS

DirFS is shorthand for NewFS(os.DirFS(dir))

func NewWrapFS

func NewWrapFS(fsys fs.FS) *WrapFS

NewWrapFS returns a *WrapFS for accessing files in fsys.

func (*WrapFS) DirEntries

func (fsys *WrapFS) DirEntries(ctx context.Context, name string) iter.Seq2[fs.DirEntry, error]

DirEntries implements DirEntriesFS for WrapFS.

func (*WrapFS) OpenFile

func (fsys *WrapFS) OpenFile(ctx context.Context, name string) (fs.File, error)

OpenFile implementes FS for WrapFS

type WriteFS

type WriteFS interface {
	FS
	Write(ctx context.Context, name string, buffer io.Reader) (int64, error)
	// Remove the file with path name
	Remove(ctx context.Context, name string) error
	// Remove the directory with path name and all its contents. If the path
	// does not exist, return nil.
	RemoveAll(ctx context.Context, name string) error
}

WriteFS is a storage backend that supports write and remove operations.

Directories

Path Synopsis
package http implements and http-based backend that supports basic object access.
package http implements and http-based backend that supports basic object access.
s3
example/allops command
example/etag command
example/readir command

Jump to

Keyboard shortcuts

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