resolver.go 4.76 KB
Newer Older
1
// Package path implements utilities for resolving paths within ipfs.
Jeromy's avatar
Jeromy committed
2 3 4
package path

import (
5
	"context"
6
	"errors"
Jeromy's avatar
Jeromy committed
7
	"fmt"
Jeromy's avatar
Jeromy committed
8
	"time"
Jeromy's avatar
Jeromy committed
9

10
	dag "github.com/ipfs/go-ipfs/merkledag"
11

12 13
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
	node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
Jeromy's avatar
Jeromy committed
14
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
Jeromy's avatar
Jeromy committed
15 16
)

Jeromy's avatar
Jeromy committed
17
var log = logging.Logger("path")
Jeromy's avatar
Jeromy committed
18

19 20 21 22
// Paths after a protocol must contain at least one component
var ErrNoComponents = errors.New(
	"path must contain at least one component")

23 24
// ErrNoLink is returned when a link is not found in a path
type ErrNoLink struct {
25
	Name string
26
	Node *cid.Cid
27 28 29
}

func (e ErrNoLink) Error() string {
30
	return fmt.Sprintf("no link named %q under %s", e.Name, e.Node.String())
31 32
}

Jeromy's avatar
Jeromy committed
33 34
// Resolver provides path resolution to IPFS
// It has a pointer to a DAGService, which is uses to resolve nodes.
35 36
// TODO: now that this is more modular, try to unify this code with the
//       the resolvers in namesys
Jeromy's avatar
Jeromy committed
37
type Resolver struct {
38 39
	DAG dag.DAGService

Jeromy's avatar
Jeromy committed
40
	ResolveOnce func(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error)
41 42 43 44 45 46 47
}

func NewBasicResolver(ds dag.DAGService) *Resolver {
	return &Resolver{
		DAG:         ds,
		ResolveOnce: ResolveSingle,
	}
Jeromy's avatar
Jeromy committed
48 49
}

50 51
// SplitAbsPath clean up and split fpath. It extracts the first component (which
// must be a Multihash) and return it separately.
Jeromy's avatar
Jeromy committed
52
func SplitAbsPath(fpath Path) (*cid.Cid, []string, error) {
53

Jeromy's avatar
Jeromy committed
54 55 56 57 58 59 60 61 62
	log.Debugf("Resolve: '%s'", fpath)

	parts := fpath.Segments()
	if parts[0] == "ipfs" {
		parts = parts[1:]
	}

	// if nothing, bail.
	if len(parts) == 0 {
63
		return nil, nil, ErrNoComponents
Jeromy's avatar
Jeromy committed
64 65
	}

Jeromy's avatar
Jeromy committed
66
	c, err := cid.Decode(parts[0])
67
	// first element in the path is a cid
Jeromy's avatar
Jeromy committed
68
	if err != nil {
69
		log.Debug("given path element is not a cid.\n")
70 71 72
		return nil, nil, err
	}

Jeromy's avatar
Jeromy committed
73
	return c, parts[1:], nil
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath Path) (node.Node, []string, error) {
	c, p, err := SplitAbsPath(fpath)
	if err != nil {
		return nil, nil, err
	}

	nd, err := r.DAG.Get(ctx, c)
	if err != nil {
		return nil, nil, err
	}

	for len(p) > 0 {
		val, rest, err := nd.Resolve(p)
		if err != nil {
			return nil, nil, err
		}

		switch val := val.(type) {
		case *node.Link:
			next, err := val.GetNode(ctx, r.DAG)
			if err != nil {
				return nil, nil, err
			}
			nd = next
			p = rest
		default:
			return nd, p, nil
		}
	}

	return nd, nil, nil
}

109 110
// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
Jeromy's avatar
Jeromy committed
111
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (node.Node, error) {
112 113 114 115 116
	// validate path
	if err := fpath.IsValid(); err != nil {
		return nil, err
	}

117
	nodes, err := s.ResolvePathComponents(ctx, fpath)
118 119 120
	if err != nil || nodes == nil {
		return nil, err
	}
121
	return nodes[len(nodes)-1], err
122 123
}

Jeromy's avatar
Jeromy committed
124 125 126 127
// ResolveSingle simply resolves one hop of a path through a graph with no
// extra context (does not opaquely resolve through sharded nodes)
func ResolveSingle(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error) {
	return nd.ResolveLink(names)
128 129
}

130 131 132
// 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.
Jeromy's avatar
Jeromy committed
133
func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]node.Node, error) {
134 135
	h, parts, err := SplitAbsPath(fpath)
	if err != nil {
Jeromy's avatar
Jeromy committed
136 137 138
		return nil, err
	}

139
	log.Debug("resolve dag get")
Jeromy's avatar
Jeromy committed
140
	nd, err := s.DAG.Get(ctx, h)
Jeromy's avatar
Jeromy committed
141 142 143 144
	if err != nil {
		return nil, err
	}

145
	return s.ResolveLinks(ctx, nd, parts)
Jeromy's avatar
Jeromy committed
146 147 148 149
}

// ResolveLinks iteratively resolves names by walking the link hierarchy.
// Every node is fetched from the DAGService, resolving the next name.
150 151
// Returns the list of nodes forming the path, starting with ndd. This list is
// guaranteed never to be empty.
Jeromy's avatar
Jeromy committed
152 153 154
//
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
Jeromy's avatar
Jeromy committed
155
func (s *Resolver) ResolveLinks(ctx context.Context, ndd node.Node, names []string) ([]node.Node, error) {
Jeromy's avatar
Jeromy committed
156

Jeromy's avatar
Jeromy committed
157
	result := make([]node.Node, 0, len(names)+1)
158 159
	result = append(result, ndd)
	nd := ndd // dup arg workaround
Jeromy's avatar
Jeromy committed
160 161

	// for each of the path components
162
	for len(names) > 0 {
163 164 165
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, time.Minute)
		defer cancel()
Jeromy's avatar
Jeromy committed
166

Jeromy's avatar
Jeromy committed
167
		lnk, rest, err := s.ResolveOnce(ctx, s.DAG, nd, names)
168 169
		if err == dag.ErrLinkNotFound {
			return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
170
		} else if err != nil {
171 172 173
			return result, err
		}

174
		nextnode, err := lnk.GetNode(ctx, s.DAG)
175 176
		if err != nil {
			return result, err
Jeromy's avatar
Jeromy committed
177 178
		}

179 180
		nd = nextnode
		result = append(result, nextnode)
Jeromy's avatar
Jeromy committed
181
		names = rest
Jeromy's avatar
Jeromy committed
182
	}
183
	return result, nil
Jeromy's avatar
Jeromy committed
184
}