Skip to content

Commit e7acb96

Browse files
Merge pull request #4285 from ipfs/feat/raw-dag
add raw support to the dag put command.
2 parents 1e73c3f + 613508a commit e7acb96

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

core/coredag/dagtransl.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ var defaultRawParsers = FormatParsers{
3737

3838
"protobuf": dagpbRawParser,
3939
"dag-pb": dagpbRawParser,
40+
41+
"raw": rawRawParser,
4042
}
4143

4244
var defaultCborParsers = FormatParsers{

core/coredag/raw.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package coredag
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
"math"
7+
8+
"github.com/ipfs/go-ipfs/merkledag"
9+
10+
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
11+
node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
12+
block "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
13+
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
14+
)
15+
16+
func rawRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
17+
if mhType == math.MaxUint64 {
18+
mhType = mh.SHA2_256
19+
}
20+
21+
data, err := ioutil.ReadAll(r)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
h, err := mh.Sum(data, mhType, mhLen)
27+
if err != nil {
28+
return nil, err
29+
}
30+
c := cid.NewCidV1(cid.Raw, h)
31+
blk, err := block.NewBlockWithCid(data, c)
32+
if err != nil {
33+
return nil, err
34+
}
35+
nd := &merkledag.RawNode{Block: blk}
36+
return []node.Node{nd}, nil
37+
}

test/sharness/t0053-dag.sh

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ test_dag_cmd() {
167167
test_cmp dag_put_exp dag_put_out
168168
'
169169

170+
test_expect_success "dag put with raw node works" '
171+
echo "foo bar" > raw_node_in &&
172+
HASH=$(ipfs dag put --format=raw --input-enc=raw -- raw_node_in) &&
173+
ipfs block get "$HASH" > raw_node_out &&
174+
test_cmp raw_node_in raw_node_out'
175+
170176
test_expect_success "dag put multiple files" '
171177
printf {\"foo\":\"bar\"} > a.json &&
172178
printf {\"foo\":\"baz\"} > b.json &&

0 commit comments

Comments
 (0)