Skip to content

Commit

Permalink
Output dirs on ipfs add -rn
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Stephen Whitmore <noffle@ipfs.io>
  • Loading branch information
hackergrrl committed Apr 7, 2016
1 parent a0ebdf5 commit edeca42
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
10 changes: 5 additions & 5 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ MerkleDAG.
}
}

if hash {
return nil
}

// copy intermediary nodes from editor to our actual dagservice
_, err := fileAdder.Finalize()
_, err := fileAdder.Finalize(hash)
if err != nil {
return err
}

if hash {
return nil
}

return fileAdder.PinRoot()
}

Expand Down
46 changes: 30 additions & 16 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coreunix

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -173,53 +172,68 @@ func (adder *Adder) PinRoot() error {
return adder.node.Pinning.Flush()
}

func (adder *Adder) Finalize() (*dag.Node, error) {
func (adder *Adder) Finalize(write bool) (*dag.Node, error) {
root := adder.mr.GetValue()

// cant just call adder.RootNode() here as we need the name for printing
root, err := adder.mr.GetValue().GetNode()
rootNode, err := root.GetNode()
if err != nil {
return nil, err
}

var name string
if !adder.Wrap {
name = root.Links[0].Name
child, err := root.Links[0].GetNode(adder.ctx, adder.node.DAG)
name = rootNode.Links[0].Name

root, err = adder.mr.GetValue().(*mfs.Directory).Child(name)
if err != nil {
return nil, err
}
root = child
}

err = adder.outputDirs(name, root)
if err != nil {
return nil, err
}

err = adder.mr.Close()
if write {
err = adder.mr.Close()
if err != nil {
return nil, err
}
}

rootNode, err = root.GetNode()
if err != nil {
return nil, err
}

return root, nil
return rootNode, nil
}

func (adder *Adder) outputDirs(path string, nd *dag.Node) error {
if !bytes.Equal(nd.Data, folderData) {
func (adder *Adder) outputDirs(path string, fs mfs.FSNode) error {
if fs.Type() != mfs.TDir {
return nil
}

for _, l := range nd.Links {
child, err := l.GetNode(adder.ctx, adder.node.DAG)
dir := fs.(*mfs.Directory)

for _, name := range dir.ListNames() {
child, err := dir.Child(name)
if err != nil {
return err
}

err = adder.outputDirs(gopath.Join(path, l.Name), child)
err = adder.outputDirs(gopath.Join(path, name), child)
if err != nil {
return err
}
}

nd, err := fs.GetNode()
if err != nil {
return err
}

return outputDagnode(adder.out, path, nd)
}

Expand Down Expand Up @@ -270,7 +284,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
return "", err
}

nd, err := fileAdder.Finalize()
nd, err := fileAdder.Finalize(true)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -302,7 +316,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *dag.No
return "", nil, err
}

dagnode, err := fileAdder.Finalize()
dagnode, err := fileAdder.Finalize(true)
if err != nil {
return "", nil, err
}
Expand Down

0 comments on commit edeca42

Please sign in to comment.