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>
227 lines
8.3 KiB
Go
227 lines
8.3 KiB
Go
package transfer
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"vcom/internal/testutil/fstree"
|
|
"vcom/internal/vfs"
|
|
"vcom/internal/vfs/vfstest"
|
|
)
|
|
|
|
func inode(t *testing.T, p string) uint64 {
|
|
t.Helper()
|
|
fi, err := os.Lstat(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return fi.Sys().(*syscall.Stat_t).Ino
|
|
}
|
|
|
|
func gone(t *testing.T, p string) {
|
|
t.Helper()
|
|
if _, err := os.Lstat(p); !os.IsNotExist(err) {
|
|
t.Errorf("%s still exists", p)
|
|
}
|
|
}
|
|
|
|
func TestMoveSameVolumeIsRename(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("proj/big", "").WithSize(1 << 20), fstree.File("f.txt", "f")},
|
|
fstree.Spec{fstree.File("merge/keep", "k")})
|
|
inoDir, inoFile := inode(t, filepath.Join(src, "proj")), inode(t, filepath.Join(src, "f.txt"))
|
|
c := &counter{}
|
|
to := &vfstest.FaultFS{FS: local, Hook: c.hook}
|
|
plan, rep := run(t, req(Move, local, join(src, "proj", "f.txt"), to, dst), Hooks{})
|
|
if !plan.SameConn || rep.Err != nil || rep.Done != 2 {
|
|
t.Fatalf("plan.SameConn=%v report %+v", plan.SameConn, rep)
|
|
}
|
|
if inode(t, filepath.Join(dst, "proj")) != inoDir || inode(t, filepath.Join(dst, "f.txt")) != inoFile {
|
|
t.Fatal("same-volume move copied instead of renaming (inode changed)")
|
|
}
|
|
if c.get(vfstest.OpCreate) != 0 || c.get(vfstest.OpWrite) != 0 {
|
|
t.Fatalf("data was written: create=%d write=%d", c.get(vfstest.OpCreate), c.get(vfstest.OpWrite))
|
|
}
|
|
gone(t, filepath.Join(src, "proj"))
|
|
gone(t, filepath.Join(src, "f.txt"))
|
|
|
|
// Merging into an existing directory renames file by file.
|
|
fstree.Build(t, src, fstree.Spec{fstree.File("merge/new", "n")})
|
|
ino := inode(t, filepath.Join(src, "merge", "new"))
|
|
if _, rep := run(t, req(Move, local, join(src, "merge"), local, dst), Hooks{}); rep.Done != 1 || rep.Err != nil {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
if inode(t, filepath.Join(dst, "merge", "new")) != ino || read(t, filepath.Join(dst, "merge", "keep")) != "k" {
|
|
t.Fatal("merge move")
|
|
}
|
|
gone(t, filepath.Join(src, "merge"))
|
|
}
|
|
|
|
func TestMoveFallsBackOnEXDEV(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("d/a", "aaa"), fstree.Symlink("d/l", "a")}, nil)
|
|
to := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, p string) error {
|
|
if op == vfstest.OpRename && strings.HasPrefix(p, src+"/") {
|
|
return &os.LinkError{Op: "rename", Old: p, Err: syscall.EXDEV}
|
|
}
|
|
return nil
|
|
}}
|
|
_, rep := run(t, req(Move, local, join(src, "d"), to, dst), Hooks{})
|
|
if rep.Err != nil || rep.Done != 2 || rep.Failed != 0 {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
if read(t, filepath.Join(dst, "d", "a")) != "aaa" {
|
|
t.Fatal("not copied")
|
|
}
|
|
gone(t, filepath.Join(src, "d"))
|
|
noLeftovers(t, dst)
|
|
}
|
|
|
|
func TestMoveSkippedItemStaysInSource(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("d/conflict", "mine"), fstree.File("d/free", "f"), fstree.File("d/sub/x", "x")},
|
|
fstree.Spec{fstree.File("d/conflict", "theirs")})
|
|
for _, to := range []vfs.FS{local, otherVolume{local}} {
|
|
_, rep := run(t, req(Move, local, join(src, "d"), to, dst, func(o *Options) { o.Conflict = Skip }), Hooks{})
|
|
if rep.Err != nil || rep.Skipped != 1 || rep.Done != 2 {
|
|
t.Fatalf("%T: %+v", to, rep)
|
|
}
|
|
fstree.Equal(t, fstree.FromSpec(fstree.Spec{fstree.File("d/conflict", "mine")}), fstree.Snapshot(t, src), fstree.Opts{})
|
|
if read(t, filepath.Join(dst, "d", "conflict")) != "theirs" || read(t, filepath.Join(dst, "d", "sub", "x")) != "x" {
|
|
t.Fatal("target wrong")
|
|
}
|
|
// Reset for the second (cross-volume) round.
|
|
fstree.Build(t, src, fstree.Spec{fstree.File("d/free", "f"), fstree.File("d/sub/x", "x")})
|
|
_ = os.RemoveAll(filepath.Join(dst, "d", "sub"))
|
|
_ = os.Remove(filepath.Join(dst, "d", "free"))
|
|
}
|
|
}
|
|
|
|
func TestMoveFailedItemStaysInSource(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("d/bad", "b"), fstree.File("d/good", "g")}, nil)
|
|
to := &vfstest.FaultFS{FS: otherVolume{local}, Hook: func(op vfstest.Op, p string) error {
|
|
if op == vfstest.OpCreate && hasSuffixName(p, "bad") {
|
|
return syscall.EIO
|
|
}
|
|
return nil
|
|
}}
|
|
_, rep := run(t, req(Move, local, join(src, "d"), to, dst, skipAll), Hooks{})
|
|
if rep.Failed != 1 || rep.Done != 1 || rep.Errors[0].Item.Rel != "d/bad" {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
if read(t, filepath.Join(src, "d", "bad")) != "b" {
|
|
t.Fatal("failed item removed from the source")
|
|
}
|
|
gone(t, filepath.Join(src, "d", "good"))
|
|
gone(t, filepath.Join(dst, "d", "bad"))
|
|
noLeftovers(t, dst)
|
|
|
|
// A failed removal of the source is reported, the copy stays.
|
|
src2, dst2 := layout(t, fstree.Spec{fstree.File("e/f", "f")}, nil)
|
|
from := &vfstest.FaultFS{FS: local, Hook: func(op vfstest.Op, _ string) error {
|
|
if op == vfstest.OpRemove {
|
|
return syscall.EACCES
|
|
}
|
|
return nil
|
|
}}
|
|
plan, err := Prepare(bg, Request{Op: Move, From: FSSource(from, join(src2, "e"), CopyLinks), To: otherVolume{local}, ToDir: dst2, Opts: DefaultOptions()}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rep = Execute(bg, plan, Hooks{OnError: func(context.Context, ItemError) (ErrorAction, error) { return ErrorSkip, nil }})
|
|
if rep.Failed != 1 || rep.Errors[0].Op != "remove" || read(t, filepath.Join(dst2, "e", "f")) != "f" || read(t, filepath.Join(src2, "e", "f")) != "f" {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
}
|
|
|
|
func TestMoveDirWithSymlinkKeepsLink(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{
|
|
fstree.File("target/data", "precious"),
|
|
fstree.Symlink("d/link", "../target"),
|
|
fstree.Symlink("d/broken", "nowhere"),
|
|
fstree.File("d/f", "f"),
|
|
}, nil)
|
|
_, rep := run(t, req(Move, local, join(src, "d"), otherVolume{local}, dst, follow), Hooks{}) // follow is ignored for move
|
|
if rep.Err != nil || rep.Failed != 0 || rep.Done != 3 {
|
|
t.Fatalf("%+v", rep)
|
|
}
|
|
for name, want := range map[string]string{"link": "../target", "broken": "nowhere"} {
|
|
if tgt, err := os.Readlink(filepath.Join(dst, "d", name)); err != nil || tgt != want {
|
|
t.Fatalf("%s: %q %v (N-P1-8: link lost in move)", name, tgt, err)
|
|
}
|
|
}
|
|
gone(t, filepath.Join(src, "d"))
|
|
if read(t, filepath.Join(src, "target", "data")) != "precious" {
|
|
t.Fatal("link target damaged")
|
|
}
|
|
}
|
|
|
|
func TestMoveNeverFollowsSourceLinks(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{
|
|
fstree.File("outside/keep1", "1"),
|
|
fstree.File("outside/sub/keep2", "2"),
|
|
fstree.Symlink("rootlink", "outside"),
|
|
fstree.Symlink("d/inner", "../outside"),
|
|
fstree.File("d/own", "o"),
|
|
}, nil)
|
|
for _, to := range []vfs.FS{otherVolume{local}, local} {
|
|
_, rep := run(t, req(Move, local, join(src, "rootlink", "d"), to, dst), Hooks{})
|
|
if rep.Err != nil || rep.Failed != 0 {
|
|
t.Fatalf("%T: %+v", to, rep)
|
|
}
|
|
fstree.Equal(t, fstree.FromSpec(fstree.Spec{fstree.File("outside/keep1", "1"), fstree.File("outside/sub/keep2", "2")}),
|
|
fstree.Snapshot(t, src), fstree.Opts{Links: true})
|
|
if fi, err := os.Lstat(filepath.Join(dst, "rootlink")); err != nil || fi.Mode()&fs.ModeSymlink == 0 {
|
|
t.Fatal("root link not moved as a link")
|
|
}
|
|
// Back for the second round.
|
|
_ = os.RemoveAll(filepath.Join(dst, "d"))
|
|
_ = os.Remove(filepath.Join(dst, "rootlink"))
|
|
fstree.Build(t, src, fstree.Spec{fstree.Symlink("rootlink", "outside"), fstree.Symlink("d/inner", "../outside"), fstree.File("d/own", "o")})
|
|
}
|
|
}
|
|
|
|
func TestMoveAffectedIncludesSource(t *testing.T) {
|
|
src, dst := layout(t, fstree.Spec{fstree.File("d/sub/f", "f"), fstree.File("top", "t")}, nil)
|
|
to := otherVolume{local}
|
|
_, rep := run(t, req(Move, local, join(src, "d", "top"), to, dst), Hooks{})
|
|
if rep.Err != nil {
|
|
t.Fatal(rep.Err)
|
|
}
|
|
var srcDirs, dstDirs []string
|
|
for _, l := range rep.Affected {
|
|
if l.FS == vfs.FS(local) {
|
|
srcDirs = append(srcDirs, l.Path)
|
|
} else {
|
|
dstDirs = append(dstDirs, l.Path)
|
|
}
|
|
}
|
|
sort.Strings(srcDirs)
|
|
if len(srcDirs) == 0 || srcDirs[0] != src {
|
|
t.Fatalf("source dir not affected: %v", srcDirs)
|
|
}
|
|
if len(dstDirs) == 0 || dstDirs[0] != dst {
|
|
t.Fatalf("target dir not affected: %v", dstDirs)
|
|
}
|
|
// Same-volume rename path too.
|
|
src, dst = layout(t, fstree.Spec{fstree.File("x", "x")}, nil)
|
|
_, rep = run(t, req(Move, local, join(src, "x"), local, dst), Hooks{})
|
|
found := false
|
|
for _, l := range rep.Affected {
|
|
found = found || l.Path == src
|
|
}
|
|
if !found {
|
|
t.Fatalf("rename move: source not in Affected %v", rep.Affected)
|
|
}
|
|
}
|
|
|
|
func TestMoveRejectsNonFSSourceAndSelf(t *testing.T) {
|
|
src, _ := layout(t, fstree.Spec{fstree.File("d/f", "")}, nil)
|
|
if _, err := Prepare(bg, req(Move, local, join(src, "d"), local, filepath.Join(src, "d")), nil); !errors.Is(err, ErrIntoItself) {
|
|
t.Fatalf("move into itself: %v", err)
|
|
}
|
|
}
|