mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
Add pin option for ipfs dag put
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
path "github.com/ipfs/go-ipfs/path"
|
||||
pin "github.com/ipfs/go-ipfs/pin"
|
||||
|
||||
ipldcbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
|
||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||
@ -48,6 +49,7 @@ into an object of the specified format.
|
||||
Options: []cmds.Option{
|
||||
cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
|
||||
cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"),
|
||||
cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
n, err := req.InvocContext().GetNode()
|
||||
@ -64,7 +66,17 @@ into an object of the specified format.
|
||||
|
||||
ienc, _, _ := req.Option("input-enc").String()
|
||||
format, _, _ := req.Option("format").String()
|
||||
dopin, _, err := req.Option("pin").Bool()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if dopin {
|
||||
defer n.Blockstore.PinLock().Unlock()
|
||||
}
|
||||
|
||||
var c *cid.Cid
|
||||
switch ienc {
|
||||
case "json":
|
||||
nd, err := convertJsonToType(fi, format)
|
||||
@ -73,14 +85,11 @@ into an object of the specified format.
|
||||
return
|
||||
}
|
||||
|
||||
c, err := n.DAG.Add(nd)
|
||||
c, err = n.DAG.Add(nd)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
res.SetOutput(&OutputObject{Cid: c})
|
||||
return
|
||||
case "raw":
|
||||
nd, err := convertRawToType(fi, format)
|
||||
if err != nil {
|
||||
@ -88,18 +97,27 @@ into an object of the specified format.
|
||||
return
|
||||
}
|
||||
|
||||
c, err := n.DAG.Add(nd)
|
||||
c, err = n.DAG.Add(nd)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
res.SetOutput(&OutputObject{Cid: c})
|
||||
return
|
||||
default:
|
||||
res.SetError(fmt.Errorf("unrecognized input encoding: %s", ienc), cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if dopin {
|
||||
n.Pinning.PinWithMode(c, pin.Recursive)
|
||||
|
||||
err := n.Pinning.Flush()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
res.SetOutput(&OutputObject{Cid: c})
|
||||
},
|
||||
Type: OutputObject{},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
|
@ -113,9 +113,18 @@ test_dag_cmd() {
|
||||
'
|
||||
|
||||
test_expect_success "non-canonical cbor input is normalized" '
|
||||
HASH=$(cat ../t0053-dag-data/non-canon.cbor | ipfs dag put --format=cbor --input-enc=raw) &&
|
||||
test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
|
||||
test_fsh echo $HASH
|
||||
HASH=$(cat ../t0053-dag-data/non-canon.cbor | ipfs dag put --format=cbor --input-enc=raw) &&
|
||||
test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
|
||||
test_fsh echo $HASH
|
||||
'
|
||||
|
||||
test_expect_success "add an ipld with pin" '
|
||||
PINHASH=$(printf {\"foo\":\"bar\"} | ipfs dag put --pin=true)
|
||||
'
|
||||
|
||||
test_expect_success "after gc, objects still acessible" '
|
||||
ipfs repo gc > /dev/null &&
|
||||
ipfs refs -r --timeout=2s $PINHASH > /dev/null
|
||||
'
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user