2015-08-16 20:27:07 +00:00
|
|
|
// +build !openbsd
|
2015-08-17 17:40:34 +00:00
|
|
|
// +build !windows
|
2015-08-16 20:27:07 +00:00
|
|
|
|
2015-07-19 12:28:11 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
"bazil.org/fuse/fs"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
2016-05-08 20:20:46 +00:00
|
|
|
"restic/debug"
|
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 {
|
2016-09-03 11:34:04 +00:00
|
|
|
repo restic.Repository
|
2015-07-26 18:41:29 +00:00
|
|
|
items map[string]*restic.Node
|
|
|
|
inode uint64
|
|
|
|
node *restic.Node
|
|
|
|
ownerIsRoot bool
|
2015-07-19 12:28:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
func newDir(repo restic.Repository, node *restic.Node, ownerIsRoot bool) (*dir, error) {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("newDir", "new dir for %v (%v)", node.Name, node.Subtree.Str())
|
2016-09-03 09:22:01 +00:00
|
|
|
tree, err := repo.LoadTree(*node.Subtree)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("newDir", " error loading tree %v: %v", node.Subtree.Str(), err)
|
2015-07-19 12:28:11 +00:00
|
|
|
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-26 18:41:29 +00:00
|
|
|
repo: repo,
|
|
|
|
node: node,
|
|
|
|
items: items,
|
|
|
|
inode: node.Inode,
|
|
|
|
ownerIsRoot: ownerIsRoot,
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-07 18:50:17 +00:00
|
|
|
// replaceSpecialNodes replaces nodes with name "." and "/" by their contents.
|
|
|
|
// Otherwise, the node is returned.
|
2016-09-03 11:34:04 +00:00
|
|
|
func replaceSpecialNodes(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
|
|
|
|
}
|
|
|
|
|
2016-09-03 09:22:01 +00:00
|
|
|
tree, err := repo.LoadTree(*node.Subtree)
|
2016-02-07 18:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree.Nodes, nil
|
|
|
|
}
|
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
func newDirFromSnapshot(repo restic.Repository, snapshot SnapshotWithId, ownerIsRoot bool) (*dir, error) {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("newDirFromSnapshot", "new dir for snapshot %v (%v)", snapshot.ID.Str(), snapshot.Tree.Str())
|
2016-09-03 09:22:01 +00:00
|
|
|
tree, err := repo.LoadTree(*snapshot.Tree)
|
2015-07-19 12:28:11 +00:00
|
|
|
if err != nil {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("newDirFromSnapshot", " loadTree(%v) failed: %v", snapshot.ID.Str(), err)
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
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 {
|
|
|
|
nodes, err := replaceSpecialNodes(repo, n)
|
|
|
|
if err != nil {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("newDirFromSnapshot", " replaceSpecialNodes(%v) failed: %v", n, err)
|
2016-02-07 18:50:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
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-26 18:41:29 +00:00
|
|
|
items: items,
|
2016-09-03 12:13:26 +00:00
|
|
|
inode: inodeFromBackendID(snapshot.ID),
|
2015-07-26 18:41:29 +00:00
|
|
|
ownerIsRoot: ownerIsRoot,
|
2015-07-19 12:28:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("dir.Attr", "called")
|
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
|
|
|
|
|
2015-07-26 18:41:29 +00:00
|
|
|
if !d.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
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("dir.ReadDirAll", "called")
|
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
|
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{
|
|
|
|
Inode: node.Inode,
|
|
|
|
Type: typ,
|
|
|
|
Name: node.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("dir.Lookup", "Lookup(%v)", name)
|
2015-07-21 19:34:59 +00:00
|
|
|
node, ok := d.items[name]
|
2015-07-19 12:28:11 +00:00
|
|
|
if !ok {
|
2016-05-08 20:20:46 +00:00
|
|
|
debug.Log("dir.Lookup", " Lookup(%v) -> not found", name)
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2016-09-01 19:20:03 +00:00
|
|
|
switch node.Type {
|
2015-07-21 19:23:40 +00:00
|
|
|
case "dir":
|
2015-07-26 18:41:29 +00:00
|
|
|
return newDir(d.repo, node, d.ownerIsRoot)
|
2015-07-21 19:23:40 +00:00
|
|
|
case "file":
|
2015-07-26 18:41:29 +00:00
|
|
|
return newFile(d.repo, node, d.ownerIsRoot)
|
2015-07-21 19:25:05 +00:00
|
|
|
case "symlink":
|
2015-07-26 18:41:29 +00:00
|
|
|
return newLink(d.repo, node, d.ownerIsRoot)
|
2015-07-19 12:28:11 +00:00
|
|
|
default:
|
2016-09-01 19:20:03 +00:00
|
|
|
debug.Log("dir.Lookup", " node %v has unknown type %v", name, node.Type)
|
2015-07-19 12:28:11 +00:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
}
|