Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dag2file.go #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Update dag2file.go #5

wants to merge 3 commits into from

Conversation

YZQ-zing
Copy link

@YZQ-zing YZQ-zing commented Apr 7, 2024

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant