aboutsummaryrefslogtreecommitdiffstats
path: root/utils/merkletrie
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2024-01-10 11:47:41 +0000
committerGitHub <noreply@github.com>2024-01-10 11:47:41 +0000
commitf7c30f52dca01f68790fdd74f519817c35fdf257 (patch)
tree13dd879b2224169db153002a5101199578ee6da4 /utils/merkletrie
parentd0f4aa16e061617f64f12fd852970fd30b0cfc5c (diff)
parentc5b2e50b8907555538f52356fc04b2a5820b5c6f (diff)
downloadgo-git-f7c30f52dca01f68790fdd74f519817c35fdf257.tar.gz
Merge pull request #825 from candid82/master
Calculate filesystem node's hash lazily.
Diffstat (limited to 'utils/merkletrie')
-rw-r--r--utils/merkletrie/filesystem/node.go76
1 files changed, 37 insertions, 39 deletions
diff --git a/utils/merkletrie/filesystem/node.go b/utils/merkletrie/filesystem/node.go
index 7bba0d0..6c91f44 100644
--- a/utils/merkletrie/filesystem/node.go
+++ b/utils/merkletrie/filesystem/node.go
@@ -29,6 +29,8 @@ type node struct {
hash []byte
children []noder.Noder
isDir bool
+ mode os.FileMode
+ size int64
}
// NewRootNode returns the root node based on a given billy.Filesystem.
@@ -50,6 +52,9 @@ func NewRootNode(
//
// The hash of a directory is always a 24-bytes slice of zero values
func (n *node) Hash() []byte {
+ if n.hash == nil {
+ n.calculateHash()
+ }
return n.hash
}
@@ -121,81 +126,74 @@ func (n *node) calculateChildren() error {
func (n *node) newChildNode(file os.FileInfo) (*node, error) {
path := path.Join(n.path, file.Name())
- hash, err := n.calculateHash(path, file)
- if err != nil {
- return nil, err
- }
-
node := &node{
fs: n.fs,
submodules: n.submodules,
path: path,
- hash: hash,
isDir: file.IsDir(),
+ size: file.Size(),
+ mode: file.Mode(),
}
- if hash, isSubmodule := n.submodules[path]; isSubmodule {
- node.hash = append(hash[:], filemode.Submodule.Bytes()...)
+ if _, isSubmodule := n.submodules[path]; isSubmodule {
node.isDir = false
}
return node, nil
}
-func (n *node) calculateHash(path string, file os.FileInfo) ([]byte, error) {
- if file.IsDir() {
- return make([]byte, 24), nil
- }
-
- var hash plumbing.Hash
- var err error
- if file.Mode()&os.ModeSymlink != 0 {
- hash, err = n.doCalculateHashForSymlink(path, file)
- } else {
- hash, err = n.doCalculateHashForRegular(path, file)
+func (n *node) calculateHash() {
+ if n.isDir {
+ n.hash = make([]byte, 24)
+ return
}
-
+ mode, err := filemode.NewFromOSFileMode(n.mode)
if err != nil {
- return nil, err
+ n.hash = plumbing.ZeroHash[:]
+ return
}
-
- mode, err := filemode.NewFromOSFileMode(file.Mode())
- if err != nil {
- return nil, err
+ if submoduleHash, isSubmodule := n.submodules[n.path]; isSubmodule {
+ n.hash = append(submoduleHash[:], filemode.Submodule.Bytes()...)
+ return
}
-
- return append(hash[:], mode.Bytes()...), nil
+ var hash plumbing.Hash
+ if n.mode&os.ModeSymlink != 0 {
+ hash = n.doCalculateHashForSymlink()
+ } else {
+ hash = n.doCalculateHashForRegular()
+ }
+ n.hash = append(hash[:], mode.Bytes()...)
}
-func (n *node) doCalculateHashForRegular(path string, file os.FileInfo) (plumbing.Hash, error) {
- f, err := n.fs.Open(path)
+func (n *node) doCalculateHashForRegular() plumbing.Hash {
+ f, err := n.fs.Open(n.path)
if err != nil {
- return plumbing.ZeroHash, err
+ return plumbing.ZeroHash
}
defer f.Close()
- h := plumbing.NewHasher(plumbing.BlobObject, file.Size())
+ h := plumbing.NewHasher(plumbing.BlobObject, n.size)
if _, err := io.Copy(h, f); err != nil {
- return plumbing.ZeroHash, err
+ return plumbing.ZeroHash
}
- return h.Sum(), nil
+ return h.Sum()
}
-func (n *node) doCalculateHashForSymlink(path string, file os.FileInfo) (plumbing.Hash, error) {
- target, err := n.fs.Readlink(path)
+func (n *node) doCalculateHashForSymlink() plumbing.Hash {
+ target, err := n.fs.Readlink(n.path)
if err != nil {
- return plumbing.ZeroHash, err
+ return plumbing.ZeroHash
}
- h := plumbing.NewHasher(plumbing.BlobObject, file.Size())
+ h := plumbing.NewHasher(plumbing.BlobObject, n.size)
if _, err := h.Write([]byte(target)); err != nil {
- return plumbing.ZeroHash, err
+ return plumbing.ZeroHash
}
- return h.Sum(), nil
+ return h.Sum()
}
func (n *node) String() string {