pin: use new constants instead of literal values

License: MIT
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
This commit is contained in:
Christian Couder 2016-05-15 17:13:55 +02:00
parent c4195e5328
commit 39f23677f5
2 changed files with 27 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import (
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)
@ -273,7 +274,12 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN
return nil, err
}
pinType, pinned, err := n.Pinning.IsPinnedWithType(k, typeStr)
mode, ok := pin.StringToPinMode(typeStr)
if !ok {
return nil, fmt.Errorf("Invalid pin mode '%s'", typeStr)
}
pinType, pinned, err := n.Pinning.IsPinnedWithType(k, mode)
if err != nil {
return nil, err
}

View File

@ -71,7 +71,7 @@ func StringToPinMode(s string) (PinMode, bool) {
type Pinner interface {
IsPinned(key.Key) (string, bool, error)
IsPinnedWithType(key.Key, string) (string, bool, error)
IsPinnedWithType(key.Key, PinMode) (string, bool, error)
Pin(context.Context, *mdag.Node, bool) error
Unpin(context.Context, key.Key, bool) error
@ -164,7 +164,7 @@ var ErrNotPinned = fmt.Errorf("not pinned")
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
p.lock.Lock()
defer p.lock.Unlock()
reason, pinned, err := p.isPinnedWithType(k, "all")
reason, pinned, err := p.isPinnedWithType(k, Any)
if err != nil {
return err
}
@ -197,46 +197,47 @@ func (p *pinner) isInternalPin(key key.Key) bool {
func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinnedWithType(k, "all")
return p.isPinnedWithType(k, Any)
}
func (p *pinner) IsPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
func (p *pinner) IsPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.isPinnedWithType(k, typeStr)
return p.isPinnedWithType(k, mode)
}
// isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
// intended for use by other pinned methods that already take locks
func (p *pinner) isPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
switch typeStr {
case "all", "direct", "indirect", "recursive", "internal":
func (p *pinner) isPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
switch mode {
case Any, Direct, Indirect, Recursive, Internal:
default:
err := fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, internal, all}", typeStr)
err := fmt.Errorf("Invalid Pin Mode '%d', must be one of {%d, %d, %d, %d, %d}",
mode, Direct, Indirect, Recursive, Internal, Any)
return "", false, err
}
if (typeStr == "recursive" || typeStr == "all") && p.recursePin.HasKey(k) {
return "recursive", true, nil
if (mode == Recursive || mode == Any) && p.recursePin.HasKey(k) {
return linkRecursive, true, nil
}
if typeStr == "recursive" {
if mode == Recursive {
return "", false, nil
}
if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) {
return "direct", true, nil
if (mode == Direct || mode == Any) && p.directPin.HasKey(k) {
return linkDirect, true, nil
}
if typeStr == "direct" {
if mode == Direct {
return "", false, nil
}
if (typeStr == "internal" || typeStr == "all") && p.isInternalPin(k) {
return "internal", true, nil
if (mode == Internal || mode == Any) && p.isInternalPin(k) {
return linkInternal, true, nil
}
if typeStr == "internal" {
if mode == Internal {
return "", false, nil
}
// Default is "indirect"
// Default is Indirect
for _, rk := range p.recursePin.GetKeys() {
rnd, err := p.dserv.Get(context.Background(), rk)
if err != nil {