diff --git a/core/builder.go b/core/builder.go index 76953ad048beac88b4b2ca99530fb84b87cf1986..2eb168a2135172e7a4722c1752a1541eaa973674 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 2aad4a3b95a81d8b11e820179e16df7153c4e2d7..721858c62880019b3332d1e6065aad530277de48 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)) diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index 03cdb75d4df7d3d187ea77fa16b1eeaa271c3a94..0e3e6fb08fdd12426836087b384d33677821c1d0 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 cb370eec00910f31c35f38e91347fa9e9a8c1747..a8c0e36a6afaf821772da881acd5b03743a8b174 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 '