fsutil

package module
v0.0.0-...-dbbdc9a Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 19 Imported by: 2

Documentation

Index

Constants

View Source
const (
	EqualReasonModeMismatch             = "mode mismatch"
	EqualReasonFileContentMismatch      = "file content mismatch"
	EqualReasonDirectoryContentMismatch = "directory content mismatch"
)
View Source
const (
	ObservableFsOpNameCreate    = "Create"
	ObservableFsOpNameMkdir     = "Mkdir"
	ObservableFsOpNameMkdirAll  = "MkdirAll"
	ObservableFsOpNameOpen      = "Open"
	ObservableFsOpNameOpenFile  = "OpenFile"
	ObservableFsOpNameRemove    = "Remove"
	ObservableFsOpNameRemoveAll = "RemoveAll"
	ObservableFsOpNameRename    = "Rename"
	ObservableFsOpNameStat      = "Stat"
	ObservableFsOpNameChmod     = "Chmod"
	ObservableFsOpNameChown     = "Chown"
	ObservableFsOpNameChtimes   = "Chtimes"
)
View Source
const (
	ObservableFsFileOpNameClose        = "Close"
	ObservableFsFileOpNameRead         = "Read"
	ObservableFsFileOpNameReadAt       = "ReadAt"
	ObservableFsFileOpNameSeek         = "Seek"
	ObservableFsFileOpNameWrite        = "Write"
	ObservableFsFileOpNameWriteAt      = "WriteAt"
	ObservableFsFileOpNameName         = "Name"
	ObservableFsFileOpNameReaddir      = "Readdir"
	ObservableFsFileOpNameReaddirnames = "Readdirnames"
	ObservableFsFileOpNameStat         = "Stat"
	ObservableFsFileOpNameSync         = "Sync"
	ObservableFsFileOpNameTruncate     = "Truncate"
	ObservableFsFileOpNameWriteString  = "WriteString"
)

Variables

View Source
var (
	ErrBadInput        = errors.New("bad input")
	ErrBadName         = errors.New("bad name")
	ErrBadPattern      = errors.New("bad pattern")
	ErrMaxRetry        = errors.New("max retry")
	ErrHashSumMismatch = errors.New("hash sum mismatch")
)

Functions

func CleanDir

func CleanDir(fsys afero.Fs, path string) error

func CopyFS

func CopyFS(dst afero.Fs, src fs.FS, opts ...CopyFsOption) error

CopyFS copies from fs.FS to afero.FS.

The default behavior of CopyFS is:

  • returns an error if src contains non regular files
  • copies permission bits
  • makes directories with fs.ModePerm (0o777) before umask.

func CopyFsPath

func CopyFsPath(dst afero.Fs, src fs.FS, paths []string, opts ...CopyFsOption) error

CopyFsPath copies contents of src specified by paths into dst.

func IsPackageErr

func IsPackageErr(err error) bool

func MkdirRandom

func MkdirRandom(fsys afero.Fs, dir string, pattern string, perm fs.FileMode) (afero.File, error)

func OpenFileRandom

func OpenFileRandom(fsys afero.Fs, dir string, pattern string, perm fs.FileMode) (afero.File, error)

Types

type CopyFsOption

type CopyFsOption func(o *copyFsOption)

func CopyFsWithContext

func CopyFsWithContext(ctx context.Context) CopyFsOption

func CopyFsWithIgnoreNonRegularFile

func CopyFsWithIgnoreNonRegularFile() CopyFsOption

func CopyFsWithNoChmod

func CopyFsWithNoChmod(noChmod bool) CopyFsOption

func CopyFsWithOverridePermission

func CopyFsWithOverridePermission(chmodIf func(path string) (perm fs.FileMode, ok bool)) CopyFsOption

type EqualReason

type EqualReason string

type EqualReport

type EqualReport struct {
	Reason EqualReason
	// Path is a path for a mismatching file or directory.
	Path string
	// Values for Reason of Path.
	// fs.FileMode for EqualReasonModeMismatch,
	// nil for EqualReasonFileContentMismatch
	// and []string describing names of dirents for EqualReasonDirectoryContentMismatch.
	DstVal, SrcVal any
}

