fileops

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureDir

func EnsureDir(path string) error

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

func ExpandHome(path string) (string, error)

ExpandHome expands ~ to the user's home directory.

func GetConflictNumber

func GetConflictNumber(path string) int

GetConflictNumber extracts the conflict number from a path, or 0 if none.

func HasConflictSuffix

func HasConflictSuffix(path string) bool

HasConflictSuffix checks if a path has a conflict suffix like -1, -2, etc.

func IsDir

func IsDir(path string) bool

IsDir checks if path exists and is a directory.

func IsFile

func IsFile(path string) bool

IsFile checks if path exists and is a regular file.

func IsInDirectory

func IsInDirectory(path, dir string) (bool, error)

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

func PathExists(path string) bool

PathExists checks if a path exists (file or directory).

func RelativePath

func RelativePath(base, target string) (string, error)

RelativePath returns the relative path from base to target. If target is not within base, returns an error.

func ResolveConflict

func ResolveConflict(path string) (string, error)

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

func ValidatePath(path string) error

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

func ValidateSubdirectory(subdir string) error

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.

func CopyBatch

func CopyBatch(items []CopyItem, force bool) ([]CopyResult, []error)

CopyBatch copies multiple files/directories. It continues on failure and returns all results and errors. Errors are indexed to match the corresponding CopyItem.

func CopyToDir

func CopyToDir(src, dstDir string, force bool) (CopyResult, error)

CopyToDir copies src to a file with the same name inside dstDir. This is a convenience function for the common case of copying a file into a directory while preserving its name.

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.

Jump to

Keyboard shortcuts

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