Skip to content

Commit 988fb7a

Browse files
committed
block cmd: use coreapi
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 83f22d5 commit 988fb7a

File tree

5 files changed

+82
-127
lines changed

5 files changed

+82
-127
lines changed

core/commands/block.go

Lines changed: 62 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package commands
22

33
import (
4-
"bytes"
5-
"context"
6-
"errors"
74
"fmt"
85
"io"
9-
"io/ioutil"
106
"os"
117

128
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
13-
e "github.com/ipfs/go-ipfs/core/commands/e"
9+
"github.com/ipfs/go-ipfs/core/commands/e"
10+
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
11+
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1412

1513
"gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
1614
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
17-
blocks "gx/ipfs/QmVzK524a2VWLqyvtBeiHKsUAWYgeAk4DBeZoY7vpNPNRx/go-block-format"
18-
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
1915
"gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
2016
)
2117

@@ -63,15 +59,27 @@ on raw IPFS blocks. It outputs the following to stdout:
6359
cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to stat.").EnableStdin(),
6460
},
6561
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
66-
b, err := getBlockForKey(req.Context, env, req.Arguments[0])
62+
api, err := GetApi(env)
63+
if err != nil {
64+
res.SetError(err, cmdkit.ErrNormal)
65+
return
66+
}
67+
68+
p, err := coreiface.ParsePath(req.Arguments[0])
69+
if err != nil {
70+
res.SetError(err, cmdkit.ErrNormal)
71+
return
72+
}
73+
74+
b, err := api.Block().Stat(req.Context, p)
6775
if err != nil {
6876
res.SetError(err, cmdkit.ErrNormal)
6977
return
7078
}
7179

7280
err = cmds.EmitOnce(res, &BlockStat{
73-
Key: b.Cid().String(),
74-
Size: len(b.RawData()),
81+
Key: b.Path().Cid().String(),
82+
Size: b.Size(),
7583
})
7684
if err != nil {
7785
log.Error(err)
@@ -103,13 +111,25 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
103111
cmdkit.StringArg("key", true, false, "The base58 multihash of an existing block to get.").EnableStdin(),
104112
},
105113
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
106-
b, err := getBlockForKey(req.Context, env, req.Arguments[0])
114+
api, err := GetApi(env)
115+
if err != nil {
116+
res.SetError(err, cmdkit.ErrNormal)
117+
return
118+
}
119+
120+
p, err := coreiface.ParsePath(req.Arguments[0])
121+
if err != nil {
122+
res.SetError(err, cmdkit.ErrNormal)
123+
return
124+
}
125+
126+
r, err := api.Block().Get(req.Context, p)
107127
if err != nil {
108128
res.SetError(err, cmdkit.ErrNormal)
109129
return
110130
}
111131

112-
err = res.Emit(bytes.NewReader(b.RawData()))
132+
err = res.Emit(r)
113133
if err != nil {
114134
log.Error(err)
115135
}
@@ -137,7 +157,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
137157
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
138158
},
139159
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
140-
n, err := GetNode(env)
160+
api, err := GetApi(env)
141161
if err != nil {
142162
res.SetError(err, cmdkit.ErrNormal)
143163
return
@@ -149,18 +169,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
149169
return
150170
}
151171

152-
data, err := ioutil.ReadAll(file)
153-
if err != nil {
154-
res.SetError(err, cmdkit.ErrNormal)
155-
return
156-
}
157-
158-
err = file.Close()
159-
if err != nil {
160-
res.SetError(err, cmdkit.ErrNormal)
161-
return
162-
}
163-
164172
mhtype, _ := req.Options["mhtype"].(string)
165173
mhtval, ok := mh.Names[mhtype]
166174
if !ok {
@@ -169,8 +177,11 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
169177
return
170178
}
171179

172-
var pref cid.Prefix
173-
pref.Version = 1
180+
mhlen, ok := req.Options["mhlen"].(int)
181+
if !ok {
182+
res.SetError("missing option \"mhlen\"", cmdkit.ErrNormal)
183+
return
184+
}
174185

175186
format, formatSet := req.Options["format"].(string)
176187
if !formatSet {
@@ -181,50 +192,15 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
181192
}
182193
}
183194

