Skip to content

Commit 07b7fc6

Browse files
committed
cmds/pin: use coreapi/pin
License: MIT Signed-off-by: Overbool <[email protected]>
1 parent c72f6c9 commit 07b7fc6

File tree

5 files changed

+148
-127
lines changed

5 files changed

+148
-127
lines changed

core/commands/pin.go

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
core "github.com/ipfs/go-ipfs/core"
1111
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1212
e "github.com/ipfs/go-ipfs/core/commands/e"
13+
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1314
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1415
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
15-
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
1616
pin "github.com/ipfs/go-ipfs/pin"
1717

1818
cmds "gx/ipfs/QmPdvMtgpnMuU68mWhGtzCxnddXJoV96tT9aPcNbQsqPaM/go-ipfs-cmds"
@@ -39,7 +39,14 @@ var PinCmd = &cmds.Command{
3939
}
4040

4141
type PinOutput struct {
42-
Pins []string
42+
Hash string
43+
Error string
44+
}
45+
46+
// PinUpdateOutput represents the pin update output
47+
type PinUpdateOutput struct {
48+
From string
49+
To string
4350
}
4451

4552
type AddPinOutput struct {
@@ -88,24 +95,25 @@ var addPinCmd = &cmds.Command{
8895
}
8996

9097
if !showProgress {
91-
added, err := corerepo.Pin(n, api, req.Context, req.Arguments, recursive)
98+
added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
9299
if err != nil {
93100
return err
94101
}
95-
return cmds.EmitOnce(res, &AddPinOutput{Pins: cidsToStrings(added)})
102+
103+
return cmds.EmitOnce(res, &AddPinOutput{Pins: added})
96104
}
97105

98106
v := new(dag.ProgressTracker)
99107
ctx := v.DeriveContext(req.Context)
100108

101109
type pinResult struct {
102-
pins []cid.Cid
110+
pins []string
103111
err error
104112
}
105113

106114
ch := make(chan pinResult, 1)
107115
go func() {
108-
added, err := corerepo.Pin(n, api, ctx, req.Arguments, recursive)
116+
added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
109117
ch <- pinResult{pins: added, err: err}
110118
}()
111119

@@ -124,7 +132,7 @@ var addPinCmd = &cmds.Command{
124132
return err
125133
}
126134
}
127-
return res.Emit(&AddPinOutput{Pins: cidsToStrings(val.pins)})
135+
return res.Emit(&AddPinOutput{Pins: val.pins})
128136
case <-ticker.C:
129137
if err := res.Emit(&AddPinOutput{Progress: v.Value()}); err != nil {
130138
return err
@@ -181,6 +189,28 @@ var addPinCmd = &cmds.Command{
181189
},
182190
}
183191

