From d8868e27fde79a292ff690c87dc3967e29823100 Mon Sep 17 00:00:00 2001 From: Michael Woolnough Date: Mon, 7 Oct 2024 12:57:11 +0100 Subject: [PATCH] Fix for negative timestamps in summaries. --- summary/summary.go | 4 ++-- summary/summary_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/summary/summary.go b/summary/summary.go index 99c4bd197..f8de51366 100644 --- a/summary/summary.go +++ b/summary/summary.go @@ -54,11 +54,11 @@ type summaryWithAtime struct { func (s *summaryWithAtime) add(size int64, atime int64, mtime int64) { s.summary.add(size) - if s.atime == 0 || atime < s.atime { + if atime > 0 && (s.atime == 0 || atime < s.atime) { s.atime = atime } - if s.mtime == 0 || mtime > s.mtime { + if mtime > 0 && (s.mtime == 0 || mtime > s.mtime) { s.mtime = mtime } } diff --git a/summary/summary_test.go b/summary/summary_test.go index 34ef8685b..30e0a9d93 100644 --- a/summary/summary_test.go +++ b/summary/summary_test.go @@ -45,4 +45,28 @@ func TestSummary(t *testing.T) { So(s.size, ShouldEqual, 30) }) }) + + Convey("Given a summaryWithAtime", t, func() { + s := &summaryWithAtime{} + + Convey("You can add sizes and atime/mtimes to it", func() { + s.add(10, 12, 24) + So(s.count, ShouldEqual, 1) + So(s.size, ShouldEqual, 10) + So(s.atime, ShouldEqual, 12) + So(s.mtime, ShouldEqual, 24) + + s.add(20, -5, -10) + So(s.count, ShouldEqual, 2) + So(s.size, ShouldEqual, 30) + So(s.atime, ShouldEqual, 12) + So(s.mtime, ShouldEqual, 24) + + s.add(30, 1, 30) + So(s.count, ShouldEqual, 3) + So(s.size, ShouldEqual, 60) + So(s.atime, ShouldEqual, 1) + So(s.mtime, ShouldEqual, 30) + }) + }) }