forked from CUIT-CBI/merkle-dag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag2file.go
82 lines (75 loc) · 1.82 KB
/
dag2file.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
81
82
package merkledag
import (
"encoding/json"
"strings"
)
const STEP = 4
// Hash to file
// example a path : /doc/tmp/temp.txt
func Hash2File(store KVStore, hash []byte, path string, hp HashPool) []byte {
// 根据hash和path, 返curObjBinary回对应的文件, hash对应的类型是tree
flag, _ := store.Has(hash)
if flag {
objBinary, _ := store.Get(hash)
obj := binaryToObj(objBinary)
pathArr := strings.Split(path, "\\")
cur := 1
return getFileByDir(obj, pathArr, cur, store)
}
return nil
}
func getFileByDir(obj *Object, pathArr []string, cur int, store KVStore) []byte {
if cur >= len(pathArr) {
return nil
}
index := 0
for i := range obj.Links {
objType := string(obj.Data[index : index+STEP])
index += STEP
objInfo := obj.Links[i]
if objInfo.Name != pathArr[cur] {
continue
}
switch objType {
case TREE:
objDirBinary, _ := store.Get(objInfo.Hash)
objDir := binaryToObj(objDirBinary)
ans := getFileByDir(objDir, pathArr, cur+1, store)
if ans != nil {
return ans
}
case BLOB:
ans, _ := store.Get(objInfo.Hash)
return ans
case LIST:
objLinkBinary, _ := store.Get(objInfo.Hash)
objList := binaryToObj(objLinkBinary)
ans := getFileByList(objList, store)
return ans
}
}
return nil
}
func getFileByList(obj *Object, store KVStore) []byte {
ans := make([]byte, 0)
index := 0
for i := range obj.Links {
curObjType := string(obj.Data[index : index+STEP])
index += STEP
curObjLink := obj.Links[i]
curObjBinary, _ := store.Get(curObjLink.Hash)
curObj := binaryToObj(curObjBinary)
if curObjType == BLOB {
ans = append(ans, curObjBinary...)
} else { //List
tmp := getFileByList(curObj, store)
ans = append(ans, tmp...)
}
}
return ans
}
func binaryToObj(objBinary []byte) *Object {
var res Object
json.Unmarshal(objBinary, &res)
return &res
}