Merge pull request #240 from restic/fix-fuse-symlinks

fuse: Display symlinks properly
This commit is contained in:
Alexander Neumann 2015-07-21 22:16:33 +02:00
commit ec3893e655
3 changed files with 61 additions and 27 deletions

View File

@ -16,9 +16,9 @@ var _ = fs.HandleReadDirAller(&dir{})
var _ = fs.NodeStringLookuper(&dir{})
type dir struct {
repo *repository.Repository
children map[string]*restic.Node
inode uint64
repo *repository.Repository
items map[string]*restic.Node
inode uint64
}
func newDir(repo *repository.Repository, node *restic.Node) (*dir, error) {
@ -26,15 +26,15 @@ func newDir(repo *repository.Repository, node *restic.Node) (*dir, error) {
if err != nil {
return nil, err
}
children := make(map[string]*restic.Node)
for _, child := range tree.Nodes {
children[child.Name] = child
items := make(map[string]*restic.Node)
for _, node := range tree.Nodes {
items[node.Name] = node
}
return &dir{
repo: repo,
children: children,
inode: node.Inode,
repo: repo,
items: items,
inode: node.Inode,
}, nil
}
@ -43,15 +43,15 @@ func newDirFromSnapshot(repo *repository.Repository, snapshot SnapshotWithId) (*
if err != nil {
return nil, err
}
children := make(map[string]*restic.Node)
items := make(map[string]*restic.Node)
for _, node := range tree.Nodes {
children[node.Name] = node
items[node.Name] = node
}
return &dir{
repo: repo,
children: children,
inode: inodeFromBackendId(snapshot.ID),
repo: repo,
items: items,
inode: inodeFromBackendId(snapshot.ID),
}, nil
}
@ -62,15 +62,17 @@ func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
}
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
ret := make([]fuse.Dirent, 0, len(d.children))
ret := make([]fuse.Dirent, 0, len(d.items))
for _, node := range d.children {
for _, node := range d.items {
var typ fuse.DirentType
switch {
case node.Mode.IsDir():
switch node.Type {
case "dir":
typ = fuse.DT_Dir
case node.Mode.IsRegular():
case "file":
typ = fuse.DT_File
case "symlink":
typ = fuse.DT_Link
}
ret = append(ret, fuse.Dirent{
@ -84,15 +86,17 @@ func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
}
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
child, ok := d.children[name]
node, ok := d.items[name]
if !ok {
return nil, fuse.ENOENT
}
switch {
case child.Mode.IsDir():
return newDir(d.repo, child)
case child.Mode.IsRegular():
return newFile(d.repo, child)
switch node.Type {
case "dir":
return newDir(d.repo, node)
case "file":
return newFile(d.repo, node)
case "symlink":
return newLink(d.repo, node)
default:
return nil, fuse.ENOENT
}

View File

@ -23,8 +23,8 @@ type file struct {
func newFile(repo *repository.Repository, node *restic.Node) (*file, error) {
sizes := make([]uint32, len(node.Content))
for i, blobId := range node.Content {
length, err := repo.Index().LookupSize(blobId)
for i, blobID := range node.Content {
length, err := repo.Index().LookupSize(blobID)
if err != nil {
return nil, err
}

30
cmd/restic/fuse/link.go Normal file
View File

@ -0,0 +1,30 @@
package fuse
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/restic/restic"
"github.com/restic/restic/repository"
"golang.org/x/net/context"
)
// Statically ensure that *file implements the given interface
var _ = fs.NodeReadlinker(&link{})
type link struct {
node *restic.Node
}
func newLink(repo *repository.Repository, node *restic.Node) (*link, error) {
return &link{node: node}, nil
}
func (l *link) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
return l.node.LinkTarget, nil
}
func (l *link) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = l.node.Inode
a.Mode = l.node.Mode
return nil
}