From e42c967297d048dbf28152f81310008056758003 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 22 Jun 2015 15:21:53 -0700 Subject: [PATCH 1/2] add option to only hash input License: MIT Signed-off-by: Jeromy --- core/builder.go | 17 ++++++++++++++--- core/commands/add.go | 11 +++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/core/builder.go b/core/builder.go index 76953ad04..2eb168a21 100644 --- a/core/builder.go +++ b/core/builder.go @@ -23,6 +23,7 @@ type NodeBuilder struct { peerhost HostOption repo repo.Repo built bool + nilrepo bool } func NewNodeBuilder() *NodeBuilder { @@ -33,7 +34,7 @@ func NewNodeBuilder() *NodeBuilder { } } -func defaultRepo() (repo.Repo, error) { +func defaultRepo(dstore ds.ThreadSafeDatastore) (repo.Repo, error) { c := cfg.Config{} priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 1024, rand.Reader) if err != nil { @@ -56,7 +57,7 @@ func defaultRepo() (repo.Repo, error) { c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb) return &repo.Mock{ - D: dsync.MutexWrap(ds.NewMapDatastore()), + D: dstore, C: c, }, nil } @@ -86,13 +87,23 @@ func (nb *NodeBuilder) SetRepo(r repo.Repo) *NodeBuilder { return nb } +func (nb *NodeBuilder) NilRepo() *NodeBuilder { + nb.nilrepo = true + return nb +} + func (nb *NodeBuilder) Build(ctx context.Context) (*IpfsNode, error) { if nb.built { return nil, ErrAlreadyBuilt } nb.built = true if nb.repo == nil { - r, err := defaultRepo() + var d ds.Datastore + d = ds.NewMapDatastore() + if nb.nilrepo { + d = ds.NewNullDatastore() + } + r, err := defaultRepo(dsync.MutexWrap(d)) if err != nil { return nil, err } diff --git a/core/commands/add.go b/core/commands/add.go index 2aad4a3b9..721858c62 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -58,6 +58,7 @@ remains to be implemented. cmds.BoolOption(progressOptionName, "p", "Stream progress data"), cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"), cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation"), + cmds.BoolOption("only-hash", "n", "Only chunk and hash the specified content, don't write to disk"), }, PreRun: func(req cmds.Request) error { if quiet, _, _ := req.Option("quiet").Bool(); quiet { @@ -92,6 +93,16 @@ remains to be implemented. progress, _, _ := req.Option(progressOptionName).Bool() trickle, _, _ := req.Option(trickleOptionName).Bool() wrap, _, _ := req.Option(wrapOptionName).Bool() + hash, _, _ := req.Option("only-hash").Bool() + + if hash { + nilnode, err := core.NewNodeBuilder().NilRepo().Build(n.Context()) + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + n = nilnode + } outChan := make(chan interface{}, 8) res.SetOutput((<-chan interface{})(outChan)) From 60ad7a5afc7059e418755d3ffcd194eca857a47e Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 23 Jun 2015 09:04:31 -0700 Subject: [PATCH 2/2] sharness tests for --only-hash License: MIT Signed-off-by: Jeromy --- test/sharness/t0040-add-and-cat.sh | 8 ++++++++ test/sharness/t0041-add-cat-offline.sh | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index 03cdb75d4..0e3e6fb08 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -39,6 +39,14 @@ test_expect_success "ipfs add output looks good" ' test_cmp expected actual ' +test_expect_success "ipfs add --only-hash succeeds" ' + ipfs add --only-hash mountdir/hello.txt > oh_actual +' + +test_expect_success "ipfs add --only-hash output looks good" ' + ipfs add --only-hash mountdir/hello.txt > oh_actual +' + test_expect_success "ipfs cat succeeds" ' ipfs cat "$HASH" >actual ' diff --git a/test/sharness/t0041-add-cat-offline.sh b/test/sharness/t0041-add-cat-offline.sh index cb370eec0..a8c0e36a6 100755 --- a/test/sharness/t0041-add-cat-offline.sh +++ b/test/sharness/t0041-add-cat-offline.sh @@ -15,6 +15,20 @@ test_expect_success "ipfs add file succeeds" ' HASH=$(ipfs add -q afile) ' +test_expect_success "ipfs add output looks good" ' + echo Qmb1EXrDyKhNWfvLPYK4do3M9nU7BuLAcbqBir6aUrDsRY > expected && + echo $HASH > actual && + test_cmp expected actual +' + +test_expect_success "ipfs add --only-hash succeeds" ' + ipfs add -q --only-hash afile > ho_output +' + +test_expect_success "ipfs add --only-hash output looks good" ' + test_cmp expected ho_output +' + test_expect_success "ipfs cat file suceeds" ' ipfs cat $HASH > out_1 '