fio

package module
v0.7.7 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: GPL-2.0 Imports: 23 Imported by: 4 modules, 5 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-fio - optimized file I/O routines

Description

This is a collection of cross platform file I/O and File system functions. These use concurrency to speed up the underlying functions.

  • fio.Info: a serializable version of Stat/Lstat that uses OS specific functions
  • Utilities to copy a file or dir along with all its metadata (including XATTR) The file copy functions will use the best underlying primitive like reflink(2); and fallback to using mmap based copy.
  • Safefile: a wrapper over os.OpenFile that atomically commits the writes. This uses a temporary file for all the I/O and calling Close() atomically renames the temporary file to the intended file.
Subpackages
  • cmp: compares two directory trees and returns their differences
  • clone: clones a source directory tree to a destination - skipping over identical files.
  • walk: A concurrent directory tree traversal library. Emits walk.Entry values which embed a stat-filled fio.Info plus a Depth field (relative to the root passed to Walk). Roots are at depth 0, their direct children at 1, and so on; depth is available inside the Options.Filter callback, enabling max-depth style pruning.

Documentation

Index

Constants

View Source
const (
	OptOverwrite uint32 = 1 << iota
)

Variables

View Source
var (
	ErrAborted = errors.New("safefile: aborted; file not committed")
)
View Source
var ErrTooSmall = errors.New("buffer is not big enough")

ErrTooSmall is returned when a caller-supplied buffer cannot hold the encoded form of a value. Detect with errors.Is.

View Source
var ErrXattrCapabilityMissing = errors.New("xattr: required capability not held")

ErrXattrCapabilityMissing is returned (wrapped) when a caller attempts to write an xattr whose namespace requires a Linux capability the process does not hold:

  • trusted.* requires CAP_SYS_ADMIN
  • security.* requires CAP_MAC_ADMIN (or CAP_SYS_ADMIN depending on LSM)

Surfacing this as a typed error lets callers emit a clear diagnostic ("needs CAP_SYS_ADMIN to restore trusted.xattr") instead of the raw EPERM that getxattr/setxattr would otherwise return. clone.WithIgnoreUnsupported also treats this error as skippable since the clone cannot succeed with the current privileges.

View Source
var ErrXattrUnsupported = errors.New("xattr: filesystem does not support extended attributes")

ErrXattrUnsupported is returned (wrapped) from xattr operations when the target filesystem does not support extended attributes (the kernel reports ENOTSUP / EOPNOTSUPP). Callers that want to treat this as a non-fatal condition use errors.Is to detect it and downgrade to a skip - see clone.WithIgnoreUnsupported.

Functions

func ClearXattr added in v0.2.0

func ClearXattr(nm string) error

ClearXattr deletes all the extended attributes of a file.

func CopyFd

func CopyFd(dst, src *os.File) error

CopyFd copies open files 'src' to 'dst' using the most efficient OS primitive available on the runtime platform. CopyFile will use copy-on-write facailities if the underlying file-system implements it. It will fallback to copying via memory mapping 'src' and writing the blocks to 'dst'.

func CopyFile

func CopyFile(dst, src string, perm fs.FileMode) error

CopyFile copies files 'src' to 'dst' using the most efficient OS primitive available on the runtime platform. CopyFile will use copy-on-write facilities if the underlying file-system implements it. It will fallback to copying via memory mapping 'src' and writing the blocks to 'dst'.

func DelXattr added in v0.2.0

func DelXattr(nm string, keys ...string) error

DelXattr deletes one or more extended attributes of a file. Errors from an xattr-less filesystem are wrapped as ErrXattrUnsupported.

func Fstatm added in v0.5.14

func Fstatm(fd *os.File, fi *Info) error

Fstatm is like Fstat except it uses caller supplied memory

func LclearXattr added in v0.2.0

func LclearXattr(nm string) error

