From ad32c0a09190fc2cbeafa1d1a2962b80406789cf Mon Sep 17 00:00:00 2001 From: William Murphy Date: Fri, 28 Dec 2018 06:38:15 -0800 Subject: [PATCH] Make changes to mode or owner show in UI (#142) Previously, changes that did not affect the data in a file, such as a chmod or chown, would incorrectly show up as unmodified in the UI. This change makes it so that changes to the mode or owner (Mode, Gid, or Uid) of a file cause the file to appear modified in the UI. --- .data/Dockerfile | 1 + filetree/data.go | 5 ++++- filetree/tree_test.go | 46 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/.data/Dockerfile b/.data/Dockerfile index 1ed949c1..78bad004 100644 --- a/.data/Dockerfile +++ b/.data/Dockerfile @@ -11,3 +11,4 @@ RUN rm -rf /root/example/ ADD .data/ /root/.data/ RUN cp /root/saved.txt /tmp/saved.again1.txt RUN cp /root/saved.txt /root/.data/saved.again2.txt +RUN chmod +x /root/saved.txt diff --git a/filetree/data.go b/filetree/data.go index 379454e3..64cecebb 100644 --- a/filetree/data.go +++ b/filetree/data.go @@ -122,7 +122,10 @@ func (data *FileInfo) Copy() *FileInfo { // Compare determines the DiffType between two FileInfos based on the type and contents of each given FileInfo func (data *FileInfo) Compare(other FileInfo) DiffType { if data.TypeFlag == other.TypeFlag { - if data.hash == other.hash { + if data.hash == other.hash && + data.Mode == other.Mode && + data.Uid == other.Uid && + data.Gid == other.Gid { return Unchanged } } diff --git a/filetree/tree_test.go b/filetree/tree_test.go index 92c83f2d..15eb4980 100644 --- a/filetree/tree_test.go +++ b/filetree/tree_test.go @@ -348,9 +348,9 @@ func TestCompareWithAdds(t *testing.T) { func TestCompareWithChanges(t *testing.T) { lowerTree := NewFileTree() upperTree := NewFileTree() - paths := [...]string{"/etc", "/usr", "/etc/hosts", "/etc/sudoers", "/usr/bin"} + changedPaths := []string{"/etc", "/usr", "/etc/hosts", "/etc/sudoers", "/usr/bin"} - for _, value := range paths { + for _, value := range changedPaths { lowerTree.AddPath(value, FileInfo{ Path: value, TypeFlag: 1, @@ -363,13 +363,53 @@ func TestCompareWithChanges(t *testing.T) { }) } + chmodPath := "/etc/non-data-change" + + lowerTree.AddPath(chmodPath, FileInfo{ + Path: chmodPath, + TypeFlag: 1, + hash: 123, + Mode: 0, + }) + + upperTree.AddPath(chmodPath, FileInfo{ + Path: chmodPath, + TypeFlag: 1, + hash: 123, + Mode: 1, + }) + + changedPaths = append(changedPaths, chmodPath) + + chownPath := "/etc/non-data-change-2" + + lowerTree.AddPath(chmodPath, FileInfo{ + Path: chownPath, + TypeFlag: 1, + hash: 123, + Mode: 1, + Gid: 0, + Uid: 0, + }) + + upperTree.AddPath(chmodPath, FileInfo{ + Path: chownPath, + TypeFlag: 1, + hash: 123, + Mode: 1, + Gid: 12, + Uid: 12, + }) + + changedPaths = append(changedPaths, chownPath) + lowerTree.Compare(upperTree) failedAssertions := []error{} asserter := func(n *FileNode) error { p := n.Path() if p == "/" { return nil - } else if stringInSlice(p, []string{"/etc", "/usr", "/etc/hosts", "/etc/sudoers", "/usr/bin"}) { + } else if stringInSlice(p, changedPaths) { if err := AssertDiffType(n, Changed); err != nil { failedAssertions = append(failedAssertions, err) }