core/commands: Moved commands that were rebased into the wrong directory
Matt Bell committed
Oct 30, 2014 at 22:28 UTC
6ae09b167d0870ad408089506278a549a1f562ee
6 files changed
+26
-339
core/commands/add.go
+20
-2
@@ -10,8 +10,11 @@ import (
10
11
"github.com/jbenet/go-ipfs/core"
12
"github.com/jbenet/go-ipfs/importer"
13
+ "github.com/jbenet/go-ipfs/importer/chunk"
14
dag "github.com/jbenet/go-ipfs/merkledag"
15
+ "github.com/jbenet/go-ipfs/pin"
16
ft "github.com/jbenet/go-ipfs/unixfs"
17
+ uio "github.com/jbenet/go-ipfs/unixfs/io"
18
)
19
20
// Error indicating the max depth has been exceded.
@@ -87,7 +90,14 @@ func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node
90
}
91
92
func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
90
- root, err := importer.NewDagFromFile(fpath)
93
+ dw := uio.NewDagWriter(n.DAG, chunk.DefaultSplitter)
94
+ mp, ok := n.Pinning.(pin.ManualPinner)
95
+ if !ok {
96
+ return nil, errors.New("invalid pinner type! expected manual pinner")
97
+ }
98
+ dw.Pinner = mp
99
+
100
+ root, err := importer.ImportFileDag(fpath, dw)
101
if err != nil {
102
return nil, err
103
}
@@ -98,7 +108,15 @@ func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Nod
108
log.Info("adding subblock: %s %s", l.Name, l.Hash.B58String())
109
}
110
101
- return root, addNode(n, root, fpath, out)
111
+ k, err := root.Key()
112
+ if err != nil {
113
+ return nil, err
114
+ }
115
+
116
+ // output that we've added this node
117
+ fmt.Fprintf(out, "added %s %s\n", k, fpath)
118
+
119
+ return root, nil
120
}
121
122
// addNode adds the node to the graph + local storage
core/commands/block.go
+3
-3
@@ -7,7 +7,7 @@ import (
7
"os"
8
"time"
9
10
- "code.google.com/p/go.net/context"
10
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
12
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
13
"github.com/jbenet/go-ipfs/blocks"
@@ -28,7 +28,7 @@ func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out
28
}
29
30
k := u.Key(h)
31
- log.Debug("BlockGet key: '%q'", k)
31
+ log.Debugf("BlockGet key: '%q'", k)
32
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
33
b, err := n.Blocks.GetBlock(ctx, k)
34
if err != nil {
@@ -48,7 +48,7 @@ func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out
48
}
49
50
b := blocks.NewBlock(data)
51
- log.Debug("BlockPut key: '%q'", b.Key())
51
+ log.Debugf("BlockPut key: '%q'", b.Key())
52
53
k, err := n.Blocks.AddBlock(b)
54
if err != nil {
core/commands/object.go
+3
-3
@@ -19,7 +19,7 @@ func ObjectData(n *core.IpfsNode, args []string, opts map[string]interface{}, ou
19
if err != nil {
20
return fmt.Errorf("objectData error: %v", err)
21
}
22
- log.Debug("objectData: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
22
+ log.Debugf("objectData: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
23
24
_, err = io.Copy(out, bytes.NewReader(dagnode.Data))
25
return err
@@ -31,7 +31,7 @@ func ObjectLinks(n *core.IpfsNode, args []string, opts map[string]interface{}, o
31
if err != nil {
32
return fmt.Errorf("objectLinks error: %v", err)
33
}
34
- log.Debug("ObjectLinks: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
34
+ log.Debugf("ObjectLinks: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
35
36
for _, link := range dagnode.Links {
37
_, err = fmt.Fprintf(out, "%s %d %q\n", link.Hash.B58String(), link.Size, link.Name)
@@ -70,7 +70,7 @@ func ObjectGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out
70
if err != nil {
71
return fmt.Errorf("ObjectGet error: %v", err)
72
}
73
- log.Debug("objectGet: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
73
+ log.Debugf("objectGet: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
74
75
// sadly all encodings dont implement a common interface
76
var data []byte
core/commands2/add.go
deleted
-136
@@ -1,136 +0,0 @@
1
-package commands
2
-
3
-import (
4
- "errors"
5
- "fmt"
6
- "io"
7
- "io/ioutil"
8
- "os"
9
- "path/filepath"
10
-
11
- "github.com/jbenet/go-ipfs/core"
12
- "github.com/jbenet/go-ipfs/importer"
13
- dag "github.com/jbenet/go-ipfs/merkledag"
14
- "github.com/jbenet/go-ipfs/pin"
15
- ft "github.com/jbenet/go-ipfs/unixfs"
16
-)
17
-
18
-// Error indicating the max depth has been exceded.
19
-var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
20
-
21
-// Add is a command that imports files and directories -- given as arguments -- into ipfs.
22
-func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
23
- depth := 1
24
-
25
- // if recursive, set depth to reflect so
26
- if r, ok := opts["r"].(bool); r && ok {
27
- depth = -1
28
- }
29
-
30
- // add every path in args
31
- for _, path := range args {
32
-
33
- // Add the file
34
- _, err := AddPath(n, path, depth, out)
35
- if err != nil {
36
- if err == ErrDepthLimitExceeded && depth == 1 {
37
- err = errors.New("use -r to recursively add directories")
38
- }
39
- return fmt.Errorf("addFile error: %v", err)
40
- }
41
-
42
- }
43
- return nil
44
-}
45
-
46
-// AddPath adds a particular path to ipfs.
47
-func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
48
- if depth == 0 {
49
- return nil, ErrDepthLimitExceeded
50
- }
51
-
52
- fi, err := os.Stat(fpath)
53
- if err != nil {
54
- return nil, err
55
- }
56
-
57
- if fi.IsDir() {
58
- return addDir(n, fpath, depth, out)
59
- }
60
-
61
- return addFile(n, fpath, depth, out)
62
-}
63
-
64
-func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
65
- tree := &dag.Node{Data: ft.FolderPBData()}
66
-
67
- files, err := ioutil.ReadDir(fpath)
68
- if err != nil {
69
- return nil, err
70
- }
71
-
72
- // construct nodes for containing files.
73
- for _, f := range files {
74
- fp := filepath.Join(fpath, f.Name())
75
- nd, err := AddPath(n, fp, depth-1, out)
76
- if err != nil {
77
- return nil, err
78
- }
79
-
80
- if err = tree.AddNodeLink(f.Name(), nd); err != nil {
81
- return nil, err
82
- }
83
- }
84
-
85
- log.Infof("adding dir: %s", fpath)
86
-
87
- return tree, addNode(n, tree, fpath, out)
88
-}
89
-
90
-func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
91
- mp, ok := n.Pinning.(pin.ManualPinner)
92
- if !ok {
93
- return nil, errors.New("invalid pinner type! expected manual pinner")
94
- }
95
-
96
- root, err := importer.BuildDagFromFile(fpath, n.DAG, mp)
97
- if err != nil {
98
- return nil, err
99
- }
100
-
101
- log.Infof("adding file: %s", fpath)
102
-
103
- for _, l := range root.Links {
104
- log.Infof("adding subblock: '%s' %s", l.Name, l.Hash.B58String())
105
- }
106
-
107
- k, err := root.Key()
108
- if err != nil {
109
- return nil, err
110
- }
111
-
112
- // output that we've added this node
113
- fmt.Fprintf(out, "added %s %s\n", k, fpath)
114
-
115
- return root, nil
116
-}
117
-
118
-// addNode adds the node to the graph + local storage
119
-func addNode(n *core.IpfsNode, nd *dag.Node, fpath string, out io.Writer) error {
120
- // add the file to the graph + local storage
121
- err := n.DAG.AddRecursive(nd)
122
- if err != nil {
123
- return err
124
- }
125
-
126
- k, err := nd.Key()
127
- if err != nil {
128
- return err
129
- }
130
-
131
- // output that we've added this node
132
- fmt.Fprintf(out, "added %s %s\n", k, fpath)
133
-
134
- // ensure we keep it
135
- return n.Pinning.Pin(nd, true)
136
-}
core/commands2/block.go
deleted
-60
@@ -1,60 +0,0 @@
1
-package commands
2
-
3
-import (
4
- "fmt"
5
- "io"
6
- "io/ioutil"
7
- "os"
8
- "time"
9
-
10
- "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
-
12
- mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
13
- "github.com/jbenet/go-ipfs/blocks"
14
- "github.com/jbenet/go-ipfs/core"
15
- u "github.com/jbenet/go-ipfs/util"
16
-)
17
-
18
-// BlockGet retrives a raw ipfs block from the node's BlockService
19
-func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
20
-
21
- if !u.IsValidHash(args[0]) {
22
- return fmt.Errorf("block get: not a valid hash")
23
- }
24
-
25
- h, err := mh.FromB58String(args[0])
26
- if err != nil {
27
- return fmt.Errorf("block get: %v", err)
28
- }
29
-
30
- k := u.Key(h)
31
- log.Debugf("BlockGet key: '%q'", k)
32
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
33
- b, err := n.Blocks.GetBlock(ctx, k)
34
- if err != nil {
35
- return fmt.Errorf("block get: %v", err)
36
- }
37
-
38
- _, err = out.Write(b.Data)
39
- return err
40
-}
41
-
42
-// BlockPut reads everything from conn and saves the data to the nodes BlockService
43
-func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
44
- // TODO: this should read from an io.Reader arg
45
- data, err := ioutil.ReadAll(os.Stdin)
46
- if err != nil {
47
- return err
48
- }
49
-
50
- b := blocks.NewBlock(data)
51
- log.Debugf("BlockPut key: '%q'", b.Key())
52
-
53
- k, err := n.Blocks.AddBlock(b)
54
- if err != nil {
55
- return err
56
- }
57
- fmt.Fprintf(out, "added as '%s'\n", k)
58
-
59
- return nil
60
-}
core/commands2/object.go
deleted
-135
@@ -1,135 +0,0 @@
1
-package commands
2
-
3
-import (
4
- "bytes"
5
- "encoding/json"
6
- "errors"
7
- "fmt"
8
- "io"
9
- "io/ioutil"
10
- "os"
11
-
12
- "github.com/jbenet/go-ipfs/core"
13
- dag "github.com/jbenet/go-ipfs/merkledag"
14
-)
15
-
16
-// ObjectData takes a key string from args and writes out the raw bytes of that node (if there is one)
17
-func ObjectData(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
18
- dagnode, err := n.Resolver.ResolvePath(args[0])
19
- if err != nil {
20
- return fmt.Errorf("objectData error: %v", err)
21
- }
22
- log.Debugf("objectData: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
23
-
24
- _, err = io.Copy(out, bytes.NewReader(dagnode.Data))
25
- return err
26
-}
27
-
28
-// ObjectLinks takes a key string from args and lists the links it points to
29
-func ObjectLinks(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
30
- dagnode, err := n.Resolver.ResolvePath(args[0])
31
- if err != nil {
32
- return fmt.Errorf("objectLinks error: %v", err)
33
- }
34
- log.Debugf("ObjectLinks: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
35
-
36
- for _, link := range dagnode.Links {
37
- _, err = fmt.Fprintf(out, "%s %d %q\n", link.Hash.B58String(), link.Size, link.Name)
38
- if err != nil {
39
- break
40
- }
41
- }
42
-
43
- return err
44
-}
45
-
46
-// ErrUnknownObjectEnc is returned if a invalid encoding is supplied
47
-var ErrUnknownObjectEnc = errors.New("unknown object encoding")
48
-
49
-type objectEncoding string
50
-
51
-const (
52
- objectEncodingJSON objectEncoding = "json"
53
- objectEncodingProtobuf = "protobuf"
54
-)
55
-
56
-func getObjectEnc(o interface{}) objectEncoding {
57
- v, ok := o.(string)
58
- if !ok {
59
- // chosen as default because it's human readable
60
- log.Warning("option is not a string - falling back to json")
61
- return objectEncodingJSON
62
- }
63
-
64
- return objectEncoding(v)
65
-}
66
-
67
-// ObjectGet takes a key string from args and a format option and serializes the dagnode to that format
68
-func ObjectGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
69
- dagnode, err := n.Resolver.ResolvePath(args[0])
70
- if err != nil {
71
- return fmt.Errorf("ObjectGet error: %v", err)
72
- }
73
- log.Debugf("objectGet: found dagnode %q (# of bytes: %d - # links: %d)", args[0], len(dagnode.Data), len(dagnode.Links))
74
-
75
- // sadly all encodings dont implement a common interface
76
- var data []byte
77
- switch getObjectEnc(opts["encoding"]) {
78
- case objectEncodingJSON:
79
- data, err = json.MarshalIndent(dagnode, "", " ")
80
-
81
- case objectEncodingProtobuf:
82
- data, err = dagnode.Marshal()
83
-
84
- default:
85
- return ErrUnknownObjectEnc
86
- }
87
-
88
- if err != nil {
89
- return fmt.Errorf("ObjectGet error: %v", err)
90
- }
91
-
92
- _, err = io.Copy(out, bytes.NewReader(data))
93
- return err
94
-}
95
-
96
-// ErrObjectTooLarge is returned when too much data was read from stdin. current limit 512k
97
-var ErrObjectTooLarge = errors.New("input object was too large. limit is 512kbytes")
98
-
99
-const inputLimit = 512 * 1024
100
-
101
-// ObjectPut takes a format option, serilizes bytes from stdin and updates the dag with that data
102
-func ObjectPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
103
- var (
104
- dagnode *dag.Node
105
- data []byte
106
- err error
107
- )
108
-
109
- data, err = ioutil.ReadAll(io.LimitReader(os.Stdin, inputLimit+10))
110
- if err != nil {
111
- return fmt.Errorf("ObjectPut error: %v", err)
112
- }
113
-
114
- if len(data) >= inputLimit {
115
- return ErrObjectTooLarge
116
- }
117
-
118
- switch getObjectEnc(opts["encoding"]) {
119
- case objectEncodingJSON:
120
- dagnode = new(dag.Node)
121
- err = json.Unmarshal(data, dagnode)
122
-
123
- case objectEncodingProtobuf:
124
- dagnode, err = dag.Decoded(data)
125
-
126
- default:
127
- return ErrUnknownObjectEnc
128
- }
129
-
130
- if err != nil {
131
- return fmt.Errorf("ObjectPut error: %v", err)
132
- }
133
-
134
- return addNode(n, dagnode, "stdin", out)
135
-}