Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon code #44

Merged
merged 10 commits into from
Sep 14, 2014
101 changes: 13 additions & 88 deletions cmd/ipfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/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"
"github.com/jbenet/go-ipfs/core/commands"
"github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)

Expand Down Expand Up @@ -41,92 +38,20 @@ func addCmd(c *commander.Command, inp []string) error {
return nil
}

n, err := localNode(false)
cmd := daemon.NewCommand()
cmd.Command = "add"
fmt.Println(inp)
cmd.Args = inp
cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get()
err := daemon.SendCommand(cmd, "localhost:12345")
if err != nil {
return err
}

recursive := c.Flag.Lookup("r").Value.Get().(bool)
var depth int
if recursive {
depth = -1
} else {
depth = 1
}

for _, fpath := range inp {
_, err := addPath(n, fpath, depth)
if err != nil {
if !recursive {
return fmt.Errorf("%s is a directory. Use -r to add recursively", fpath)
}

u.PErr("error adding %s: %v\n", fpath, err)
}
}
return err
}

func addPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
if depth == 0 {
return nil, ErrDepthLimitExceeded
}

fi, err := os.Stat(fpath)
if err != nil {
return nil, err
}

if fi.IsDir() {
return addDir(n, fpath, depth)
}

return addFile(n, fpath, depth)
}

func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
tree := &dag.Node{Data: dag.FolderPBData()}

files, err := ioutil.ReadDir(fpath)
if err != nil {
return nil, err
}

// construct nodes for containing files.
for _, f := range files {
fp := filepath.Join(fpath, f.Name())
nd, err := addPath(n, fp, depth-1)
// Do locally
n, err := localNode(false)
if err != nil {
return nil, err
return err
}

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

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 nil, err
return commands.Add(n, cmd.Args, cmd.Opts, os.Stdout)
}

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
err := n.DAG.AddRecursive(nd)
if err != nil {
return err
}

u.POut("added %s\n", fpath)

// ensure we keep it. atm no-op
return n.PinDagNode(nd)
return nil
}
28 changes: 11 additions & 17 deletions cmd/ipfs/cat.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package main

import (
"fmt"
"io"
"os"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
dag "github.com/jbenet/go-ipfs/merkledag"
"github.com/jbenet/go-ipfs/core/commands"
"github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)

Expand All @@ -29,28 +28,23 @@ func catCmd(c *commander.Command, inp []string) error {
return nil
}

n, err := localNode(false)
expanded, err := u.ExpandPathnames(inp)
if err != nil {
return err
}

for _, fn := range inp {
nd, err := n.Resolver.ResolvePath(fn)
if err != nil {
return err
}
com := daemon.NewCommand()
com.Command = "cat"
com.Args = inp

read, err := dag.NewDagReader(nd, n.DAG)
err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
n, err := localNode(false)
if err != nil {
fmt.Println(err)
continue
return err
}

_, err = io.Copy(os.Stdout, read)
if err != nil {
fmt.Println(err)
continue
}
return commands.Cat(n, com.Args, com.Opts, os.Stdout)
}
return nil
}
22 changes: 13 additions & 9 deletions cmd/ipfs/ls.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package main

import (
"fmt"
"os"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
"github.com/jbenet/go-ipfs/core/commands"
"github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)

Expand All @@ -27,20 +32,19 @@ func lsCmd(c *commander.Command, inp []string) error {
return nil
}

n, err := localNode(false)
com := daemon.NewCommand()
com.Command = "ls"
com.Args = inp
err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
return err
}

for _, fn := range inp {
nd, err := n.Resolver.ResolvePath(fn)
fmt.Println(err)
n, err := localNode(false)
if err != nil {
return err
}

for _, link := range nd.Links {
u.POut("%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
}
return commands.Ls(n, com.Args, com.Opts, os.Stdout)
}

return nil
}
11 changes: 11 additions & 0 deletions cmd/ipfs/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
"github.com/jbenet/go-ipfs/daemon"
rofs "github.com/jbenet/go-ipfs/fuse/readonly"
u "github.com/jbenet/go-ipfs/util"
)
Expand All @@ -26,16 +27,26 @@ var cmdIpfsMount = &commander.Command{
}

func mountCmd(c *commander.Command, inp []string) error {
u.Debug = true
if len(inp) < 1 || len(inp[0]) == 0 {
u.POut(c.Long)
return nil
}
fmt.Println("wtf.")

n, err := localNode(true)
if err != nil {
return err
}

fmt.Println("starting new daemon listener...")
dl, err := daemon.NewDaemonListener(n, "localhost:12345")
if err != nil {
return err
}
go dl.Listen()
defer dl.Close()

mp := inp[0]
fmt.Printf("Mounting at %s\n", mp)

Expand Down
102 changes: 102 additions & 0 deletions core/commands/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package commands

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/importer"
dag "github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
)

// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
depth := 1
if r, ok := opts["r"].(bool); r && ok {
depth = -1
}
for _, path := range args {
nd, err := AddPath(n, path, depth)
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}

k, err := nd.Key()
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}

fmt.Fprintf(out, "Added node: %s = %s\n", path, k.Pretty())
}
return nil
}

func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
if depth == 0 {
return nil, ErrDepthLimitExceeded
}

fi, err := os.Stat(fpath)
if err != nil {
return nil, err
}

if fi.IsDir() {
return addDir(n, fpath, depth)
}

return addFile(n, fpath, depth)
}

func addDir(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
tree := &dag.Node{Data: dag.FolderPBData()}

files, err := ioutil.ReadDir(fpath)
if err != nil {
return nil, err
}

// 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
}

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

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 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
err := n.DAG.AddRecursive(nd)
if err != nil {
return err
}

u.POut("added %s\n", fpath)

// ensure we keep it. atm no-op
return n.PinDagNode(nd)
}
29 changes: 29 additions & 0 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package commands

import (
"fmt"
"io"

"github.com/jbenet/go-ipfs/core"
mdag "github.com/jbenet/go-ipfs/merkledag"
)

func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
for _, fn := range args {
dagnode, err := n.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("catFile error: %v", err)
}

read, err := mdag.NewDagReader(dagnode, n.DAG)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}

_, err = io.Copy(out, read)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}
}
return nil
}
Loading