fsutils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fsutils provides filesystem path cleaning and normalization utilities.

Index

Constants

This section is empty.

Variables

View Source
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

func CleanPath(dir string) (string, error)

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

func CopyFile(srcPath, dstPath string, perm fs.FileMode) error

CopyFile copies srcPath to dstPath atomically via a sibling temporary file and rename, setting destination permissions to perm.

func ExpandAndCleanPath

func ExpandAndCleanPath(targetPath, baseDir, homeDir string) string

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

func HasDanglingSymlinkUnder(targetPath, baseDir string) bool

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

func IsConfinedInsideDir(targetPath, baseDir string) bool

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

func IsInsideDir(targetPath, dir string) bool

IsInsideDir reports whether targetPath is equal to or located within dir. Both paths are cleaned before checking.

func LinkOrCopy

func LinkOrCopy(srcPath, dstPath string, perm fs.FileMode) error

LinkOrCopy hardlinks srcPath to dstPath in O(1), falling back to CopyFile when hardlinking fails (such as across filesystems).

func PortablePath

func PortablePath(targetPath, rootDir, homeDir string) string

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

func ResolveConfigPath(targetPath, baseDir string) string

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

func ResolveDeepestExisting(p string) string

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

func WriteFileAtomic(dstPath string, data []byte, perm fs.FileMode) error

WriteFileAtomic writes data to dstPath atomically via a sibling temporary file and rename, setting file permissions to perm. The parent directory must exist.

func WriteFileAtomicInRoot

func WriteFileAtomicInRoot(root *os.Root, name string, data []byte, perm fs.FileMode) error

Types

type Glob

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

Glob represents a compiled glob pattern.

func Compile

func Compile(pattern string) (*Glob, error)

Compile compiles pattern into a Glob. It supports standard wildcard syntax including '*', '?', '[...]', '[!...]', and '**' multi-directory wildcards.

func (*Glob) Match

func (g *Glob) Match(text string) bool

Match reports whether text matches the compiled glob pattern.

Jump to

Keyboard shortcuts

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