Commit 6f750572 authored by Jeromy's avatar Jeromy

add lock to pinner and rework cli

parent 31b0ff03
......@@ -8,36 +8,47 @@ import (
var cmdIpfsPin = &commander.Command{
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.",
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
on disk.
`,
Run: pinCmd,
Run: pinSubCmd,
Flag: *flag.NewFlagSet("ipfs-pin", flag.ExitOnError),
}
var pinCmd = makeCommand(command{
var pinSubCmd = makeCommand(command{
name: "pin",
args: 1,
flags: []string{"r", "d"},
cmdFn: commands.Pin,
})
var cmdIpfsUnpin = &commander.Command{
UsageLine: "unpin",
var cmdIpfsSubUnpin = &commander.Command{
UsageLine: "rm",
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
collected if needed.
`,
Run: unpinCmd,
Run: unpinSubCmd,
Flag: *flag.NewFlagSet("ipfs-unpin", flag.ExitOnError),
}
var unpinCmd = makeCommand(command{
var unpinSubCmd = makeCommand(command{
name: "unpin",
args: 1,
flags: []string{"r", "d"},
......@@ -45,7 +56,7 @@ var unpinCmd = makeCommand(command{
})
func init() {
cmdIpfsPin.Flag.Bool("r", false, "pin objects recursively")
cmdIpfsPin.Flag.Int("d", 1, "recursive depth")
cmdIpfsUnpin.Flag.Bool("r", false, "unpin objects recursively")
cmdIpfsSubPin.Flag.Bool("r", false, "pin objects recursively")
cmdIpfsSubPin.Flag.Int("d", 1, "recursive depth")
cmdIpfsSubUnpin.Flag.Bool("r", false, "unpin objects recursively")
}
......@@ -3,6 +3,8 @@ package pin
import (
//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"
nsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go/namespace"
"github.com/jbenet/go-ipfs/blocks/set"
......@@ -22,6 +24,7 @@ type Pinner interface {
}
type pinner struct {
lock sync.RWMutex
recursePin set.BlockSet
directPin set.BlockSet
indirPin *indirectPin
......@@ -49,6 +52,8 @@ func NewPinner(dstore ds.Datastore, serv *mdag.DAGService) Pinner {
}
func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
k, err := node.Key()
if err != nil {
return err
......@@ -72,6 +77,8 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
}
func (p *pinner) Unpin(k util.Key, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
if recurse {
p.recursePin.RemoveBlock(k)
node, err := p.dserv.Get(k)
......@@ -134,6 +141,8 @@ func (p *pinner) pinLinks(node *mdag.Node) error {
}
func (p *pinner) IsPinned(key util.Key) bool {
p.lock.RLock()
defer p.lock.RUnlock()
return p.recursePin.HasKey(key) ||
p.directPin.HasKey(key) ||
p.indirPin.HasKey(key)
......@@ -164,6 +173,8 @@ func LoadPinner(d ds.Datastore, dserv *mdag.DAGService) (Pinner, error) {
}
func (p *pinner) Flush() error {
p.lock.RLock()
defer p.lock.RUnlock()
recurse := p.recursePin.GetKeys()
err := p.dstore.Put(recursePinDatastoreKey, recurse)
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