add lock to pinner and rework cli
Jeromy committed
Oct 20, 2014 at 12:40 UTC
6f750572cd03ec4b06912020c6880cd09afe383d
2 files changed
+33
-11
cmd/ipfs/pin.go
+22
-11
@@ -8,36 +8,47 @@ import (
8
9
var cmdIpfsPin = &commander.Command{
10
UsageLine: "pin",
11
+ Short: "",
12
+ Long: `ipfs pin [add|rm] - object pinning commands
13
+`,
14
+ Subcommands: []*commander.Command{
15
+ cmdIpfsSubPin,
16
+ cmdIpfsSubUnpin,
17
+ },
18
+}
19
+
20
+var cmdIpfsSubPin = &commander.Command{
21
+ UsageLine: "add",
22
Short: "pin an ipfs object to local storage.",
12
- Long: `ipfs pin <ipfs-path> - pin ipfs object to local storage.
23
+ Long: `ipfs pin add <ipfs-path> - pin ipfs object to local storage.
24
25
Retrieves the object named by <ipfs-path> and stores it locally
26
on disk.
27
`,
17
- Run: pinCmd,
28
+ Run: pinSubCmd,
29
Flag: *flag.NewFlagSet("ipfs-pin", flag.ExitOnError),
30
}
31
21
-var pinCmd = makeCommand(command{
32
+var pinSubCmd = makeCommand(command{
33
name: "pin",
34
args: 1,
35
flags: []string{"r", "d"},
36
cmdFn: commands.Pin,
37
})
38
28
-var cmdIpfsUnpin = &commander.Command{
29
- UsageLine: "unpin",
39
+var cmdIpfsSubUnpin = &commander.Command{
40
+ UsageLine: "rm",
41
Short: "unpin an ipfs object from local storage.",
31
- Long: `ipfs unpin <ipfs-path> - unpin ipfs object from local storage.
42
+ Long: `ipfs pin rm <ipfs-path> - unpin ipfs object from local storage.
43
44
Removes the pin from the given object allowing it to be garbage
45
collected if needed.
46
`,
36
- Run: unpinCmd,
47
+ Run: unpinSubCmd,
48
Flag: *flag.NewFlagSet("ipfs-unpin", flag.ExitOnError),
49
}
50
40
-var unpinCmd = makeCommand(command{
51
+var unpinSubCmd = makeCommand(command{
52
name: "unpin",
53
args: 1,
54
flags: []string{"r", "d"},
@@ -45,7 +56,7 @@ var unpinCmd = makeCommand(command{
56
})
57
58
func init() {
48
- cmdIpfsPin.Flag.Bool("r", false, "pin objects recursively")
49
- cmdIpfsPin.Flag.Int("d", 1, "recursive depth")
50
- cmdIpfsUnpin.Flag.Bool("r", false, "unpin objects recursively")
59
+ cmdIpfsSubPin.Flag.Bool("r", false, "pin objects recursively")
60
+ cmdIpfsSubPin.Flag.Int("d", 1, "recursive depth")
61
+ cmdIpfsSubUnpin.Flag.Bool("r", false, "unpin objects recursively")
62
}
pin/pin.go
+11
@@ -3,6 +3,8 @@ package pin
3
import (
4
5
//ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
6
+ "sync"
7
+
8
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
9
nsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/namespace"
10
"github.com/jbenet/go-ipfs/blocks/set"
@@ -22,6 +24,7 @@ type Pinner interface {
24
}
25
26
type pinner struct {
27
+ lock sync.RWMutex
28
recursePin set.BlockSet
29
directPin set.BlockSet
30
indirPin *indirectPin
@@ -49,6 +52,8 @@ func NewPinner(dstore ds.Datastore, serv *mdag.DAGService) Pinner {
52
}
53
54
func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
55
+ p.lock.Lock()
56
+ defer p.lock.Unlock()
57
k, err := node.Key()
58
if err != nil {
59
return err
@@ -72,6 +77,8 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
77
}
78
79
func (p *pinner) Unpin(k util.Key, recurse bool) error {
80
+ p.lock.Lock()
81
+ defer p.lock.Unlock()
82
if recurse {
83
p.recursePin.RemoveBlock(k)
84
node, err := p.dserv.Get(k)
@@ -134,6 +141,8 @@ func (p *pinner) pinLinks(node *mdag.Node) error {
141
}
142
143
func (p *pinner) IsPinned(key util.Key) bool {
144
+ p.lock.RLock()
145
+ defer p.lock.RUnlock()
146
return p.recursePin.HasKey(key) ||
147
p.directPin.HasKey(key) ||
148
p.indirPin.HasKey(key)
@@ -164,6 +173,8 @@ func LoadPinner(d ds.Datastore, dserv *mdag.DAGService) (Pinner, error) {
173
}
174
175
func (p *pinner) Flush() error {
176
+ p.lock.RLock()
177
+ defer p.lock.RUnlock()
178
recurse := p.recursePin.GetKeys()
179
err := p.dstore.Put(recursePinDatastoreKey, recurse)
180
if err != nil {