2015-10-01 13:45:32 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-11-03 17:49:50 -05:00
|
|
|
"syscall"
|
2015-10-01 13:45:32 -04:00
|
|
|
"time"
|
2015-11-03 17:49:50 -05:00
|
|
|
"unsafe"
|
2015-10-01 13:45:32 -04:00
|
|
|
)
|
|
|
|
|
2015-11-03 17:49:50 -05:00
|
|
|
var (
|
|
|
|
maxTime time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if unsafe.Sizeof(syscall.Timespec{}.Nsec) == 8 {
|
|
|
|
// This is a 64 bit timespec
|
|
|
|
// os.Chtimes limits time to the following
|
|
|
|
maxTime = time.Unix(0, 1<<63-1)
|
|
|
|
} else {
|
|
|
|
// This is a 32 bit timespec
|
|
|
|
maxTime = time.Unix(1<<31-1, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:45:32 -04:00
|
|
|
// Chtimes changes the access time and modified time of a file at the given path
|
|
|
|
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
|
|
unixMinTime := time.Unix(0, 0)
|
2015-11-03 17:49:50 -05:00
|
|
|
unixMaxTime := maxTime
|
2015-10-01 13:45:32 -04:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
mtime = unixMinTime
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chtimes(name, atime, mtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|