96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|||
|
|
// SPDX-License-Identifier: MIT
|
||
|
|
|
||
|
|
//go:build linux
|
||
|
|
|
||
|
|
package nfsfs
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"syscall"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// cloneTestFS builds a backend over a fresh directory holding a source
|
||
|
|
// file of the given content.
|
||
|
|
func cloneTestFS(t *testing.T, content []byte) (*Local, Handle, Handle, string) {
|
||
|
|
t.Helper()
|
||
|
|
root := t.TempDir()
|
||
|
|
l, err := NewLocal(root)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("NewLocal: %v", err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "src"), content, 0o644); err != nil {
|
||
|
|
t.Fatalf("WriteFile: %v", err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "dst"), make([]byte, len(content)), 0o644); err != nil {
|
||
|
|
t.Fatalf("WriteFile: %v", err)
|
||
|
|
}
|
||
|
|
rh, err := l.Root()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Root: %v", err)
|
||
|
|
}
|
||
|
|
src, _, err := l.Lookup(rh, "src")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Lookup src: %v", err)
|
||
|
|
}
|
||
|
|
dst, _, err := l.Lookup(rh, "dst")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Lookup dst: %v", err)
|
||
|
|
}
|
||
|
|
return l, src, dst, filepath.Join(root, "dst")
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestCopyRangeKernel drives the kernel copy and verifies the bytes
|
||
|
|
// landed. Filesystems that refuse the syscall for their files skip the
|
||
|
|
// test: the handler falls back to its userspace path either way.
|
||
|
|
func TestCopyRangeKernel(t *testing.T) {
|
||
|
|
content := make([]byte, 1<<20)
|
||
|
|
for i := range content {
|
||
|
|
content[i] = byte(i * 3)
|
||
|
|
}
|
||
|
|
l, src, dst, dstPath := cloneTestFS(t, content)
|
||
|
|
if err := l.CopyRange(src, 0, dst, 0, int64(len(content))); err != nil {
|
||
|
|
if errors.Is(err, syscall.ENOSYS) || errors.Is(err, syscall.EOPNOTSUPP) ||
|
||
|
|
errors.Is(err, syscall.EXDEV) || errors.Is(err, syscall.EINVAL) {
|
||
|
|
t.Skipf("the filesystem refuses copy_file_range: %v", err)
|
||
|
|
}
|
||
|
|
t.Fatalf("CopyRange: %v", err)
|
||
|
|
}
|
||
|
|
got, err := os.ReadFile(dstPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("ReadFile: %v", err)
|
||
|
|
}
|
||
|
|
if !bytes.Equal(got, content) {
|
||
|
|
t.Fatalf("the copied bytes differ: %d vs %d", len(got), len(content))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestCloneRangeKernel drives the reflink where the filesystem provides
|
||
|
|
// one, and verifies the clone reads back as the source.
|
||
|
|
func TestCloneRangeKernel(t *testing.T) {
|
||
|
|
content := make([]byte, 1<<20)
|
||
|
|
for i := range content {
|
||
|
|
content[i] = byte(i * 7)
|
||
|
|
}
|
||
|
|
l, src, dst, dstPath := cloneTestFS(t, content)
|
||
|
|
err := l.CloneRange(src, 0, dst, 0, int64(len(content)))
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, syscall.EOPNOTSUPP) || errors.Is(err, syscall.EXDEV) ||
|
||
|
|
errors.Is(err, syscall.EINVAL) || errors.Is(err, syscall.EBADF) {
|
||
|
|
t.Skipf("the filesystem refuses FICLONERANGE: %v", err)
|
||
|
|
}
|
||
|
|
t.Fatalf("CloneRange: %v", err)
|
||
|
|
}
|
||
|
|
got, err := os.ReadFile(dstPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("ReadFile: %v", err)
|
||
|
|
}
|
||
|
|
if !bytes.Equal(got, content) {
|
||
|
|
t.Fatalf("the cloned bytes differ: %d vs %d", len(got), len(content))
|
||
|
|
}
|
||
|
|
}
|