Skip to content

Commit

Permalink
Remove intermediate InsertNodeAtPath in ipfs add
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <rhtbot@gmail.com>

Small cleanup coreunix.Add

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
  • Loading branch information
rht committed Dec 1, 2015
1 parent 28fa917 commit aec8e82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
8 changes: 7 additions & 1 deletion core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ remains to be implemented.
return nil // done
}

if _, err := fileAdder.AddFile(file); err != nil {
n, err := fileAdder.AddFile(file)
if err != nil {
return err
}

// patch it into root
if err := fileAdder.InsertNodeAtPath(file.FileName(), n); err != nil {
return err
}
}
Expand Down
34 changes: 19 additions & 15 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *dag.No
return gopath.Join(k.String(), filename), dagnode, nil
}

func (params *Adder) addNode(node *dag.Node, path string) error {
// patch it into the root
func (params *Adder) outputDagnode(node *dag.Node, path string) error {
if path == "" {
key, err := node.Key()
if err != nil {
Expand All @@ -241,11 +240,19 @@ func (params *Adder) addNode(node *dag.Node, path string) error {
path = key.Pretty()
}

if err := params.editor.InsertNodeAtPath(params.ctx, path, node, newDirNode); err != nil {
return err
return outputDagnode(params.out, path, node)
}

func (params *Adder) InsertNodeAtPath(path string, node *dag.Node) error {
if path == "" {
key, err := node.Key()
if err != nil {
return err
}
path = key.Pretty()
}

return outputDagnode(params.out, path, node)
return params.editor.InsertNodeAtPath(params.ctx, path, node, newDirNode)
}

// Add the given file while respecting the params.
Expand All @@ -260,6 +267,7 @@ func (params *Adder) AddFile(file files.File) (*dag.Node, error) {

// case for symlink
if s, ok := file.(*files.Symlink); ok {
log.Infof("adding symlink: %s", s.FileName())
sdata, err := unixfs.SymlinkData(s.Target)
if err != nil {
return nil, err
Expand All @@ -271,7 +279,7 @@ func (params *Adder) AddFile(file files.File) (*dag.Node, error) {
return nil, err
}

err = params.addNode(dagnode, s.FileName())
err = params.outputDagnode(dagnode, s.FileName())
return dagnode, err
}

Expand All @@ -283,20 +291,19 @@ func (params *Adder) AddFile(file files.File) (*dag.Node, error) {
reader = &progressReader{file: file, out: params.out}
}

log.Infof("adding file: %s", file.FileName())
dagnode, err := params.add(reader)
if err != nil {
return nil, err
}

// patch it into the root
log.Infof("adding file: %s", file.FileName())
err = params.addNode(dagnode, file.FileName())
err = params.outputDagnode(dagnode, file.FileName())
return dagnode, err
}

func (params *Adder) addDir(dir files.File) (*dag.Node, error) {
tree := newDirNode()
log.Infof("adding directory: %s", dir.FileName())
tree := newDirNode()

for {
file, err := dir.NextFile()
Expand All @@ -322,15 +329,12 @@ func (params *Adder) addDir(dir files.File) (*dag.Node, error) {
}
}

if err := params.addNode(tree, dir.FileName()); err != nil {
return nil, err
}

if _, err := params.node.DAG.Add(tree); err != nil {
return nil, err
}

return tree, nil
err := params.outputDagnode(tree, dir.FileName())
return tree, err
}

// outputDagnode sends dagnode info over the output channel
Expand Down

0 comments on commit aec8e82

Please sign in to comment.