resolver.go 3.64 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
// package path implements utilities for resolving paths within ipfs.
package path

import (
	"fmt"
Jeromy's avatar
Jeromy committed
6
	"time"
Jeromy's avatar
Jeromy committed
7

8
	mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Jeromy's avatar
Jeromy committed
9
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 11
	merkledag "github.com/ipfs/go-ipfs/merkledag"
	u "github.com/ipfs/go-ipfs/util"
Jeromy's avatar
Jeromy committed
12 13 14 15
)

var log = u.Logger("path")

16 17 18 19 20 21 22 23 24 25
// 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
26 27 28 29 30 31
// 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
}

32 33 34 35
// 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
36 37 38 39 40 41 42 43 44
	log.Debugf("Resolve: '%s'", fpath)

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

	// if nothing, bail.
	if len(parts) == 0 {
45
		return nil, nil, fmt.Errorf("ipfs path must contain at least one component")
Jeromy's avatar
Jeromy committed
46 47 48 49 50 51
	}

	// 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")
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		return nil, nil, err
	}

	return h, parts[1:], nil
}

// ResolvePath fetches the node for given path. It returns the last item
// returned by ResolvePathComponents.
func (s *Resolver) ResolvePath(fpath Path) (*merkledag.Node, error) {
	nodes, err := s.ResolvePathComponents(fpath)
	if err != nil || nodes == nil {
		return nil, err
	} else {
		return nodes[len(nodes)-1], err
	}
}

// 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.
func (s *Resolver) ResolvePathComponents(fpath Path) ([]*merkledag.Node, error) {
	h, parts, err := SplitAbsPath(fpath)
	if err != nil {
Jeromy's avatar
Jeromy committed
75 76 77 78
		return nil, err
	}

	log.Debug("Resolve dag get.\n")
Jeromy's avatar
Jeromy committed
79 80 81
	ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
	defer cancel()
	nd, err := s.DAG.Get(ctx, u.Key(h))
Jeromy's avatar
Jeromy committed
82 83 84 85
	if err != nil {
		return nil, err
	}

86
	return s.ResolveLinks(nd, parts)
Jeromy's avatar
Jeromy committed
87 88 89 90
}

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

99 100 101
	result = make([]*merkledag.Node, 0, len(names)+1)
	result = append(result, ndd)
	nd := ndd // dup arg workaround
Jeromy's avatar
Jeromy committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

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

		var next u.Key
		var nlink *merkledag.Link
		// for each of the links in nd, the current object
		for _, link := range nd.Links {
			if link.Name == name {
				next = u.Key(link.Hash)
				nlink = link
				break
			}
		}

		if next == "" {
118 119
			n, _ := nd.Multihash()
			return result, ErrNoLink{name: name, node: n}
Jeromy's avatar
Jeromy committed
120 121 122 123
		}

		if nlink.Node == nil {
			// fetch object for link and assign to nd
Jeromy's avatar
Jeromy committed
124 125 126
			ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
			defer cancel()
			nd, err = s.DAG.Get(ctx, next)
Jeromy's avatar
Jeromy committed
127
			if err != nil {
128
				return append(result, nd), err
Jeromy's avatar
Jeromy committed
129 130 131 132 133
			}
			nlink.Node = nd
		} else {
			nd = nlink.Node
		}
134 135

		result = append(result, nlink.Node)
Jeromy's avatar
Jeromy committed
136 137 138
	}
	return
}