Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 724fe0f

Browse files
authored
Merge pull request #5331 from ipfs/refactor/coreapi/block
block cmd: Use coreapi
2 parents 618aaa4 + 6834071 commit 724fe0f

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type BlockStat interface {
1919
// BlockAPI specifies the interface to the block layer
2020
type BlockAPI interface {
2121
// Put imports raw block data, hashing it using specified settings.
22-
Put(context.Context, io.Reader, ...options.BlockPutOption) (ResolvedPath, error)
22+
Put(context.Context, io.Reader, ...options.BlockPutOption) (BlockStat, error)
2323

2424
// Get attempts to resolve the path and return a reader for data in the block
2525
Get(context.Context, Path) (io.Reader, error)

options/block.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package options
22

33
import (
4-
"gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
4+
"fmt"
5+
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
6+
cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
57
)
68

79
type BlockPutSettings struct {
@@ -17,20 +19,52 @@ type BlockRmSettings struct {
1719
type BlockPutOption func(*BlockPutSettings) error
1820
type BlockRmOption func(*BlockRmSettings) error
1921

20-
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
22+
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, error) {
2123
options := &BlockPutSettings{
22-
Codec: "v0",
23-
MhType: multihash.SHA2_256,
24+
Codec: "",
25+
MhType: mh.SHA2_256,
2426
MhLength: -1,
2527
}
2628

2729
for _, opt := range opts {
2830
err := opt(options)
2931
if err != nil {
30-
return nil, err
32+
return nil, cid.Prefix{}, err
3133
}
3234
}
33-
return options, nil
35+
36+
var pref cid.Prefix
37+
pref.Version = 1
38+
39+
if options.Codec == "" {
40+
if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
41+
options.Codec = "protobuf"
42+
} else {
43+
options.Codec = "v0"
44+
}
45+
}
46+
47+
if options.Codec == "v0" && options.MhType == mh.SHA2_256 {
48+
pref.Version = 0
49+
}
50+
51+
formatval, ok := cid.Codecs[options.Codec]
52+
if !ok {
53+
return nil, cid.Prefix{}, fmt.Errorf("unrecognized format: %s", options.Codec)
54+
}
55+
56+
if options.Codec == "v0" {
57+
if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
58+
return nil, cid.Prefix{}, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
59+
}
60+
}
61+
62+
pref.Codec = formatval
63+
64+
pref.MhType = options.MhType
65+
pref.MhLength = options.MhLength
66+
67+
return options, pref, nil
3468
}
3569

3670
func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {

0 commit comments

Comments
 (0)