Files
nfs/internal/nfsfs/sparse_linux_test.go
T

75 lines
2.3 KiB
Go
Raw Normal View History

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
//go:build linux
package nfsfs
import (
"os"
"path/filepath"
"testing"
)
func TestLocalSparse(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "s.bin"), make([]byte, 8192), 0o644); err != nil {
t.Fatal(err)
}
l, err := NewLocal(root)
if err != nil {
t.Fatal(err)
}
rootHandle, err := l.Root()
if err != nil {
t.Fatal(err)
}
fh, _, err := l.Lookup(rootHandle, "s.bin")
if err != nil {
t.Fatal(err)
}
// The file is all data: the only hole is the virtual one every file
// carries at its end, so the seek answers the size with the eof flag,
// RFC 7862 section 15.11.
off, eof, err := l.SeekHole(fh, 0)
if err != nil || off != 8192 || !eof {
t.Fatalf("hole in a full file: %d %v %v", off, eof, err)
}
if off, eof, err := l.SeekData(fh, 0); err != nil || off != 0 || eof {
t.Fatalf("seek data: %d %v %v", off, eof, err)
}
// A seek past the end is NXIO, not an answer.
if _, _, err := l.SeekData(fh, 8193); err != ErrBeyondEOF {
t.Fatalf("seek past the end: %v", err)
}
// Punching a hole moves the first hole to the punched offset.
if err := l.Deallocate(fh, 4096, 4096); err != nil {
t.Fatalf("deallocate: %v", err)
}
off, eof, err = l.SeekHole(fh, 0)
if err != nil || off != 4096 || eof {
t.Fatalf("seek hole: %d %v %v", off, eof, err)
}
// No data follows inside the hole: the answer names the size with
// the eof flag.
if off, eof, err := l.SeekData(fh, 4096); err != nil || off != 8192 || !eof {
t.Fatalf("data in the hole: %d %v %v", off, eof, err)
}
// Allocate reserves the space without moving the hole back.
if err := l.Allocate(fh, 4096, 4096); err != nil {
t.Fatalf("allocate: %v", err)
}
if off, eof, err := l.SeekHole(fh, 0); err != nil || off != 4096 || eof {
t.Fatalf("seek hole after allocate: %d %v %v", off, eof, err)
}
// Allocate past the end grows the file to the end of the reserved
// range, which RFC 7862 section 15.1 requires.
if err := l.Allocate(fh, 8192, 4096); err != nil {
t.Fatalf("allocate past eof: %v", err)
}
info, err := l.Getattr(fh)
if err != nil || info.Size != 12288 {
t.Fatalf("size after the allocate past eof: %d, %v", info.Size, err)
}
}