aboutsummaryrefslogtreecommitdiffstats
path: root/utils/merkletrie/filesystem/node.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-06-18 21:16:23 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-06-18 21:16:23 +0200
commit1fd0a4963b4a5002b23bf4faa7814adca73818d6 (patch)
tree628051b686f71898965c0febd3e13097b2f64362 /utils/merkletrie/filesystem/node.go
parent5d1c674ea818ea534476ed0ca5436e0b513f86a4 (diff)
downloadgo-git-1fd0a4963b4a5002b23bf4faa7814adca73818d6.tar.gz
utils: merkletrie support for symlinks
Diffstat (limited to 'utils/merkletrie/filesystem/node.go')
-rw-r--r--utils/merkletrie/filesystem/node.go41
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 {