type EqualResult

type EqualResult []EqualReport

func Equal

func Equal(dst, src fs.FS, opts ...CopyFsOption) (EqualResult, error)

Equal compares dst and src and reports result.

The comparison evaluates

  • mode bits of dirents
  • content of directory
  • content of regular files

Equal takes also CopyFsOption. Options work as if dst was dst of CopyFs. That is, for example, if CopyFsWithOverridePermission is set, Equal compares dst's file mode against returned value of chmodIf instead of src's.

Note that mode bits of the root directory is ignored since often it is not controlled.

Performance:

  • Equal takes stat of every file in l and r.
  • Also all dirents of directories are read.
  • Files are entirely read

func (EqualResult) Equal

func (r EqualResult) Equal() bool

func (EqualResult) Select

func (r EqualResult) Select(filterFn func(r EqualReport) bool) EqualResult

Select filters r and returns a shallowly copied instance. The returned result only contains reports for which filterFn returns true.

type ObservableFs

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

func NewObservableFs

func NewObservableFs(base afero.Fs) *ObservableFs

func (*ObservableFs) Chmod

func (fsys *ObservableFs) Chmod(name string, mode os.FileMode) error

func (*ObservableFs) Chown

func (fsys *ObservableFs) Chown(name string, uid, gid int) error

func (*ObservableFs) Chtimes

func (fsys *ObservableFs) Chtimes(name string, atime time.Time, mtime time.Time) error

func (*ObservableFs) Create

func (fsys *ObservableFs) Create(name string) (afero.File, error)

func (*ObservableFs) Mkdir

func (fsys *ObservableFs) Mkdir(name string, perm os.FileMode) error

func (*ObservableFs) MkdirAll

func (fsys *ObservableFs) MkdirAll(path string, perm os.FileMode) error

func (*ObservableFs) Name

func (fsys *ObservableFs) Name() string

func (*ObservableFs) Observer

func (fsys *ObservableFs) Observer() *Observer

func (*ObservableFs) Open

func (fsys *ObservableFs) Open(name string) (afero.File, error)

func (*ObservableFs) OpenFile

