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

import (
	"context"

6 7 8
	dag "gitlab.dms3.io/dms3/go-merkledag"
	ft "gitlab.dms3.io/dms3/go-unixfs"
	hamt "gitlab.dms3.io/dms3/go-unixfs/hamt"
9

10
	ld "gitlab.dms3.io/dms3/go-ld-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.
15
func ResolveUnixfsOnce(ctx context.Context, ds ld.NodeGetter, nd ld.Node, names []string) (*ld.Link, []string, error) {
16 17 18
	pn, ok := nd.(*dag.ProtoNode)
	if ok {
		fsn, err := ft.FSNodeFromBytes(pn.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
		}

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

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

Jeromy's avatar
Jeromy committed
36
			return out, names[1:], nil
37
		}
38
	}
39

Overbool's avatar
Overbool committed
40
	return nd.ResolveLink(names)
41
}