From c4195e532880156692f6bd8f6e65413f1aba3a33 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 15 May 2016 18:15:28 +0200 Subject: [PATCH 1/2] pin: add missing consts and convertion functions License: MIT Signed-off-by: Christian Couder --- pin/pin.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/pin/pin.go b/pin/pin.go index 64c1bf264..df7440fac 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -22,8 +22,13 @@ var pinDatastoreKey = ds.NewKey("/local/pins") var emptyKey = key.B58KeyDecode("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n") const ( - linkDirect = "direct" linkRecursive = "recursive" + linkDirect = "direct" + linkIndirect = "indirect" + linkInternal = "internal" + linkNotPinned = "not pinned" + linkAny = "any" + linkAll = "all" ) type PinMode int @@ -31,9 +36,39 @@ type PinMode int const ( Recursive PinMode = iota Direct + Indirect + Internal NotPinned + Any ) +func PinModeToString(mode PinMode) (string, bool) { + m := map[PinMode]string{ + Recursive: linkRecursive, + Direct: linkDirect, + Indirect: linkIndirect, + Internal: linkInternal, + NotPinned: linkNotPinned, + Any: linkAny, + } + s, ok := m[mode] + return s, ok +} + +func StringToPinMode(s string) (PinMode, bool) { + m := map[string]PinMode{ + linkRecursive: Recursive, + linkDirect: Direct, + linkIndirect: Indirect, + linkInternal: Internal, + linkNotPinned: NotPinned, + linkAny: Any, + linkAll: Any, // "all" and "any" means the same thing + } + mode, ok := m[s] + return mode, ok +} + type Pinner interface { IsPinned(key.Key) (string, bool, error) IsPinnedWithType(key.Key, string) (string, bool, error) From 39f23677f57f693d9909e095cda436d866af5e73 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 15 May 2016 17:13:55 +0200 Subject: [PATCH 2/2] pin: use new constants instead of literal values License: MIT Signed-off-by: Christian Couder --- core/commands/pin.go | 8 +++++++- pin/pin.go | 39 ++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/core/commands/pin.go b/core/commands/pin.go index 2803922c0..1b902ebbc 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -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 } diff --git a/pin/pin.go b/pin/pin.go index df7440fac..0696e7984 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -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 {