Skip to content

Commit bb4a520

Browse files
committed
Add "id-hash-limit" option to "ipfs add" command.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
1 parent 42ee69d commit bb4a520

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

core/commands/add.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
dag "github.com/ipfs/go-ipfs/merkledag"
1515
dagtest "github.com/ipfs/go-ipfs/merkledag/test"
1616
mfs "github.com/ipfs/go-ipfs/mfs"
17-
ft "github.com/ipfs/go-ipfs/unixfs"
1817
cide "github.com/ipfs/go-ipfs/thirdparty/cidextra"
18+
ft "github.com/ipfs/go-ipfs/unixfs"
1919

2020
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
2121
bstore "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore"
@@ -44,6 +44,7 @@ const (
4444
fstoreCacheOptionName = "fscache"
4545
cidVersionOptionName = "cid-version"
4646
hashOptionName = "hash"
47+
idHashLimitOptionName = "id-hash-limit"
4748
)
4849

4950
const adderOutChanSize = 8
@@ -120,6 +121,7 @@ You can now check what blocks have been created by:
120121
cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
121122
cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
122123
cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
124+
cmdkit.IntOption(idHashLimitOptionName, "Id hash maxium size. -1 disables. (experimental)").WithDefault(-1),
123125
},
124126
PreRun: func(req *cmds.Request, env cmds.Environment) error {
125127
quiet, _ := req.Options[quietOptionName].(bool)
@@ -173,6 +175,7 @@ You can now check what blocks have been created by:
173175
fscache, _ := req.Options[fstoreCacheOptionName].(bool)
174176
cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
175177
hashFunStr, _ := req.Options[hashOptionName].(string)
178+
idHashLimit, _ := req.Options[idHashLimitOptionName].(int)
176179

177180
// The arguments are subject to the following constraints.
178181
//
@@ -282,6 +285,12 @@ You can now check what blocks have been created by:
282285
fileAdder.NoCopy = nocopy
283286
fileAdder.CidOpts = &cide.Opts{Prefix: prefix}
284287

288+
err = fileAdder.CidOpts.SetIdHashLimit(idHashLimit)
289+
if err != nil {
290+
res.SetError(err, cmdkit.ErrNormal)
291+
return
292+
}
293+
285294
if hash {
286295
md := dagtest.Mock()
287296
emptyDirNode := ft.EmptyDirNode()

test/sharness/t0040-add-and-cat.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ test_add_cat_expensive "--cid-version=1" "zdj7WcatQrtuE4WMkS4XsfsMixuQN2po4irkYh
554554
# encoded with the blake2b-256 hash funtion
555555
test_add_cat_expensive '--hash=blake2b-256' "zDMZof1kwndounDzQCANUHjiE3zt1mPEgx7RE3JTHoZrRRa79xcv"
556556

557-
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&pin=true&progress=true&recursive=true&stream-channels=true:"
557+
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&id-hash-limit=-1&pin=true&progress=true&recursive=true&stream-channels=true:"
558558

559559
test_add_pwd_is_symlink
560560

thirdparty/cidextra/cidextra.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package cidextra
22

33
import (
4+
"fmt"
5+
46
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
57
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
68
)
79

10+
var MaxIdHashLen = 64
11+
812
type Opts struct {
9-
cid.Prefix
13+
cid.Prefix
1014
idHashThres int // 0 disables, otherwise 1 + limit
1115
}
1216

1317
// SetIdHashLimit is self explanatory
14-
func (o *Opts) SetIdHashLimit(l int) *Opts {
15-
// FIXME: Check against hard limit
18+
func (o *Opts) SetIdHashLimit(l int) error {
19+
if l > MaxIdHashLen {
20+
return fmt.Errorf("identity hash limit of %d larger then maxium allowed limit of %d",
21+
l, MaxIdHashLen)
22+
}
1623
o.idHashThres = l + 1
17-
return o
24+
return nil
1825
}
1926

20-
2127
// Sum returns a newly constructed Cid
2228
func (o Opts) Sum(data []byte) (*cid.Cid, error) {
2329
if len(data) < o.idHashThres {

thirdparty/idstore/idstore.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"fmt"
66

7+
cide "github.com/ipfs/go-ipfs/thirdparty/cidextra"
8+
79
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
810
bls "gx/ipfs/QmaG4DZ4JaqEfvPWt5nPPgoTzhc1tr1T3f4Nu9Jpdm8ymY/go-ipfs-blockstore"
911
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
1012
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
1113
)
1214

13-
var MaxIdHashLen = 64
14-
1515
// idstore wraps a BlockStore to add support for identity hashes
1616
type idstore struct {
1717
bs bls.Blockstore
@@ -26,9 +26,9 @@ func extractContents(k *cid.Cid) (bool, []byte, error) {
2626
if err != nil || dmh.Code != mh.ID {
2727
return false, nil, nil
2828
}
29-
if len(dmh.Digest) > MaxIdHashLen {
29+
if len(dmh.Digest) > cide.MaxIdHashLen {
3030
return true, nil, fmt.Errorf("identity hash of %d bytes is longer than maximum size of %d bytes",
31-
len(dmh.Digest), MaxIdHashLen)
31+
len(dmh.Digest), cide.MaxIdHashLen)
3232
}
3333
return true, dmh.Digest, nil
3434
}

0 commit comments

Comments
 (0)