-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirbuilder.go
44 lines (36 loc) · 1.09 KB
/
dirbuilder.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
package io
import (
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
key "github.com/ipfs/go-ipfs/blocks/key"
mdag "github.com/ipfs/go-ipfs/merkledag"
format "github.com/ipfs/go-ipfs/unixfs"
)
type directoryBuilder struct {
dserv mdag.DAGService
dirnode *mdag.Node
}
// NewEmptyDirectory returns an empty merkledag Node with a folder Data chunk
func NewEmptyDirectory() *mdag.Node {
nd := new(mdag.Node)
nd.SetData(format.FolderPBData())
return nd
}
// NewDirectory returns a directoryBuilder. It needs a DAGService to add the Children
func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
db := new(directoryBuilder)
db.dserv = dserv
db.dirnode = NewEmptyDirectory()
return db
}
// AddChild adds a (name, key)-pair to the root node.
func (d *directoryBuilder) AddChild(ctx context.Context, name string, k key.Key) error {
cnode, err := d.dserv.Get(ctx, k)
if err != nil {
return err
}
return d.dirnode.AddNodeLinkClean(name, cnode)
}
// GetNode returns the root of this directoryBuilder
func (d *directoryBuilder) GetNode() *mdag.Node {
return d.dirnode
}