func (fsys *ObservableFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

func (*ObservableFs) Remove

func (fsys *ObservableFs) Remove(name string) error

func (*ObservableFs) RemoveAll

func (fsys *ObservableFs) RemoveAll(path string) error

func (*ObservableFs) Rename

func (fsys *ObservableFs) Rename(oldname, newname string) error

func (*ObservableFs) Stat

func (fsys *ObservableFs) Stat(name string) (os.FileInfo, error)

type ObservableFsFileOp

type ObservableFsFileOp struct {
	Name string
	Op   ObservableFsFileOpName
	Args []any
	Err  error
}

type ObservableFsFileOpName

type ObservableFsFileOpName string

type ObservableFsOp

type ObservableFsOp struct {
	Name string
	Op   ObservableFsOpName
	Args []any
	Err  error
}

type ObservableFsOpName

type ObservableFsOpName string

type Observer

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

func (*Observer) FileOp

func (o *Observer) FileOp(name string) []ObservableFsFileOp

func (*Observer) FileOps

func (o *Observer) FileOps() map[string][]ObservableFsFileOp

func (*Observer) FsOp

func (o *Observer) FsOp() []ObservableFsOp

type SafeWriteOption

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

SafeWriteOption holds options for safe-write.

func NewSafeWriteOption

func NewSafeWriteOption(opts ...SafeWriteOptionOption) *SafeWriteOption

NewSafeWriteOption returns a newly allocated SafeWriteOption. Without any options, it uses "-*" as random file suffix pattern.

func (SafeWriteOption) Apply

Apply clones o and then applies options to the cloned instance.

func (SafeWriteOption) CleanTmp

func (o SafeWriteOption) CleanTmp(fsys afero.Fs) error

CleanTmp erases all temporal file under fsys. If o is configured to have a dedicated tmp directory, then CleanTmp removes all dirents under the directory.

If temp file suffix and prefix is specified, CleanTmp removes matched files.

func (SafeWriteOption) SafeWrite

func (o SafeWriteOption) SafeWrite(
	fsys afero.Fs,
	path string,
	perm fs.FileMode,
	r io.Reader,
	postProcesses ...SafeWritePostProcess,
) (err error)

SafeWrite writes the content of r to path under fsys safely.

SafeWrite first creates a temporal directory and a temporal file there. Then it writes the content of r to the file. After the content is fully written, it calls rename to move the file to path.

Be cautious when path already exists, SafeWrite overwrites the file.

SafeWrite switches its behavior based on configuration of o.

func (SafeWriteOption) SafeWriteFs

func (o SafeWriteOption) SafeWriteFs(
	fsys afero.Fs,
	dir string,
	perm fs.FileMode,
	src fs.FS,
	postProcesses ...SafeWritePostProcess,
) error

SafeWriteFs copies content of src into dir under fsys.

SafeWriteFs first creates a temporal directory. Then it writes the content of src to there. After src is fully copied, it calls rename to move the file to path, which also indicates that if dir already exists and non empty, SafeWriteFs fails to rename the directory.

SafeWriteFs switches its behavior based on configuration of o.

type SafeWriteOptionOption

type SafeWriteOptionOption func(o *SafeWriteOption)

func WithCopyFsOptions

func WithCopyFsOptions(copyFsOptions []CopyFsOption) SafeWriteOptionOption

func WithDefaultPostProcesses

func WithDefaultPostProcesses(defaultPostProcesses []SafeWritePostProcess) SafeWriteOptionOption

func WithDefaultPreProcesses

func WithDefaultPreProcesses(defaultPreProcess []SafeWritePreProcess) SafeWriteOptionOption

func WithDisableMkdir

func WithDisableMkdir(disableMkdir bool) SafeWriteOptionOption

func WithDisableRemoveOnErr

func WithDisableRemoveOnErr(disableRemoveOnErr bool) SafeWriteOptionOption

func WithDisableSync

func WithDisableSync(disableSync bool) SafeWriteOptionOption

func WithForcePerm

func WithForcePerm(forcePerm bool) SafeWriteOptionOption

func WithIgnoreMatchedErr

func WithIgnoreMatchedErr(ignoreMatchedErr func(err error) bool) SafeWriteOptionOption

func WithOwner

func WithOwner(uid, gid int) SafeWriteOptionOption

func WithPrefixSuffix

func WithPrefixSuffix(prefix, suffix string) (SafeWriteOptionOption, error)

func WithRandomPattern

func WithRandomPattern(randomPattern string) (SafeWriteOptionOption, error)

func WithTmpDir

func WithTmpDir(tmpDirName string) SafeWriteOptionOption

type SafeWritePostProcess

type SafeWritePostProcess func(fsys afero.Fs, tmpName, dstName string, file afero.File) error

func PostProcessClose

func PostProcessClose(r io.Closer) SafeWritePostProcess

func PostProcessValidateCheckSum

func PostProcessValidateCheckSum(h hash.Hash, expected []byte) SafeWritePostProcess

func TeeHasher

func TeeHasher(r io.Reader, h hash.Hash, expected []byte) (piped io.Reader, validator SafeWritePostProcess)

TeeHasher creates a reader reading from r and tee-ing data to h. validator can be passed to SafeWrite to so that it can prevent corrupted files from appearing final destination. validator returns ErrHashSumMismatch on mismatching hashes.

type SafeWritePreProcess

type SafeWritePreProcess func(fsys afero.Fs, tmpName, dstName string, file afero.File) error

func PreProcessAssertSizeZero

func PreProcessAssertSizeZero() SafeWritePreProcess

func PreProcessSeek

func PreProcessSeek(offset int64, whence int) SafeWritePreProcess

PreProcessSeek seeks given files to offset from whence.

func PreProcessSeekEnd

func PreProcessSeekEnd() SafeWritePreProcess

PreProcessSeekEnd seeks given files to the end of files.

Jump to

Keyboard shortcuts

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