fileutil

package
v0.0.0-...-b79a0d8 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package fileutil contains various utility functions useful for dealing with files and dirs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDirPeriodic

func CheckDirPeriodic(dir string, delay time.Duration, action func() error)

Checks that a dir's inode does not change; if it does, call action(). Call from goroutine. Returns (exiting goroutine) if action returns error.

func CopyFile

func CopyFile(src, dest string, destFlags int) error

Copy a file. Assumes any dirs have already been created. Copies metadata.

func CopySomeFiles

func CopySomeFiles(srcDir, destDir string, flist []string) error

Copy files in flist to destDir, stripping srcDir. flist must be absolute paths. NOTE: may be slow to return, due to use of O_SYNC

func DirMatchCaseInsensitive

func DirMatchCaseInsensitive(dir string, entries []string) (hit bool, results []string)

Case-insensitive search of 'dir' for all of 'entries'. results will be an array the same size as entries, containing any matches - in the same order as entries. In the event of multiple files matching one of entries, the first seen is chosen.

func DirSizeM

func DirSizeM(dir string) string

Computes size of dir and contents.

func FindCaseInsensitive

func FindCaseInsensitive(root, name string, maxdepth int) (files []string, err error)

Perform case-insensitive search for file matching name (not a glob), under root, max depth maxdepth. Ignores symlinks. Returns absolute paths.

func ForcePathCase

func ForcePathCase(basepath, target string) (success bool)

ForcePathCase ensures correct case for some dir or file target in basepath. If target exists with different case, rename. If multiple variations exist, delete all but one.

func FreeSpace

func FreeSpace(dir string) int64

Return free space for FS containing dir, or -1 in the event of an error

func FreeSpaceM

func FreeSpaceM(dir string) string

Returns human-readable free space (in MB) for FS containing given dir.

func Globlike

func Globlike(p string) bool

Crude check that given path is not a glob. Does not understand escape sequences, so could return false positives.

func IsEmptyDir

func IsEmptyDir(dir string) bool

Returns true if given path is a dir and is empty

func IsMountpoint

func IsMountpoint(dir string) bool

IsMountpoint searchs for given dir in /proc/self/mountinfo, returns true if found

func IsXZ

func IsXZ(fname string) bool

checks for XZ header

func IsXZSha256

func IsXZSha256(fname string) bool

Checks for XZ header and stream option byte indicating sha256

func ListFilesAndSize

func ListFilesAndSize(dir, pattern string) (size int64, files []string)

Recursively find all files matching pattern in dir; return them and their total size. 'dir' must not be a glob pattern.

func MkdirOwned

func MkdirOwned(root, dir, owner, group string, mode os.FileMode) bool

Create dir in root with given owner, group, and mode

func ReadConfigLines

func ReadConfigLines(path string, maxLines int) ([]string, error)

ReadConfigLines reads a config file at the given path. Whitespace is stripped, as are comments (anything between # and \n). Individual lines are returned, up to maxLines.

func ReadHeader

func ReadHeader(fname string, n int64) (head []byte, err error)

return n bytes from beginning of file

func RecursiveCopy

func RecursiveCopy(src, dest string) error

RecursiveCopy walks tree rooted at src, copies dir 'src' to a subdir of dest.

NOTE: may be slow to return, due to use of O_SYNC.

func RenameUnique

func RenameUnique(old, newPfx string) (success bool)

Renames old in same dir, using newPfx + random suffix (via os.TempFile)

func ShowProgress

func ShowProgress(done chan struct{}, activityDesc, path string)

Called from a goroutine, updates lcd with size of a file. Use with download or decompress operation; close(done) to stop.

func ToMegs

func ToMegs(size int64) string

Converts a size in bytes to megabytes; returns string with suffix 'MB'.

func WaitFor

func WaitFor(path string, timeout time.Duration) (found bool)

WaitFor waits for a file to appear or times out. Returns true if file appears, false otherwise. Sleeps .1s between checks.

func WaitForChan

func WaitForChan(path string, stop chan struct{}) (found bool)

WaitForChan is like WaitFor, but returns no later than when stop chan is closed

func WaitForDir

func WaitForDir(watchedIsMountpoint bool, watchDir string)

WaitForDir waits for a dir to exist, and, if watchedIsMountpoint is true, waits for a fs to mount there.

Types

type UtfVariant

type UtfVariant uint
const (
	None UtfVariant = iota
	Utf32be
	Utf32le
	Utf16be
	Utf16le
	Utf8
)

func DetectBOM

func DetectBOM(fd io.ReadSeeker) (v UtfVariant, err error)

Check for UTF byte order mark; if present, seeks past it

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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