resolver.go 3.43 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
	merkledag "github.com/ipfs/go-ipfs/merkledag"
11

Jeromy's avatar
Jeromy committed
12
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
13
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
Jeromy's avatar
Jeromy committed
14 15
)

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

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

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

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

Jeromy's avatar
Jeromy committed
32 33 34 35 36 37
// 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
}

38 39
// 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
40
func SplitAbsPath(fpath Path) (*cid.Cid, []string, error) {
41

Jeromy's avatar
Jeromy committed
42 43 44 45 46 47 48 49 50
	log.Debugf("Resolve: '%s'", fpath)

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

	// if nothing, bail.
	if len(parts) == 0 {
51
		return nil, nil, ErrNoComponents
Jeromy's avatar
Jeromy committed
52 53
	}

Jeromy's avatar
Jeromy committed
54
	c, err := cid.Decode(parts[0])
Jeromy's avatar
Jeromy committed
55
	if err != nil {
56 57 58
		return nil, nil, err
	}

Jeromy's avatar
Jeromy committed
59
	return c, parts[1:], nil
60 61 62 63
}

// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
64
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (merkledag.Node, error) {
65 66 67 68 69
	// validate path
	if err := fpath.IsValid(); err != nil {
		return nil, err
	}

70
	nodes, err := s.ResolvePathComponents(ctx, fpath)
71 72 73
	if err != nil || nodes == nil {
		return nil, err
	}
74
	return nodes[len(nodes)-1], err
75 76 77 78 79
}

// 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.
80
func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]merkledag.Node, error) {
81 82
	h, parts, err := SplitAbsPath(fpath)
	if err != nil {
Jeromy's avatar
Jeromy committed
83 84 85
		return nil, err
	}

86
	log.Debug("resolve dag get")
Jeromy's avatar
Jeromy committed
87
	nd, err := s.DAG.Get(ctx, h)
Jeromy's avatar
Jeromy committed
88 89 90 91
	if err != nil {
		return nil, err
	}

92
	return s.ResolveLinks(ctx, nd, parts)
Jeromy's avatar
Jeromy committed
93 94 95 96
}

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

104
	result := make([]merkledag.Node, 0, len(names)+1)
105 106
	result = append(result, ndd)
	nd := ndd // dup arg workaround
Jeromy's avatar
Jeromy committed
107 108

	// for each of the path components
109
	for len(names) > 0 {
110 111 112
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, time.Minute)
		defer cancel()
Jeromy's avatar
Jeromy committed
113

114
		lnk, rest, err := nd.Resolve(names)
115
		if err == merkledag.ErrLinkNotFound {
116 117
			n := nd.Cid()
			return result, ErrNoLink{Name: names[0], Node: n}
118
		} else if err != nil {
119 120 121 122 123 124
			return result, err
		}

		nextnode, err := s.DAG.Get(ctx, lnk.Cid)
		if err != nil {
			return result, err
Jeromy's avatar
Jeromy committed
125 126
		}

127
		nd = nextnode
128
		names = rest
129
		result = append(result, nextnode)
Jeromy's avatar
Jeromy committed
130
	}
131
	return result, nil
Jeromy's avatar
Jeromy committed
132
}