aboutsummaryrefslogtreecommitdiffstats
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
parent5d1c674ea818ea534476ed0ca5436e0b513f86a4 (diff)
downloadgo-git-1fd0a4963b4a5002b23bf4faa7814adca73818d6.tar.gz
utils: merkletrie support for symlinks
-rw-r--r--utils/merkletrie/filesystem/node.go41
-rw-r--r--utils/merkletrie/filesystem/node_test.go19
2 files changed, 54 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 {
diff --git a/utils/merkletrie/filesystem/node_test.go b/utils/merkletrie/filesystem/node_test.go
index a383716..bf1178a 100644
--- a/utils/merkletrie/filesystem/node_test.go
+++ b/utils/merkletrie/filesystem/node_test.go
@@ -25,11 +25,13 @@ func (s *NoderSuite) TestDiff(c *C) {
WriteFile(fsA, "foo", []byte("foo"), 0644)
WriteFile(fsA, "qux/bar", []byte("foo"), 0644)
WriteFile(fsA, "qux/qux", []byte("foo"), 0644)
+ fsA.Symlink("foo", "bar")
fsB := memfs.New()
WriteFile(fsB, "foo", []byte("foo"), 0644)
WriteFile(fsB, "qux/bar", []byte("foo"), 0644)
WriteFile(fsB, "qux/qux", []byte("foo"), 0644)
+ fsB.Symlink("foo", "bar")
ch, err := merkletrie.DiffTree(
NewRootNode(fsA, nil),
@@ -41,6 +43,23 @@ func (s *NoderSuite) TestDiff(c *C) {
c.Assert(ch, HasLen, 0)
}
+func (s *NoderSuite) TestDiffChangeLink(c *C) {
+ fsA := memfs.New()
+ fsA.Symlink("qux", "foo")
+
+ fsB := memfs.New()
+ fsB.Symlink("bar", "foo")
+
+ ch, err := merkletrie.DiffTree(
+ NewRootNode(fsA, nil),
+ NewRootNode(fsB, nil),
+ IsEquals,
+ )
+
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 1)
+}
+
func (s *NoderSuite) TestDiffChangeContent(c *C) {
fsA := memfs.New()
WriteFile(fsA, "foo", []byte("foo"), 0644)