@cryptotaxi247 / kubo / commits / abebd4e1d

block put --pin option

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@005752045c872e4dabb17e5c9ba1732f3cf04ea6 This commit was moved from ipfs/boxo@9ea89f38600dab60d9a09740c28d935ef9bfc202

Łukasz Magiera committed Feb 4, 2019 at 18:05 UTC abebd4e1d81dc18a53db02f740784084c7ab3a05
2 files changed +49
core/coreiface/options/block.go
+11
@@ -10,6 +10,7 @@ type BlockPutSettings struct {
10 Codec string
11 MhType uint64
12 MhLength int
13 + Pin bool
14 }
15
16 type BlockRmSettings struct {
@@ -24,6 +25,7 @@ func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, err
25 Codec: "",
26 MhType: mh.SHA2_256,
27 MhLength: -1,
28 + Pin: false,
29 }
30
31 for _, opt := range opts {
@@ -105,6 +107,15 @@ func (blockOpts) Hash(mhType uint64, mhLen int) BlockPutOption {
107 }
108 }
109
110 +// Pin is an option for Block.Put which specifies whether to (recursively) pin
111 +// added blocks
112 +func (blockOpts) Pin(pin bool) BlockPutOption {
113 + return func(settings *BlockPutSettings) error {
114 + settings.Pin = pin
115 + return nil
116 + }
117 +}
118 +
119 // Force is an option for Block.Rm which, when set to true, will ignore
120 // non-existing blocks
121 func (blockOpts) Force(force bool) BlockRmOption {
core/coreiface/tests/block.go
+38
@@ -26,6 +26,7 @@ func (tp *provider) TestBlock(t *testing.T) {
26 t.Run("TestBlockGet", tp.TestBlockGet)
27 t.Run("TestBlockRm", tp.TestBlockRm)
28 t.Run("TestBlockStat", tp.TestBlockStat)
29 + t.Run("TestBlockPin", tp.TestBlockPin)
30 }
31
32 func (tp *provider) TestBlockPut(t *testing.T) {
@@ -203,3 +204,40 @@ func (tp *provider) TestBlockStat(t *testing.T) {
204 t.Error("length doesn't match")
205 }
206 }
207 +
208 +func (tp *provider) TestBlockPin(t *testing.T) {
209 + ctx, cancel := context.WithCancel(context.Background())
210 + defer cancel()
211 + api, err := tp.makeAPI(ctx)
212 + if err != nil {
213 + t.Error(err)
214 + }
215 +
216 + _, err = api.Block().Put(ctx, strings.NewReader(`Hello`))
217 + if err != nil {
218 + t.Fatal(err)
219 + }
220 +
221 + if pins, err := api.Pin().Ls(ctx); err != nil || len(pins) != 0 {
222 + t.Fatal("expected 0 pins")
223 + }
224 +
225 + res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Pin(true))
226 + if err != nil {
227 + t.Fatal(err)
228 + }
229 +
230 + pins, err := api.Pin().Ls(ctx)
231 + if err != nil {
232 + return
233 + }
234 + if len(pins) != 1 {
235 + t.Fatal("expected 1 pin")
236 + }
237 + if pins[0].Type() != "recursive" {
238 + t.Error("expected a recursive pin")
239 + }
240 + if pins[0].Path().String() != res.Path().String() {
241 + t.Error("pin path didn't match")
242 + }
243 +}