resolve.go 998 Bytes
Newer Older
1 2 3 4 5
package io

import (
	"context"

Jeromy's avatar
Jeromy committed
6 7 8
	dag "github.com/ipfs/go-merkledag"
	ft "github.com/ipfs/go-unixfs"
	hamt "github.com/ipfs/go-unixfs/hamt"
9

Jeromy's avatar
Jeromy committed
10
	ipld "github.com/ipfs/go-ipld-format"
11 12
)

Jeromy's avatar
Jeromy committed
13 14
// ResolveUnixfsOnce resolves a single hop of a path through a graph in a
// unixfs context. This includes handling traversing sharded directories.
Jeromy's avatar
Jeromy committed
15
func ResolveUnixfsOnce(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
16 17
	switch nd := nd.(type) {
	case *dag.ProtoNode:
Overbool's avatar
Overbool committed
18
		fsn, err := ft.FSNodeFromBytes(nd.Data())
19 20
		if err != nil {
			// Not a unixfs node, use standard object traversal code
Overbool's avatar
Overbool committed
21
			return nd.ResolveLink(names)
22 23
		}

Overbool's avatar
Overbool committed
24
		switch fsn.Type() {
25
		case ft.THAMTShard:
Jeromy's avatar
Jeromy committed
26 27
			rods := dag.NewReadOnlyDagService(ds)
			s, err := hamt.NewHamtFromDag(rods, nd)
28
			if err != nil {
Jeromy's avatar
Jeromy committed
29
				return nil, nil, err
30 31
			}

Jeromy's avatar
Jeromy committed
32
			out, err := s.Find(ctx, names[0])
33
			if err != nil {
Jeromy's avatar
Jeromy committed
34
				return nil, nil, err
35 36
			}

Jeromy's avatar
Jeromy committed
37
			return out, names[1:], nil
38
		}
39
	}
Overbool's avatar
Overbool committed
40
	return nd.ResolveLink(names)
41
}