mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-15 11:13:37 +08:00

This commit adds a new set of sharness tests for pinning, and addresses bugs that were pointed out by said tests. test/sharness: added more pinning tests Pinning is currently broken. See issue #1051. This commit introduces a few more pinning tests. These are by no means exhaustive, but definitely surface the present problems going on. I believe these tests are correct, but not sure. Pushing them as failing so that pinning is fixed in this PR. make pinning and merkledag.Get take contexts improve 'add' commands usage of pinning FIXUP: fix 'pin lists look good' ipfs-pin-stat simple script to help check pinning This is a simple shell script to help check pinning. We ought to strive towards making adding commands this easy. The http api is great and powerful, but our setup right now gets in the way. Perhaps we can clean up that area. updated t0081-repo-pinning - fixed a couple bugs with the tests - made it a bit clearer (still a lot going on) - the remaining tests are correct and highlight a problem with pinning. Namely, that recursive pinning is buggy. At least: towards the end of the test, $HASH_DIR4 and $HASH_FILE4 should be pinned indirectly, but they're not. And thus get gc-ed out. There may be other problems too. cc @whyrusleeping fix grep params for context deadline check fix bugs in pin and pin tests check for block local before checking recursive pin
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
// package path implements utilities for resolving paths within ipfs.
|
|
package path
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
|
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
merkledag "github.com/ipfs/go-ipfs/merkledag"
|
|
u "github.com/ipfs/go-ipfs/util"
|
|
)
|
|
|
|
var log = u.Logger("path")
|
|
|
|
// ErrNoLink is returned when a link is not found in a path
|
|
type ErrNoLink struct {
|
|
name string
|
|
node mh.Multihash
|
|
}
|
|
|
|
func (e ErrNoLink) Error() string {
|
|
return fmt.Sprintf("no link named %q under %s", e.name, e.node.B58String())
|
|
}
|
|
|
|
// Resolver provides path resolution to IPFS
|
|
// It has a pointer to a DAGService, which is uses to resolve nodes.
|
|
type Resolver struct {
|
|
DAG merkledag.DAGService
|
|
}
|
|
|
|
// SplitAbsPath clean up and split fpath. It extracts the first component (which
|
|
// must be a Multihash) and return it separately.
|
|
func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {
|
|
|
|
log.Debugf("Resolve: '%s'", fpath)
|
|
|
|
parts := fpath.Segments()
|
|
if parts[0] == "ipfs" {
|
|
parts = parts[1:]
|
|
}
|
|
|
|
// if nothing, bail.
|
|
if len(parts) == 0 {
|
|
return nil, nil, fmt.Errorf("ipfs path must contain at least one component")
|
|
}
|
|
|
|
// first element in the path is a b58 hash (for now)
|
|
h, err := mh.FromB58String(parts[0])
|
|
if err != nil {
|
|
log.Debug("given path element is not a base58 string.\n")
|
|
return nil, nil, err
|
|
}
|
|
|
|
return h, parts[1:], nil
|
|
}
|
|
|
|
// ResolvePath fetches the node for given path. It returns the last item
|
|
// returned by ResolvePathComponents.
|
|
func (s *Resolver) ResolvePath(fpath Path) (*merkledag.Node, error) {
|
|
nodes, err := s.ResolvePathComponents(fpath)
|
|
if err != nil || nodes == nil {
|
|
return nil, err
|
|
} else {
|
|
return nodes[len(nodes)-1], err
|
|
}
|
|
}
|
|
|
|
// ResolvePathComponents fetches the nodes for each segment of the given path.
|
|
// It uses the first path component as a hash (key) of the first node, then
|
|
// resolves all other components walking the links, with ResolveLinks.
|
|
func (s *Resolver) ResolvePathComponents(fpath Path) ([]*merkledag.Node, error) {
|
|
h, parts, err := SplitAbsPath(fpath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Debug("Resolve dag get.\n")
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
|
defer cancel()
|
|
nd, err := s.DAG.Get(ctx, u.Key(h))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s.ResolveLinks(nd, parts)
|
|
}
|
|
|
|
// ResolveLinks iteratively resolves names by walking the link hierarchy.
|
|
// Every node is fetched from the DAGService, resolving the next name.
|
|
// Returns the list of nodes forming the path, starting with ndd. This list is
|
|
// guaranteed never to be empty.
|
|
//
|
|
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
|
|
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
|
|
func (s *Resolver) ResolveLinks(ndd *merkledag.Node, names []string) (
|
|
result []*merkledag.Node, err error) {
|
|
|
|
result = make([]*merkledag.Node, 0, len(names)+1)
|
|
result = append(result, ndd)
|
|
nd := ndd // dup arg workaround
|
|
|
|
// for each of the path components
|
|
for _, name := range names {
|
|
|
|
var next u.Key
|
|
var nlink *merkledag.Link
|
|
// for each of the links in nd, the current object
|
|
for _, link := range nd.Links {
|
|
if link.Name == name {
|
|
next = u.Key(link.Hash)
|
|
nlink = link
|
|
break
|
|
}
|
|
}
|
|
|
|
if next == "" {
|
|
n, _ := nd.Multihash()
|
|
return result, ErrNoLink{name: name, node: n}
|
|
}
|
|
|
|
if nlink.Node == nil {
|
|
// fetch object for link and assign to nd
|
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
|
|
defer cancel()
|
|
nd, err = s.DAG.Get(ctx, next)
|
|
if err != nil {
|
|
return append(result, nd), err
|
|
}
|
|
nlink.Node = nd
|
|
} else {
|
|
nd = nlink.Node
|
|
}
|
|
|
|
result = append(result, nlink.Node)
|
|
}
|
|
return
|
|
}
|