file

package
v0.0.0-...-1a92f83 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package file provides the capability to parse from and write to disk a roster configuration and index file. The roster file is currently implemented in YAML format to minimize file size and also permit user annotation with comments.

Index

Constants

View Source
const (
	RuntimeThreadsNoLimit = 0 // number of threads limited to number of CPUs
	RuntimeDepthNoLimit   = 0 // unlimited recursion
)

Constants representing special-purpose values for Runtime fields.

View Source
const (
	StatusNoFsize   int64  = -1
	StatusPermsMask uint64 = 0x00000000FFFFFFFF
	StatusNoPerms   string = "(none)"
	StatusNoMtime   string = "(none)"
	StatusNoCheck   string = ""
)

Constants defining Status field values with special meaning.

Variables

View Source
var IgnoreDefault = Ignore{"\\.git", "\\.svn"}

IgnoreDefault defines the default Ignore patterns used when creating a new roster file. The default items are VCS metadata directories.

View Source
var Permissions os.FileMode = 0600

Permissions defines the default permissions of roster files written to disk.

Functions

func Checksum

func Checksum(filePath string) (sum string, err error)

Checksum computes the checksum of a file at given path.

Types

type Absent

type Absent map[string]bool

Absent stores a record of all files in the roster, which are removed once the file is discovered.

type Config

type Config struct {
	Rt  Runtime `yaml:"runtime"` // various runtime settings
	Ver Verify  `yaml:"verify"`  // attributes used to identify changed files
	Ign Ignore  `yaml:"ignore"`  // file patterns to exclude from roster index
	// contains filtered or unexported fields
}

Config contains settings for constructing and verifying the roster index.

type DirectoryNotFoundError

type DirectoryNotFoundError string

func (DirectoryNotFoundError) Error

func (e DirectoryNotFoundError) Error() string

Error returns the error message for DirectoryNotFoundError.

type Ignore

type Ignore []string

Ignore stores a list of file patterns to exclude from the roster index.

func (Ignore) Compile

func (i Ignore) Compile() (*IgnoreRegexp, error)

Compile builds a list of regular expressions from a string slice of ignore patterns.

type IgnoreRegexp

type IgnoreRegexp []*regexp.Regexp

IgnoreRegexp stores a list of compiled regular expressions created from a slice of strings of type Ignore.

type InvalidPathError

type InvalidPathError string

func (InvalidPathError) Error

func (e InvalidPathError) Error() string

Error returns the error message for InvalidPathError.

type Member

type Member map[string]Status

Member stores the index of all roster members as a mapping from file path to Status struct containing file attributes.

type NotRegularFileError

type NotRegularFileError string

func (NotRegularFileError) Error

func (e NotRegularFileError) Error() string

Error returns the error message for NotRegularFileError.

type Roster

type Roster struct {
	Cfg Config `yaml:"config"`  // roster configuration
	Mem Member `yaml:"members"` // index of all files
	// contains filtered or unexported fields
}

Roster represents a roster file, containing the index of all member files in a directory tree.

func New

func New(fileExists bool, filePath string) *Roster

New constructs a new roster file at the given file path, initialized with all default data. The returned file is stored in-memory only. The Write method must be called to write the file to disk.

func Parse

func Parse(filePath string) (*Roster, error)

Parse parses the roster configuration and member data from a given roster file into the returned Roster struct, or returns a Roster struct with default configuration and empty member data if the roster file does not exist. Returns a nil Roster and descriptive error if the given path is invalid.

func (*Roster) Absentees

func (ros *Roster) Absentees() []string

Absentees returns a list of files that remain in the receiver Roster ros's list of missing files.

func (*Roster) Changed

func (ros *Roster) Changed(root string, relPath string, info os.FileInfo) (
	new bool, changed bool, stat Status, err error,
)

Changed determines if the given file path and os.FileInfo already exists in the roster index, computes the Status struct for the given file, and returns whether it is a new file, whether the Status info has changed, and what the new Status is, along with any error encountered.

func (*Roster) Expel

func (ros *Roster) Expel(filePath string)

Expel removes the given file path from the receiver Roster ros.

func (*Roster) Keep

func (ros *Roster) Keep(filePath string, info os.FileInfo) bool

Keep returns whether or not a file with the given path should be considered candidate for indexing. Directories, files matching an ignore pattern, and the roster index file itself all return false.

func (*Roster) Status

func (ros *Roster) Status(filePath string) (Status, bool)

Status checks if the given file path exists in the index and returns its corresponding Status struct and true. If the file path does not exist, it returns the unique NoStatus struct and false.

func (*Roster) Update

func (ros *Roster) Update(filePath string, stat Status) error

Update replaces the Status struct associated with a given file path in the roster index if valid.

func (*Roster) Write

func (ros *Roster) Write() error

Write formats and writes the receiver Roster ros's configuration and member data to disk. Returns an error if formatting or writing fails.

type Runtime

type Runtime struct {
	Thr int `yaml:"threads"`
	Dep int `yaml:"maxdepth"`
}

Runtime fine-tunes the construction/verification operations.

type Status

type Status struct {
	Fsize int64  `yaml:"size"`
	Perms string `yaml:"perm"`
	Mtime string `yaml:"last"`
	Check string `yaml:"hash"`
}

Status represents all verifiable attributes of an indexed file.

func MakeStatus

func MakeStatus(root string, relPath string, info os.FileInfo) (Status, error)

MakeStatus constructs a new Status struct. This method does not consider the Verify settings, and it will always analyze all attributes of the given file.

func NoStatus

func NoStatus() Status

NoStatus returns a default Status struct for files that have not been analyzed.

func (Status) Equals

func (s Status) Equals(t Status, ver Verify) bool

Equals compares two Status structs for equality, per Verify settings.

func (Status) Valid

func (s Status) Valid() bool

Valid verifies the receiver Status s is not equal to the unique NoStatus struct, using all Status attributes.

type Verify

type Verify struct {
	Fsize bool `yaml:"filesize"`
	Perms bool `yaml:"permissions"`
	Mtime bool `yaml:"lastmodtime"`
	Check bool `yaml:"checksum"`
}

Verify defines file attributes that are recorded for all indexed files and used to identify changed files.

func AllVerify

func AllVerify() Verify

AllVerify returns a Verify struct with all attributes set true for verification.

Jump to

Keyboard shortcuts

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