From dbe44ebd46e9e2497b4b37e0c387f03f7e048f6b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 30 Oct 2024 01:02:55 +0100 Subject: [PATCH] fs: implement Atime, Ctime, Mtime for bsd and darwin Signed-off-by: Sebastiaan van Stijn --- fs/stat_darwinbsd.go | 26 ++++++++++++++++++++++++++ fs/stat_unix.go | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/fs/stat_darwinbsd.go b/fs/stat_darwinbsd.go index 7a34eda..8192465 100644 --- a/fs/stat_darwinbsd.go +++ b/fs/stat_darwinbsd.go @@ -19,10 +19,36 @@ package fs import ( + "fmt" + "io/fs" "syscall" "time" ) +func Atime(st fs.FileInfo) (time.Time, error) { + stSys, ok := st.Sys().(*syscall.Stat_t) + if !ok { + return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys()) + } + return time.Unix(stSys.Atimespec.Unix()), nil +} + +func Ctime(st fs.FileInfo) (time.Time, error) { + stSys, ok := st.Sys().(*syscall.Stat_t) + if !ok { + return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys()) + } + return time.Unix(stSys.Ctimespec.Unix()), nil +} + +func Mtime(st fs.FileInfo) (time.Time, error) { + stSys, ok := st.Sys().(*syscall.Stat_t) + if !ok { + return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys()) + } + return time.Unix(stSys.Mtimespec.Unix()), nil +} + // StatAtime returns the access time from a stat struct func StatAtime(st *syscall.Stat_t) syscall.Timespec { return st.Atimespec diff --git a/fs/stat_unix.go b/fs/stat_unix.go index 0edebdf..4e04ed8 100644 --- a/fs/stat_unix.go +++ b/fs/stat_unix.go @@ -30,7 +30,7 @@ func Atime(st fs.FileInfo) (time.Time, error) { if !ok { return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys()) } - return StatATimeAsTime(stSys), nil + return time.Unix(stSys.Atim.Unix()), nil } func Ctime(st fs.FileInfo) (time.Time, error) {