Commit aefdb4e0 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

Merge pull request #1417 from ipfs/feat/only-hash

add option to only hash input
parents 65874eb0 60ad7a5a
......@@ -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
}
......
......@@ -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))
......
......@@ -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
'
......
......@@ -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
'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment