resolver.go 3.85 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
	mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Jeromy's avatar
Jeromy committed
10
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11

12
	key "github.com/ipfs/go-ipfs/blocks/key"
13 14
	merkledag "github.com/ipfs/go-ipfs/merkledag"
	u "github.com/ipfs/go-ipfs/util"
Jeromy's avatar
Jeromy committed
15 16 17 18
)

var log = u.Logger("path")

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 25 26 27 28 29 30 31 32
// ErrNoLink is returned when a link is not found in a path
type ErrNoLink struct {
	name string
	node mh.Multihash
}

func (e ErrNoLink) Error() string {
	return fmt.Sprintf("no link named %q under %s", e.name, e.node.B58String())
}

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 41 42
// SplitAbsPath clean up and split fpath. It extracts the first component (which
// must be a Multihash) and return it separately.
func SplitAbsPath(fpath Path) (mh.Multihash, []string, error) {

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 55 56 57 58
	}

	// first element in the path is a b58 hash (for now)
	h, err := mh.FromB58String(parts[0])
	if err != nil {
		log.Debug("given path element is not a base58 string.\n")
59 60 61 62 63 64 65 66
		return nil, nil, err
	}

	return h, parts[1:], nil
}

// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
67 68
func (s *Resolver) ResolvePath(ctx context.Context, fpath Path) (*merkledag.Node, error) {
	nodes, err := s.ResolvePathComponents(ctx, fpath)
69 70 71
	if err != nil || nodes == nil {
		return nil, err
	}
72
	return nodes[len(nodes)-1], err
73 74 75 76 77
}

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

84 85
	log.Debug("Resolve dag get.")
	ctx, cancel := context.WithTimeout(ctx, time.Minute)
Jeromy's avatar
Jeromy committed
86
	defer cancel()
87
	nd, err := s.DAG.Get(ctx, key.Key(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 109 110

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

111
		var next key.Key
Jeromy's avatar
Jeromy committed
112 113 114 115
		var nlink *merkledag.Link
		// for each of the links in nd, the current object
		for _, link := range nd.Links {
			if link.Name == name {
116
				next = key.Key(link.Hash)
Jeromy's avatar
Jeromy committed
117 118 119 120 121 122
				nlink = link
				break
			}
		}

		if next == "" {
123 124
			n, _ := nd.Multihash()
			return result, ErrNoLink{name: name, node: n}
Jeromy's avatar
Jeromy committed
125 126 127 128
		}

		if nlink.Node == nil {
			// fetch object for link and assign to nd
129
			ctx, cancel := context.WithTimeout(ctx, time.Minute)
Jeromy's avatar
Jeromy committed
130
			defer cancel()
131 132
			var err error
			nd, err = s.DAG.Get(ctx, next)
Jeromy's avatar
Jeromy committed
133
			if err != nil {
134
				return append(result, nd), err
Jeromy's avatar
Jeromy committed
135 136 137 138 139
			}
			nlink.Node = nd
		} else {
			nd = nlink.Node
		}
140 141

		result = append(result, nlink.Node)
Jeromy's avatar
Jeromy committed
142
	}
143
	return result, nil
Jeromy's avatar
Jeromy committed
144
}