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 <github@gone.nl>
This commit is contained in:
parent
2c9684e35c
commit
0b8444aa0c
|
@ -8,9 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Used by Chtimes
|
// Used by Chtimes
|
||||||
var maxTime time.Time
|
var unixEpochTime, unixMaxTime time.Time
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
unixEpochTime = time.Unix(0, 0)
|
||||||
if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 {
|
if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 {
|
||||||
// This is a 64 bit timespec
|
// This is a 64 bit timespec
|
||||||
// os.Chtimes limits time to the following
|
// 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
|
// Note that this intentionally sets nsec (not sec), which sets both sec
|
||||||
// and nsec internally in time.Unix();
|
// and nsec internally in time.Unix();
|
||||||
// https://github.com/golang/go/blob/go1.19.2/src/time/time.go#L1364-L1380
|
// 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 {
|
} else {
|
||||||
// This is a 32 bit timespec
|
// 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 {
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
||||||
unixMinTime := time.Unix(0, 0)
|
if atime.Before(unixEpochTime) || atime.After(unixMaxTime) {
|
||||||
unixMaxTime := maxTime
|
atime = unixEpochTime
|
||||||
|
|
||||||
// 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 mtime.Before(unixMinTime) || mtime.After(unixMaxTime) {
|
if mtime.Before(unixEpochTime) || mtime.After(unixMaxTime) {
|
||||||
mtime = unixMinTime
|
mtime = unixEpochTime
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Chtimes(name, atime, mtime); err != nil {
|
if err := os.Chtimes(name, atime, mtime); err != nil {
|
||||||
|
|
|
@ -12,10 +12,8 @@ func TestChtimesLinux(t *testing.T) {
|
||||||
file, dir := prepareTempFile(t)
|
file, dir := prepareTempFile(t)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
|
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||||
unixEpochTime := time.Unix(0, 0)
|
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||||
afterUnixEpochTime := time.Unix(100, 0)
|
|
||||||
unixMaxTime := maxTime
|
|
||||||
|
|
||||||
// Test both aTime and mTime set to Unix Epoch
|
// Test both aTime and mTime set to Unix Epoch
|
||||||
Chtimes(file, unixEpochTime, unixEpochTime)
|
Chtimes(file, unixEpochTime, unixEpochTime)
|
||||||
|
|
|
@ -26,10 +26,8 @@ func TestChtimes(t *testing.T) {
|
||||||
file, dir := prepareTempFile(t)
|
file, dir := prepareTempFile(t)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
|
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||||
unixEpochTime := time.Unix(0, 0)
|
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||||
afterUnixEpochTime := time.Unix(100, 0)
|
|
||||||
unixMaxTime := maxTime
|
|
||||||
|
|
||||||
// Test both aTime and mTime set to Unix Epoch
|
// Test both aTime and mTime set to Unix Epoch
|
||||||
Chtimes(file, unixEpochTime, unixEpochTime)
|
Chtimes(file, unixEpochTime, unixEpochTime)
|
||||||
|
|
|
@ -15,10 +15,8 @@ func TestChtimesWindows(t *testing.T) {
|
||||||
file, dir := prepareTempFile(t)
|
file, dir := prepareTempFile(t)
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
beforeUnixEpochTime := time.Unix(0, 0).Add(-100 * time.Second)
|
beforeUnixEpochTime := unixEpochTime.Add(-100 * time.Second)
|
||||||
unixEpochTime := time.Unix(0, 0)
|
afterUnixEpochTime := unixEpochTime.Add(100 * time.Second)
|
||||||
afterUnixEpochTime := time.Unix(100, 0)
|
|
||||||
unixMaxTime := maxTime
|
|
||||||
|
|
||||||
// Test both aTime and mTime set to Unix Epoch
|
// Test both aTime and mTime set to Unix Epoch
|
||||||
Chtimes(file, unixEpochTime, unixEpochTime)
|
Chtimes(file, unixEpochTime, unixEpochTime)
|
||||||
|
|
Loading…
Reference in New Issue