Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
4.0 KiB
Go
174 lines
4.0 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func mustWrite(t *testing.T, path string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte("identical contents"), 0640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestHardlinkTopologyHandlesSplitAndRejoin(t *testing.T) {
|
|
if _, err := exec.LookPath("rsync"); err != nil {
|
|
t.Skip("rsync is not installed")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
src, dst, scratch := t.TempDir(), t.TempDir(), t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(src, "d"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
a, b := filepath.Join(src, "a"), filepath.Join(src, "d", "b")
|
|
mustWrite(t, a)
|
|
if err := os.Link(a, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
r := Rsync{Exec: Process{Stderr: io.Discard}, Log: io.Discard}
|
|
if err := r.Copy(ctx, src, dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := hardlinkTopology(ctx, src, dst, scratch, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Break one source link without changing bytes, mode or mtime. Rsync -H
|
|
// alone can leave an extra target link in this exact case.
|
|
old, err := os.Stat(b)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = os.Remove(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
mustWrite(t, b)
|
|
if err = os.Chtimes(b, old.ModTime(), old.ModTime()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, false); err == nil {
|
|
t.Fatal("extra destination hardlink was missed")
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = r.Copy(ctx, src, dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
x, _ := os.Stat(filepath.Join(dst, "a"))
|
|
y, _ := os.Stat(filepath.Join(dst, "d", "b"))
|
|
if os.SameFile(x, y) {
|
|
t.Fatal("split source files remained linked")
|
|
}
|
|
|
|
if err = os.Remove(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = os.Link(a, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, false); err == nil {
|
|
t.Fatal("missing target hardlink was missed")
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = r.Copy(ctx, src, dst); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = hardlinkTopology(ctx, src, dst, scratch, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
entries, err := os.ReadDir(scratch)
|
|
if err != nil || len(entries) != 0 {
|
|
t.Fatal("scratch indexes were not removed")
|
|
}
|
|
}
|
|
|
|
func TestHardlinkRepairDoesNotFollowTargetSymlinkDirectories(t *testing.T) {
|
|
src, dst, outside, scratch := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(src, "d"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
mustWrite(t, filepath.Join(src, "d", "a"))
|
|
mustWrite(t, filepath.Join(src, "d", "b"))
|
|
mustWrite(t, filepath.Join(outside, "a"))
|
|
if err := os.Link(filepath.Join(outside, "a"), filepath.Join(outside, "b")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := os.Symlink(outside, filepath.Join(dst, "d")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := hardlinkTopology(context.Background(), src, dst, scratch, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
a, err := os.Stat(filepath.Join(outside, "a"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
b, err := os.Stat(filepath.Join(outside, "b"))
|
|
if err != nil || !os.SameFile(a, b) {
|
|
t.Fatal("repair touched files outside the target tree")
|
|
}
|
|
}
|
|
|
|
func TestHardlinkDiskIndexSurvivesBatchBoundaries(t *testing.T) {
|
|
x, err := openLinkIndex(filepath.Join(t.TempDir(), "index"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer x.close()
|
|
for i := uint64(0); i < 5000; i++ {
|
|
var key, value [16]byte
|
|
binary.BigEndian.PutUint64(key[:8], i)
|
|
binary.BigEndian.PutUint64(value[:8], i+1)
|
|
same, err := x.observe("target", key, value)
|
|
if err != nil || !same {
|
|
t.Fatalf("insert %d: %v", i, err)
|
|
}
|
|
}
|
|
|
|
var key, value [16]byte
|
|
binary.BigEndian.PutUint64(value[:8], 1)
|
|
if same, err := x.observe("target", key, value); err != nil || !same {
|
|
t.Fatalf("committed entry lost: %v", err)
|
|
}
|
|
|
|
value[15] = 1
|
|
if same, err := x.observe("target", key, value); err != nil || same {
|
|
t.Fatalf("conflict missed: %v", err)
|
|
}
|
|
}
|