Documentation
¶
Index ¶
- func EnsureDir(path string) error
- func ExpandHome(path string) (string, error)
- func GetConflictNumber(path string) int
- func HasConflictSuffix(path string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- func IsInDirectory(path, dir string) (bool, error)
- func PathExists(path string) bool
- func RelativePath(base, target string) (string, error)
- func ResolveConflict(path string) (string, error)
- func ValidatePath(path string) error
- func ValidateSubdirectory(subdir string) error
- type CopyItem
- type CopyResult
- type MoveItem
- type MoveResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureDir ¶
EnsureDir creates a directory and all necessary parents if they don't exist. If the directory already exists, this is a no-op.
func ExpandHome ¶
ExpandHome expands ~ to the user's home directory.
func GetConflictNumber ¶
GetConflictNumber extracts the conflict number from a path, or 0 if none.
func HasConflictSuffix ¶
HasConflictSuffix checks if a path has a conflict suffix like -1, -2, etc.
func IsInDirectory ¶
IsInDirectory checks if the given path is within the specified directory. Both paths are cleaned and resolved to absolute paths before comparison. Returns true if path is inside dir (or is dir itself), false otherwise.
func PathExists ¶
PathExists checks if a path exists (file or directory).
func RelativePath ¶
RelativePath returns the relative path from base to target. If target is not within base, returns an error.
func ResolveConflict ¶
ResolveConflict finds a non-conflicting name for the given path by appending a -N suffix (where N is an incrementing integer starting from 1).
For files, the suffix is inserted before the extension:
- file.md → file-1.md → file-2.md
- archive.tar.gz → archive-1.tar.gz → archive-2.tar.gz
For directories:
- mydir → mydir-1 → mydir-2
If the path doesn't exist, it returns the original path unchanged.
func ValidatePath ¶
ValidatePath checks if a path is safe for use. It rejects paths containing:
- Parent directory references (..)
- Null bytes
- Absolute paths when relative is expected
func ValidateSubdirectory ¶
ValidateSubdirectory checks if a subdirectory path is safe for use within a parent directory. This is more restrictive than ValidatePath.
Types ¶
type CopyItem ¶
type CopyItem struct {
Src string // Source path (file or directory)
Dst string // Destination path
}
CopyItem represents a single copy operation.
type CopyResult ¶
type CopyResult struct {
Src string // Original source path
Dst string // Final destination path (may differ from requested if conflict resolved)
Size int64 // Total bytes copied
IsDir bool // Whether source was a directory
FileCount int // Number of files copied (1 for single file, N for directory)
Renamed bool // Whether destination was renamed due to conflict
}
CopyResult contains the result of a copy operation.
func Copy ¶
func Copy(src, dst string, force bool) (CopyResult, error)
Copy copies a file or directory from src to dst. If force is false and dst exists, the destination is renamed with -N suffix. If force is true, existing files are overwritten.
type MoveItem ¶
type MoveItem struct {
Src string // Source path (file or directory)
Dst string // Destination path
}
MoveItem represents a single move operation.
type MoveResult ¶
type MoveResult struct {
Src string // Original source path
Dst string // Final destination path (may differ if conflict resolved)
IsDir bool // Whether source was a directory
FileCount int // Number of files moved (1 for single file, N for directory)
Renamed bool // Whether destination was renamed due to conflict
CrossFilesystem bool // Whether move required copy+delete (cross-filesystem)
}
MoveResult contains the result of a move operation.
func Move ¶
func Move(src, dst string) (MoveResult, error)
Move moves a file or directory from src to dst. If dst exists, it is renamed with -N suffix to avoid conflict. If src and dst are on different filesystems, this falls back to copy+delete.
func MoveBatch ¶
func MoveBatch(items []MoveItem) ([]MoveResult, []error)
MoveBatch moves multiple files/directories. It continues on failure and returns all results and errors. Errors are indexed to match the corresponding MoveItem.
func MoveToDir ¶
func MoveToDir(src, dstDir string) (MoveResult, error)
MoveToDir moves src to a file with the same name inside dstDir. This is a convenience function for the common case of moving a file into a directory while preserving its name.