resolver.go 4.61 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

Jeromy's avatar
Jeromy committed
12
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
13 14
	cid "gx/ipfs/QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk/go-cid"
	node "gx/ipfs/QmYDscK7dmdo2GZ9aumS8s5auUUAH5mR1jvj5pYhWusfK7/go-ipld-node"
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 40 41 42 43 44 45 46 47
	DAG dag.DAGService

	ResolveOnce func(ctx context.Context, ds dag.DAGService, nd node.Node, name string) (*node.Link, error)
}

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
}

124 125 126 127 128
func ResolveSingle(ctx context.Context, ds dag.DAGService, nd node.Node, name string) (*node.Link, error) {
	lnk, _, err := nd.ResolveLink([]string{name})
	return lnk, err
}

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

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

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

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

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

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

166
		lnk, rest, err := nd.ResolveLink(names)
167 168
		if err == dag.ErrLinkNotFound {
			return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
169
		} else if err != nil {
170 171 172
			return result, err
		}

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

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