2015-07-19 12:28:11 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Statically ensure that *dir implement those interface
|
|
|
|
var _ = fs.HandleReadDirAller(&dir{})
|
|
|
|
var _ = fs.NodeStringLookuper(&dir{})
|
|
|
|
|
|
|
|
type dir struct {
|
2015-07-21 19:34:59 +00:00
|
|
|
repo *repository.Repository
|
|
|
|
items map[string]*restic.Node
|
|
|
|
inode uint64
|
2015-07-21 20:11:30 +00:00
|
|
|
node *restic.Node
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDir(repo *repository.Repository, node *restic.Node) (*dir, error) {
|
2015-07-25 15:05:45 +00:00
|
|
|
tree, err := restic.LoadTree(repo, *node.Subtree)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 19:34:59 +00:00
|
|
|
items := make(map[string]*restic.Node)
|
|
|
|
for _, node := range tree.Nodes {
|
|
|
|
items[node.Name] = node
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &dir{
|
2015-07-21 19:34:59 +00:00
|
|
|
repo: repo,
|
2015-07-21 20:11:30 +00:00
|
|
|
node: node,
|
2015-07-21 19:34:59 +00:00
|
|
|
items: items,
|
|
|
|
inode: node.Inode,
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDirFromSnapshot(repo *repository.Repository, snapshot SnapshotWithId) (*dir, error) {
|
2015-07-25 15:05:45 +00:00
|
|
|
tree, err := restic.LoadTree(repo, *snapshot.Tree)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 19:34:59 +00:00
|
|
|
items := make(map[string]*restic.Node)
|
2015-07-19 12:28:11 +00:00
|
|
|
for _, node := range tree.Nodes {
|
2015-07-21 19:34:59 +00:00
|
|
|
items[node.Name] = node
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &dir{
|
2015-07-21 20:11:30 +00:00
|
|
|
repo: repo,
|
|
|
|
node: &restic.Node{
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
AccessTime: snapshot.Time,
|
|
|
|
ModTime: snapshot.Time,
|
|
|
|
ChangeTime: snapshot.Time,
|
|
|
|
Mode: os.ModeDir | 0555,
|
|
|
|
},
|
2015-07-21 19:34:59 +00:00
|
|
|
items: items,
|
|
|
|
inode: inodeFromBackendId(snapshot.ID),
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
a.Inode = d.inode
|
2015-07-21 20:11:30 +00:00
|
|
|
a.Mode = os.ModeDir | d.node.Mode
|
|
|
|
|
|
|
|
a.Uid = d.node.UID
|
|
|
|
a.Gid = d.node.GID
|
|
|
|
a.Atime = d.node.AccessTime
|
|
|
|
a.Ctime = d.node.ChangeTime
|
|
|
|
a.Mtime = d.node.ModTime
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2015-07-21 19:34:59 +00:00
|
|
|
ret := make([]fuse.Dirent, 0, len(d.items))
|
2015-07-19 12:28:11 +00:00
|
|
|
|
2015-07-21 19:34:59 +00:00
|
|
|
for _, node := range d.items {
|
2015-07-19 12:28:11 +00:00
|
|
|
var typ fuse.DirentType
|
2015-07-21 19:23:40 +00:00
|
|
|
switch node.Type {
|
|
|
|
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{
|
|
|
|
Inode: node.Inode,
|
|
|
|
Type: typ,
|
|
|
|
Name: node.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2015-07-21 19:34:59 +00:00
|
|
|
node, ok := d.items[name]
|
2015-07-19 12:28:11 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2015-07-21 19:34:59 +00:00
|
|
|
switch node.Type {
|
2015-07-21 19:23:40 +00:00
|
|
|
case "dir":
|
2015-07-21 19:34:59 +00:00
|
|
|
return newDir(d.repo, node)
|
2015-07-21 19:23:40 +00:00
|
|
|
case "file":
|
2015-07-21 19:34:59 +00:00
|
|
|
return newFile(d.repo, node)
|
2015-07-21 19:25:05 +00:00
|
|
|
case "symlink":
|
2015-07-21 19:34:59 +00:00
|
|
|
return newLink(d.repo, node)
|
2015-07-19 12:28:11 +00:00
|
|
|
default:
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
}
|