Skip to content

Commit

Permalink
cmds2/add: temp fix for -r. horrible hack
Browse files Browse the repository at this point in the history
This will be removed soon, just gets us past landing cmds2.
  • Loading branch information
jbenet committed Nov 14, 2014
1 parent e32ed36 commit 6faeee8
Showing 1 changed file with 104 additions and 12 deletions.
116 changes: 104 additions & 12 deletions core/commands2/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core"
Expand All @@ -12,6 +15,7 @@ import (
"github.com/jbenet/go-ipfs/importer/chunk"
dag "github.com/jbenet/go-ipfs/merkledag"
pinning "github.com/jbenet/go-ipfs/pin"
ft "github.com/jbenet/go-ipfs/unixfs"
)

// Error indicating the max depth has been exceded.
Expand All @@ -26,7 +30,7 @@ var addCmd = &cmds.Command{
cmds.BoolOption("recursive", "r", "Must be specified when adding directories"),
},
Arguments: []cmds.Argument{
cmds.FileArg("file", true, true, "The path to a file to be added to IPFS"),
cmds.StringArg("file", true, true, "The path to a file to be added to IPFS"),
},
Description: "Add an object to ipfs.",
Help: `Adds contents of <path> to ipfs. Use -r to add directories.
Expand All @@ -37,27 +41,115 @@ remains to be implemented.
Run: func(req cmds.Request) (interface{}, error) {
n := req.Context().Node

readers, err := internal.CastToReaders(req.Arguments())
if err != nil {
return nil, err
// THIS IS A HORRIBLE HACK -- FIXME!!!
// see https://github.com/jbenet/go-ipfs/issues/309
var added []*Object

// returns the last one
addDagnodes := func(dns []*dag.Node) error {
for _, dn := range dns {
o, err := getOutput(dn)
if err != nil {
return err
}

added = append(added, o)
}
return nil
}

dagnodes, err := add(n, readers)
if err != nil {
return nil, err
addFile := func(name string) (*dag.Node, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()

dns, err := add(n, []io.Reader{f})
if err != nil {
return nil, err
}

if err := addDagnodes(dns); err != nil {
return nil, err
}
return dns[len(dns)-1], nil // last dag node is the file.
}

// TODO: include fs paths in output (will need a way to specify paths in underlying filearg system)
added := make([]*Object, 0, len(req.Arguments()))
for _, dagnode := range dagnodes {
object, err := getOutput(dagnode)
var addPath func(name string) (*dag.Node, error)
addDir := func(name string) (*dag.Node, error) {
tree := &dag.Node{Data: ft.FolderPBData()}

entries, err := ioutil.ReadDir(name)
if err != nil {
return nil, err
}

added = append(added, object)
// construct nodes for containing files.
for _, e := range entries {
fp := filepath.Join(name, e.Name())
nd, err := addPath(fp)
if err != nil {
return nil, err
}

if err = tree.AddNodeLink(e.Name(), nd); err != nil {
return nil, err
}
}

log.Infof("adding dir: %s", name)

if err := addDagnodes([]*dag.Node{tree}); err != nil {
return nil, err
}
return tree, nil
}

addPath = func(fpath string) (*dag.Node, error) {
fi, err := os.Stat(fpath)
if err != nil {
return nil, err
}

if fi.IsDir() {
return addDir(fpath)
}
return addFile(fpath)
}

paths, err := internal.CastToStrings(req.Arguments())
if err != nil {
return nil, err
}

for _, f := range paths {
if _, err := addPath(f); err != nil {
return nil, err
}
}

// readers, err := internal.CastToReaders(req.Arguments())
// if err != nil {
// return nil, err
// }
//
// dagnodes, err := add(n, readers)
// if err != nil {
// return nil, err
// }
//
// // TODO: include fs paths in output (will need a way to specify paths in underlying filearg system)
// added := make([]*Object, 0, len(req.Arguments()))
// for _, dagnode := range dagnodes {
// object, err := getOutput(dagnode)
// if err != nil {
// return nil, err
// }
//
// added = append(added, object)
// }

return &AddOutput{added}, nil
},
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
Expand Down

0 comments on commit 6faeee8

Please sign in to comment.