
FileFlow Package
The fileflow package provides utilities to safely move, copy, rename, and explicitly replace files, including across drives/filesystems. Move, Copy, and Rename preserve non-identical destinations; Replace deliberately publishes new content at the exact requested path.
Features
- Safe File Move: Moves files between paths, with support for cross-filesystem transfers.
- Unique Destination Naming: If the destination file already exists, appends incrementing suffixes (
-1, -2, etc.) to avoid overwriting non-identical files.
- Identical File Check: Compares files to determine if they are identical, preventing unnecessary overwrites.
- Path Creation: Automatically creates directories for destination paths if they don't exist.
- Customizable Naming Strategy: Flexible naming strategy for handling file conflicts through customizable functions.
- Explicit Atomic Replacement:
Replace overwrites an exact destination without suffixing, with destination-side staging for cross-filesystem moves.
Safety guarantees
Move, Copy, and Rename never overwrite a non-identical file. Renames use no-replace primitives (MoveFileEx without MOVEFILE_REPLACE_EXISTING on Windows, atomic link+remove on Unix), so even a file created by a concurrent process between conflict resolution and the rename is safe: FileFlow detects the collision, resolves a new name, and retries. On Unix filesystems without hard link support (e.g. FAT), the rename falls back to a plain rename and the no-overwrite guarantee is best-effort. Copies are written to a temporary file in the destination directory, synced, set to the source file's mode and (by default, best-effort) its modification and access times, and renamed into place with the same no-replace primitive, so a failed copy does not leave a partial or truncated destination. When the destination is identical to the source, FileFlow deduplicates the operation instead of writing another copy.
Replace is the explicit exception: it intentionally overwrites the exact destination, without conflict naming or a delete-then-move window. On the same filesystem it publishes by rename; across filesystems it first copies and syncs a temporary file in the destination directory, publishes that file, and then removes the source. The destination receives the source file's permissions. Atomic visibility requires filesystem rename support; this is not a promise of power-loss durability. Sync and close your staged source before calling Replace when durability matters. On Windows, open-file sharing rules can reject a replacement; the error is returned rather than deleting the old destination.
Installation
Simply include the package in your Go project:
import "github.com/spf13/fileflow"
Usage
Move
Moves a file from src to dst and returns the final destination path, which may differ from dst after conflict resolution. Move uses an atomic rename when possible and falls back to copy and delete across filesystems or drives, including on Windows. If src and dst refer to the same file through different paths, Move returns ErrSameFile rather than risking data loss.
finalPath, err := fileflow.Move("source.txt", "destination.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println("File moved to:", finalPath)
Rename
Renames src to dst, creates missing destination directories using DirMode, and returns the final destination path. Rename fails across filesystems; use Move if you want the copy fallback.
destination, err := fileflow.Rename("source.txt", "destination.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println("File renamed to:", destination)
Copy
Copies src to dst and returns the final destination path, which may differ from dst after conflict resolution. If dst exists with different content, the returned path contains the conflict suffix. If dst is identical, Copy is a no-op and returns dst. Missing destination directories are created automatically.
finalPath, err := fileflow.Copy("source.txt", "backup/source.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println("File copied to:", finalPath)
Cancellation and progress
CopyContext and MoveContext (package-level and on Flow) take a context.Context. A context that is already done returns its error before anything is touched. During a copy the context is checked while comparing against an existing destination, between 1 MiB chunks, and once more before publishing; on cancellation the temporary file is removed, no destination is created, and the source is left intact. The error wraps ctx.Err() in *ErrFailedMovingFile, so errors.Is(err, context.Canceled) works. A same-filesystem MoveContext is a single rename, so it is checked only at entry and while comparing against an existing destination. Once the destination is published the operation completes; a cross-filesystem move still removes the source.
Set Flow.Progress to observe copies. It is called synchronously from the copying goroutine after each chunk is written, with total taken from the source's pre-copy stat; the last call has done equal to the bytes copied. It is not called when nothing is copied (same-filesystem rename, identical-destination dedup), and must not block for long. Replace also honours Progress across filesystems.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
f := fileflow.Flow{Progress: func(done, total int64) {
fmt.Printf("\r%d/%d bytes", done, total)
}}
finalPath, err := f.MoveContext(ctx, "/mnt/usb/video.mkv", "videos/video.mkv")
if errors.Is(err, context.DeadlineExceeded) {
log.Fatal("move timed out; source left in place")
}
Zero-value Copy and Move (no Progress, uncancellable context) keep using io.Copy, so kernel fast paths such as Linux copy_file_range still apply. A cancellable context or a Progress callback switches to the chunked loop.
Verified copies and digests
Set Flow.Verify (off by default) to verify copies with SHA-256. Copy, and Move/Replace across filesystems, hash the source while copying, sync the temporary file, re-read and hash it, and publish only when the digests match. A mismatch returns an error wrapping ErrDigestMismatch (inside *ErrFailedMovingFile), removes the temporary file, publishes nothing, and leaves the source intact; a cross-filesystem Move never removes its source unless verification passed. Verify routes the copy through the hash, so kernel fast paths such as copy_file_range do not apply.
Flow.CopyResult and Flow.MoveResult return a Result with the final Path and, when Verify is set, the content's Digest (a [32]byte with a hex String method), so callers can journal it without reading the file again. CopyContext and MoveContext return the same Path. Without Verify the digest is zero.
- Identical-destination dedup hashes the source during the byte comparison; no extra read.
- A same-filesystem
MoveResult is a rename, so it hashes the file once at its final path afterwards. If that read fails, MoveResult returns the final Path together with the error. As with *ErrFailedRemovingOriginal, a non-empty Path alongside an error means the file is already at Path.
Replace honours Verify across filesystems (a mismatch leaves the old destination and the source intact) and still returns only the path. Rename ignores Verify.
The re-read may be served from the page cache: verification catches short writes and corruption in the copy path, not media faults below the cache.
f := fileflow.Flow{Verify: true}
r, err := f.MoveResult(ctx, "/mnt/card/IMG_0001.CR3", "photos/IMG_0001.CR3")
if err != nil {
// errors.Is(err, fileflow.ErrDigestMismatch): nothing published, source left in place.
// A non-empty r.Path alongside an error means the file already moved there.
log.Fatal(err)
}
journal(r.Path, r.Digest.String())
Replace
Use Replace when updating a canonical file such as kennyb.toml, where a conflict-suffixed filename would leave readers using the old content. This is a move: on success the source is consumed and the returned path is exactly dst, including when creating the destination for the first time.
// Write, sync, and close stagedPath before publishing it.
flow := fileflow.Flow{NoCreateDirs: true}
finalPath, err := flow.Replace(stagedPath, "kennyb.toml")
if err != nil {
log.Fatal(err)
}
fmt.Println("Published:", finalPath)
Flow.Replace respects DirMode and NoCreateDirs; FindAvailableName is ignored. The source must be a regular file: symlinks and other non-regular sources return an error matching fs.ErrInvalid. A destination symlink is replaced, not followed. Paths identifying the same file return ErrSameFile, leaving it untouched.
Before publication, failure preserves the existing destination and source and cleans up destination-side staging. If a cross-filesystem publication succeeds but source removal fails, the returned path is dst together with *ErrFailedRemovingOriginal; the new destination is already committed. Existing Move, Copy, and Rename behavior is unchanged.
Exists
Reports whether an accessible regular file exists at the specified path. It returns false for directories.
exists := fileflow.Exists("path/to/file.txt")
if exists {
fmt.Println("File exists.")
}
Equal
Compares two files byte by byte to check if they are identical.
identical, err := fileflow.Equal("file1.txt", "file2.txt")
if err != nil {
log.Fatal(err)
}
if identical {
fmt.Println("Files are identical.")
}
FindAvailableName
FileFlow provides four naming strategies for handling conflicts. The default preserves the complete requested name before appending a counter; the continuation strategy treats a trailing -N as an existing counter. FileFlow cannot know whether trailing digits are a counter or meaningful data, so choose the behavior that fits your filenames.
FindAvailableNameInc (Default)
The default implementation always appends -1, -2, and so on. It tries up to 100 candidates, then returns ErrMaxAttemptsReached. Existing numeric endings are preserved:
- For "document.txt", generates "document-1.txt", "document-2.txt", etc.
- For "report-2024.txt", generates "report-2024-1.txt", not "report-1.txt".
- For "file-1.txt", generates "file-1-1.txt".
FindAvailableNameCont
This implementation treats a trailing -N suffix as a counter and continues from N+1. If "file-1.txt", "file-2.txt", and "file-3.txt" exist, requesting "file-1.txt" yields "file-4.txt". A name without a trailing -N behaves like FindAvailableNameInc.
This is useful for downloaded files: if one batch contains "file-1.txt", "file-2.txt", and "file-3.txt", a second batch can land as "file-4.txt", "file-5.txt", and "file-6.txt" instead of "file-1-1.txt", "file-2-1.txt", and "file-3-1.txt".
Do not use this strategy when filenames end in meaningful digits. For example, "report-2024.txt" would continue as "report-2025.txt".
FindAvailableNameAuto
This implementation picks between the two strategies by inspecting the trailing digits. A trailing -N counter of one or two digits is continued like FindAvailableNameCont, while three or more trailing digits (years, zero-padded sequence numbers) are treated as meaningful and preserved like FindAvailableNameInc.
- For "file-1.txt" when "file-1.txt" through "file-3.txt" exist, yields "file-4.txt".
- For "report-2024.txt", yields "report-2024-1.txt", not "report-2025.txt".
FindAvailableNameTS
An alternative implementation that appends one nanosecond-precision timestamp using the format 20060102-150405.000000000:
- For "document.txt", generates a name such as "document-20230615-143022.123456789.txt".
If that exact timestamped candidate already exists, it falls back to FindAvailableNameInc on the candidate.
You can customize the naming strategy by providing your own implementation:
// Example of a custom naming strategy that adds a random suffix
func customNamingStrategy(baseName string) (string, error) {
ext := filepath.Ext(baseName)
nameWOExt := baseName[:len(baseName)-len(ext)]
// Generate a random 6-character string
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
suffix := make([]byte, 6)
for i := range suffix {
suffix[i] = charset[rand.Intn(len(charset))]
}
newName := fmt.Sprintf("%s-%s%s", nameWOExt, string(suffix), ext)
if !fileflow.Exists(newName) {
return newName, nil
}
return "", fileflow.ErrMaxAttemptsReached
}
f := fileflow.Flow{FindAvailableName: customNamingStrategy}
finalPath, err := f.Move("source.txt", "destination.txt")
Configuration with Flow
Flow provides per-instance configuration for file operations:
type Flow struct {
FindAvailableName func(string) (string, error)
DirMode fs.FileMode
NoCreateDirs bool
NoPreserveTimes bool
Progress func(done, total int64)
Verify bool
}
The zero value is ready to use. A nil FindAvailableName uses FindAvailableNameInc, and a zero DirMode uses 0755. Flow has Move, MoveContext, Rename, Copy, CopyContext, and Replace methods with the same signatures and behavior as the package-level functions, plus CopyResult and MoveResult (Flow-only, since Verify is off in the zero value); Replace ignores the naming strategy and deliberately overwrites the exact destination.
FileFlow creates missing destination directories by default — a deliberate departure from os.Rename and OS defaults, which fail when the directory doesn't exist. Set NoCreateDirs: true to get standard-library behavior: operations then fail with the underlying fs.ErrNotExist