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

add raw support to the dag put command. #4285

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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