only look at first 1kb of data.yaml when building tree

master
Bel LaPointe 2022-02-16 15:21:51 -07:00
parent 51a8c8b425
commit 2781114863
1 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -92,7 +93,7 @@ func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error)
} }
for _, entry := range entries { for _, entry := range entries {
if entry.Name() == "data.yaml" { if entry.Name() == "data.yaml" {
if b, err := ioutil.ReadFile(path.Join(tree.root, entry.Name())); err != nil { if b, err := peekFile(withContent, path.Join(tree.root, entry.Name())); err != nil {
return Branch{}, err return Branch{}, err
} else if err := yaml.Unmarshal(b, &m.Leaf); err != nil { } else if err := yaml.Unmarshal(b, &m.Leaf); err != nil {
return Branch{}, err return Branch{}, err
@ -115,6 +116,19 @@ func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error)
return m, nil return m, nil
} }
func peekFile(all bool, path string) ([]byte, error) {
if !all {
return ioutil.ReadFile(path)
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
r := io.LimitReader(f, 1024)
return ioutil.ReadAll(r)
}
func (tree Tree) toDir(id ID) string { func (tree Tree) toDir(id ID) string {
return path.Dir(tree.toData(id)) return path.Dir(tree.toData(id))
} }