From 2facedc401ccf1bebfec56d2984bf06f1c6e885b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 26 Jul 2016 09:02:30 -0400 Subject: [PATCH] check: functionality for symlinks Default behavior (according to upstream mtree) for validating symlinks is to just validate the link itself, not to follow it. Signed-off-by: Stephen Chung --- check.go | 27 ++++++++++++------- check_test.go | 18 +++++++++++++ testdata/dirwithbrokenlink/ file with spaces | 0 .../deepdir/dir3/dir4/deepfile | 0 .../deepdir/dir3/dir4/dir5/dir6/deeperfile | 1 + .../deepdir/dir3/dir4/dir5/dir7/deeperfile | 1 + testdata/dirwithbrokenlink/deepdir/dir3/file | 0 testdata/dirwithbrokenlink/deepdir/dir3/file6 | 0 testdata/dirwithbrokenlink/deepdir/dir3/file7 | 0 .../dirwithbrokenlink/deepdir/this_is_a_file | 0 testdata/dirwithbrokenlink/dir1/.hiddenfile | 0 testdata/dirwithbrokenlink/dir1/badlink | 1 + .../dir1/dir2/.hidden/goodlink | 1 + testdata/dirwithbrokenlink/dir1/goodfile | 1 + testdata/dirwithbrokenlink/file1 | 0 testdata/dirwithbrokenlink/file2 | 0 16 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 testdata/dirwithbrokenlink/ file with spaces create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/dir4/deepfile create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/file create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/file6 create mode 100644 testdata/dirwithbrokenlink/deepdir/dir3/file7 create mode 100644 testdata/dirwithbrokenlink/deepdir/this_is_a_file create mode 100644 testdata/dirwithbrokenlink/dir1/.hiddenfile create mode 120000 testdata/dirwithbrokenlink/dir1/badlink create mode 120000 testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink create mode 100644 testdata/dirwithbrokenlink/dir1/goodfile create mode 100644 testdata/dirwithbrokenlink/file1 create mode 100644 testdata/dirwithbrokenlink/file2 diff --git a/check.go b/check.go index c7752b0..4adfe73 100644 --- a/check.go +++ b/check.go @@ -84,16 +84,25 @@ func Check(root string, dh *DirectoryHierarchy, keywords []string) (*Result, err if keywords != nil && !inSlice(kv.Keyword(), keywords) { continue } - fh, err := os.Open(pathname) - if err != nil { - return nil, err - } - curKeyVal, err := keywordFunc(pathname, info, fh) - if err != nil { + + var curKeyVal string + if info.Mode().IsRegular() { + fh, err := os.Open(pathname) + if err != nil { + return nil, err + } + curKeyVal, err = keywordFunc(pathname, info, fh) + if err != nil { + fh.Close() + return nil, err + } fh.Close() - return nil, err + } else { + curKeyVal, err = keywordFunc(pathname, info, nil) + if err != nil { + return nil, err + } } - fh.Close() if string(kv) != curKeyVal { failure := Failure{Path: pathname, Keyword: kv.Keyword(), Expected: kv.Value(), Got: KeyVal(curKeyVal).Value()} result.Failures = append(result.Failures, failure) @@ -174,7 +183,7 @@ func TarCheck(tarDH, dh *DirectoryHierarchy, keywords []string) (*Result, error) } for _, kv := range kvs { - + // TODO: keep track of symlinks if _, ok := KeywordFuncs[kv.Keyword()]; !ok { return nil, fmt.Errorf("Unknown keyword %q for file %q", kv.Keyword(), pathname) } diff --git a/check_test.go b/check_test.go index 035c2b3..5a54e40 100644 --- a/check_test.go +++ b/check_test.go @@ -97,6 +97,24 @@ func ExampleCheck() { } } +// Tests default action for evaluating a symlink, which is just to compare the +// link itself, not to follow it +func TestDefaultBrokenLink(t *testing.T) { + dh, err := Walk("./testdata/dirwithbrokenlink", nil, append(DefaultKeywords, "sha1")) + if err != nil { + t.Fatal(err) + } + res, err := Check("./testdata/dirwithbrokenlink", dh, nil) + if err != nil { + t.Fatal(err) + } + if res != nil && len(res.Failures) > 0 { + for _, f := range res.Failures { + t.Error(f) + } + } +} + // https://github.com/vbatts/go-mtree/issues/8 func TestTimeComparison(t *testing.T) { dir, err := ioutil.TempDir("", "test-time.") diff --git a/testdata/dirwithbrokenlink/ file with spaces b/testdata/dirwithbrokenlink/ file with spaces new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/deepfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/deepfile new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile new file mode 100644 index 0000000..fae9c31 --- /dev/null +++ b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir6/deeperfile @@ -0,0 +1 @@ +some spacious text diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile new file mode 100644 index 0000000..b0cb8ea --- /dev/null +++ b/testdata/dirwithbrokenlink/deepdir/dir3/dir4/dir5/dir7/deeperfile @@ -0,0 +1 @@ +some nice text diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file b/testdata/dirwithbrokenlink/deepdir/dir3/file new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file6 b/testdata/dirwithbrokenlink/deepdir/dir3/file6 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/dir3/file7 b/testdata/dirwithbrokenlink/deepdir/dir3/file7 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/deepdir/this_is_a_file b/testdata/dirwithbrokenlink/deepdir/this_is_a_file new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/dir1/.hiddenfile b/testdata/dirwithbrokenlink/dir1/.hiddenfile new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/dir1/badlink b/testdata/dirwithbrokenlink/dir1/badlink new file mode 120000 index 0000000..7768d5f --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/badlink @@ -0,0 +1 @@ +badfile \ No newline at end of file diff --git a/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink b/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink new file mode 120000 index 0000000..e66f4ae --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/dir2/.hidden/goodlink @@ -0,0 +1 @@ +../../goodfile \ No newline at end of file diff --git a/testdata/dirwithbrokenlink/dir1/goodfile b/testdata/dirwithbrokenlink/dir1/goodfile new file mode 100644 index 0000000..9c7b8e1 --- /dev/null +++ b/testdata/dirwithbrokenlink/dir1/goodfile @@ -0,0 +1 @@ +link text diff --git a/testdata/dirwithbrokenlink/file1 b/testdata/dirwithbrokenlink/file1 new file mode 100644 index 0000000..e69de29 diff --git a/testdata/dirwithbrokenlink/file2 b/testdata/dirwithbrokenlink/file2 new file mode 100644 index 0000000..e69de29