From 0b8444aa0c3398cccab7bbc706205cd5effba6f5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 7 Oct 2022 14:06:47 +0200 Subject: [PATCH] pkg/system: rename maxTime and re-use, define unixEpochTime, update GoDoc This more closely matches to how it's used everywhere. Also move the comment describing "what" ChTimes() does inside its GoDoc. Signed-off-by: Sebastiaan van Stijn --- pkg/system/chtimes.go | 27 ++++++++++++--------------- pkg/system/chtimes_linux_test.go | 6 ++---- pkg/system/chtimes_test.go | 6 ++---- pkg/system/chtimes_windows_test.go | 6 ++---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/pkg/system/chtimes.go b/pkg/system/chtimes.go index 537ddadc63..6a6bca43ed 100644 --- a/pkg/system/chtimes.go +++ b/pkg/system/chtimes.go @@ -8,9 +8,10 @@ import ( ) // Used by Chtimes -var maxTime time.Time +var unixEpochTime, unixMaxTime time.Time func init() { + unixEpochTime = time.Unix(0, 0) if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 { // This is a 64 bit timespec // os.Chtimes limits time to the following @@ -18,28 +19,24 @@ func init() { // Note that this intentionally sets nsec (not sec), which sets both sec // and nsec internally in time.Unix(); // https://github.com/golang/go/blob/go1.19.2/src/time/time.go#L1364-L1380 - maxTime = time.Unix(0, 1<<63-1) + unixMaxTime = time.Unix(0, 1<<63-1) } else { // This is a 32 bit timespec - maxTime = time.Unix(1<<31-1, 0) + unixMaxTime = time.Unix(1<<31-1, 0) } } -// Chtimes changes the access time and modified time of a file at the given path +// Chtimes changes the access time and modified time of a file at the given path. +// If the modified time is prior to the Unix Epoch (unixMinTime), or after the +// end of Unix Time (unixEpochTime), os.Chtimes has undefined behavior. In this +// case, Chtimes defaults to Unix Epoch, just in case. func Chtimes(name string, atime time.Time, mtime time.Time) error { - unixMinTime := time.Unix(0, 0) - unixMaxTime := maxTime - - // If the modified time is prior to the Unix Epoch, or after the - // end of Unix Time, os.Chtimes has undefined behavior - // default to Unix Epoch in this case, just in case - - if atime.Before(unixMinTime) || atime.After(unixMaxTime) { - atime = unixMinTime + if atime.Before(unixEpochTime) || atime.After(unixMaxTime) { + atime = unixEpochTime } - if mtime.Before(unixMinTime) || mtime.After(unixMaxTime) { - mtime = unixMinTime + if mtime.Before(unixEpochTime) || mtime.After(unixMaxTime) { + mtime = unixEpochTime } if err := os.Chtimes(name, atime, mtime); err != nil { diff --git a/pkg/system/chtimes_linux_test.go b/pkg/system/chtimes_linux_test.go index 97f860443c..5235fb7895 100644 --- a/pkg/system/chtimes_linux_test.go +++ b/pkg/system/chtimes_linux_test.go @@ -12,10 +12,8 @@ func TestChtimesLinux(t *testing.T) { file, dir := prepareTempFile(t) defer os.RemoveAll(dir) - beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) - unixEpochTime := time.Unix(0, 0) - afterUnixEpochTime := time.Unix(100, 0) - unixMaxTime := maxTime + beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second) + afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch Chtimes(file, unixEpochTime, unixEpochTime) diff --git a/pkg/system/chtimes_test.go b/pkg/system/chtimes_test.go index 3bb1fb2a60..5b634cccff 100644 --- a/pkg/system/chtimes_test.go +++ b/pkg/system/chtimes_test.go @@ -26,10 +26,8 @@ func TestChtimes(t *testing.T) { file, dir := prepareTempFile(t) defer os.RemoveAll(dir) - beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) - unixEpochTime := time.Unix(0, 0) - afterUnixEpochTime := time.Unix(100, 0) - unixMaxTime := maxTime + beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second) + afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch Chtimes(file, unixEpochTime, unixEpochTime) diff --git a/pkg/system/chtimes_windows_test.go b/pkg/system/chtimes_windows_test.go index 060c515003..2461414079 100644 --- a/pkg/system/chtimes_windows_test.go +++ b/pkg/system/chtimes_windows_test.go @@ -15,10 +15,8 @@ func TestChtimesWindows(t *testing.T) { file, dir := prepareTempFile(t) defer os.RemoveAll(dir) - beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second) - unixEpochTime := time.Unix(0, 0) - afterUnixEpochTime := time.Unix(100, 0) - unixMaxTime := maxTime + beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second) + afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch Chtimes(file, unixEpochTime, unixEpochTime)