object put --pin option
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jul 27, 2017 at 12:47 UTC
e1bc025e38f31cd43da1ff60eb634a5aac3397be
2 files changed
+44
-1
core/commands/object/object.go
+27
@@ -16,6 +16,7 @@ import (
16
core "github.com/ipfs/go-ipfs/core"
17
dag "github.com/ipfs/go-ipfs/merkledag"
18
path "github.com/ipfs/go-ipfs/path"
19
+ pin "github.com/ipfs/go-ipfs/pin"
20
ft "github.com/ipfs/go-ipfs/unixfs"
21
22
cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
@@ -355,6 +356,7 @@ And then run:
356
Options: []cmds.Option{
357
cmds.StringOption("inputenc", "Encoding type of input data. One of: {\"protobuf\", \"json\"}.").Default("json"),
358
cmds.StringOption("datafieldenc", "Encoding type of the data field, either \"text\" or \"base64\".").Default("text"),
359
+ cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
360
},
361
Run: func(req cmds.Request, res cmds.Response) {
362
n, err := req.InvocContext().GetNode()
@@ -381,6 +383,16 @@ And then run:
383
return
384
}
385
386
+ dopin, _, err := req.Option("pin").Bool()
387
+ if err != nil {
388
+ res.SetError(err, cmds.ErrNormal)
389
+ return
390
+ }
391
+
392
+ if dopin {
393
+ defer n.Blockstore.PinLock().Unlock()
394
+ }
395
+
396
output, err := objectPut(n, input, inputenc, datafieldenc)
397
if err != nil {
398
errType := cmds.ErrNormal
@@ -391,6 +403,21 @@ And then run:
403
return
404
}
405
406
+ if dopin {
407
+ c, err := cid.Decode(output.Hash)
408
+ if err != nil {
409
+ res.SetError(err, cmds.ErrNormal)
410
+ return
411
+ }
412
+
413
+ n.Pinning.PinWithMode(c, pin.Recursive)
414
+ err = n.Pinning.Flush()
415
+ if err != nil {
416
+ res.SetError(err, cmds.ErrNormal)
417
+ return
418
+ }
419
+ }
420
+
421
res.SetOutput(output)
422
},
423
Marshalers: cmds.MarshalerMap{
test/sharness/t0051-object.sh
+17
-1
@@ -168,7 +168,23 @@ test_object_cmd() {
168
test_cmp expected actual
169
'
170
171
- test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
171
+ test_expect_success "'ipfs object put --pin' succeeds" '
172
+ HASH="QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V" &&
173
+ echo "added $HASH" >expected &&
174
+ echo "{ \"Data\": \"abc\" }" | ipfs object put --pin >actual
175
+ '
176
+
177
+ test_expect_success "'ipfs object put --pin' output looks good" '
178
+ echo "added $HASH" >expected &&
179
+ test_cmp expected actual
180
+ '
181
+
182
+ test_expect_success "after gc, objects still acessible" '
183
+ ipfs repo gc > /dev/null &&
184
+ ipfs refs -r --timeout=2s $HASH > /dev/null
185
+ '
186
+
187
+ test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
188
EMPTY_DIR=$(ipfs object new) &&
189
OUTPUT=$(ipfs object patch $EMPTY_DIR add-link foo $EMPTY_DIR) &&
190
ipfs object stat $OUTPUT