184-
if format == "v0" {
185-
pref.Version = 0
186-
}
187-
formatval, ok := cid.Codecs[format]
188-
if !ok {
189-
res.SetError(fmt.Errorf("unrecognized format: '%s'", format), cmdkit.ErrNormal)
190-
return
191-
}
192-
if mhtval != mh.SHA2_256 && pref.Version == 0 {
193-
res.SetError(errors.New("cannot generate CIDv0 with non-sha256 hash function"), cmdkit.ErrNormal)
194-
return
195-
}
196-
197-
pref.Codec = formatval
198-
pref.MhType = mhtval
199-
200-
mhlen, ok := req.Options["mhlen"].(int)
201-
if !ok {
202-
res.SetError("missing option \"mhlen\"", cmdkit.ErrNormal)
203-
return
204-
}
205-
pref.MhLength = mhlen
206-
207-
bcid, err := pref.Sum(data)
208-
if err != nil {
209-
res.SetError(err, cmdkit.ErrNormal)
210-
return
211-
}
212-
213-
b, err := blocks.NewBlockWithCid(data, bcid)
214-
if err != nil {
215-
res.SetError(err, cmdkit.ErrNormal)
216-
return
217-
}
218-
219-
err = n.Blocks.AddBlock(b)
195+
p, err := api.Block().Put(req.Context, file, options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
220196
if err != nil {
221197
res.SetError(err, cmdkit.ErrNormal)
222198
return
223199
}
224200

225201
err = cmds.EmitOnce(res, &BlockStat{
226-
Key: b.Cid().String(),
227-
Size: len(data),
202+
Key: p.Path().Cid().String(),
203+
Size: p.Size(),
228204
})
229205
if err != nil {
230206
log.Error(err)
@@ -243,29 +219,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
243219
Type: BlockStat{},
244220
}
245221

246-
func getBlockForKey(ctx context.Context, env cmds.Environment, skey string) (blocks.Block, error) {
247-
if len(skey) == 0 {
248-
return nil, fmt.Errorf("zero length cid invalid")
249-
}
250-
251-
n, err := GetNode(env)
252-
if err != nil {
253-
return nil, err
254-
}
255-
256-
c, err := cid.Decode(skey)
257-
if err != nil {
258-
return nil, err
259-
}
260-
261-
b, err := n.Blocks.GetBlock(ctx, c)
262-
if err != nil {
263-
return nil, err
264-
}
265-
266-
return b, nil
267-
}
268-
269222
var blockRmCmd = &cmds.Command{
270223
Helptext: cmdkit.HelpText{
271224
Tagline: "Remove IPFS block(s).",
@@ -282,38 +235,40 @@ It takes a list of base58 encoded multihashes to remove.
282235
cmdkit.BoolOption("quiet", "q", "Write minimal output."),
283236
},
284237
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
285-
n, err := GetNode(env)
238+
api, err := GetApi(env)
286239
if err != nil {
287240
res.SetError(err, cmdkit.ErrNormal)
288241
return
289242
}
290-
hashes := req.Arguments
243+
291244
force, _ := req.Options["force"].(bool)
292245
quiet, _ := req.Options["quiet"].(bool)
293-
cids := make([]*cid.Cid, 0, len(hashes))
294-
for _, hash := range hashes {
295-
c, err := cid.Decode(hash)
246+
247+
// TODO: use batching coreapi when done
248+
for _, b := range req.Arguments {
249+
p, err := coreiface.ParsePath(b)
296250
if err != nil {
297-
err = fmt.Errorf("invalid content id: %s (%s)", hash, err)
298251
res.SetError(err, cmdkit.ErrNormal)
299252
return
300253
}
301254

302-
cids = append(cids, c)
303-
}
304-
ch, err := util.RmBlocks(n.Blockstore, n.Pinning, cids, util.RmBlocksOpts{
305-
Quiet: quiet,
306-
Force: force,
307-
})
255+
rp, err := api.ResolvePath(req.Context, p)
256+
if err != nil {
257+
res.SetError(err, cmdkit.ErrNormal)
258+
return
259+
}
308260

309-
if err != nil {
310-
res.SetError(err, cmdkit.ErrNormal)
311-
return
312-
}
261+
err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
262+
if err != nil && !quiet {
263+
res.Emit(&util.RemovedBlock{
264+
Hash: rp.Cid().String(),
265+
Error: err.Error(),
266+
})
267+
}
313268

314-
err = res.Emit(ch)
315-
if err != nil {
316-
log.Error(err)
269+
res.Emit(&util.RemovedBlock{
270+
Hash: rp.Cid().String(),
271+
})
317272
}
318273
},
319274
PostRun: cmds.PostRunMap{

core/coreapi/block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type BlockStat struct {
2424
size int
2525
}
2626

27-
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.ResolvedPath, error) {
27+
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) {
2828
settings, err := caopts.BlockPutOptions(opts...)
2929
if err != nil {
3030
return nil, err
@@ -65,7 +65,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
6565
return nil, err
6666
}
6767

68-
return coreiface.IpldPath(b.Cid()), nil
68+
return &BlockStat{path: coreiface.IpldPath(b.Cid()), size: len(data)}, nil
6969
}
7070

7171
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {

core/coreapi/block_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func TestBlockPut(t *testing.T) {
2323
t.Error(err)
2424
}
2525

26-
if res.Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
27-
t.Errorf("got wrong cid: %s", res.Cid().String())
26+
if res.Path().Cid().String() != "QmPyo15ynbVrSTVdJL9th7JysHaAbXt9dM9tXk1bMHbRtk" {
27+
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
2828
}
2929
}
3030

@@ -40,8 +40,8 @@ func TestBlockPutFormat(t *testing.T) {
4040
t.Error(err)
4141
}
4242

43-
if res.Cid().String() != "zdpuAn4amuLWo8Widi5v6VQpuo2dnpnwbVE3oB6qqs7mDSeoa" {
44-
t.Errorf("got wrong cid: %s", res.Cid().String())
43+
if res.Path().Cid().String() != "zdpuAn4amuLWo8Widi5v6VQpuo2dnpnwbVE3oB6qqs7mDSeoa" {
44+
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
4545
}
4646
}
4747

@@ -57,8 +57,8 @@ func TestBlockPutHash(t *testing.T) {
5757
t.Error(err)
5858
}
5959

60-
if res.Cid().String() != "zBurKB9YZkcDf6xa53WBE8CFX4ydVqAyf9KPXBFZt5stJzEstaS8Hukkhu4gwpMtc1xHNDbzP7sPtQKyWsP3C8fbhkmrZ" {
61-
t.Errorf("got wrong cid: %s", res.Cid().String())
60+
if res.Path().Cid().String() != "zBurKB9YZkcDf6xa53WBE8CFX4ydVqAyf9KPXBFZt5stJzEstaS8Hukkhu4gwpMtc1xHNDbzP7sPtQKyWsP3C8fbhkmrZ" {
61+
t.Errorf("got wrong cid: %s", res.Path().Cid().String())
6262
}
6363
}
6464

@@ -74,7 +74,7 @@ func TestBlockGet(t *testing.T) {
7474
t.Error(err)
7575
}
7676

77-
r, err := api.Block().Get(ctx, res)
77+
r, err := api.Block().Get(ctx, res.Path())
7878
if err != nil {
7979
t.Error(err)
8080
}
@@ -101,7 +101,7 @@ func TestBlockRm(t *testing.T) {
101101
t.Error(err)
102102
}
103103

104-
r, err := api.Block().Get(ctx, res)
104+
r, err := api.Block().Get(ctx, res.Path())
105105
if err != nil {
106106
t.Error(err)
107107
}
@@ -115,28 +115,28 @@ func TestBlockRm(t *testing.T) {
115115
t.Error("didn't get correct data back")
116116
}
117117

118-
err = api.Block().Rm(ctx, res)
118+
err = api.Block().Rm(ctx, res.Path())
119119
if err != nil {
120120
t.Error(err)
121121
}
122122

123-
_, err = api.Block().Get(ctx, res)
123+
_, err = api.Block().Get(ctx, res.Path())
124124
if err == nil {
125125
t.Error("expected err to exist")
126126
}
127127
if err.Error() != "blockservice: key not found" {
128128
t.Errorf("unexpected error; %s", err.Error())
129129
}
130130

131-
err = api.Block().Rm(ctx, res)
131+
err = api.Block().Rm(ctx, res.Path())
132132
if err == nil {
133133
t.Error("expected err to exist")
134134
}
135135
if err.Error() != "blockstore: block not found" {
136136
t.Errorf("unexpected error; %s", err.Error())
137137
}
138138

139-
err = api.Block().Rm(ctx, res, opt.Block.Force(true))
139+
err = api.Block().Rm(ctx, res.Path(), opt.Block.Force(true))
140140
if err != nil {
141141
t.Error(err)
142142
}
@@ -154,12 +154,12 @@ func TestBlockStat(t *testing.T) {
154154
t.Error(err)
155155
}
156156

157-
stat, err := api.Block().Stat(ctx, res)
157+
stat, err := api.Block().Stat(ctx, res.Path())
158158
if err != nil {
159159
t.Error(err)
160160
}
161161

162-
if stat.Path().String() != res.String() {
162+
if stat.Path().String() != res.Path().String() {
163163
t.Error("paths don't match")
164164
}
165165

core/coreapi/interface/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)

0 commit comments

Comments
 (0)