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>
189 lines
4.8 KiB
Go
189 lines
4.8 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
type inventorySourceFS struct {
|
|
storage.FS
|
|
calls int
|
|
}
|
|
|
|
func (f *inventorySourceFS) Stat(string) (storage.Node, error) {
|
|
f.calls++
|
|
return storage.Node{}, errors.New("source path lookup must use immutable inventory")
|
|
}
|
|
|
|
type inventoryTargetFS struct{ storage.FS }
|
|
|
|
func (f inventoryTargetFS) Stat(p string) (storage.Node, error) {
|
|
var s syscall.Stat_t
|
|
err := syscall.Lstat(p, &s)
|
|
return storage.Node{Mode: s.Mode, Inode: s.Ino, Nlink: s.Nlink, Size: uint64(s.Size), UID: s.Uid, GID: s.Gid,
|
|
Mtime: time.Unix(s.Mtim.Sec, s.Mtim.Nsec)}, err
|
|
}
|
|
|
|
func (f inventoryTargetFS) OpenDir(p string) (storage.DirectoryStream, error) {
|
|
d, err := os.Open(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &inventoryDir{file: d, path: p}, nil
|
|
}
|
|
|
|
func (inventoryTargetFS) Unlink(p string) error { return syscall.Unlink(p) }
|
|
func (inventoryTargetFS) Rmdir(p string) error { return syscall.Rmdir(p) }
|
|
|
|
type inventoryDir struct {
|
|
file *os.File
|
|
path string
|
|
}
|
|
|
|
func (d *inventoryDir) Next() (*storage.Entry, error) {
|
|
es, err := d.file.ReadDir(1)
|
|
if len(es) == 0 {
|
|
if err == nil {
|
|
err = io.EOF
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
n, err := (inventoryTargetFS{}).Stat(filepath.Join(d.path, es[0].Name()))
|
|
return &storage.Entry{Name: es[0].Name(), Node: n}, err
|
|
}
|
|
|
|
func (d *inventoryDir) Close() error { return d.file.Close() }
|
|
|
|
func TestNativeInventoryPruneAndVerifyWithoutSourceLookups(t *testing.T) {
|
|
x, err := createNativeIndex(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer x.close()
|
|
root, outside := t.TempDir(), t.TempDir()
|
|
for _, dir := range []string{"dir", "obsolete"} {
|
|
if err := os.Mkdir(filepath.Join(root, dir), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
for _, rel := range []string{"keep", "dir/file", "obsolete/nested"} {
|
|
if err := os.WriteFile(filepath.Join(root, rel), []byte("data"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join(outside, "sentinel"), []byte("untouched"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, rel := range []string{"link", "obsolete-link"} {
|
|
if err := os.Symlink(outside, filepath.Join(root, rel)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
for _, rel := range []string{".", "keep", "dir", "dir/file", "link"} {
|
|
if err := x.put("paths", []byte(rel), encodeEntry(baseEntry{Inode: 1})); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
source := &inventorySourceFS{}
|
|
b := nativeTransport{source: source, target: inventoryTargetFS{}, pending: x, round: Round{SourcePath: "/immutable"}}
|
|
if err := b.prune(context.Background(), "/immutable", root); err == nil {
|
|
t.Fatal("pruned without complete inventory")
|
|
}
|
|
|
|
if _, err := os.Stat(filepath.Join(root, "obsolete/nested")); err != nil {
|
|
t.Fatal("incomplete inventory deleted target data")
|
|
}
|
|
|
|
if err := x.put("meta", []byte("inventory_complete"), []byte{1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := b.verifyInventory(context.Background(), root); err == nil {
|
|
t.Fatal("unexpected entries were not detected")
|
|
}
|
|
|
|
if err := b.prune(context.Background(), "/immutable", root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := b.verifyInventory(context.Background(), root); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := os.Lstat(filepath.Join(root, "obsolete")); !os.IsNotExist(err) {
|
|
t.Fatal("obsolete directory survived", err)
|
|
}
|
|
|
|
if _, err := os.Lstat(filepath.Join(root, "obsolete-link")); !os.IsNotExist(err) {
|
|
t.Fatal("obsolete symlink survived", err)
|
|
}
|
|
|
|
if data, err := os.ReadFile(filepath.Join(outside, "sentinel")); err != nil || string(data) != "untouched" {
|
|
t.Fatal("followed symlink outside target")
|
|
}
|
|
|
|
if source.calls != 0 {
|
|
t.Fatalf("performed %d redundant source stats", source.calls)
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join(root, "dir/foreign"), nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := b.verifyInventory(context.Background(), root); err == nil {
|
|
t.Fatal("extra target path was ignored")
|
|
}
|
|
}
|
|
|
|
func TestNativeInventoryRejectsCorruptionAndUnchangedIndexIsReadOnly(t *testing.T) {
|
|
dir := t.TempDir()
|
|
x, err := createNativeIndex(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer x.close()
|
|
if err := x.put("paths", []byte("broken"), []byte{1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := x.containsPath("broken"); err == nil {
|
|
t.Fatal("corrupt source inventory accepted")
|
|
}
|
|
|
|
entry := baseEntry{Inode: 1, Version: 2, Size: 128, TargetInode: 3}
|
|
if err := x.putEntryIfChanged("file", entry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := x.commit(dir, "snapshot"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := x.putEntryIfChanged("file", entry); err != nil {
|
|
t.Fatal("unchanged entry attempted a write", err)
|
|
}
|
|
|
|
entry.TargetInode++
|
|
if err := x.putEntryIfChanged("file", entry); err == nil {
|
|
t.Fatal("changed entry silently ignored on read-only index")
|
|
}
|
|
}
|