With SameConn a top-level item without a target is renamed whole (NoReplace), and files merged into an existing directory are renamed one by one; EXDEV or any rename failure falls back to copy. Otherwise each file or link is copied, its size checked and only then removed from the source (Remove never follows links: N-P0-3); source directories are removed bottom-up only when empty, so skipped and failed items stay in place. Links are always moved as links (FollowLinks is ignored for a move: N-P1-8). Affected includes the source directories. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
238 lines
6.2 KiB
Go
238 lines
6.2 KiB
Go
// Package transfer is the one engine for copy, move and unpack (design §4):
|
|
// Prepare walks the source once and plans every item, Execute writes each file
|
|
// through WriteAtomic (temp name + rename), merges directories, copies
|
|
// symlinks as links and asks the caller about conflicts and errors through
|
|
// Hooks. It works over any pair of vfs.FS.
|
|
//
|
|
// Invariants: the source is never modified by a copy; an existing target is
|
|
// replaced only by the final rename of a fully written temp file; after an
|
|
// error or cancellation no partial file is left under a target name; a move
|
|
// removes a source item only after it was copied; directories are never
|
|
// deleted to make room for a file.
|
|
package transfer
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"time"
|
|
|
|
"vcom/internal/vfs"
|
|
)
|
|
|
|
// Op is what Execute does with the source.
|
|
type Op uint8
|
|
|
|
const (
|
|
Copy Op = iota
|
|
Move
|
|
)
|
|
|
|
// ConflictPolicy decides what happens when a target already exists.
|
|
type ConflictPolicy uint8
|
|
|
|
const (
|
|
Ask ConflictPolicy = iota // Hooks.Resolve decides (Skip if nil)
|
|
Overwrite // replace files (never directories)
|
|
Skip // keep the target
|
|
OverwriteOlder // replace only if the source is newer
|
|
KeepBoth // write under a free "name (1).ext"
|
|
)
|
|
|
|
// SymlinkPolicy: copy links as links (default) or copy what they point to.
|
|
type SymlinkPolicy uint8
|
|
|
|
const (
|
|
CopyLinks SymlinkPolicy = iota
|
|
FollowLinks
|
|
)
|
|
|
|
// ErrorPolicy decides what happens on an item error.
|
|
type ErrorPolicy uint8
|
|
|
|
const (
|
|
ErrorAsk ErrorPolicy = iota // Hooks.OnError decides (Skip if nil)
|
|
ErrorSkipAll // record and continue
|
|
ErrorAbort // stop at the first error
|
|
)
|
|
|
|
// FsyncPolicy decides when a written file is fsync'ed before its rename.
|
|
type FsyncPolicy uint8
|
|
|
|
const (
|
|
FsyncReplace FsyncPolicy = iota // only when replacing an existing file
|
|
FsyncAlways
|
|
FsyncNever
|
|
)
|
|
|
|
// Options tune a request. Use DefaultOptions for the defaults.
|
|
type Options struct {
|
|
Conflict ConflictPolicy
|
|
Symlinks SymlinkPolicy
|
|
OnError ErrorPolicy
|
|
Fsync FsyncPolicy
|
|
PreserveMode bool
|
|
PreserveTimes bool
|
|
}
|
|
|
|
// DefaultOptions: ask on conflicts and errors, copy links as links, fsync
|
|
// before replacing, keep mode and mtime.
|
|
func DefaultOptions() Options {
|
|
return Options{PreserveMode: true, PreserveTimes: true}
|
|
}
|
|
|
|
// ItemKind classifies a source item.
|
|
type ItemKind uint8
|
|
|
|
const (
|
|
File ItemKind = iota
|
|
Dir
|
|
Symlink
|
|
Special // FIFO, socket, device: skipped with a warning
|
|
)
|
|
|
|
func (k ItemKind) String() string {
|
|
switch k {
|
|
case File:
|
|
return "file"
|
|
case Dir:
|
|
return "dir"
|
|
case Symlink:
|
|
return "symlink"
|
|
}
|
|
return "special"
|
|
}
|
|
|
|
// Item is one node of the plan.
|
|
type Item struct {
|
|
Rel string // path relative to ToDir, slash-separated; first element is the root's name
|
|
Kind ItemKind
|
|
Size int64
|
|
Mode fs.FileMode
|
|
ModTime time.Time
|
|
LinkTarget string
|
|
Existing *vfs.FileInfo // what is at the target (Lstat); nil — nothing
|
|
Root int // index of the top-level source
|
|
|
|
// Src is the item's path on the source FS (FSSource); empty for
|
|
// sources without one (archives).
|
|
Src string
|
|
// Warning is set by the source when the item is degraded, e.g. a
|
|
// symlink loop under FollowLinks copied as a link.
|
|
Warning string
|
|
// ScanErr: the directory could not be listed during Prepare; Execute
|
|
// reports it as the item's error.
|
|
ScanErr error
|
|
}
|
|
|
|
// Request describes one transfer.
|
|
type Request struct {
|
|
Op Op
|
|
From Source
|
|
To vfs.FS
|
|
ToDir string
|
|
Opts Options
|
|
}
|
|
|
|
// ConflictKind: same type on both sides, or file↔dir / link↔dir.
|
|
type ConflictKind uint8
|
|
|
|
const (
|
|
FileFile ConflictKind = iota
|
|
TypeMismatch
|
|
)
|
|
|
|
// Conflict is an item whose target exists.
|
|
type Conflict struct {
|
|
Item Item
|
|
Kind ConflictKind
|
|
}
|
|
|
|
// ResolveAction answers a conflict.
|
|
type ResolveAction uint8
|
|
|
|
const (
|
|
ResolveOverwrite ResolveAction = iota
|
|
ResolveSkip
|
|
ResolveKeepBoth
|
|
ResolveAbort
|
|
)
|
|
|
|
// Resolution is the caller's answer to a conflict; ApplyToAll reuses it for
|
|
// every later conflict of the same ConflictKind.
|
|
type Resolution struct {
|
|
Action ResolveAction
|
|
ApplyToAll bool
|
|
}
|
|
|
|
// Plan is the result of Prepare.
|
|
type Plan struct {
|
|
Req Request
|
|
Items []Item
|
|
TotalFiles int // files and symlinks
|
|
TotalBytes int64
|
|
Conflicts []Conflict
|
|
Warnings []string
|
|
SameConn bool // From and To are the same connection (or both local)
|
|
}
|
|
|
|
// Phase of a running transfer.
|
|
type Phase uint8
|
|
|
|
const (
|
|
Scanning Phase = iota
|
|
Copying
|
|
Removing
|
|
Finishing
|
|
)
|
|
|
|
// Progress is a snapshot; BytesTotal comes from Prepare, BytesDone counts
|
|
// processed bytes (copied plus skipped), so it reaches BytesTotal.
|
|
type Progress struct {
|
|
Phase Phase
|
|
FilesDone, FilesTotal int
|
|
BytesDone, BytesTotal int64
|
|
Current string
|
|
CurrentDone int64
|
|
CurrentSize int64
|
|
}
|
|
|
|
// ErrorAction answers an item error.
|
|
type ErrorAction uint8
|
|
|
|
const (
|
|
ErrorRetry ErrorAction = iota
|
|
ErrorSkip
|
|
ErrorSkipRest // skip this and every later error
|
|
ErrorStop // abort the transfer
|
|
)
|
|
|
|
// Hooks connect Execute to its caller (the jobs manager).
|
|
type Hooks struct {
|
|
// Progress is called after every chunk and item, at most once per
|
|
// ProgressInterval (default 50 ms), plus a final call per phase.
|
|
Progress func(Progress)
|
|
ProgressInterval time.Duration
|
|
Resolve func(ctx context.Context, c Conflict) (Resolution, error)
|
|
OnError func(ctx context.Context, e ItemError) (ErrorAction, error)
|
|
}
|
|
|
|
// ItemError is a failed item.
|
|
type ItemError struct {
|
|
Item Item
|
|
Op string // "read", "write", "mkdir", "symlink", "rename", "remove", "scan", ...
|
|
Err error
|
|
}
|
|
|
|
func (e ItemError) Error() string { return e.Op + " " + e.Item.Rel + ": " + e.Err.Error() }
|
|
func (e ItemError) Unwrap() error { return e.Err }
|
|
|
|
// Report is the outcome of Execute.
|
|
type Report struct {
|
|
Done, Skipped, Failed int
|
|
Bytes int64 // bytes of completed files (copied, or moved by rename)
|
|
Errors []ItemError
|
|
Warnings []string
|
|
Cancelled bool
|
|
Affected []vfs.Location // directories that changed (to reload panels)
|
|
Err error // fatal: aborted, invalid plan
|
|
}
|