resolver.go 4.04 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
}

// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
Jeromy's avatar
Jeromy committed
78
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (node.Node, error) {
79 80 81 82 83
	// validate path
	if err := fpath.IsValid(); err != nil {
		return nil, err
	}

84
	nodes, err := s.ResolvePathComponents(ctx, fpath)
85 86 87
	if err != nil || nodes == nil {
		return nil, err
	}
88
	return nodes[len(nodes)-1], err
89 90
}

91 92 93 94 95
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
}

96 97 98
// 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
99
func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]node.Node, error) {
100 101
	h, parts, err := SplitAbsPath(fpath)
	if err != nil {
Jeromy's avatar
Jeromy committed
102 103 104
		return nil, err
	}

105
	log.Debug("resolve dag get")
Jeromy's avatar
Jeromy committed
106
	nd, err := s.DAG.Get(ctx, h)
Jeromy's avatar
Jeromy committed
107 108 109 110
	if err != nil {
		return nil, err
	}

111
	return s.ResolveLinks(ctx, nd, parts)
Jeromy's avatar
Jeromy committed
112 113 114 115
}

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

Jeromy's avatar
Jeromy committed
123
	result := make([]node.Node, 0, len(names)+1)
124 125
	result = append(result, ndd)
	nd := ndd // dup arg workaround
Jeromy's avatar
Jeromy committed
126 127

	// for each of the path components
128
	for len(names) > 0 {
129 130 131
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, time.Minute)
		defer cancel()
Jeromy's avatar
Jeromy committed
132

133
		lnk, rest, err := nd.ResolveLink(names)
134 135
		if err == dag.ErrLinkNotFound {
			return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
136
		} else if err != nil {
137 138 139
			return result, err
		}

140
		nextnode, err := lnk.GetNode(ctx, s.DAG)
141 142
		if err != nil {
			return result, err
Jeromy's avatar
Jeromy committed
143 144
		}

145 146
		nd = nextnode
		result = append(result, nextnode)
147
		names = rest
Jeromy's avatar
Jeromy committed
148
	}
149
	return result, nil
Jeromy's avatar
Jeromy committed
150
}