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
|
2017-06-18 15:02:07 +00:00
|
|
|
|
2017-06-18 12:59:44 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
2020-10-05 21:09:52 +00:00
|
|
|
"context"
|
2017-06-18 12:59:44 +00:00
|
|
|
"os"
|
2020-09-02 19:27:24 +00:00
|
|
|
"strings"
|
2017-06-18 12:59:44 +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"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2017-06-18 12:59:44 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
|
|
|
)
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// SnapshotsDir is a actual fuse directory generated from SnapshotsDirStructure
|
|
|
|
// It uses the saved prefix to filter out the relevant subtrees or entries
|
|
|
|
// from SnapshotsDirStructure.names and .latest, respectively.
|
2017-06-18 15:06:27 +00:00
|
|
|
type SnapshotsDir struct {
|
2020-09-02 19:27:24 +00:00
|
|
|
root *Root
|
|
|
|
inode uint64
|
|
|
|
dirStruct *SnapshotsDirStructure
|
|
|
|
prefix string
|
2017-06-18 12:59:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:34:19 +00:00
|
|
|
// ensure that *SnapshotsDir implements these interfaces
|
2017-06-18 15:06:27 +00:00
|
|
|
var _ = fs.HandleReadDirAller(&SnapshotsDir{})
|
|
|
|
var _ = fs.NodeStringLookuper(&SnapshotsDir{})
|
2017-06-18 12:59:44 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// NewSnapshotsDir returns a new directory structure containing snapshots and "latest" links
|
|
|
|
func NewSnapshotsDir(root *Root, inode uint64, dirStruct *SnapshotsDirStructure, prefix string) *SnapshotsDir {
|
2017-10-05 12:55:55 +00:00
|
|
|
debug.Log("create snapshots dir, inode %d", inode)
|
2020-09-02 19:27:24 +00:00
|
|
|
return &SnapshotsDir{
|
|
|
|
root: root,
|
|
|
|
inode: inode,
|
|
|
|
dirStruct: dirStruct,
|
|
|
|
prefix: prefix,
|
2017-10-05 12:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// Attr returns the attributes for any dir in the snapshots directory structure
|
2017-06-18 15:06:27 +00:00
|
|
|
func (d *SnapshotsDir) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2017-06-18 12:59:44 +00:00
|
|
|
attr.Inode = d.inode
|
|
|
|
attr.Mode = os.ModeDir | 0555
|
2020-02-17 15:26:53 +00:00
|
|
|
attr.Uid = d.root.uid
|
|
|
|
attr.Gid = d.root.gid
|
2017-06-18 12:59:44 +00:00
|
|
|
|
|
|
|
debug.Log("attr: %v", attr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:55:55 +00:00
|
|
|
// ReadDirAll returns all entries of the SnapshotsDir.
|
2017-06-18 15:06:27 +00:00
|
|
|
func (d *SnapshotsDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2017-06-18 12:59:44 +00:00
|
|
|
debug.Log("ReadDirAll()")
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2017-10-08 15:47:10 +00:00
|
|
|
// update snapshots
|
2020-09-02 19:27:24 +00:00
|
|
|
err := d.dirStruct.updateSnapshots(ctx)
|
2021-01-30 15:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2017-06-18 12:59:44 +00:00
|
|
|
items := []fuse.Dirent{
|
|
|
|
{
|
|
|
|
Inode: d.inode,
|
|
|
|
Name: ".",
|
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Inode: d.root.inode,
|
|
|
|
Name: "..",
|
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// map to ensure that all names are only listed once
|
|
|
|
hasName := make(map[string]struct{})
|
|
|
|
|
|
|
|
for name := range d.dirStruct.names {
|
|
|
|
if !strings.HasPrefix(name, d.prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
shortname := strings.Split(name[len(d.prefix):], "/")[0]
|
|
|
|
if shortname == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := hasName[shortname]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasName[shortname] = struct{}{}
|
2017-06-18 12:59:44 +00:00
|
|
|
items = append(items, fuse.Dirent{
|
2020-09-02 19:27:24 +00:00
|
|
|
Inode: fs.GenerateDynamicInode(d.inode, shortname),
|
|
|
|
Name: shortname,
|
2017-06-18 12:59:44 +00:00
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:44:03 +00:00
|
|
|
// Latest
|
2020-09-02 19:27:24 +00:00
|
|
|
if _, ok := d.dirStruct.latest[d.prefix]; ok {
|
2017-09-14 17:44:03 +00:00
|
|
|
items = append(items, fuse.Dirent{
|
|
|
|
Inode: fs.GenerateDynamicInode(d.inode, "latest"),
|
|
|
|
Name: "latest",
|
|
|
|
Type: fuse.DT_Link,
|
|
|
|
})
|
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// Lookup returns a specific entry from the SnapshotsDir.
|
|
|
|
func (d *SnapshotsDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
|
|
|
debug.Log("Lookup(%s)", name)
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
err := d.dirStruct.updateSnapshots(ctx)
|
2021-01-30 15:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
fullname := d.prefix + name
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// check if this is already a complete snapshot path
|
|
|
|
sn := d.dirStruct.names[fullname]
|
|
|
|
if sn != nil {
|
|
|
|
return newDirFromSnapshot(ctx, d.root, fs.GenerateDynamicInode(d.inode, name), sn)
|
2017-10-05 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// handle latest case
|
|
|
|
if name == "latest" {
|
|
|
|
link := d.dirStruct.latest[d.prefix]
|
|
|
|
sn := d.dirStruct.names[d.prefix+link]
|
|
|
|
if sn != nil {
|
|
|
|
return newSnapshotLink(ctx, d.root, fs.GenerateDynamicInode(d.inode, name), link, sn)
|
|
|
|
}
|
2021-01-30 15:32:00 +00:00
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// check if this is a valid subdir
|
|
|
|
fullname = fullname + "/"
|
|
|
|
for name := range d.dirStruct.names {
|
|
|
|
if strings.HasPrefix(name, fullname) {
|
|
|
|
return NewSnapshotsDir(d.root, fs.GenerateDynamicInode(d.inode, name), d.dirStruct, fullname), nil
|
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2017-10-05 12:55:55 +00:00
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
// SnapshotLink
|
|
|
|
type snapshotLink struct {
|
|
|
|
root *Root
|
|
|
|
inode uint64
|
|
|
|
target string
|
|
|
|
snapshot *restic.Snapshot
|
2017-09-14 17:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:27:24 +00:00
|
|
|
var _ = fs.NodeReadlinker(&snapshotLink{})
|
|
|
|
|
2017-10-05 12:55:55 +00:00
|
|
|
// newSnapshotLink
|
2017-09-14 17:44:03 +00:00
|
|
|
func newSnapshotLink(ctx context.Context, root *Root, inode uint64, target string, snapshot *restic.Snapshot) (*snapshotLink, error) {
|
|
|
|
return &snapshotLink{root: root, inode: inode, target: target, snapshot: snapshot}, nil
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:55:55 +00:00
|
|
|
// Readlink
|
2017-09-14 17:44:03 +00:00
|
|
|
func (l *snapshotLink) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
|
|
|
|
return l.target, nil
|
|
|
|
}
|
|
|
|
|
2017-10-05 12:55:55 +00:00
|
|
|
// Attr
|
2017-09-14 17:44:03 +00:00
|
|
|
func (l *snapshotLink) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
a.Inode = l.inode
|
|
|
|
a.Mode = os.ModeSymlink | 0777
|
2022-03-18 03:21:47 +00:00
|
|
|
a.Size = uint64(len(l.target))
|
|
|
|
a.Blocks = 1 + a.Size/blockSize
|
2020-02-17 15:26:53 +00:00
|
|
|
a.Uid = l.root.uid
|
|
|
|
a.Gid = l.root.gid
|
2017-09-14 17:44:03 +00:00
|
|
|
a.Atime = l.snapshot.Time
|
|
|
|
a.Ctime = l.snapshot.Time
|
|
|
|
a.Mtime = l.snapshot.Time
|
|
|
|
|
|
|
|
a.Nlink = 1
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|