fileutils

package
v0.0.0-...-cf3f34d Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotExist = os.ErrNotExist

Functions

func CopyFile

func CopyFile(from string, to string) error

CopyFile will copy the entire contents of the file at path `from`, to the file at path `to`. If `to` doesn't exist, it is created. If it does exist, it is overwritten.

func CopyFileTree

func CopyFileTree(from string, to string, whitelist []string) error

CopyFileTree copies the directory tree under `from` to the directory `to`. This function only copies files, creating directories as necessary to recreate the structure. As a result, empty directories will not be created.

The `whitelist` is a list of glob patterns on filenames (not complete paths), which are used to filter which files are copied. Only files with names matching the list will be copied. filepath.Match is used to perform the matching.

func FindFileInParents

func FindFileInParents(filename string, startingFromDir string) (string, error)

FindFileInParents proceeds up the directory hierarchy, starting from `startingFromDir`, and attempts to find a file named `filename` in any of those directories. It will return the path of the one closest (i.e. fewer steps) to `startingFromDir`. If not such file is found, returns `ErrNotExist`

func HasParentDir

func HasParentDir(child string) (bool, error)

HasParentDir returns true if the given path has a parent directory distinct from itself. Equivalently, it returns true if the provided path is *not* the root of the filesystem (e.g. '/' on unix or 'C:\' on Windows). Returns an error if a filesystem operation fails.

func IsDirPresent

func IsDirPresent(p string) (bool, error)

IsDirPresent will return true if it found a directory at the given path. It will return false if there is nothing at the given path. It returns an error if a normal file is found at the given path.

func NormalFileExists

func NormalFileExists(path string) (bool, error)

NormalFileExists will return true if a normal file (i.e. not a directory) exists at the given path. It will also return false if parent directories don't exist. Any other error encountered will be returned.

func RemoveEmptySubDirs

func RemoveEmptySubDirs(dir string) error

RemoveEmptySubDirs will delete the immediate child directories of path, but only of they are empty. Directories that are not empty will be skipped silently.

func TryCloseAll

func TryCloseAll(things ...interface{}) error

TryCloseAll takes io.Closers, or slices of things that implement io.Closer, and trys to close them all Even if one Close() fails, it will try to close all of them If there was an error, returns a multierror.Error containing all the errors that occurred.

func TryRemoveAllLocked

func TryRemoveAllLocked(dirPath string) (bool, error)

TryRemoveAllLocked will attempt to lock the given directory and delete it, including its entire contents. If the directory could not be locked, returns false but no error If the deletion fails for another reason, returns false with an error If the deletion succeeds, returns true and no error.

func WalkRelAtDepth

func WalkRelAtDepth(root string, depth int, walk filepath.WalkFunc) error

WalkRelAtDepth will walk the directory tree starting at root. For each entry whose depth is exactly `depth` into the tree, it calls the provided function. The `path` paremeter it passes to the callback is relative to `root`. Otherwise, this function behaves the same as filepath.Walk

Types

type LockedDir

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

LockedDir is a discretionary lock for a directory. There is no concept of lock inheritance; locking a directory does not automatically lock its parent or child directories or files. The lock is implemented by creating a locked "*.lock" file adjacent to the specified directory. This file has no contents. The locked directory itself is not guaranteed to exist - this allows locking the directory before it is created, and allows calling functions that expect to create the directory themselves. Shared locking is not implemented; all directory locks are exclusive.

func LockDir

func LockDir(path string) (*LockedDir, error)

LockDir opens a LockedDir at the specified path.

func LockDirOrCreate

func LockDirOrCreate(path string, constructor func(*LockedDir) error) (lock *LockedDir, err error)

LockDirOrCreate locks the dir at `path`. If the directory does not exist, the provided `constructor` is called to initialise the contents of the directory.

func (*LockedDir) Close

func (ld *LockedDir) Close() error

func (*LockedDir) Dir

func (ld *LockedDir) Dir() string

Dir returns the path of the locked directory

func (*LockedDir) DirExists

func (ld *LockedDir) DirExists() (bool, error)

DirExists checks if the locked directory exists.

type LockedFile

type LockedFile struct {
	*os.File
	*flock.Flock
	// contains filtered or unexported fields
}

LockedFile is an open file with an advisory lock. It is important that you close and unlock the file before the program exits - both can be achieved by calling Close()

func CreateLockedFile

func CreateLockedFile(path string, truncate bool) (file *LockedFile, err error)

CreateLockedFile creates a new file, opened for reading and writing and locked exclusively. If `truncate` is true, it will overwrite an existing file at `path`. Otherwise, an error will be returned if the file already exists.

func OpenRLockedFile

func OpenRLockedFile(path string) (file *LockedFile, err error)

OpenRLockedFile will open a file read-only with shared locking.

func OpenRLockedFileOrCreate

func OpenRLockedFileOrCreate(path string, constructor func(file *LockedFile) error) (file *LockedFile, err error)

OpenRLockedFileOrCreate will try to open the file at `path` for reading, in the same manner as OpenRLockedFile. If it does not exist, the file will opened for writing in the same manner as CreateLockedFile, and the provided `constructor` is called to allow the file to be filled. In either case, the open LockedFile is returned.

func (*LockedFile) Close

func (lf *LockedFile) Close() error

Close implements an io.Closer for LockedFile

Jump to

Keyboard shortcuts

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