stat_darwinbsd.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //go:build darwin || freebsd || netbsd
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package fs
  15. import (
  16. "fmt"
  17. "io/fs"
  18. "syscall"
  19. "time"
  20. )
  21. func Atime(st fs.FileInfo) (time.Time, error) {
  22. stSys, ok := st.Sys().(*syscall.Stat_t)
  23. if !ok {
  24. return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys())
  25. }
  26. return time.Unix(stSys.Atimespec.Unix()), nil
  27. }
  28. func Ctime(st fs.FileInfo) (time.Time, error) {
  29. stSys, ok := st.Sys().(*syscall.Stat_t)
  30. if !ok {
  31. return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys())
  32. }
  33. return time.Unix(stSys.Ctimespec.Unix()), nil
  34. }
  35. func Mtime(st fs.FileInfo) (time.Time, error) {
  36. stSys, ok := st.Sys().(*syscall.Stat_t)
  37. if !ok {
  38. return time.Time{}, fmt.Errorf("expected st.Sys() to be *syscall.Stat_t, got %T", st.Sys())
  39. }
  40. return time.Unix(stSys.Mtimespec.Unix()), nil
  41. }
  42. // StatAtime returns the access time from a stat struct
  43. func StatAtime(st *syscall.Stat_t) syscall.Timespec {
  44. return st.Atimespec
  45. }
  46. // StatCtime returns the created time from a stat struct
  47. func StatCtime(st *syscall.Stat_t) syscall.Timespec {
  48. return st.Ctimespec
  49. }
  50. // StatMtime returns the modified time from a stat struct
  51. func StatMtime(st *syscall.Stat_t) syscall.Timespec {
  52. return st.Mtimespec
  53. }
  54. // StatATimeAsTime returns the access time as a time.Time
  55. func StatATimeAsTime(st *syscall.Stat_t) time.Time {
  56. return time.Unix(st.Atimespec.Unix())
  57. }