Commit 6f750572 authored by Jeromy's avatar Jeromy

add lock to pinner and rework cli

parent 31b0ff03
...@@ -8,36 +8,47 @@ import ( ...@@ -8,36 +8,47 @@ import (
var cmdIpfsPin = &commander.Command{ var cmdIpfsPin = &commander.Command{
UsageLine: "pin", UsageLine: "pin",
Short: "",
Long: `ipfs pin [add|rm] - object pinning commands
`,
Subcommands: []*commander.Command{
cmdIpfsSubPin,
cmdIpfsSubUnpin,
},
}
var cmdIpfsSubPin = &commander.Command{
UsageLine: "add",
Short: "pin an ipfs object to local storage.", Short: "pin an ipfs object to local storage.",
Long: `ipfs pin <ipfs-path> - pin ipfs object to local storage. Long: `ipfs pin add <ipfs-path> - pin ipfs object to local storage.
Retrieves the object named by <ipfs-path> and stores it locally Retrieves the object named by <ipfs-path> and stores it locally
on disk. on disk.
`, `,
Run: pinCmd, Run: pinSubCmd,
Flag: *flag.NewFlagSet("ipfs-pin", flag.ExitOnError), Flag: *flag.NewFlagSet("ipfs-pin", flag.ExitOnError),
} }
var pinCmd = makeCommand(command{ var pinSubCmd = makeCommand(command{
name: "pin", name: "pin",
args: 1, args: 1,
flags: []string{"r", "d"}, flags: []string{"r", "d"},
cmdFn: commands.Pin, cmdFn: commands.Pin,
}) })
var cmdIpfsUnpin = &commander.Command{ var cmdIpfsSubUnpin = &commander.Command{
UsageLine: "unpin", UsageLine: "rm",
Short: "unpin an ipfs object from local storage.", Short: "unpin an ipfs object from local storage.",
Long: `ipfs unpin <ipfs-path> - unpin ipfs object from local storage. Long: `ipfs pin rm <ipfs-path> - unpin ipfs object from local storage.
Removes the pin from the given object allowing it to be garbage Removes the pin from the given object allowing it to be garbage
collected if needed. collected if needed.
`, `,
Run: unpinCmd, Run: unpinSubCmd,
Flag: *flag.NewFlagSet("ipfs-unpin", flag.ExitOnError), Flag: *flag.NewFlagSet("ipfs-unpin", flag.ExitOnError),
} }
var unpinCmd = makeCommand(command{ var unpinSubCmd = makeCommand(command{
name: "unpin", name: "unpin",
args: 1, args: 1,
flags: []string{"r", "d"}, flags: []string{"r", "d"},
...@@ -45,7 +56,7 @@ var unpinCmd = makeCommand(command{ ...@@ -45,7 +56,7 @@ var unpinCmd = makeCommand(command{
}) })
func init() { func init() {
cmdIpfsPin.Flag.Bool("r", false, "pin objects recursively") cmdIpfsSubPin.Flag.Bool("r", false, "pin objects recursively")
cmdIpfsPin.Flag.Int("d", 1, "recursive depth") cmdIpfsSubPin.Flag.Int("d", 1, "recursive depth")
cmdIpfsUnpin.Flag.Bool("r", false, "unpin objects recursively") cmdIpfsSubUnpin.Flag.Bool("r", false, "unpin objects recursively")
} }
...@@ -3,6 +3,8 @@ package pin ...@@ -3,6 +3,8 @@ package pin
import ( import (
//ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" //ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
"sync"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
nsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/namespace" nsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/namespace"
"github.com/jbenet/go-ipfs/blocks/set" "github.com/jbenet/go-ipfs/blocks/set"
...@@ -22,6 +24,7 @@ type Pinner interface { ...@@ -22,6 +24,7 @@ type Pinner interface {
} }
type pinner struct { type pinner struct {
lock sync.RWMutex
recursePin set.BlockSet recursePin set.BlockSet
directPin set.BlockSet directPin set.BlockSet
indirPin *indirectPin indirPin *indirectPin
...@@ -49,6 +52,8 @@ func NewPinner(dstore ds.Datastore, serv *mdag.DAGService) Pinner { ...@@ -49,6 +52,8 @@ func NewPinner(dstore ds.Datastore, serv *mdag.DAGService) Pinner {
} }
func (p *pinner) Pin(node *mdag.Node, recurse bool) error { func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
k, err := node.Key() k, err := node.Key()
if err != nil { if err != nil {
return err return err
...@@ -72,6 +77,8 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error { ...@@ -72,6 +77,8 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
} }
func (p *pinner) Unpin(k util.Key, recurse bool) error { func (p *pinner) Unpin(k util.Key, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
if recurse { if recurse {
p.recursePin.RemoveBlock(k) p.recursePin.RemoveBlock(k)
node, err := p.dserv.Get(k) node, err := p.dserv.Get(k)
...@@ -134,6 +141,8 @@ func (p *pinner) pinLinks(node *mdag.Node) error { ...@@ -134,6 +141,8 @@ func (p *pinner) pinLinks(node *mdag.Node) error {
} }
func (p *pinner) IsPinned(key util.Key) bool { func (p *pinner) IsPinned(key util.Key) bool {
p.lock.RLock()
defer p.lock.RUnlock()
return p.recursePin.HasKey(key) || return p.recursePin.HasKey(key) ||
p.directPin.HasKey(key) || p.directPin.HasKey(key) ||
p.indirPin.HasKey(key) p.indirPin.HasKey(key)
...@@ -164,6 +173,8 @@ func LoadPinner(d ds.Datastore, dserv *mdag.DAGService) (Pinner, error) { ...@@ -164,6 +173,8 @@ func LoadPinner(d ds.Datastore, dserv *mdag.DAGService) (Pinner, error) {
} }
func (p *pinner) Flush() error { func (p *pinner) Flush() error {
p.lock.RLock()
defer p.lock.RUnlock()
recurse := p.recursePin.GetKeys() recurse := p.recursePin.GetKeys()
err := p.dstore.Put(recursePinDatastoreKey, recurse) err := p.dstore.Put(recursePinDatastoreKey, recurse)
if err != nil { if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment