vcom/internal/transfer/move.go
vrubelroman b3f9452a1d feat(transfer): move by rename or copy+delete per item (3.5)
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>
2026-11-01 04:04:31 +03:00

158 lines
4 KiB
Go

package transfer
import (
"errors"
"fmt"
"strings"
"vcom/internal/vfs"
)
// renameWhole is the fast path of a move (design §4.3): with SameConn, a
// top-level item that has no target yet is renamed as a whole (NoReplace).
// Anything that cannot be renamed — EXDEV, a target that appeared, any
// other error — falls back to the per-item copy+delete path. It returns the
// items still to process.
func (e *executor) renameWhole(items []Item) []Item {
fb, ok := e.plan.Req.From.(FSBacked)
if !ok {
e.rep.Err = errors.New("transfer: move needs a file-system source")
return nil
}
if !e.plan.SameConn {
return items
}
src := fb.FS()
var rest []Item
for i := 0; i < len(items); {
root := items[i]
end := i + 1
for end < len(items) && strings.HasPrefix(items[end].Rel, root.Rel+"/") {
end++
}
if e.ctx.Err() != nil {
return append(rest, items[i:]...)
}
if root.Existing == nil && root.Src != "" && root.ScanErr == nil && !hasScanErr(items[i:end]) {
dst := dstPath(e.to, e.toDir, root.Rel)
if err := e.to.Rename(e.ctx, root.Src, dst, vfs.NoReplace); err == nil {
for _, it := range items[i:end] {
if it.Kind == File || it.Kind == Symlink {
e.prog.p.FilesDone++
e.doneBytes += it.Size
e.rep.Done++
}
}
e.prog.p.BytesDone = e.doneBytes
e.prog.emit(false)
e.parentAffected(root.Rel)
e.srcAffected(src.Paths().Dir(root.Src))
i = end
continue
}
// EXDEV (another device), ErrExist (appeared) or anything else:
// copy and delete item by item instead.
}
rest = append(rest, items[i:end]...)
i = end
}
return rest
}
func hasScanErr(items []Item) bool {
for _, it := range items {
if it.ScanErr != nil {
return true
}
}
return false
}
func (e *executor) srcFS() vfs.FS { return e.plan.Req.From.(FSBacked).FS() }
func (e *executor) srcAffected(dir string) {
if e.srcDirsAffected == nil {
e.srcDirsAffected = map[string]bool{}
}
e.srcDirsAffected[dir] = true
}
// removeSource deletes a file or link from the source after it was copied.
// Remove never follows links (N-P0-3 for move).
func (e *executor) removeSource(it Item) error {
src := e.srcFS()
for {
err := src.Remove(e.ctx, it.Src)
if err == nil || errors.Is(err, vfs.ErrNotExist) {
e.srcAffected(src.Paths().Dir(it.Src))
return nil
}
switch r := e.handleError(it, "remove", err); r {
case errRetry:
continue
case nil:
return nil
default:
return r
}
}
}
// removeSourceDirs removes moved source directories bottom-up, only those
// with nothing left behind (skipped, failed or special items stay).
func (e *executor) removeSourceDirs() {
if len(e.srcDirs) == 0 {
return
}
src := e.srcFS()
e.prog.p.Phase = Removing
e.prog.emit(true)
for i := len(e.srcDirs) - 1; i >= 0; i-- {
it := e.srcDirs[i]
if e.incomplete[it.Rel] || e.skipped[it.Rel] || e.underSkipped(it.Rel) {
continue
}
err := src.Remove(e.ctx, it.Src)
if err == nil || errors.Is(err, vfs.ErrNotExist) {
e.srcAffected(src.Paths().Dir(it.Src))
continue
}
if e.ctx.Err() != nil {
return
}
e.rep.Errors = append(e.rep.Errors, ItemError{Item: it, Op: "remove",
Err: fmt.Errorf("source directory not removed: %w", err)})
e.rep.Failed++
e.markIncomplete(it.Rel)
}
}
func (e *executor) addSourceAffected() {
src := e.srcFS()
for _, d := range sortedKeys(e.srcDirsAffected) {
e.rep.Affected = append(e.rep.Affected, vfs.Location{FS: src, Path: d})
}
}
// renameItem moves one file or link inside a merged directory by rename
// when source and target share the namespace. It reports whether it did;
// on any failure (EXDEV, a target that appeared, ...) the caller copies.
func (e *executor) renameItem(it Item, rel string, replace bool) bool {
if it.Src == "" {
return false
}
mode := vfs.NoReplace
if replace {
if !e.to.Caps().AtomicReplace {
return false
}
mode = vfs.Replace
}
if err := e.to.Rename(e.ctx, it.Src, dstPath(e.to, e.toDir, rel), mode); err != nil {
return false
}
e.parentAffected(rel)
e.srcAffected(e.srcFS().Paths().Dir(it.Src))
e.finishFile(it, true)
return true
}