| 1 |
// +build unix linux openbsd |
| 2 |
|
| 3 |
package lib |
| 4 |
|
| 5 |
import ( |
| 6 |
"time" |
| 7 |
|
| 8 |
"golang.org/x/sys/unix" |
| 9 |
) |
| 10 |
|
| 11 |
// FileTime contains the changed, modified, and accessed timestamps |
| 12 |
// for a file. |
| 13 |
type FileTime struct { |
| 14 |
Changed time.Time |
| 15 |
Modified time.Time |
| 16 |
Accessed time.Time |
| 17 |
} |
| 18 |
|
| 19 |
func timeSpecToTime(ts unix.Timespec) time.Time { |
| 20 |
// The casts to int64 are needed because on 386, these are int32s. |
| 21 |
return time.Unix(int64(ts.Sec), int64(ts.Nsec)) |
| 22 |
} |
| 23 |
|
| 24 |
// LoadFileTime returns a FileTime associated with the file. |
| 25 |
func LoadFileTime(path string) (FileTime, error) { |
| 26 |
var ft = FileTime{} |
| 27 |
var st = unix.Stat_t{} |
| 28 |
|
| 29 |
err := unix.Stat(path, &st) |
| 30 |
if err != nil { |
| 31 |
return ft, err |
| 32 |
} |
| 33 |
|
| 34 |
ft.Changed = timeSpecToTime(st.Ctim) |
| 35 |
ft.Modified = timeSpecToTime(st.Mtim) |
| 36 |
ft.Accessed = timeSpecToTime(st.Atim) |
| 37 |
return ft, nil |
| 38 |
} |