@cryptotaxi247 / kubo / commits / 03cf65bfb

dag: fix pin lock usage

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 20, 2017 at 20:45 UTC 03cf65bfb98dcf6a06197553686365c2d0552be5
1 file changed +25 -23
core/commands/dag/dag.go
+25 -23
@@ -16,7 +16,6 @@ import (
16 cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
17 u "gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util"
18 mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
19 - "reflect"
19 )
20
21 var DagCmd = &cmds.Command{
@@ -93,10 +92,6 @@ into an object of the specified format.
92 }
93 }
94
96 - if dopin {
97 - defer n.Blockstore.PinLock().Unlock()
98 - }
99 -
95 outChan := make(chan interface{}, 8)
96 res.SetOutput((<-chan interface{})(outChan))
97
@@ -118,29 +113,37 @@ into an object of the specified format.
113 return fmt.Errorf("no node returned from ParseInputs")
114 }
115
121 - b := n.DAG.Batch()
122 - for _, nd := range nds {
123 - _, err := b.Add(nd)
124 - if err != nil {
125 - return err
116 + var cid *cid.Cid
117 + err = func() error {
118 + if dopin {
119 + defer n.Blockstore.PinLock().Unlock()
120 }
127 - }
128 -
129 - if err := b.Commit(); err != nil {
130 - return err
131 - }
121
133 - root := nds[0].Cid()
134 - if dopin {
135 - n.Pinning.PinWithMode(root, pin.Recursive)
122 + b := n.DAG.Batch()
123 + for _, nd := range nds {
124 + _, err := b.Add(nd)
125 + if err != nil {
126 + return err
127 + }
128 + }
129
137 - err := n.Pinning.Flush()
138 - if err != nil {
130 + if err := b.Commit(); err != nil {
131 return err
132 }
141 - }
133
143 - outChan <- &OutputObject{Cid: root}
134 + cid = nds[0].Cid()
135 + if dopin {
136 + n.Pinning.PinWithMode(cid, pin.Recursive)
137 +
138 + err := n.Pinning.Flush()
139 + if err != nil {
140 + return err
141 + }
142 + }
143 + return nil
144 + }()
145 +
146 + outChan <- &OutputObject{Cid: cid}
147 }
148
149 return nil
@@ -159,7 +162,6 @@ into an object of the specified format.
162 cmds.Text: func(res cmds.Response) (io.Reader, error) {
163 outChan, ok := res.Output().(<-chan interface{})
164 if !ok {
162 - fmt.Println(reflect.TypeOf(res.Output()))
165 return nil, u.ErrCast()
166 }
167