Documentation
¶
Overview ¶
Package fsutils provides filesystem path cleaning and normalization utilities.
Index ¶
- Variables
- func CleanPath(dir string) (string, error)
- func CopyFile(srcPath, dstPath string, perm fs.FileMode) error
- func ExpandAndCleanPath(targetPath, baseDir, homeDir string) string
- func HasDanglingSymlinkUnder(targetPath, baseDir string) bool
- func IsConfinedInsideDir(targetPath, baseDir string) bool
- func IsInsideDir(targetPath, dir string) bool
- func LinkOrCopy(srcPath, dstPath string, perm fs.FileMode) error
- func PortablePath(targetPath, rootDir, homeDir string) string
- func ResolveConfigPath(targetPath, baseDir string) string
- func ResolveDeepestExisting(p string) string
- func WriteFileAtomic(dstPath string, data []byte, perm fs.FileMode) error
- func WriteFileAtomicInRoot(root *os.Root, name string, data []byte, perm fs.FileMode) error
- type Glob
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyPath = errors.New("fsutils: path must not be empty")
ErrEmptyPath is returned when a provided path is blank or consists only of whitespace.
Functions ¶
func CleanPath ¶
CleanPath trims whitespace from dir, returns ErrEmptyPath if empty, resolves it to an absolute path, evaluates symlinks if possible, and returns the canonical cleaned path.
func CopyFile ¶
CopyFile copies srcPath to dstPath atomically via a sibling temporary file and rename, setting destination permissions to perm.
func ExpandAndCleanPath ¶
ExpandAndCleanPath trims targetPath, expands ~ with homeDir (if provided), and returns a cleaned path relative to baseDir (if targetPath is relative and baseDir is non-empty), or a cleaned absolute path otherwise.
func HasDanglingSymlinkUnder ¶
HasDanglingSymlinkUnder reports whether any existing path component at or below baseDir along targetPath is a symlink that cannot be resolved cleanly inside baseDir.
func IsConfinedInsideDir ¶
IsConfinedInsideDir reports whether targetPath resides inside baseDir without escaping via a dangling symlink, and its canonical (symlink-resolved) form stays inside the canonical baseDir. When baseDir does not exist yet, a canonical base nested inside the canonical target (a not-yet-created intermediate directory) is still considered confined.
func IsInsideDir ¶
IsInsideDir reports whether targetPath is equal to or located within dir. Both paths are cleaned before checking.
func LinkOrCopy ¶
LinkOrCopy hardlinks srcPath to dstPath in O(1), falling back to CopyFile when hardlinking fails (such as across filesystems).
func PortablePath ¶
PortablePath converts targetPath into a portable representation. If targetPath is located within rootDir, it returns the relative path from rootDir. Otherwise, if located within homeDir, it returns the path prefixed with "~". If it cannot be made relative, the cleaned targetPath is returned.
func ResolveConfigPath ¶
ResolveConfigPath expands a leading "~" or "~/" in targetPath using the current user's home directory, then resolves the result against baseDir when targetPath is relative and baseDir is non-empty. It is meant for paths authored in configuration files, where both "~" and a path relative to the declaring file are natural to write. Failing to determine the home directory leaves "~" untouched rather than failing the caller.
func ResolveDeepestExisting ¶
ResolveDeepestExisting canonicalizes p by evaluating symlinks on p if it exists, or on its deepest existing ancestor while appending the remaining (not-yet-created) path components lexically. This keeps path confinement and OS kernel sandbox rules anchored to canonical vnode paths even when a parent directory is a symlink (e.g. /var -> /private/var on macOS) and the leaf does not exist yet.
func WriteFileAtomic ¶
WriteFileAtomic writes data to dstPath atomically via a sibling temporary file and rename, setting file permissions to perm. The parent directory must exist.