192+
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recursive bool) ([]string, error) {
193+
added := make([]string, len(paths))
194+
for i, b := range paths {
195+
p, err := coreiface.ParsePath(b)
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
rp, err := api.ResolvePath(ctx, p)
201+
if err != nil {
202+
return nil, err
203+
}
204+
205+
if err := api.Pin().Add(ctx, p, options.Pin.Recursive(recursive)); err != nil {
206+
return nil, err
207+
}
208+
added[i] = rp.Cid().String()
209+
}
210+
211+
return added, nil
212+
}
213+
184214
var rmPinCmd = &cmds.Command{
185215
Helptext: cmdkit.HelpText{
186216
Tagline: "Remove pinned objects from local storage.",
@@ -198,11 +228,6 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
198228
},
199229
Type: PinOutput{},
200230
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
201-
n, err := cmdenv.GetNode(env)
202-
if err != nil {
203-
return err
204-
}
205-
206231
api, err := cmdenv.GetApi(env)
207232
if err != nil {
208233
return err
@@ -215,20 +240,62 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
215240
return err
216241
}
217242

218-
removed, err := corerepo.Unpin(n, api, req.Context, req.Arguments, recursive)
219-
if err != nil {
220-
return err
243+
for _, b := range req.Arguments {
244+
p, err := coreiface.ParsePath(b)
245+
if err != nil {
246+
return err
247+
}
248+
249+
rp, err := api.ResolvePath(req.Context, p)
250+
if err != nil {
251+
return err
252+
}
253+
254+
if err := api.Pin().Rm(req.Context, rp, options.Pin.RmRecursive(recursive)); err != nil {
255+
if err := res.Emit(&PinOutput{
256+
Hash: rp.Cid().String(),
257+
Error: err.Error(),
258+
}); err != nil {
259+
return err
260+
}
261+
continue
262+
}
263+
264+
if err := res.Emit(&PinOutput{
265+
Hash: rp.Cid().String(),
266+
}); err != nil {
267+
return err
268+
}
221269
}
222270

223-
return cmds.EmitOnce(res, &PinOutput{cidsToStrings(removed)})
271+
return nil
224272
},
225-
Encoders: cmds.EncoderMap{
226-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
227-
for _, k := range out.Pins {
228-
fmt.Fprintf(w, "unpinned %s\n", k)
273+
PostRun: cmds.PostRunMap{
274+
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
275+
failed := false
276+
for {
277+
out, err := res.Next()
278+
if err == io.EOF {
279+
break
280+
} else if err != nil {
281+
return err
282+
}
283+
r := out.(*PinOutput)
284+
if r.Hash == "" && r.Error != "" {
285+
return fmt.Errorf("aborted: %s", r.Error)
286+
} else if r.Error != "" {
287+
failed = true
288+
fmt.Fprintf(os.Stderr, "cannot unpin %s: %s\n", r.Hash, r.Error)
289+
} else {
290+
fmt.Fprintf(os.Stdout, "unpinned %s\n", r.Hash)
291+
}
292+
}
293+
294+
if failed {
295+
return fmt.Errorf("some hash not unpinned")
229296
}
230297
return nil
231-
}),
298+
},
232299
},
233300
}
234301

@@ -364,7 +431,7 @@ new pin and removing the old one.
364431
Options: []cmdkit.Option{
365432
cmdkit.BoolOption(pinUnpinOptionName, "Remove the old pin.").WithDefault(true),
366433
},
367-
Type: PinOutput{},
434+
Type: PinUpdateOutput{},
368435
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
369436
api, err := cmdenv.GetApi(env)
370437
if err != nil {
@@ -388,11 +455,11 @@ new pin and removing the old one.
388455
return err
389456
}
390457

391-
return cmds.EmitOnce(res, &PinOutput{Pins: []string{from.String(), to.String()}})
458+
return cmds.EmitOnce(res, &PinUpdateOutput{From: from.String(), To: to.String()})
392459
},
393460
Encoders: cmds.EncoderMap{
394-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
395-
fmt.Fprintf(w, "updated %s to %s\n", out.Pins[0], out.Pins[1])
461+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinUpdateOutput) error {
462+
fmt.Fprintf(w, "updated %s to %s\n", out.From, out.To)
396463
return nil
397464
}),
398465
},
@@ -628,11 +695,3 @@ func (r PinVerifyRes) Format(out io.Writer) {
628695
}
629696
}
630697
}
631-
632-
func cidsToStrings(cs []cid.Cid) []string {
633-
out := make([]string, 0, len(cs))
634-
for _, c := range cs {
635-
out = append(out, c.String())
636-
}
637-
return out
638-
}

core/coreapi/interface/options/pin.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ type PinLsSettings struct {
88
Type string
99
}
1010

11+
// PinRmSettings represents the settings of pin rm command
12+
type PinRmSettings struct {
13+
Recursive bool
14+
Force bool
15+
}
16+
1117
type PinUpdateSettings struct {
1218
Unpin bool
1319
}
1420

