resolver.go 3.46 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
	"errors"
Jeromy's avatar
Jeromy committed
6
	"fmt"
Jeromy's avatar
Jeromy committed
7
	"time"
Jeromy's avatar
Jeromy committed
8

9 10
	"context"
	mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
11

12
	merkledag "github.com/ipfs/go-ipfs/merkledag"
Jeromy's avatar
Jeromy committed
13
	logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
14
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
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 26
	Name string
	Node mh.Multihash
27 28 29
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	// for each of the path components
	for _, name := range names {

112 113 114
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, time.Minute)
		defer cancel()
Jeromy's avatar
Jeromy committed
115

116 117
		nextnode, err := nd.GetLinkedNode(ctx, s.DAG, name)
		if err == merkledag.ErrLinkNotFound {
Jeromy's avatar
Jeromy committed
118
			n := nd.Multihash()
119
			return result, ErrNoLink{Name: name, Node: n}
120 121
		} else if err != nil {
			return append(result, nextnode), err
Jeromy's avatar
Jeromy committed
122 123
		}

124 125
		nd = nextnode
		result = append(result, nextnode)
Jeromy's avatar
Jeromy committed
126
	}
127
	return result, nil
Jeromy's avatar
Jeromy committed
128
}