mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49: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"
|
cmds "github.com/ipfs/go-ipfs/commands"
|
||||||
path "github.com/ipfs/go-ipfs/path"
|
path "github.com/ipfs/go-ipfs/path"
|
||||||
|
pin "github.com/ipfs/go-ipfs/pin"
|
||||||
|
|
||||||
ipldcbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
|
ipldcbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
|
||||||
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
|
||||||
@ -48,6 +49,7 @@ into an object of the specified format.
|
|||||||
Options: []cmds.Option{
|
Options: []cmds.Option{
|
||||||
cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
|
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.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) {
|
Run: func(req cmds.Request, res cmds.Response) {
|
||||||
n, err := req.InvocContext().GetNode()
|
n, err := req.InvocContext().GetNode()
|
||||||
@ -64,7 +66,17 @@ into an object of the specified format.
|
|||||||
|
|
||||||
ienc, _, _ := req.Option("input-enc").String()
|
ienc, _, _ := req.Option("input-enc").String()
|
||||||
format, _, _ := req.Option("format").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 {
|
switch ienc {
|
||||||
case "json":
|
case "json":
|
||||||
nd, err := convertJsonToType(fi, format)
|
nd, err := convertJsonToType(fi, format)
|
||||||
@ -73,14 +85,11 @@ into an object of the specified format.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := n.DAG.Add(nd)
|
c, err = n.DAG.Add(nd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res.SetOutput(&OutputObject{Cid: c})
|
|
||||||
return
|
|
||||||
case "raw":
|
case "raw":
|
||||||
nd, err := convertRawToType(fi, format)
|
nd, err := convertRawToType(fi, format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -88,18 +97,27 @@ into an object of the specified format.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := n.DAG.Add(nd)
|
c, err = n.DAG.Add(nd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res.SetOutput(&OutputObject{Cid: c})
|
|
||||||
return
|
|
||||||
default:
|
default:
|
||||||
res.SetError(fmt.Errorf("unrecognized input encoding: %s", ienc), cmds.ErrNormal)
|
res.SetError(fmt.Errorf("unrecognized input encoding: %s", ienc), cmds.ErrNormal)
|
||||||
return
|
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{},
|
Type: OutputObject{},
|
||||||
Marshalers: cmds.MarshalerMap{
|
Marshalers: cmds.MarshalerMap{
|
||||||
|
@ -117,6 +117,15 @@ test_dag_cmd() {
|
|||||||
test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
|
test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
|
||||||
test_fsh echo $HASH
|
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
|
||||||
|
'
|
||||||
}
|
}
|
||||||
|
|
||||||
# should work offline
|
# should work offline
|
||||||
|
Reference in New Issue
Block a user