Commit 5abf3c2a authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added ls + recusrive directory adding

parent 56b9b57b
......@@ -6,11 +6,16 @@ import (
"github.com/jbenet/commander"
core "github.com/jbenet/go-ipfs/core"
importer "github.com/jbenet/go-ipfs/importer"
dag "github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-multihash"
"io/ioutil"
"os"
"path/filepath"
)
var DepthLimitExceeded = fmt.Errorf("depth limit exceeded")
var cmdIpfsAdd = &commander.Command{
UsageLine: "add",
Short: "Add an object to ipfs.",
......@@ -49,18 +54,26 @@ func addCmd(c *commander.Command, inp []string) error {
}
for _, fpath := range inp {
err = addPath(n, fpath, depth)
_, err := addPath(n, fpath, depth)
if err != nil {
u.PErr("error adding %s: %v\n", fpath, err)
if !recursive {
return fmt.Errorf("%s is a directory. Use -r to add recursively.", fpath)
} else {
u.PErr("error adding %s: %v\n", fpath, err)
}
}
}
return err
}
func addPath(n *core.IpfsNode, fpath string, depth int) error {
func addPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
if depth == 0 {
return nil, DepthLimitExceeded
}
fi, err := os.Stat(fpath)
if err != nil {
return err
return nil, err
}
if fi.IsDir() {
......@@ -70,33 +83,43 @@ func addPath(n *core.IpfsNode, fpath string, depth int) error {
}
}
func addDir(n *core.IpfsNode, fpath string, depth int) error {
return u.NotImplementedError
}
func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
tree := &dag.Node{}
func addFile(n *core.IpfsNode, fpath string, depth int) error {
stat, err := os.Stat(fpath)
files, err := ioutil.ReadDir(fpath)
if err != nil {
return err
return nil, err
}
if stat.IsDir() {
return fmt.Errorf("addFile: `fpath` is a directory")
}
// construct nodes for containing files.
for _, f := range files {
fp := filepath.Join(fpath, f.Name())
nd, err := addPath(n, fp, depth-1)
if err != nil {
return nil, err
}
f, err := os.Open(fpath)
if err != nil {
return err
if err = tree.AddNodeLink(f.Name(), nd); err != nil {
return nil, err
}
}
defer f.Close()
root, err := importer.NewDagFromReader(f, stat.Size())
return tree, addNode(n, tree, fpath)
}
func addFile(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
root, err := importer.NewDagFromFile(fpath)
if err != nil {
return err
return nil, err
}
return root, addNode(n, root, fpath)
}
// addNode adds the node to the graph + local storage
func addNode(n *core.IpfsNode, nd *dag.Node, fpath string) error {
// add the file to the graph + local storage
k, err := n.AddDagNode(root)
k, err := n.AddDagNode(nd)
if err != nil {
return err
}
......
......@@ -38,6 +38,7 @@ Use "ipfs help <command>" for more information about a command.
Subcommands: []*commander.Command{
cmdIpfsAdd,
cmdIpfsCat,
cmdIpfsLs,
cmdIpfsVersion,
cmdIpfsCommands,
},
......
package main
import (
"github.com/gonuts/flag"
"github.com/jbenet/commander"
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-multihash"
)
var cmdIpfsLs = &commander.Command{
UsageLine: "ls",
Short: "List links from an object.",
Long: `ipfs ls <ipfs-path> - List links from an object.
Retrieves the object named by <ipfs-path> and displays the links
it contains, with the following format:
<link base58 hash> <link size in bytes> <link name>
`,
Run: lsCmd,
Flag: *flag.NewFlagSet("ipfs-ls", flag.ExitOnError),
}
func lsCmd(c *commander.Command, inp []string) error {
if len(inp) < 1 {
u.POut(c.Long)
return nil
}
// for now only hashes, no path resolution
h, err := mh.FromB58String(inp[0])
if err != nil {
return err
}
n, err := localNode()
if err != nil {
return err
}
nd, err := n.GetDagNode(u.Key(h))
if err != nil {
return err
}
for _, link := range nd.Links {
u.POut("%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
}
return nil
}
......@@ -11,7 +11,6 @@ import (
)
// IPFS Core module. It represents an IPFS instance.
type IpfsNode struct {
// the node's configuration
......
......@@ -5,6 +5,7 @@ import (
dag "github.com/jbenet/go-ipfs/merkledag"
"io"
"io/ioutil"
"os"
)
var BlockSizeLimit = int64(1048576) // 1 MB
......@@ -38,3 +39,22 @@ func NewDagFromReader(r io.Reader, size int64) (*dag.Node, error) {
// no children for now because not block splitting yet
return root, nil
}
func NewDagFromFile(fpath string) (*dag.Node, error) {
stat, err := os.Stat(fpath)
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, fmt.Errorf("`fpath` is a directory")
}
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()
return NewDagFromReader(f, stat.Size())
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment