stat

package
v5.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

func (Error) Error

func (e Error) Error() string

type FileStats

type FileStats struct {
	QuotedPath string
	Size       int64
	UID        uint32
	GID        uint32
	Atim       int64
	Mtim       int64
	Ctim       int64
	Type       FileType
	Ino        uint64
	Nlink      uint64
	Dev        uint64
}

FileStats contains all the file stats needed by wrstat, interpreted in our custom way.

func File

func File(absPath string, info os.FileInfo) *FileStats

File interprets the given file info to produce a FileStats.

You provide the absolute path to the file so that QuotedPath can be calculated correctly (the info only contains the basename).

func (*FileStats) ToString

func (fs *FileStats) ToString() string

ToString produces our special format for describing the stats of a file. It is \n terminated and ready to be written to a file.

type FileType

type FileType string
const (
	FileTypeRegular FileType = "f"
	FileTypeLink    FileType = "l"
	FileTypeDir     FileType = "d"
	FileTypeSocket  FileType = "s"
	FileTypeBlock   FileType = "b"
	FileTypeChar    FileType = "c"
	FileTypeFIFO    FileType = "F"
	FileTypeUnknown FileType = "X"
)

type LstatFunc added in v5.2.2

type LstatFunc func(string) (fs.FileInfo, error)

LstatFunc matches the signature of os.Lstat.

type Operation

type Operation func(absPath string, info fs.FileInfo) error

Operation is a callback that once added to a Paths will be called on each path encountered. It receives the absolute path to the filesystem entry, and the FileInfo returned by Statter.Lstat() on that path.

func FileOperation

func FileOperation(output *os.File) Operation

FileOperation returns an Operation that can be used with Paths that calls File() on each path the Operation receives and outputs the ToString() value to the given output file.

type Paths

type Paths struct {
	ScanTimeout time.Duration
	// contains filtered or unexported fields
}

Paths lets you get stats and carry out operations on those stats for many file paths.

func NewPaths

func NewPaths(statter Statter, pathsConfig PathsConfig) *Paths

NewPaths returns a Paths that will use the given Statter to do the Lstat calls and log issues to any configured logger. If you configure a reportFrequency greater than 0, then timings for the lstats and your operations will also be logged. You can also configure a MaxTime that a Scan() can run for before it fails.

func (*Paths) AddOperation

func (p *Paths) AddOperation(name string, op Operation) error

AddOperation adds the given Operation callback so that when you Scan(), your callback will be called for each path scanned. You give the operation a name so that timings can be reported for each operation.

You can't use the name "lstat", since that is used for reporting the Lstat timings.

func (*Paths) Scan

func (p *Paths) Scan(paths io.Reader) error

Scan scans through the given reader which should consist of quoted absolute file path per line. It calls our Statter.Lstat() on each, and passes the absolute path and FileInfo to any Operation callbacks you've added. Errors from the Statter are normally ignored, with the exeption of StatterWithTimeout's failure due to too many consecutive timeouts in a row.

Operations are run concurrently (so should not do something like write to the same file) and their errors logged, but otherwise ignored.

We wait for all operations to complete before they are all called again, so it is safe to do something like write stat details to a file.

If a MaxTime has been configured, Scan() will stop and return an error as soon as that amount of time has passed.

type PathsConfig added in v5.2.2

type PathsConfig struct {
	Logger          log15.Logger
	ReportFrequency time.Duration
	ScanTimeout     time.Duration
}

type Statter

type Statter interface {
	// Lstat calls os.Lstat() on the given path, returning the FileInfo.
	Lstat(path string) (info fs.FileInfo, err error)
}

Statter is something you use to get stats of files on disk.

type StatterWithTimeout

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

StatterWithTimeout is a Statter implementation. NB: this is NOT thread safe; you should only call Lstat() one at a time.

func WithTimeout

func WithTimeout(timeout time.Duration, maxAttempts, maxFailureCount int, logger log15.Logger) *StatterWithTimeout

WithTimeout returns a Statter with the given timeout, maxAttempts and maxFailureCount configured. Timeouts are logged with the given logger.

Timeouts on single files do not result in an error, but timeouts of maxFailureCount consecutive files does.

func (*StatterWithTimeout) Lstat

func (s *StatterWithTimeout) Lstat(path string) (info fs.FileInfo, err error)

Lstat calls os.Lstat() on the given path, but times it out after our configured timeout, retrying until we've hit our maxAttempts. NB: this is NOT thread safe, don't call this concurrently.

func (*StatterWithTimeout) SetLstat added in v5.2.2

func (s *StatterWithTimeout) SetLstat(lstat LstatFunc)

SetLstat can be used when testing when you need to mock actual Lstat calls. The lstat defaults to os.Lstat.

Jump to

Keyboard shortcuts

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