Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
package merkledag
import (
"encoding/json"
"strings"
)
// Hash to file
// example a path : /doc/tmp/temp.txt
func Hash2File(store KVStore, hash []byte, path string, hp HashPool) []byte {
// 根据hash和path, 返回对应的文件, hash对应的类型是tree
flag, _ := store.Has(hash)
if flag {
objBinary, _ := store.Get(hash)
var obj Object
json.Unmarshal(objBinary, &obj)
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+4])
index += 4
objInfo := obj.Links[i]
if objInfo.Name != pathArr[cur] {
continue
}
switch objType {
case TREE:
objDirBinary, _ := store.Get(objInfo.Hash)
var objDir Object
json.Unmarshal(objDirBinary, &objDir)
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)
var objLink Object
json.Unmarshal(objLinkBinary, &objLink)
ans := getFileByList(objLink, 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+4])
index += 4
curObjLink := obj.Links[i]
curObjBinary, _ := store.Get(curObjLink.Hash)
var curObj Object
json.Unmarshal(curObjBinary, &curObj)
if curObjType == BLOB {
ans = append(ans, curObjBinary...)
} else { //List
tmp := getFileByList(curObj, store)
ans = append(ans, tmp...)
}
}
return ans
}