From fe557e220285e5874742d83424ad6c5a02b3ea0d Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 24 Jan 2016 06:49:53 -0800 Subject: [PATCH] Make FileTime work portably. --- lib/ftime_bsd.go | 37 +++++++++++++++++++++++++++++++++++++ lib/ftime_unix.go | 37 +++++++++++++++++++++++++++++++++++++ lib/lib.go | 30 ------------------------------ 3 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 lib/ftime_bsd.go create mode 100644 lib/ftime_unix.go diff --git a/lib/ftime_bsd.go b/lib/ftime_bsd.go new file mode 100644 index 0000000..22e629d --- /dev/null +++ b/lib/ftime_bsd.go @@ -0,0 +1,37 @@ +// +build freebsd darwin netbsd + +package lib + +import ( + "time" + + "golang.org/x/sys/unix" +) + +// FileTime contains the changed, modified, and accessed timestamps +// for a file. +type FileTime struct { + Changed time.Time + Modified time.Time + Accessed time.Time +} + +func timeSpecToTime(ts unix.Timespec) time.Time { + return time.Unix(ts.Sec, ts.Nsec) +} + +// LoadFileTime returns a FileTime associated with the file. +func LoadFileTime(path string) (FileTime, error) { + var ft = FileTime{} + var st = unix.Stat_t{} + + err := unix.Stat(path, &st) + if err != nil { + return ft, err + } + + ft.Changed = timeSpecToTime(st.Ctimespec) + ft.Modified = timeSpecToTime(st.Mtimespec) + ft.Accessed = timeSpecToTime(st.Atimespec) + return ft, nil +} diff --git a/lib/ftime_unix.go b/lib/ftime_unix.go new file mode 100644 index 0000000..3dfacc2 --- /dev/null +++ b/lib/ftime_unix.go @@ -0,0 +1,37 @@ +// +build unix linux openbsd + +package lib + +import ( + "time" + + "golang.org/x/sys/unix" +) + +// FileTime contains the changed, modified, and accessed timestamps +// for a file. +type FileTime struct { + Changed time.Time + Modified time.Time + Accessed time.Time +} + +func timeSpecToTime(ts unix.Timespec) time.Time { + return time.Unix(ts.Sec, ts.Nsec) +} + +// LoadFileTime returns a FileTime associated with the file. +func LoadFileTime(path string) (FileTime, error) { + var ft = FileTime{} + var st = unix.Stat_t{} + + err := unix.Stat(path, &st) + if err != nil { + return ft, err + } + + ft.Changed = timeSpecToTime(st.Ctim) + ft.Modified = timeSpecToTime(st.Mtim) + ft.Accessed = timeSpecToTime(st.Atim) + return ft, nil +} diff --git a/lib/lib.go b/lib/lib.go index f31728b..1096e0b 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -6,8 +6,6 @@ import ( "os" "path/filepath" "time" - - "golang.org/x/sys/unix" ) var progname = filepath.Base(os.Args[0]) @@ -113,31 +111,3 @@ func Duration(d time.Duration) string { s += fmt.Sprintf("%dh%s", hours, d) return s } - -// FileTime contains the changed, modified, and accessed timestamps -// for a file. -type FileTime struct { - Changed time.Time - Modified time.Time - Accessed time.Time -} - -func timeSpecToTime(ts unix.Timespec) time.Time { - return time.Unix(ts.Sec, ts.Nsec) -} - -// LoadFileTime returns a FileTime associated with the file. -func LoadFileTime(path string) (FileTime, error) { - var ft = FileTime{} - var st = unix.Stat_t{} - - err := unix.Stat(path, &st) - if err != nil { - return ft, err - } - - ft.Changed = timeSpecToTime(st.Ctim) - ft.Modified = timeSpecToTime(st.Mtim) - ft.Accessed = timeSpecToTime(st.Atim) - return ft, nil -}