2022-03-28 20:23:47 +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-19 12:28:11 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
2020-10-05 21:09:52 +00:00
|
|
|
"context"
|
2022-08-19 18:26:35 +00:00
|
|
|
"errors"
|
2015-07-19 12:28:11 +00:00
|
|
|
"os"
|
2017-12-13 19:21:18 +00:00
|
|
|
"path/filepath"
|
2020-06-14 11:47:13 +00:00
|
|
|
"sync"
|
2023-05-18 15:47:42 +00:00
|
|
|
"syscall"
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2022-11-12 13:52:37 +00:00
|
|
|
"github.com/anacrolix/fuse"
|
|
|
|
"github.com/anacrolix/fuse/fs"
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2015-07-19 12:28:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Statically ensure that *dir implement those interface
|
|
|
|
var _ = fs.HandleReadDirAller(&dir{})
|
|
|
|
var _ = fs.NodeStringLookuper(&dir{})
|
|
|
|
|
|
|
|
type dir struct {
|
2017-06-18 15:12:05 +00:00
|
|
|
root *Root
|
|
|
|
items map[string]*restic.Node
|
|
|
|
inode uint64
|
|
|
|
parentInode uint64
|
|
|
|
node *restic.Node
|
2020-06-14 11:47:13 +00:00
|
|
|
m sync.Mutex
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:21:18 +00:00
|
|
|
func cleanupNodeName(name string) string {
|
|
|
|
return filepath.Base(name)
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:29:33 +00:00
|
|
|
func newDir(root *Root, inode, parentInode uint64, node *restic.Node) (*dir, error) {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("new dir for %v (%v)", node.Name, node.Subtree)
|
2015-07-19 12:28:11 +00:00
|
|
|
|
|
|
|
return &dir{
|
2017-06-18 15:12:05 +00:00
|
|
|
root: root,
|
|
|
|
node: node,
|
|
|
|
inode: inode,
|
|
|
|
parentInode: parentInode,
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:26:35 +00:00
|
|
|
// returing a wrapped context.Canceled error will instead result in returing
|
|
|
|
// an input / output error to the user. Thus unwrap the error to match the
|
|
|
|
// expectations of bazil/fuse
|
|
|
|
func unwrapCtxCanceled(err error) error {
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return context.Canceled
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-07 18:50:17 +00:00
|
|
|
// replaceSpecialNodes replaces nodes with name "." and "/" by their contents.
|
|
|
|
// Otherwise, the node is returned.
|
2017-06-04 09:16:55 +00:00
|
|
|
func replaceSpecialNodes(ctx context.Context, repo restic.Repository, node *restic.Node) ([]*restic.Node, error) {
|
2016-09-01 19:20:03 +00:00
|
|
|
if node.Type != "dir" || node.Subtree == nil {
|
2016-02-07 18:50:17 +00:00
|
|
|
return []*restic.Node{node}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Name != "." && node.Name != "/" {
|
|
|
|
return []*restic.Node{node}, nil
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(ctx, repo, *node.Subtree)
|
2016-02-07 18:50:17 +00:00
|
|
|
if err != nil {
|
2022-08-19 18:26:35 +00:00
|
|
|
return nil, unwrapCtxCanceled(err)
|
2016-02-07 18:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree.Nodes, nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:29:33 +00:00
|
|
|
func newDirFromSnapshot(root *Root, inode uint64, snapshot *restic.Snapshot) (*dir, error) {
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("new dir for snapshot %v (%v)", snapshot.ID(), snapshot.Tree)
|
2020-06-14 11:47:13 +00:00
|
|
|
return &dir{
|
|
|
|
root: root,
|
|
|
|
node: &restic.Node{
|
|
|
|
AccessTime: snapshot.Time,
|
|
|
|
ModTime: snapshot.Time,
|
|
|
|
ChangeTime: snapshot.Time,
|
|
|
|
Mode: os.ModeDir | 0555,
|
|
|
|
Subtree: snapshot.Tree,
|
|
|
|
},
|
|
|
|
inode: inode,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) open(ctx context.Context) error {
|
|
|
|
d.m.Lock()
|
|
|
|
defer d.m.Unlock()
|
|
|
|
|
|
|
|
if d.items != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("open dir %v (%v)", d.node.Name, d.node.Subtree)
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(ctx, d.root.repo, *d.node.Subtree)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
2020-06-14 11:47:13 +00:00
|
|
|
debug.Log(" error loading tree %v: %v", d.node.Subtree, err)
|
2022-08-19 18:26:35 +00:00
|
|
|
return unwrapCtxCanceled(err)
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
2015-07-21 19:34:59 +00:00
|
|
|
items := make(map[string]*restic.Node)
|
2016-02-07 18:50:17 +00:00
|
|
|
for _, n := range tree.Nodes {
|
2020-06-14 11:47:13 +00:00
|
|
|
nodes, err := replaceSpecialNodes(ctx, d.root.repo, n)
|
2016-02-07 18:50:17 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" replaceSpecialNodes(%v) failed: %v", n, err)
|
2020-06-14 11:47:13 +00:00
|
|
|
return err
|
2016-02-07 18:50:17 +00:00
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
2017-12-13 19:21:18 +00:00
|
|
|
items[cleanupNodeName(node.Name)] = node
|
2016-02-07 18:50:17 +00:00
|
|
|
}
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
2020-06-14 11:47:13 +00:00
|
|
|
d.items = items
|
|
|
|
return nil
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (d *dir) Attr(_ context.Context, a *fuse.Attr) error {
|
2020-02-17 15:26:08 +00:00
|
|
|
debug.Log("Attr()")
|
2015-07-19 12:28:11 +00:00
|
|
|
a.Inode = d.inode
|
2015-07-21 20:11:30 +00:00
|
|
|
a.Mode = os.ModeDir | d.node.Mode
|
2020-11-30 22:24:28 +00:00
|
|
|
|
|
|
|
if !d.root.cfg.OwnerIsRoot {
|
|
|
|
a.Uid = d.node.UID
|
|
|
|
a.Gid = d.node.GID
|
|
|
|
}
|
2015-07-21 20:11:30 +00:00
|
|
|
a.Atime = d.node.AccessTime
|
|
|
|
a.Ctime = d.node.ChangeTime
|
|
|
|
a.Mtime = d.node.ModTime
|
2017-02-11 20:50:03 +00:00
|
|
|
|
|
|
|
a.Nlink = d.calcNumberOfLinks()
|
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:50:03 +00:00
|
|
|
func (d *dir) calcNumberOfLinks() uint32 {
|
|
|
|
// a directory d has 2 hardlinks + the number
|
|
|
|
// of directories contained by d
|
2020-03-06 22:27:37 +00:00
|
|
|
count := uint32(2)
|
2017-02-11 20:50:03 +00:00
|
|
|
for _, node := range d.items {
|
|
|
|
if node.Type == "dir" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2020-02-17 15:26:08 +00:00
|
|
|
debug.Log("ReadDirAll()")
|
2020-06-14 11:47:13 +00:00
|
|
|
err := d.open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-18 15:12:05 +00:00
|
|
|
ret := make([]fuse.Dirent, 0, len(d.items)+2)
|
|
|
|
|
|
|
|
ret = append(ret, fuse.Dirent{
|
|
|
|
Inode: d.inode,
|
|
|
|
Name: ".",
|
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
})
|
|
|
|
|
|
|
|
ret = append(ret, fuse.Dirent{
|
|
|
|
Inode: d.parentInode,
|
|
|
|
Name: "..",
|
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
})
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2015-07-21 19:34:59 +00:00
|
|
|
for _, node := range d.items {
|
2017-12-13 19:21:18 +00:00
|
|
|
name := cleanupNodeName(node.Name)
|
2015-07-19 12:28:11 +00:00
|
|
|
var typ fuse.DirentType
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2015-07-21 19:23:40 +00:00
|
|
|
case "dir":
|
2015-07-19 12:28:11 +00:00
|
|
|
typ = fuse.DT_Dir
|
2015-07-21 19:23:40 +00:00
|
|
|
case "file":
|
2015-07-19 12:28:11 +00:00
|
|
|
typ = fuse.DT_File
|
2015-07-21 19:25:05 +00:00
|
|
|
case "symlink":
|
|
|
|
typ = fuse.DT_Link
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, fuse.Dirent{
|
2022-11-11 09:52:47 +00:00
|
|
|
Inode: inodeFromNode(d.inode, node),
|
2015-07-19 12:28:11 +00:00
|
|
|
Type: typ,
|
2017-12-13 19:21:18 +00:00
|
|
|
Name: name,
|
2015-07-19 12:28:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Lookup(%v)", name)
|
2020-06-14 11:47:13 +00:00
|
|
|
|
|
|
|
err := d.open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:34:59 +00:00
|
|
|
node, ok := d.items[name]
|
2015-07-19 12:28:11 +00:00
|
|
|
if !ok {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" Lookup(%v) -> not found", name)
|
2023-05-18 15:47:42 +00:00
|
|
|
return nil, syscall.ENOENT
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
2022-11-27 12:53:42 +00:00
|
|
|
inode := inodeFromNode(d.inode, node)
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2015-07-21 19:23:40 +00:00
|
|
|
case "dir":
|
2022-11-27 12:53:42 +00:00
|
|
|
return newDir(d.root, inode, d.inode, node)
|
2015-07-21 19:23:40 +00:00
|
|
|
case "file":
|
2022-11-27 12:53:42 +00:00
|
|
|
return newFile(d.root, inode, node)
|
2015-07-21 19:25:05 +00:00
|
|
|
case "symlink":
|
2022-11-27 12:53:42 +00:00
|
|
|
return newLink(d.root, inode, node)
|
2017-12-03 16:25:00 +00:00
|
|
|
case "dev", "chardev", "fifo", "socket":
|
2022-11-27 12:53:42 +00:00
|
|
|
return newOther(d.root, inode, node)
|
2015-07-19 12:28:11 +00:00
|
|
|
default:
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" node %v has unknown type %v", name, node.Type)
|
2023-05-18 15:47:42 +00:00
|
|
|
return nil, syscall.ENOENT
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-02 11:23:13 +00:00
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (d *dir) Listxattr(_ context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
|
2023-07-08 15:41:45 +00:00
|
|
|
nodeToXattrList(d.node, req, resp)
|
2017-02-02 11:23:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:18:09 +00:00
|
|
|
func (d *dir) Getxattr(_ context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
|
2023-07-08 15:41:45 +00:00
|
|
|
return nodeGetXattr(d.node, req, resp)
|
2017-02-02 11:23:13 +00:00
|
|
|
}
|