vcom/internal/transfer/conflict.go
vrubelroman 86c110f0b1 feat(transfer): WriteAtomic and Execute for files, dirs and links (3.2)
Every file is written to .vcom-part-<rand>-<name>, closed (a Close error
fails the item: N-P2-6), chmod/chtimes'ed and only then renamed over the
target, so a failed or cancelled overwrite keeps the old file (N-P0-4) and
leaves no temp. Without atomic replace the old file is renamed aside and
restored on failure. Directories are merged, never replaced (N-P0-7), and
get their real mode/mtime post-order (N-P1-17); symlinks are copied as
links, broken ones too (N-P1-8); mode and mtime are preserved (N-P1-9);
FIFOs and devices are skipped with a warning. The copy loop goes through a
ctx-aware reader with Size() and throttled byte progress. Conflict and
error policies are wired in here; their tests follow in 3.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 04:00:31 +03:00

161 lines
3.8 KiB
Go

package transfer
import (
"context"
"errors"
"fmt"
"path"
"strings"
"vcom/internal/vfs"
)
// resolve decides what to do with an item whose target exists.
func (e *executor) resolve(it Item, existing vfs.FileInfo, kind ConflictKind) (ResolveAction, error) {
if r, ok := e.applyAll[kind]; ok {
return e.restrict(it, kind, r), nil
}
var a ResolveAction
switch e.opts.Conflict {
case Overwrite:
a = ResolveOverwrite
case Skip:
a = ResolveSkip
case KeepBoth:
a = ResolveKeepBoth
case OverwriteOlder:
a = ResolveSkip
if it.ModTime.After(existing.ModTime) {
a = ResolveOverwrite
}
default: // Ask
if e.h.Resolve == nil {
a = ResolveSkip
break
}
c := Conflict{Item: it, Kind: kind}
c.Item.Existing = &existing
res, err := e.h.Resolve(e.ctx, c)
if err != nil {
return ResolveAbort, err
}
if res.ApplyToAll && res.Action != ResolveAbort {
e.applyAll[kind] = res.Action
}
a = res.Action
}
return e.restrict(it, kind, a), nil
}
// restrict: a type mismatch (file↔dir, link↔dir) is never overwritten —
// that would mean deleting a directory tree.
func (e *executor) restrict(it Item, kind ConflictKind, a ResolveAction) ResolveAction {
if kind == TypeMismatch && a == ResolveOverwrite {
e.rep.Warnings = append(e.rep.Warnings, it.Rel+": target is of another type (file/directory); not overwritten")
return ResolveSkip
}
return a
}
func conflictKind(it Item, existing vfs.FileInfo) ConflictKind {
if (it.Kind == Dir) != existing.IsDir() {
return TypeMismatch
}
return FileFile
}
// keepBothName returns "name (n).ext" for n = 1, 2, ... (".tar.gz" and
// friends stay together; dot-files and directories get no extension split).
func keepBothName(name string, n int, isDir bool) string {
stem, ext := name, ""
if !isDir {
lower := strings.ToLower(name)
for _, double := range []string{".tar.gz", ".tar.bz2", ".tar.xz", ".tar.zst"} {
if strings.HasSuffix(lower, double) && len(name) > len(double) {
ext = name[len(name)-len(double):]
break
}
}
if ext == "" {
if ext = path.Ext(name); ext == name {
ext = ""
}
}
stem = name[:len(name)-len(ext)]
}
return fmt.Sprintf("%s (%d)%s", stem, n, ext)
}
// freeRel finds a rel "name (n).ext" that does not exist in the target.
func (e *executor) freeRel(rel string, isDir bool) (string, error) {
parent, name := splitRel(rel)
for n := 1; n <= 10000; n++ {
cand := keepBothName(name, n, isDir)
if parent != "" {
cand = parent + "/" + cand
}
_, err := e.to.Lstat(e.ctx, dstPath(e.to, e.toDir, cand))
if errors.Is(err, vfs.ErrNotExist) {
return cand, nil
}
if err != nil {
return "", err
}
}
return "", fmt.Errorf("no free name for %s", rel)
}
var errStop = errors.New("transfer stopped")
// handleError applies the error policy. It returns errStop to end the
// transfer, nil to skip the item, or errRetry.
func (e *executor) handleError(it Item, op string, err error) error {
if e.ctx.Err() != nil {
return errStop
}
ie := ItemError{Item: it, Op: op, Err: err}
record := func() {
e.rep.Errors = append(e.rep.Errors, ie)
e.rep.Failed++
e.markIncomplete(it.Rel)
}
if e.skipErrors || e.opts.OnError == ErrorSkipAll {
record()
return nil
}
if e.opts.OnError == ErrorAbort {
record()
e.rep.Err = fmt.Errorf("%w: %v", ErrAborted, ie)
return errStop
}
if e.h.OnError == nil {
record()
return nil
}
a, herr := e.h.OnError(e.ctx, ie)
if herr != nil {
record()
if e.ctx.Err() == nil {
e.rep.Err = herr
}
return errStop
}
switch a {
case ErrorRetry:
return errRetry
case ErrorSkipRest:
e.skipErrors = true
case ErrorStop:
record()
e.rep.Err = fmt.Errorf("%w: %v", ErrAborted, ie)
return errStop
}
record()
return nil
}
var errRetry = errors.New("retry")
func isCancel(ctx context.Context, err error) bool {
return ctx.Err() != nil || errors.Is(err, context.Canceled)
}