Use functions to create names

This commit is contained in:
Matthieu Rakotojaona 2015-07-19 14:10:19 +02:00
parent a016f82051
commit 3731a94367
1 changed files with 52 additions and 32 deletions

View File

@ -177,14 +177,7 @@ func (sn *snapshots) Lookup(ctx context.Context, name string) (fs.Node, error) {
} }
} }
tree, err := restic.LoadTree(sn.repo, snapshot.Tree) return newDirFromSnapshot(sn.repo, snapshot)
if err != nil {
return nil, err
}
return &dir{
repo: sn.repo,
tree: tree,
}, nil
} }
// Statically ensure that *dir implement those interface // Statically ensure that *dir implement those interface
@ -192,9 +185,43 @@ var _ = fs.HandleReadDirAller(&dir{})
var _ = fs.NodeStringLookuper(&dir{}) var _ = fs.NodeStringLookuper(&dir{})
type dir struct { type dir struct {
repo *repository.Repository repo *repository.Repository
tree *restic.Tree children map[string]*restic.Node
inode uint64 inode uint64
}
func newDir(repo *repository.Repository, node *restic.Node) (*dir, error) {
tree, err := restic.LoadTree(repo, node.Subtree)
if err != nil {
return nil, err
}
children := make(map[string]*restic.Node)
for _, child := range tree.Nodes {
children[child.Name] = child
}
return &dir{
repo: repo,
children: children,
inode: node.Inode,
}, nil
}
func newDirFromSnapshot(repo *repository.Repository, snapshot snapshotWithId) (*dir, error) {
tree, err := restic.LoadTree(repo, snapshot.Tree)
if err != nil {
return nil, err
}
children := make(map[string]*restic.Node)
for _, node := range tree.Nodes {
children[node.Name] = node
}
return &dir{
repo: repo,
children: children,
inode: binary.BigEndian.Uint64(snapshot.ID),
}, nil
} }
func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error { func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
@ -204,9 +231,9 @@ func (d *dir) Attr(ctx context.Context, a *fuse.Attr) error {
} }
func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
ret := make([]fuse.Dirent, 0, len(d.tree.Nodes)) ret := make([]fuse.Dirent, 0, len(d.children))
for _, node := range d.tree.Nodes { for _, node := range d.children {
var typ fuse.DirentType var typ fuse.DirentType
switch { switch {
case node.Mode.IsDir(): case node.Mode.IsDir():
@ -226,25 +253,18 @@ func (d *dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
} }
func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) { func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
for _, node := range d.tree.Nodes { child, ok := d.children[name]
if node.Name == name { if !ok {
switch { return nil, fuse.ENOENT
case node.Mode.IsDir(): }
subtree, err := restic.LoadTree(d.repo, node.Subtree) switch {
if err != nil { case child.Mode.IsDir():
return nil, err return newDir(d.repo, child)
} case child.Mode.IsRegular():
return &dir{ return newFile(d.repo, child)
repo: d.repo, default:
tree: subtree, return nil, fuse.ENOENT
inode: binary.BigEndian.Uint64(node.Subtree[:8]),
}, nil
case node.Mode.IsRegular():
return makeFile(d.repo, node)
}
}
} }
} }
// Statically ensure that *file implements the given interface // Statically ensure that *file implements the given interface
@ -260,7 +280,7 @@ type file struct {
clearContent [][]byte clearContent [][]byte
} }
func makeFile(repo *repository.Repository, node *restic.Node) (*file, error) { func newFile(repo *repository.Repository, node *restic.Node) (*file, error) {
sizes := make([]uint32, len(node.Content)) sizes := make([]uint32, len(node.Content))
for i, blobId := range node.Content { for i, blobId := range node.Content {
_, _, _, length, err := repo.Index().Lookup(blobId) _, _, _, length, err := repo.Index().Lookup(blobId)