提交 79e21bd9 作者: Łukasz Magiera

Add pin option for ipfs dag put

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 07162dd5
......@@ -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
'
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论