diff options
Diffstat (limited to 'utils/merkletrie/filesystem/node.go')
-rw-r--r-- | utils/merkletrie/filesystem/node.go | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/utils/merkletrie/filesystem/node.go b/utils/merkletrie/filesystem/node.go index 5461bd3..a8f3b86 100644 --- a/utils/merkletrie/filesystem/node.go +++ b/utils/merkletrie/filesystem/node.go @@ -136,25 +136,54 @@ func (n *node) calculateHash(path string, file os.FileInfo) ([]byte, error) { return make([]byte, 24), nil } - f, err := n.fs.Open(path) + 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) + } + + if err != nil { + return nil, err + } + + mode, err := filemode.NewFromOSFileMode(file.Mode()) if err != nil { return nil, err } + return append(hash[:], mode.Bytes()...), nil +} + +func (n *node) doCalculateHashForRegular(path string, file os.FileInfo) (plumbing.Hash, error) { + f, err := n.fs.Open(path) + if err != nil { + return plumbing.ZeroHash, err + } + defer f.Close() h := plumbing.NewHasher(plumbing.BlobObject, file.Size()) if _, err := io.Copy(h, f); err != nil { - return nil, err + return plumbing.ZeroHash, err } - mode, err := filemode.NewFromOSFileMode(file.Mode()) + return h.Sum(), nil +} + +func (n *node) doCalculateHashForSymlink(path string, file os.FileInfo) (plumbing.Hash, error) { + target, err := n.fs.Readlink(path) if err != nil { - return nil, err + return plumbing.ZeroHash, err } - hash := h.Sum() - return append(hash[:], mode.Bytes()...), nil + h := plumbing.NewHasher(plumbing.BlobObject, file.Size()) + if _, err := h.Write([]byte(target)); err != nil { + return plumbing.ZeroHash, err + } + + return h.Sum(), nil } func (n *node) String() string { |