Skip to content

Commit

Permalink
Merge pull request #4285 from ipfs/feat/raw-dag
Browse files Browse the repository at this point in the history
add raw support to the dag put command.
  • Loading branch information
whyrusleeping authored Oct 11, 2017
2 parents 1e73c3f + 613508a commit e7acb96
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/coredag/dagtransl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var defaultRawParsers = FormatParsers{

"protobuf": dagpbRawParser,
"dag-pb": dagpbRawParser,

"raw": rawRawParser,
}

var defaultCborParsers = FormatParsers{
Expand Down
37 changes: 37 additions & 0 deletions core/coredag/raw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package coredag

import (
"io"
"io/ioutil"
"math"

"github.com/ipfs/go-ipfs/merkledag"

cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
block "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
)

func rawRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
if mhType == math.MaxUint64 {
mhType = mh.SHA2_256
}

data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

h, err := mh.Sum(data, mhType, mhLen)
if err != nil {
return nil, err
}
c := cid.NewCidV1(cid.Raw, h)
blk, err := block.NewBlockWithCid(data, c)
if err != nil {
return nil, err
}
nd := &merkledag.RawNode{Block: blk}
return []node.Node{nd}, nil
}
6 changes: 6 additions & 0 deletions test/sharness/t0053-dag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ test_dag_cmd() {
test_cmp dag_put_exp dag_put_out
'

test_expect_success "dag put with raw node works" '
echo "foo bar" > raw_node_in &&
HASH=$(ipfs dag put --format=raw --input-enc=raw -- raw_node_in) &&
ipfs block get "$HASH" > raw_node_out &&
test_cmp raw_node_in raw_node_out'

test_expect_success "dag put multiple files" '
printf {\"foo\":\"bar\"} > a.json &&
printf {\"foo\":\"baz\"} > b.json &&
Expand Down

0 comments on commit e7acb96

Please sign in to comment.