1521
type PinAddOption func(*PinAddSettings) error
16-
type PinLsOption func(settings *PinLsSettings) error
22+
23+
// PinRmOption pin rm option func
24+
type PinRmOption func(*PinRmSettings) error
25+
26+
// PinLsOption pin ls option func
27+
type PinLsOption func(*PinLsSettings) error
1728
type PinUpdateOption func(*PinUpdateSettings) error
1829

1930
func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
@@ -31,6 +42,21 @@ func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
3142
return options, nil
3243
}
3344

45+
// PinRmOptions pin rm options
46+
func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
47+
options := &PinRmSettings{
48+
Recursive: true,
49+
}
50+
51+
for _, opt := range opts {
52+
if err := opt(options); err != nil {
53+
return nil, err
54+
}
55+
}
56+
57+
return options, nil
58+
}
59+
3460
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
3561
options := &PinLsSettings{
3662
Type: "all",
@@ -102,6 +128,14 @@ func (pinOpts) Recursive(recursive bool) PinAddOption {
102128
}
103129
}
104130

131+
// RmRecursive is an option for Pin.Rm
132+
func (pinOpts) RmRecursive(recursive bool) PinRmOption {
133+
return func(settings *PinRmSettings) error {
134+
settings.Recursive = recursive
135+
return nil
136+
}
137+
}
138+
105139
// Type is an option for Pin.Ls which allows to specify which pin types should
106140
// be returned
107141
//

core/coreapi/interface/pin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type PinAPI interface {
4343
Ls(context.Context, ...options.PinLsOption) ([]Pin, error)
4444

4545
// Rm removes pin for object specified by the path
46-
Rm(context.Context, Path) error
46+
Rm(context.Context, Path, ...options.PinRmOption) error
4747

4848
// Update changes one pin to another, skipping checks for matching paths in
4949
// the old tree

core/coreapi/pin.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@ import (
66

77
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
88
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9-
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
109
bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
11-
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
12-
1310
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1411
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
12+
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
1513
)
1614

1715
type PinAPI CoreAPI
1816

1917
func (api *PinAPI) Add(ctx context.Context, p coreiface.Path, opts ...caopts.PinAddOption) error {
20-
settings, err := caopts.PinAddOptions(opts...)
18+
defer api.node.Blockstore.PinLock().Unlock()
19+
20+
dagNode, err := api.core().ResolveNode(ctx, p)
2121
if err != nil {
22-
return err
22+
return fmt.Errorf("pin: %s", err)
2323
}
2424

25-
rp, err := api.core().ResolvePath(ctx, p)
25+
settings, err := caopts.PinAddOptions(opts...)
2626
if err != nil {
2727
return err
2828
}
2929

30-
defer api.node.Blockstore.PinLock().Unlock()
31-
32-
_, err = corerepo.Pin(api.node, api.core(), ctx, []string{rp.Cid().String()}, settings.Recursive)
30+
err = api.node.Pinning.Pin(ctx, dagNode, settings.Recursive)
3331
if err != nil {
34-
return err
32+
return fmt.Errorf("pin: %s", err)
3533
}
3634

3735
return api.node.Pinning.Flush()
@@ -52,12 +50,22 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreif
5250
return api.pinLsAll(settings.Type, ctx)
5351
}
5452

55-
func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error {
56-
_, err := corerepo.Unpin(api.node, api.core(), ctx, []string{p.String()}, true)
53+
// Rm pin rm api
54+
func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.PinRmOption) error {
55+
rp, err := api.core().ResolvePath(ctx, p)
56+
if err != nil {
57+
return err
58+
}
59+
60+
settings, err := caopts.PinRmOptions(opts...)
5761
if err != nil {
5862
return err
5963
}
6064

65+
if err = api.node.Pinning.Unpin(ctx, rp.Cid(), settings.Recursive); err != nil {
66+
return err
67+
}
68+
6169
return api.node.Pinning.Flush()
6270
}
6371

0 commit comments

Comments
 (0)