errorfs

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInjected = errors.New("injected error")

ErrInjected is an error artificially injected for testing fs error paths.

Functions

func WrapFile

func WrapFile(f vfs.File, inj Injector) vfs.File

WrapFile wraps an existing vfs.File, returning a new vfs.File that shadows operations to the provided vfs.File. It uses the provided Injector for deciding when to inject errors. If an error is injected, the file propagates the error instead of shadowing the operation.

Types

type FS

type FS struct {
	// contains filtered or unexported fields
}

FS implements vfs.FS, injecting errors into its operations.

func Wrap

func Wrap(fs vfs.FS, inj Injector) *FS

Wrap wraps an existing vfs.FS implementation, returning a new vfs.FS implementation that shadows operations to the provided FS. It uses the provided Injector for deciding when to inject errors. If an error is injected, FS propagates the error instead of shadowing the operation.

func (*FS) Create

func (fs *FS) Create(name string) (vfs.File, error)

Create implements FS.Create.

func (*FS) GetDiskUsage

func (fs *FS) GetDiskUsage(path string) (vfs.DiskUsage, error)

GetDiskUsage implements FS.GetDiskUsage.

func (fs *FS) Link(oldname, newname string) error

Link implements FS.Link.

func (*FS) List

func (fs *FS) List(dir string) ([]string, error)

List implements FS.List.

func (*FS) Lock

func (fs *FS) Lock(name string) (io.Closer, error)

Lock implements FS.Lock.

func (*FS) MkdirAll

func (fs *FS) MkdirAll(dir string, perm os.FileMode) error

MkdirAll implements FS.MkdirAll.

func (*FS) Open

func (fs *FS) Open(name string, opts ...vfs.OpenOption) (vfs.File, error)

Open implements FS.Open.

func (*FS) OpenDir

func (fs *FS) OpenDir(name string) (vfs.File, error)

OpenDir implements FS.OpenDir.

func (*FS) OpenReadWrite

func (fs *FS) OpenReadWrite(name string, opts ...vfs.OpenOption) (vfs.File, error)

OpenReadWrite implements FS.OpenReadWrite.

func (*FS) PathBase

func (fs *FS) PathBase(p string) string

PathBase implements FS.PathBase.

func (*FS) PathDir

func (fs *FS) PathDir(p string) string

PathDir implements FS.PathDir.

func (*FS) PathJoin

func (fs *FS) PathJoin(elem ...string) string

PathJoin implements FS.PathJoin.

func (*FS) Remove

func (fs *FS) Remove(name string) error

Remove implements FS.Remove.

func (*FS) RemoveAll

func (fs *FS) RemoveAll(fullname string) error

RemoveAll implements FS.RemoveAll.

func (*FS) Rename

func (fs *FS) Rename(oldname, newname string) error

Rename implements FS.Rename.

func (*FS) ReuseForWrite

func (fs *FS) ReuseForWrite(oldname, newname string) (vfs.File, error)

ReuseForWrite implements FS.ReuseForWrite.

func (*FS) Stat

func (fs *FS) Stat(name string) (os.FileInfo, error)

Stat implements FS.Stat.

func (*FS) Unwrap

func (fs *FS) Unwrap() vfs.FS

Unwrap returns the FS implementation underlying fs. See pebble/vfs.Root.

type InjectIndex

type InjectIndex struct {
	// contains filtered or unexported fields
}

InjectIndex implements Injector, injecting an error at a specific index.

func OnIndex

func OnIndex(index int32) *InjectIndex

OnIndex constructs an injector that returns an error on the (n+1)-th invocation of its MaybeError function. It may be passed to Wrap to inject an error into an FS.

func (*InjectIndex) Index

func (ii *InjectIndex) Index() int32

Index returns the index at which the error will be injected.

func (*InjectIndex) MaybeError

func (ii *InjectIndex) MaybeError(_ Op, _ string) error

MaybeError implements the Injector interface.

func (*InjectIndex) SetIndex

func (ii *InjectIndex) SetIndex(v int32)

SetIndex sets the index at which the error will be injected.

type Injector

type Injector interface {
	// MaybeError is invoked by an errorfs before an operation is executed. It
	// is passed an enum indicating the type of operation and a path of the
	// subject file or directory. If the operation takes two paths (eg,
	// Rename, Link), the original source path is provided.
	MaybeError(op Op, path string) error
}

Injector injects errors into FS operations.

func WithProbability

func WithProbability(op OpKind, p float64) Injector

WithProbability returns a function that returns an error with the provided probability when passed op. It may be passed to Wrap to inject an error into an ErrFS with the provided probability. p should be within the range [0.0,1.0].

type InjectorFunc

type InjectorFunc func(Op, string) error

InjectorFunc implements the Injector interface for a function with MaybeError's signature.

func (InjectorFunc) MaybeError

func (f InjectorFunc) MaybeError(op Op, path string) error

MaybeError implements the Injector interface.

type Op

type Op int

Op is an enum describing the type of operation.

const (
	// OpCreate describes a create file operation.
	OpCreate Op = iota
	// OpLink describes a hardlink operation.
	OpLink
	// OpOpen describes a file open operation.
	OpOpen
	// OpOpenDir describes a directory open operation.
	OpOpenDir
	// OpRemove describes a remove file operation.
	OpRemove
	// OpRemoveAll describes a recursive remove operation.
	OpRemoveAll
	// OpRename describes a rename operation.
	OpRename
	// OpReuseForRewrite describes a reuse for rewriting operation.
	OpReuseForRewrite
	// OpMkdirAll describes a make directory including parents operation.
	OpMkdirAll
	// OpLock describes a lock file operation.
	OpLock
	// OpList describes a list directory operation.
	OpList
	// OpFilePreallocate describes a file preallocate operation.
	OpFilePreallocate
	// OpStat describes a path-based stat operation.
	OpStat
	// OpGetDiskUsage describes a disk usage operation.
	OpGetDiskUsage
	// OpFileClose describes a close file operation.
	OpFileClose
	// OpFileRead describes a file read operation.
	OpFileRead
	// OpFileReadAt describes a file seek read operation.
	OpFileReadAt
	// OpFileWrite describes a file write operation.
	OpFileWrite
	// OpFileWriteAt describes a file seek write operation.
	OpFileWriteAt
	// OpFileStat describes a file stat operation.
	OpFileStat
	// OpFileSync describes a file sync operation.
	OpFileSync
	// OpFileFlush describes a file flush operation.
	OpFileFlush
)

func (Op) OpKind

func (o Op) OpKind() OpKind

OpKind returns the operation's kind.

type OpKind

type OpKind int

OpKind is an enum describing whether an operation is a read or write operation.

const (
	// OpKindRead describes read operations.
	OpKindRead OpKind = iota
	// OpKindWrite describes write operations.
	OpKindWrite
)

Jump to

Keyboard shortcuts

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