ClearXattr deletes all the extended attributes of a file. If 'nm' points to a symlink, LSetXattr will delete the extended attributes of the symlink and *not* the target.

func LdelXattr added in v0.2.0

func LdelXattr(nm string, keys ...string) error

LDelXattr deletes one or more extended attributes of a file. If 'nm' points to a symlink, LSetXattr will delete the extended attributes of the symlink and *not* the target. Errors from an xattr-less filesystem are wrapped as ErrXattrUnsupported.

func LreplaceXattr added in v0.2.0

func LreplaceXattr(nm string, x Xattr) error

LReplaceXattr replaces all the extended attributes of 'nm' with new attributes in 'x'. This function is a combination of LclearXattr() and LsetXattr(). If 'nm' points to a symlink, LReplaceXattr will set/update the extended attributes of the symlink and *not* the target.

func LsetXattr added in v0.2.0

func LsetXattr(nm string, x Xattr) error

LSetXattr sets/updates the xattr list for a given file. If 'nm' points to a symlink, LSetXattr will set/update the extended attributes of the symlink and *not* the target. Errors caused by a filesystem that does not support extended attributes are wrapped as ErrXattrUnsupported; missing Linux capabilities surface as ErrXattrCapabilityMissing. Platform- reserved keys (darwin's com.apple.system.Security) are skipped.

func Lstatm added in v0.2.0

func Lstatm(nm string, fi *Info) error

Lstatm is like Lstat except it uses the caller supplied memory.

func ReplaceXattr added in v0.2.0

func ReplaceXattr(nm string, x Xattr) error

ReplaceXattr replaces all the extended attributes of 'nm' with new attributes in 'x'. This function is a combination of ClearXattr() and SetXattr().

func SetXattr added in v0.2.0

func SetXattr(nm string, x Xattr) error

SetXattr sets/updates the xattr list for a given file. Errors caused by a filesystem that does not support extended attributes are wrapped as ErrXattrUnsupported. On Linux, a preflight checks whether the calling process has the capability required to write any privileged-namespace keys (trusted.*, security.*); a missing capability surfaces as ErrXattrCapabilityMissing before we hit the kernel's EPERM.

Platform-reserved keys that cannot round-trip via setxattr (currently darwin's com.apple.system.Security) are silently skipped - use acl_*(3) for those.

func Statm added in v0.2.0

func Statm(nm string, fi *Info) error

Statm is like Stat above - except it uses caller supplied memory for the stat(2) info

Types

type CopyError added in v0.4.0

type CopyError struct {
	Op  string
	Src string
	Dst string
	Err error
}

CopyError represents the errors returned by CopyFile and CopyFd

func (*CopyError) Error added in v0.4.0

func (e *CopyError) Error() string

Error returns a string representation of CopyError

func (*CopyError) Unwrap added in v0.4.0

func (e *CopyError) Unwrap() error

Unwrap returns the underlying wrapped error

type Info added in v0.2.0

type Info struct {
	Ino  uint64
	Siz  int64
	Dev  uint64
	Rdev uint64

	// POSIX st_blocks: count of 512-byte allocation units. The
	// 512-byte unit is fixed by POSIX across every Unix kernel,
	// independent of the filesystem's actual block size. Use
	// DiskBytes() to recover bytes-on-disk.
	Blocks int64

	Mod   fs.FileMode
	Uid   uint32
	Gid   uint32
	Nlink uint32

	Atim time.Time
	Mtim time.Time
	Ctim time.Time

	Xattr Xattr
	// contains filtered or unexported fields
}

Info represents a file/dir metadata in a normalized form. It satisfies the fs.FileInfo interface and notably supports extended file system attributes (`xattr(7)`). It can also be marshaled and unmarshaled into a portable byte stream.

Callers construct an Info either by passing a *Info to Statm / Lstatm / Fstatm, or by struct literal (`&fio.Info{Mod: m}`). The wire encoding is handled via a local pb.Info inside the Marshal path; none of that surfaces to callers.

func Fstat added in v0.5.14

func Fstat(fd *os.File) (*Info, error)

Fstat is like os.File.Stat() but also returns xattr

func Lstat added in v0.2.0

func Lstat(nm string) (*Info, error)

Lstat is like os.Lstat() but also returns xattr

func Stat added in v0.2.0

func Stat(nm string) (*Info, error)

Stat is like os.Stat() but also returns xattr

func (*Info) Clone added in v0.5.8

func (ii *Info) Clone() *Info

Clone makes a deep copy of ii and returns the new instance

func (*Info) CopyTo added in v0.5.8

func (ii *Info) CopyTo(dest *Info)

CopyTo does a deep-copy of the contents of ii to dest.

func (*Info) DiskBytes added in v0.7.2

func (ii *Info) DiskBytes() int64

DiskBytes returns the actually-allocated bytes-on-disk for this entry. Differs from Size() for sparse files (where DiskBytes can be far smaller than Size) and for tiny files (where DiskBytes is rounded up to a filesystem block, so DiskBytes can exceed Size).

func (*Info) IsDir added in v0.2.0

func (ii *Info) IsDir() bool

IsDir returns true if this Info represents a directory entry

func (*Info) IsRegular added in v0.2.0

func (ii *Info) IsRegular() bool

IsRegular returns true if this Info represents a regular file

func (*Info) IsSameFS added in v0.3.2

func (ii *Info) IsSameFS(b *Info) bool

IsSameFs returns true if ii and b represent file entries on the same file system

func (*Info) Marshal added in v0.5.4

func (ii *Info) Marshal(flag MarshalFlag) ([]byte, error)

Marshal allocates a correctly-sized buffer, marshals ii into it, and returns it.

func (*Info) MarshalJSON added in v0.7.6

func (ii *Info) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler so an *Info can be passed directly to json.Marshal / json.Encoder.Encode.

The full relative path (ii.Path()) is always emitted; the JunkPath MarshalFlag governs only the protobuf wire format and does not apply to JSON.

func (*Info) MarshalSize added in v0.2.0

func (ii *Info) MarshalSize(flag MarshalFlag) int

MarshalSize returns the number of bytes Marshal / MarshalTo will write for this Info under the given flag.

func (*Info) MarshalTo added in v0.2.0

func (ii *Info) MarshalTo(b []byte, flag MarshalFlag) (int, error)

MarshalTo marshals ii into the caller-supplied buffer, returning the number of bytes written. The buffer must be at least MarshalSize bytes.

func (*Info) ModTime added in v0.2.0

func (ii *Info) ModTime() time.Time

ModTime returns the file modification time

func (*Info) Mode added in v0.2.0

func (ii *Info) Mode() fs.FileMode

Mode returns the file mode bits

func (*Info) Name added in v0.2.0

func (ii *Info) Name() string

Name satisfies fs.FileInfo and returns the basename of the fs entry.

func (*Info) Path added in v0.5.8

func (ii *Info) Path() string

Path returns the relative path of this file ("relative" to current working dir of the calling process).

func (*Info) SetPath added in v0.5.9

func (ii *Info) SetPath(p string)

SetPath sets the path to 'p'

func (*Info) Size added in v0.2.0

func (ii *Info) Size() int64

Size returns the fs entry's size

func (*Info) String added in v0.2.0

func (ii *Info) String() string

String is a string representation of Info

func (*Info) Sys added in v0.2.0

func (ii *Info) Sys() any

Sys returns the platform specific info - in our case it returns a pointer to the underlying Info instance.

func (*Info) Unmarshal added in v0.2.0

func (ii *Info) Unmarshal(b []byte) (int, error)

Unmarshal decodes a byte stream previously produced by Marshal / MarshalTo. Returns the number of bytes consumed.

type Map added in v0.5.13

type Map = xsync.Map[string, *Info]

Map is a concurrency safe map of path name and the corresponding Stat/Lstat info.

func NewMap added in v0.5.13

func NewMap() *Map

NewMap makes a new concurrent map of name to stat/lstat info

type MarshalFlag added in v0.5.8

type MarshalFlag uint32

MarshalFlag tunes the marshaling of an Info.

const (
	// JunkPath strips Path() to its basename before marshaling.
	// Useful when the caller wants to serialize metadata without
	// leaking the full directory hierarchy.
	JunkPath MarshalFlag = 1 << iota
)

type Pair added in v0.5.3

type Pair struct {
	Src, Dst *Info
}

Pair represents the Stat/Lstat info of a pair of related file system entries in the source and destination

type PairMap added in v0.5.13

type PairMap = xsync.Map[string, Pair]

PairMap is a concurrency safe map of path name and the corresponding Stat/Lstat info of both the source and destination.

func NewPairMap added in v0.5.13

func NewPairMap() *PairMap

NewPairMap makes a new concurrent map of name to a Pair

type SafeFile

type SafeFile struct {
	*os.File
	// contains filtered or unexported fields
}

SafeFile is an io.WriteCloser which uses a temporary file that will be atomically renamed when there are no errors and caller invokes Close(). The recommended usage is:

sf, err := NewSafeFile(...)
... error handling

defer sf.Abort()

... write to sf ..
sf.Close()

It is safe to call Abort on a closed SafeFile; the first call to Close() or Abort() seals the outcome. Similarly, it is safe to call Close() after Abort() - the first call to either takes precedence.

func NewSafeFile

func NewSafeFile(nm string, opts uint32, flag int, perm os.FileMode) (*SafeFile, error)

NewSafeFile creates a new temporary file that would either be aborted or safely renamed to the correct name. 'nm' is the name of the final file; if 'ovwrite' is true, then the file is overwritten if it exists.

func (*SafeFile) Abort

func (sf *SafeFile) Abort()

Abort the file write and remove any temporary artifacts; it is safe to call Close() on a different code path; the first call to Abort() or Close() takes precedence.

func (*SafeFile) Close

func (sf *SafeFile) Close() error

Close flushes all file data & metadata to disk, closes the file and atomically renames the temp file to the actual file - ONLY if there were no intervening errors.

func (*SafeFile) RealName added in v0.5.13

func (sf *SafeFile) RealName() string

RealName returns the actual name of the final file

func (*SafeFile) Write

func (sf *SafeFile) Write(b []byte) (int, error)

Attempt to write everything in 'b' and don't proceed if there was a previous error or the file was already closed.

func (*SafeFile) WriteAt

func (sf *SafeFile) WriteAt(b []byte, off int64) (int, error)

WriteAt writes 'b' at absolute offset 'off'

type Xattr

type Xattr map[string]string

Xattr is a collection of all the extended attributes of a given file

func GetXattr added in v0.2.0

func GetXattr(nm string) (Xattr, error)

GetXattr returns all the extended attributes of a file. This function will traverse symlinks.

func LgetXattr added in v0.2.0

func LgetXattr(nm string) (Xattr, error)

LGetXattr returns all the extended attributes of a file. If 'nm' points to a symlink, LGetXattr will return the extended attributes of the symlink and *not* the target.

func (Xattr) Equal added in v0.2.0

func (x Xattr) Equal(y Xattr) bool

Equal returns true if all xattr of 'x' is the same as all the xattr of 'y' and returns false otherwise.

func (Xattr) String

func (x Xattr) String() string

String returns the string representation of all the extended attributes

Directories

Path Synopsis
internal
pb
testutil
Package testutil provides tiny helpers for go-fio's privileged tests.
Package testutil provides tiny helpers for go-fio's privileged tests.
Package walk does a concurrent file system traversal and returns each entry.
Package walk does a concurrent file system traversal and returns each entry.

Jump to

Keyboard shortcuts

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