From ac37b23f38bfa3f1d6f8af06b0a71a58fcdc031a Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 27 Jun 2016 13:16:24 -0400 Subject: [PATCH 1/3] keywords: time can be 0 Signed-off-by: Vincent Batts --- keywords.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/keywords.go b/keywords.go index bffe169..dcf368f 100644 --- a/keywords.go +++ b/keywords.go @@ -208,6 +208,9 @@ var ( } timeKeywordFunc = func(path string, info os.FileInfo) (string, error) { t := info.ModTime().UnixNano() + if t == 0 { + return "time=0.0", nil + } return fmt.Sprintf("time=%d.%d", (t / 1e9), (t % (t / 1e9))), nil } linkKeywordFunc = func(path string, info os.FileInfo) (string, error) { From fca9d4b5b6271865cd5f90f24877e88017b619f2 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 27 Jun 2016 13:17:03 -0400 Subject: [PATCH 2/3] check: failing test case for #8 https://github.com/vbatts/go-mtree/issues/8 Signed-off-by: Vincent Batts --- check_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/check_test.go b/check_test.go index de7c96c..81dab47 100644 --- a/check_test.go +++ b/check_test.go @@ -1,6 +1,7 @@ package mtree import ( + "bytes" "io/ioutil" "os" "path/filepath" @@ -95,3 +96,46 @@ func ExampleCheck() { // handle failed validity ... } } + +// https://github.com/vbatts/go-mtree/issues/8 +func TestTimeComparison(t *testing.T) { + dir, err := ioutil.TempDir("", "test-time.") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + // This is the format of time from FreeBSD + spec := ` +/set type=file time=0.000000000 +. type=dir + file time=0.000000000 +.. +` + + fh, err := os.Create(filepath.Join(dir, "file")) + if err != nil { + t.Fatal(err) + } + // This is what mode we're checking for. Round integer of epoch seconds + epoch := time.Unix(0, 0) + if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil { + t.Fatal(err) + } + if err := fh.Close(); err != nil { + t.Error(err) + } + + dh, err := ParseSpec(bytes.NewBufferString(spec)) + if err != nil { + t.Fatal(err) + } + + res, err := Check(dir, dh, nil) + if err != nil { + t.Error(err) + } + if len(res.Failures) > 0 { + t.Fatal(res.Failures) + } +} From 6adcc98b22c83250d13acac24e33026e77f73bb9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 27 Jun 2016 13:24:03 -0400 Subject: [PATCH 3/3] keywords: time keyword 9 postition decimal Matching with the FreeBSD standard format Signed-off-by: Vincent Batts --- check_test.go | 6 +++--- keywords.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/check_test.go b/check_test.go index 81dab47..bd757ef 100644 --- a/check_test.go +++ b/check_test.go @@ -107,9 +107,9 @@ func TestTimeComparison(t *testing.T) { // This is the format of time from FreeBSD spec := ` -/set type=file time=0.000000000 +/set type=file time=5.000000000 . type=dir - file time=0.000000000 + file time=5.000000000 .. ` @@ -118,7 +118,7 @@ func TestTimeComparison(t *testing.T) { t.Fatal(err) } // This is what mode we're checking for. Round integer of epoch seconds - epoch := time.Unix(0, 0) + epoch := time.Unix(5, 0) if err := os.Chtimes(fh.Name(), epoch, epoch); err != nil { t.Fatal(err) } diff --git a/keywords.go b/keywords.go index dcf368f..8b01c80 100644 --- a/keywords.go +++ b/keywords.go @@ -209,9 +209,9 @@ var ( timeKeywordFunc = func(path string, info os.FileInfo) (string, error) { t := info.ModTime().UnixNano() if t == 0 { - return "time=0.0", nil + return "time=0.000000000", nil } - return fmt.Sprintf("time=%d.%d", (t / 1e9), (t % (t / 1e9))), nil + return fmt.Sprintf("time=%d.%9.9d", (t / 1e9), (t % (t / 1e9))), nil } linkKeywordFunc = func(path string, info os.FileInfo) (string, error) { if info.Mode()&os.ModeSymlink != 0 {