Skip to content

Commit

Permalink
integrate the HAMT into mfs
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Aug 5, 2016
1 parent bbc1baa commit 259aeb4
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 48 deletions.
13 changes: 11 additions & 2 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {

fname := filepath.Base(p)
k := key.B58KeyDecode(s)
if err := dirb.AddChild(nd.Context(), fname, k); err != nil {
node, err := nd.DAG.Get(nd.Context(), k)
if err != nil {
return nil, err
}

if err := dirb.AddChild(nd.Context(), fname, node); err != nil {
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
}
}

dir := dirb.GetNode()
dir, err := dirb.GetNode()
if err != nil {
return nil, err
}

dkey, err := nd.DAG.Add(dir)
if err != nil {
return nil, fmt.Errorf("assets: DAG.Add(dir) failed: %s", err)
Expand Down
8 changes: 7 additions & 1 deletion core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ Examples:
case *mfs.Directory:
if !long {
var output []mfs.NodeListing
for _, name := range fsn.ListNames() {
names, err := fsn.ListNames()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

for _, name := range names {
output = append(output, mfs.NodeListing{
Name: name,
})
Expand Down
7 changes: 6 additions & 1 deletion core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
case *mfs.File:
return nil
case *mfs.Directory:
for _, name := range fsn.ListNames() {
names, err := fsn.ListNames()
if err != nil {
return err
}

for _, name := range names {
child, err := fsn.Child(name)
if err != nil {
return err
Expand Down
108 changes: 66 additions & 42 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
ufspb "github.com/ipfs/go-ipfs/unixfs/pb"
)

Expand All @@ -28,25 +29,31 @@ type Directory struct {
files map[string]*File

lock sync.Mutex
node *dag.Node
ctx context.Context

dirbuilder *uio.Directory

modTime time.Time

name string
}

func NewDirectory(ctx context.Context, name string, node *dag.Node, parent childCloser, dserv dag.DAGService) *Directory {
return &Directory{
dserv: dserv,
ctx: ctx,
name: name,
node: node,
parent: parent,
childDirs: make(map[string]*Directory),
files: make(map[string]*File),
modTime: time.Now(),
func NewDirectory(ctx context.Context, name string, node *dag.Node, parent childCloser, dserv dag.DAGService) (*Directory, error) {
db, err := uio.NewDirectoryFromNode(dserv, node)
if err != nil {
return nil, err
}

return &Directory{
dserv: dserv,
ctx: ctx,
name: name,
dirbuilder: db,
parent: parent,
childDirs: make(map[string]*Directory),
files: make(map[string]*File),
modTime: time.Now(),
}, nil
}

// closeChild updates the child by the given name to the dag node 'nd'
Expand Down Expand Up @@ -80,21 +87,21 @@ func (d *Directory) closeChildUpdate(name string, nd *dag.Node, sync bool) (*dag
}

func (d *Directory) flushCurrentNode() (*dag.Node, error) {
_, err := d.dserv.Add(d.node)
nd, err := d.dirbuilder.GetNode()
if err != nil {
return nil, err
}

return d.node.Copy(), nil
_, err = d.dserv.Add(nd)
if err != nil {
return nil, err
}

return nd.Copy(), nil
}

func (d *Directory) updateChild(name string, nd *dag.Node) error {
err := d.node.RemoveNodeLink(name)
if err != nil && err != dag.ErrNotFound {
return err
}

err = d.node.AddNodeLinkClean(name, nd)
err := d.dirbuilder.AddChild(d.ctx, name, nd)
if err != nil {
return err
}
Expand Down Expand Up @@ -128,7 +135,11 @@ func (d *Directory) cacheNode(name string, nd *dag.Node) (FSNode, error) {

switch i.GetType() {
case ufspb.Data_Directory:
ndir := NewDirectory(d.ctx, name, nd, d, d.dserv)
ndir, err := NewDirectory(d.ctx, name, nd, d, d.dserv)
if err != nil {
return nil, err
}

d.childDirs[name] = ndir
return ndir, nil
case ufspb.Data_File, ufspb.Data_Raw, ufspb.Data_Symlink:
Expand Down Expand Up @@ -162,13 +173,12 @@ func (d *Directory) Uncache(name string) {
// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
for _, lnk := range d.node.Links {
if lnk.Name == name {
return lnk.GetNode(d.ctx, d.dserv)
}
nd, err := d.dirbuilder.Find(d.ctx, name)
if err != nil {
return nil, err
}

return nil, os.ErrNotExist
return nd.Copy(), nil
}

// childUnsync returns the child under this directory by the given name
Expand All @@ -194,7 +204,7 @@ type NodeListing struct {
Hash string
}

func (d *Directory) ListNames() []string {
func (d *Directory) ListNames() ([]string, error) {
d.lock.Lock()
defer d.lock.Unlock()

Expand All @@ -206,7 +216,12 @@ func (d *Directory) ListNames() []string {
names[n] = struct{}{}
}

for _, l := range d.node.Links {
links, err := d.dirbuilder.Links()
if err != nil {
return nil, err
}

for _, l := range links {
names[l.Name] = struct{}{}
}

Expand All @@ -216,15 +231,21 @@ func (d *Directory) ListNames() []string {
}
sort.Strings(out)

return out
return out, nil
}

func (d *Directory) List() ([]NodeListing, error) {
d.lock.Lock()
defer d.lock.Unlock()

var out []NodeListing
for _, l := range d.node.Links {

links, err := d.dirbuilder.Links()
if err != nil {
return nil, err
}

for _, l := range links {
child := NodeListing{}
child.Name = l.Name

Expand Down Expand Up @@ -275,20 +296,23 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
}
}

ndir := new(dag.Node)
ndir.SetData(ft.FolderPBData())
ndir := ft.EmptyDirNode()

_, err = d.dserv.Add(ndir)
if err != nil {
return nil, err
}

err = d.node.AddNodeLinkClean(name, ndir)
err = d.dirbuilder.AddChild(d.ctx, name, ndir)
if err != nil {
return nil, err
}

dirobj, err := NewDirectory(d.ctx, name, ndir, d, d.dserv)
if err != nil {
return nil, err
}

dirobj := NewDirectory(d.ctx, name, ndir, d, d.dserv)
d.childDirs[name] = dirobj
return dirobj, nil
}
Expand All @@ -300,12 +324,7 @@ func (d *Directory) Unlink(name string) error {
delete(d.childDirs, name)
delete(d.files, name)

err := d.node.RemoveNodeLink(name)
if err != nil {
return err
}

_, err = d.dserv.Add(d.node)
err := d.dirbuilder.RemoveChild(d.ctx, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -340,7 +359,7 @@ func (d *Directory) AddChild(name string, nd *dag.Node) error {
return err
}

err = d.node.AddNodeLinkClean(name, nd)
err = d.dirbuilder.AddChild(d.ctx, name, nd)
if err != nil {
return err
}
Expand Down Expand Up @@ -396,10 +415,15 @@ func (d *Directory) GetNode() (*dag.Node, error) {
return nil, err
}

_, err = d.dserv.Add(d.node)
nd, err := d.dirbuilder.GetNode()
if err != nil {
return nil, err
}

_, err = d.dserv.Add(nd)
if err != nil {
return nil, err
}

return d.node.Copy(), nil
return nd, err
}
Loading

0 comments on commit 259aeb4

Please sign in to comment.