Skip to content

Commit

Permalink
Merge pull request #504 from ipfs/fix/pinls
Browse files Browse the repository at this point in the history
IPFSPinLsCid should try to find pins in the direct pinset too.
  • Loading branch information
hsanjuan committed Aug 20, 2018
2 parents 8d3510e + eba96c3 commit 77df433
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions ipfsconn/ipfshttp/ipfshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,21 +724,35 @@ func (ipfs *Connector) PinLs(ctx context.Context, typeFilter string) (map[string
return statusMap, nil
}

// PinLsCid performs a "pin ls --type=recursive <hash> "request and returns
// an api.IPFSPinStatus for that hash.
// PinLsCid performs a "pin ls <hash>" request. It first tries with
// "type=recursive" and then, if not found, with "type=direct". It returns an
// api.IPFSPinStatus for that hash.
func (ipfs *Connector) PinLsCid(ctx context.Context, hash *cid.Cid) (api.IPFSPinStatus, error) {
ctx, cancel := context.WithTimeout(ctx, ipfs.config.IPFSRequestTimeout)
defer cancel()
lsPath := fmt.Sprintf("pin/ls?arg=%s&type=recursive", hash)
body, err := ipfs.postCtx(ctx, lsPath, "", nil)
pinLsType := func(pinType string) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, ipfs.config.IPFSRequestTimeout)
defer cancel()
lsPath := fmt.Sprintf("pin/ls?arg=%s&type=%s", hash, pinType)
return ipfs.postCtx(ctx, lsPath, "", nil)
}

var body []byte
var err error
// FIXME: Sharding may need to check more pin types here.
for _, pinType := range []string{"recursive", "direct"} {
body, err = pinLsType(pinType)
// Network error, daemon down
if body == nil && err != nil {
return api.IPFSPinStatusError, err
}

// Network error, daemon down
if body == nil && err != nil {
return api.IPFSPinStatusError, err
// Pin not found. Try next type
if err != nil {
continue

}
}

// Pin not found likely here
if err != nil { // Not pinned
if err != nil { // we could not find the pin
return api.IPFSPinStatusUnpinned, nil
}

Expand Down

0 comments on commit 77df433

Please sign in to comment.