@cryptotaxi247 / kubo / commits / 879cfeeec

pin rm fails appropriately for indirect pins

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jul 10, 2015 at 11:03 UTC 879cfeeec97be93c558b5776af91e1402b3dac93
1 file changed +18 -4
pin/pin.go
+18 -4
@@ -126,18 +126,26 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {
126 func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
127 p.lock.Lock()
128 defer p.lock.Unlock()
129 - if p.recursePin.HasKey(k) {
129 + reason, pinned, err := p.isPinned(k)
130 + if err != nil {
131 + return err
132 + }
133 + if !pinned {
134 + return fmt.Errorf("%s is not pinned", k)
135 + }
136 + switch reason {
137 + case "recursive":
138 if recursive {
139 p.recursePin.RemoveBlock(k)
140 return nil
141 } else {
142 return fmt.Errorf("%s is pinned recursively", k)
143 }
136 - } else if p.directPin.HasKey(k) {
144 + case "direct":
145 p.directPin.RemoveBlock(k)
146 return nil
139 - } else {
140 - return fmt.Errorf("%s is not pinned", k)
147 + default:
148 + return fmt.Errorf("%s is pinned indirectly under %s", k, reason)
149 }
150 }
151
@@ -151,6 +159,12 @@ func (p *pinner) isInternalPin(key key.Key) bool {
159 func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
160 p.lock.RLock()
161 defer p.lock.RUnlock()
162 + return p.isPinned(k)
163 +}
164 +
165 +// isPinned is the implementation of IsPinned that does not lock.
166 +// intended for use by other pinned methods that already take locks
167 +func (p *pinner) isPinned(k key.Key) (string, bool, error) {
168 if p.recursePin.HasKey(k) {
169 return "recursive", true, nil
170 }