fs

package
v13.3.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package fs provides various filesystem helpers.

Index

Constants

View Source
const DirPermissions = os.ModeDir | 0775

DirPermissions are the default permission bits we apply to directories.

Variables

This section is empty.

Functions

func CopyFile

func CopyFile(from string, to string, mode os.FileMode) error

CopyFile copies a file from 'from' to 'to', with an attempt to perform a copy & rename to avoid chaos if anything goes wrong partway.

func CopyOrLinkFile

func CopyOrLinkFile(from, to string, mode os.FileMode, link, fallback bool) error

CopyOrLinkFile either copies or hardlinks a file based on the link argument. Falls back to a copy if link fails and fallback is true.

func EnsureDir

func EnsureDir(filename string) error

EnsureDir ensures that the directory of the given file has been created.

func ExpandHomePath

func ExpandHomePath(path string) string

ExpandHomePath expands all prefixes of ~ without a user specifier to $HOME.

func FileExists

func FileExists(filename string) bool

FileExists returns true if the given path exists and is a file.

func Glob

func Glob(buildFileNames []string, rootPath string, includes, prefixedExcludes, excludes []string, includeHidden bool) []string

Glob implements matching using Go's built-in filepath.Glob, but extends it to support Ant-style patterns using **.

func IsDirectory

func IsDirectory(path string) bool

IsDirectory checks if a given path is a directory

func IsGlob

func IsGlob(pattern string) bool

IsGlob returns true if the given pattern requires globbing (i.e. contains characters that would be expanded by it)

func IsPackage

func IsPackage(buildFileNames []string, name string) bool

IsPackage returns true if the given directory name is a package (i.e. contains a build file)

func IsSameFile

func IsSameFile(a, b string) bool

IsSameFile returns true if two filenames describe the same underlying file (i.e. inode)

func IsSymlink(filename string) bool

IsSymlink returns true if the given path exists and is a symlink.

func PathExists

func PathExists(filename string) bool

PathExists returns true if the given path exists, as a file or a directory.

func RecursiveCopy

func RecursiveCopy(from string, to string, mode os.FileMode) error

RecursiveCopy copies either a single file or a directory.

func RecursiveLink(from string, to string, mode os.FileMode) error

RecursiveLink hardlinks either a single file or a directory. Note that you can't hardlink directories so the behaviour is much the same as a recursive copy. If it can't link then it falls back to a copy.

func SortPaths

func SortPaths(files []string) []string

SortPaths sorts a list of filepaths in lexicographic order by component (i.e. so all files in a directory sort after all subdirectories).

func Walk

func Walk(rootPath string, callback func(name string, isDir bool) error) error

Walk implements an equivalent to filepath.Walk. It's implemented over github.com/karrick/godirwalk but the provided interface doesn't use that to make it a little easier to handle.

func WalkMode

func WalkMode(rootPath string, callback func(name string, isDir bool, mode os.FileMode) error) error

WalkMode is like Walk but the callback receives an additional type specifying the file mode type. N.B. This only includes the bits of the mode that determine the mode type, not the permissions.

func WriteFile

func WriteFile(fromFile io.Reader, to string, mode os.FileMode) error

WriteFile writes data from a reader to the file named 'to', with an attempt to perform a copy & rename to avoid chaos if anything goes wrong partway.

Types

type PathHasher

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

A PathHasher is responsible for hashing & remembering paths.

func NewPathHasher

func NewPathHasher(root string) *PathHasher

NewPathHasher returns a new PathHasher based on the given root directory.

func (*PathHasher) Hash

func (hasher *PathHasher) Hash(path string, recalc bool) ([]byte, error)

Hash hashes a single path. It is memoised and so will only hash each path once, unless recalc is true which will then force a recalculation of it.

func (*PathHasher) MoveHash

func (hasher *PathHasher) MoveHash(oldPath, newPath string, copy bool)

MoveHash is used when we move files from tmp to out and there was one there before; that's the only case in which the hash of a filepath could change.

func (*PathHasher) MustHash

func (hasher *PathHasher) MustHash(path string) []byte

MustHash is as Hash but panics on error.

func (*PathHasher) SetHash

func (hasher *PathHasher) SetHash(path string, hash []byte)

SetHash is used to directly set a hash for a path. This is used for remote files where we download them & therefore know the hash as they come in. TODO(peterebden): We should probably use this more for things like caches and so forth...

Jump to

Keyboard shortcuts

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