-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunixfs_cat.go
80 lines (66 loc) · 1.71 KB
/
unixfs_cat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package unixfs_cat
import (
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs"
unixfspb "github.com/ipfs/go-unixfs/pb"
"github.com/multiformats/go-multihash"
)
type nodeWithLinks struct {
node *unixfs.FSNode
links []ipld.Link
}
type NodeWithName struct {
node ipld.Node
name string
}
type ParentDagBuilder struct {
maxLinks int
}
func (pdb ParentDagBuilder) ConcatFileNodes(nodes ...ipld.Node) ([]*merkledag.ProtoNode, error) {
var pbns []*merkledag.ProtoNode
ndwl := nodeWithLinks{node: unixfs.NewFSNode(unixfspb.Data_File)}
for _, node := range nodes {
if len(ndwl.node.BlockSizes()) < pdb.maxLinks {
if err := ndwl.concatFileNode(node); err != nil {
return nil, err
}
} else {
pbn, err := ndwl.constructPbNode()
if err != nil {
return nil, err
}
pbns = append(pbns, pbn)
ndwl = nodeWithLinks{node: unixfs.NewFSNode(unixfspb.Data_File)}
if err := ndwl.concatFileNode(node); err != nil {
return nil, err
}
}
}
pbn, err := ndwl.constructPbNode()
if err != nil {
return nil, err
}
pbns = append(pbns, pbn)
return pbns, nil
}
func (pdb ParentDagBuilder) ConstructParentDirectory(nodes ...NodeWithName) (*merkledag.ProtoNode, error) {
ndbs, err := unixfs.NewFSNode(unixfspb.Data_Directory).GetBytes()
if err != nil {
return nil, err
}
nd := merkledag.NodeWithData(ndbs)
err = nd.SetCidBuilder(cid.V1Builder{Codec: cid.DagProtobuf, MhType: multihash.SHA2_256})
if err != nil {
return nil, err
}
for _, node := range nodes {
s, _ := node.node.Size()
err = nd.AddRawLink(node.name, &ipld.Link{Cid: node.node.Cid(), Size: s})
if err != nil {
return nil, err
}
}
return nd, nil
}