2021-09-11 11:26:10 +00:00
|
|
|
//go:build darwin || freebsd || linux
|
2020-05-12 09:30:41 +00:00
|
|
|
// +build darwin freebsd linux
|
2015-08-16 20:27:07 +00:00
|
|
|
|
2015-07-26 14:43:42 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-05 21:09:52 +00:00
|
|
|
"context"
|
2015-07-26 14:43:42 +00:00
|
|
|
"math/rand"
|
2020-02-17 15:26:53 +00:00
|
|
|
"os"
|
2023-03-07 21:12:08 +00:00
|
|
|
"strings"
|
2015-07-26 14:43:42 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-09-11 11:26:10 +00:00
|
|
|
"github.com/restic/restic/internal/bloblru"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2022-11-12 13:52:37 +00:00
|
|
|
"github.com/anacrolix/fuse"
|
|
|
|
"github.com/anacrolix/fuse/fs"
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2015-07-26 14:43:42 +00:00
|
|
|
)
|
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
func testRead(t testing.TB, f fs.Handle, offset, length int, data []byte) {
|
2017-01-24 10:41:29 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
req := &fuse.ReadRequest{
|
|
|
|
Offset: int64(offset),
|
|
|
|
Size: length,
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
2017-01-24 10:41:29 +00:00
|
|
|
resp := &fuse.ReadResponse{
|
|
|
|
Data: data,
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
2020-06-14 11:47:13 +00:00
|
|
|
fr := f.(fs.HandleReader)
|
|
|
|
rtest.OK(t, fr.Read(ctx, req, resp))
|
2017-01-24 10:41:29 +00:00
|
|
|
}
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
func firstSnapshotID(t testing.TB, repo restic.Repository) (first restic.ID) {
|
2018-01-21 16:25:36 +00:00
|
|
|
err := repo.List(context.TODO(), restic.SnapshotFile, func(id restic.ID, size int64) error {
|
2017-01-24 10:41:29 +00:00
|
|
|
if first.IsNull() {
|
|
|
|
first = id
|
|
|
|
}
|
2018-01-21 16:25:36 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
2018-01-21 16:25:36 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
return first
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
func loadFirstSnapshot(t testing.TB, repo restic.Repository) *restic.Snapshot {
|
|
|
|
id := firstSnapshotID(t, repo)
|
2017-06-05 21:56:59 +00:00
|
|
|
sn, err := restic.LoadSnapshot(context.TODO(), repo, id)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-01-24 10:41:29 +00:00
|
|
|
return sn
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
func loadTree(t testing.TB, repo restic.Repository, id restic.ID) *restic.Tree {
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(context.TODO(), repo, id)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-01-24 10:41:29 +00:00
|
|
|
return tree
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
func TestFuseFile(t *testing.T) {
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepository(t)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
timestamp, err := time.Parse(time.RFC3339, "2017-01-24T10:42:56+01:00")
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-01-24 10:41:29 +00:00
|
|
|
restic.TestCreateSnapshot(t, repo, timestamp, 2, 0.1)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
sn := loadFirstSnapshot(t, repo)
|
|
|
|
tree := loadTree(t, repo, *sn.Tree)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
var content restic.IDs
|
|
|
|
for _, node := range tree.Nodes {
|
|
|
|
content = append(content, node.Content...)
|
|
|
|
}
|
|
|
|
t.Logf("tree loaded, content: %v", content)
|
|
|
|
|
|
|
|
var (
|
|
|
|
filesize uint64
|
|
|
|
memfile []byte
|
|
|
|
)
|
|
|
|
for _, id := range content {
|
2018-01-12 06:20:12 +00:00
|
|
|
size, found := repo.LookupBlobSize(id, restic.DataBlob)
|
|
|
|
rtest.Assert(t, found, "Expected to find blob id %v", id)
|
2017-01-24 10:41:29 +00:00
|
|
|
filesize += uint64(size)
|
|
|
|
|
2020-03-10 15:41:22 +00:00
|
|
|
buf, err := repo.LoadBlob(context.TODO(), restic.DataBlob, id, nil)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-01-24 10:41:29 +00:00
|
|
|
|
2020-03-10 15:41:22 +00:00
|
|
|
if len(buf) != int(size) {
|
|
|
|
t.Fatalf("not enough bytes read for id %v: want %v, got %v", id.Str(), size, len(buf))
|
2017-01-24 10:41:29 +00:00
|
|
|
}
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
if uint(len(buf)) != size {
|
|
|
|
t.Fatalf("buffer has wrong length for id %v: want %v, got %v", id.Str(), size, len(buf))
|
|
|
|
}
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
memfile = append(memfile, buf...)
|
|
|
|
}
|
|
|
|
|
2017-01-24 10:41:29 +00:00
|
|
|
t.Logf("filesize is %v, memfile has size %v", filesize, len(memfile))
|
|
|
|
|
2015-07-26 14:43:42 +00:00
|
|
|
node := &restic.Node{
|
|
|
|
Name: "foo",
|
|
|
|
Inode: 23,
|
|
|
|
Mode: 0742,
|
2017-01-24 10:41:29 +00:00
|
|
|
Size: filesize,
|
|
|
|
Content: content,
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
2021-09-11 11:26:10 +00:00
|
|
|
root := &Root{repo: repo, blobCache: bloblru.New(blobCacheSize)}
|
2017-06-18 14:29:00 +00:00
|
|
|
|
2022-11-11 09:52:47 +00:00
|
|
|
inode := inodeFromNode(1, node)
|
2022-08-19 18:29:33 +00:00
|
|
|
f, err := newFile(root, inode, node)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2020-06-14 11:47:13 +00:00
|
|
|
of, err := f.Open(context.TODO(), nil, nil)
|
|
|
|
rtest.OK(t, err)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
attr := fuse.Attr{}
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, f.Attr(ctx, &attr))
|
2015-07-26 14:43:42 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Equals(t, inode, attr.Inode)
|
|
|
|
rtest.Equals(t, node.Mode, attr.Mode)
|
|
|
|
rtest.Equals(t, node.Size, attr.Size)
|
|
|
|
rtest.Equals(t, (node.Size/uint64(attr.BlockSize))+1, attr.Blocks)
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
for i := 0; i < 200; i++ {
|
2017-01-24 10:41:29 +00:00
|
|
|
offset := rand.Intn(int(filesize))
|
|
|
|
length := rand.Intn(int(filesize)-offset) + 100
|
2015-07-26 14:43:42 +00:00
|
|
|
|
|
|
|
b := memfile[offset : offset+length]
|
2017-01-24 10:41:29 +00:00
|
|
|
|
2016-09-03 12:03:43 +00:00
|
|
|
buf := make([]byte, length)
|
2017-01-24 10:41:29 +00:00
|
|
|
|
2020-06-14 11:47:13 +00:00
|
|
|
testRead(t, of, offset, length, buf)
|
2016-09-03 12:03:43 +00:00
|
|
|
if !bytes.Equal(b, buf) {
|
2017-01-24 10:41:29 +00:00
|
|
|
t.Errorf("test %d failed, wrong data returned (offset %v, length %v)", i, offset, length)
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 15:26:53 +00:00
|
|
|
|
2020-11-30 22:24:28 +00:00
|
|
|
func TestFuseDir(t *testing.T) {
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepository(t)
|
2020-11-30 22:24:28 +00:00
|
|
|
|
2021-09-11 11:26:10 +00:00
|
|
|
root := &Root{repo: repo, blobCache: bloblru.New(blobCacheSize)}
|
2020-11-30 22:24:28 +00:00
|
|
|
|
|
|
|
node := &restic.Node{
|
|
|
|
Mode: 0755,
|
|
|
|
UID: 42,
|
|
|
|
GID: 43,
|
|
|
|
AccessTime: time.Unix(1606773731, 0),
|
|
|
|
ChangeTime: time.Unix(1606773732, 0),
|
|
|
|
ModTime: time.Unix(1606773733, 0),
|
|
|
|
}
|
2022-11-11 09:52:47 +00:00
|
|
|
parentInode := inodeFromName(0, "parent")
|
|
|
|
inode := inodeFromName(1, "foo")
|
2022-08-19 18:29:33 +00:00
|
|
|
d, err := newDir(root, inode, parentInode, node)
|
2020-11-30 22:24:28 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
// don't open the directory as that would require setting up a proper tree blob
|
|
|
|
attr := fuse.Attr{}
|
|
|
|
rtest.OK(t, d.Attr(context.TODO(), &attr))
|
|
|
|
|
|
|
|
rtest.Equals(t, inode, attr.Inode)
|
|
|
|
rtest.Equals(t, node.UID, attr.Uid)
|
|
|
|
rtest.Equals(t, node.GID, attr.Gid)
|
|
|
|
rtest.Equals(t, node.AccessTime, attr.Atime)
|
|
|
|
rtest.Equals(t, node.ChangeTime, attr.Ctime)
|
|
|
|
rtest.Equals(t, node.ModTime, attr.Mtime)
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:26:53 +00:00
|
|
|
// Test top-level directories for their UID and GID.
|
2021-01-30 19:45:57 +00:00
|
|
|
func TestTopUIDGID(t *testing.T) {
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepository(t)
|
2020-02-17 15:26:53 +00:00
|
|
|
restic.TestCreateSnapshot(t, repo, time.Unix(1460289341, 207401672), 0, 0)
|
|
|
|
|
2021-01-30 19:45:57 +00:00
|
|
|
testTopUIDGID(t, Config{}, repo, uint32(os.Getuid()), uint32(os.Getgid()))
|
|
|
|
testTopUIDGID(t, Config{OwnerIsRoot: true}, repo, 0, 0)
|
2020-02-17 15:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 19:45:57 +00:00
|
|
|
func testTopUIDGID(t *testing.T, cfg Config, repo restic.Repository, uid, gid uint32) {
|
2020-02-17 15:26:53 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-06-14 10:49:39 +00:00
|
|
|
root := NewRoot(repo, cfg)
|
2020-02-17 15:26:53 +00:00
|
|
|
|
|
|
|
var attr fuse.Attr
|
2020-06-17 10:17:55 +00:00
|
|
|
err := root.Attr(ctx, &attr)
|
2020-02-17 15:26:53 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, uid, attr.Uid)
|
|
|
|
rtest.Equals(t, gid, attr.Gid)
|
|
|
|
|
|
|
|
idsdir, err := root.Lookup(ctx, "ids")
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
err = idsdir.Attr(ctx, &attr)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, uid, attr.Uid)
|
|
|
|
rtest.Equals(t, gid, attr.Gid)
|
|
|
|
|
|
|
|
snapID := loadFirstSnapshot(t, repo).ID().Str()
|
|
|
|
snapshotdir, err := idsdir.(fs.NodeStringLookuper).Lookup(ctx, snapID)
|
2020-07-13 04:42:31 +00:00
|
|
|
rtest.OK(t, err)
|
2020-02-17 15:26:53 +00:00
|
|
|
|
2020-11-30 22:24:28 +00:00
|
|
|
// restic.TestCreateSnapshot does not set the UID/GID thus it must be always zero
|
2020-02-17 15:26:53 +00:00
|
|
|
err = snapshotdir.Attr(ctx, &attr)
|
|
|
|
rtest.OK(t, err)
|
2020-11-30 22:24:28 +00:00
|
|
|
rtest.Equals(t, uint32(0), attr.Uid)
|
|
|
|
rtest.Equals(t, uint32(0), attr.Gid)
|
2020-02-17 15:26:53 +00:00
|
|
|
}
|
2022-11-11 09:52:47 +00:00
|
|
|
|
2023-03-07 21:12:08 +00:00
|
|
|
// Test reporting of fuse.Attr.Blocks in multiples of 512.
|
|
|
|
func TestBlocks(t *testing.T) {
|
|
|
|
root := &Root{}
|
|
|
|
|
|
|
|
for _, c := range []struct {
|
|
|
|
size, blocks uint64
|
|
|
|
}{
|
|
|
|
{0, 0},
|
|
|
|
{1, 1},
|
|
|
|
{511, 1},
|
|
|
|
{512, 1},
|
|
|
|
{513, 2},
|
|
|
|
{1024, 2},
|
|
|
|
{1025, 3},
|
|
|
|
{41253, 81},
|
|
|
|
} {
|
|
|
|
target := strings.Repeat("x", int(c.size))
|
|
|
|
|
|
|
|
for _, n := range []fs.Node{
|
|
|
|
&file{root: root, node: &restic.Node{Size: uint64(c.size)}},
|
|
|
|
&link{root: root, node: &restic.Node{LinkTarget: target}},
|
|
|
|
&snapshotLink{root: root, snapshot: &restic.Snapshot{}, target: target},
|
|
|
|
} {
|
|
|
|
var a fuse.Attr
|
|
|
|
err := n.Attr(context.TODO(), &a)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, c.blocks, a.Blocks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 09:52:47 +00:00
|
|
|
func TestInodeFromNode(t *testing.T) {
|
|
|
|
node := &restic.Node{Name: "foo.txt", Type: "chardev", Links: 2}
|
|
|
|
ino1 := inodeFromNode(1, node)
|
|
|
|
ino2 := inodeFromNode(2, node)
|
|
|
|
rtest.Assert(t, ino1 == ino2, "inodes %d, %d of hard links differ", ino1, ino2)
|
|
|
|
|
|
|
|
node.Links = 1
|
|
|
|
ino1 = inodeFromNode(1, node)
|
|
|
|
ino2 = inodeFromNode(2, node)
|
|
|
|
rtest.Assert(t, ino1 != ino2, "same inode %d but different parent", ino1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sink uint64
|
|
|
|
|
|
|
|
func BenchmarkInode(b *testing.B) {
|
|
|
|
for _, sub := range []struct {
|
|
|
|
name string
|
|
|
|
node restic.Node
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no_hard_links",
|
|
|
|
node: restic.Node{Name: "a somewhat long-ish filename.svg.bz2", Type: "fifo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hard_link",
|
|
|
|
node: restic.Node{Name: "some other filename", Type: "file", Links: 2},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
b.Run(sub.name, func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sink = inodeFromNode(1, &sub.node)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|