Add pin option for ipfs dag put
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jun 22, 2017 at 15:13 UTC
79e21bd96bab8331cb402d0c7944ef06b7ab9b45
2 files changed
+38
-11
core/commands/dag/dag.go
+26
-8
@@ -8,6 +8,7 @@ import (
8
9
cmds "github.com/ipfs/go-ipfs/commands"
10
path "github.com/ipfs/go-ipfs/path"
11
+ pin "github.com/ipfs/go-ipfs/pin"
12
13
ipldcbor "gx/ipfs/QmNrbCt8j9DT5W9Pmjy2SdudT9k8GpaDr4sRuFix3BXhgR/go-ipld-cbor"
14
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
@@ -48,6 +49,7 @@ into an object of the specified format.
49
Options: []cmds.Option{
50
cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"),
51
cmds.StringOption("input-enc", "Format that the input object will be.").Default("json"),
52
+ cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
53
},
54
Run: func(req cmds.Request, res cmds.Response) {
55
n, err := req.InvocContext().GetNode()
@@ -64,7 +66,17 @@ into an object of the specified format.
66
67
ienc, _, _ := req.Option("input-enc").String()
68
format, _, _ := req.Option("format").String()
69
+ dopin, _, err := req.Option("pin").Bool()
70
+ if err != nil {
71
+ res.SetError(err, cmds.ErrNormal)
72
+ return
73
+ }
74
75
+ if dopin {
76
+ defer n.Blockstore.PinLock().Unlock()
77
+ }
78
+
79
+ var c *cid.Cid
80
switch ienc {
81
case "json":
82
nd, err := convertJsonToType(fi, format)
@@ -73,14 +85,11 @@ into an object of the specified format.
85
return
86
}
87
76
- c, err := n.DAG.Add(nd)
88
+ c, err = n.DAG.Add(nd)
89
if err != nil {
90
res.SetError(err, cmds.ErrNormal)
91
return
92
}
81
-
82
- res.SetOutput(&OutputObject{Cid: c})
83
- return
93
case "raw":
94
nd, err := convertRawToType(fi, format)
95
if err != nil {
@@ -88,18 +97,27 @@ into an object of the specified format.
97
return
98
}
99
91
- c, err := n.DAG.Add(nd)
100
+ c, err = n.DAG.Add(nd)
101
if err != nil {
102
res.SetError(err, cmds.ErrNormal)
103
return
104
}
96
-
97
- res.SetOutput(&OutputObject{Cid: c})
98
- return
105
default:
106
res.SetError(fmt.Errorf("unrecognized input encoding: %s", ienc), cmds.ErrNormal)
107
return
108
}
109
+
110
+ if dopin {
111
+ n.Pinning.PinWithMode(c, pin.Recursive)
112
+
113
+ err := n.Pinning.Flush()
114
+ if err != nil {
115
+ res.SetError(err, cmds.ErrNormal)
116
+ return
117
+ }
118
+ }
119
+
120
+ res.SetOutput(&OutputObject{Cid: c})
121
},
122
Type: OutputObject{},
123
Marshalers: cmds.MarshalerMap{
test/sharness/t0053-dag.sh
+12
-3
@@ -113,9 +113,18 @@ test_dag_cmd() {
113
'
114
115
test_expect_success "non-canonical cbor input is normalized" '
116
- HASH=$(cat ../t0053-dag-data/non-canon.cbor | ipfs dag put --format=cbor --input-enc=raw) &&
117
- test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
118
- test_fsh echo $HASH
116
+ HASH=$(cat ../t0053-dag-data/non-canon.cbor | ipfs dag put --format=cbor --input-enc=raw) &&
117
+ test $HASH = "zdpuAmxF8q6iTUtkB3xtEYzmc5Sw762qwQJftt5iW8NTWLtjC" ||
118
+ test_fsh echo $HASH
119
+ '
120
+
121
+ test_expect_success "add an ipld with pin" '
122
+ PINHASH=$(printf {\"foo\":\"bar\"} | ipfs dag put --pin=true)
123
+ '
124
+
125
+ test_expect_success "after gc, objects still acessible" '
126
+ ipfs repo gc > /dev/null &&
127
+ ipfs refs -r --timeout=2s $PINHASH > /dev/null
128
'
129
}
130