stat_atimespec.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package os
  5. import (
  6. "syscall"
  7. "time"
  8. )
  9. func sameFile(fs1, fs2 *fileStat) bool {
  10. stat1 := fs1.sys.(*syscall.Stat_t)
  11. stat2 := fs2.sys.(*syscall.Stat_t)
  12. return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino
  13. }
  14. func fileInfoFromStat(st *syscall.Stat_t, name string) FileInfo {
  15. fs := &fileStat{
  16. name: basename(name),
  17. size: int64(st.Size),
  18. modTime: timespecToTime(st.Mtimespec),
  19. sys: st,
  20. }
  21. fs.mode = FileMode(st.Mode & 0777)
  22. switch st.Mode & syscall.S_IFMT {
  23. case syscall.S_IFBLK:
  24. fs.mode |= ModeDevice
  25. case syscall.S_IFCHR:
  26. fs.mode |= ModeDevice | ModeCharDevice
  27. case syscall.S_IFDIR:
  28. fs.mode |= ModeDir
  29. case syscall.S_IFIFO:
  30. fs.mode |= ModeNamedPipe
  31. case syscall.S_IFLNK:
  32. fs.mode |= ModeSymlink
  33. case syscall.S_IFREG:
  34. // nothing to do
  35. case syscall.S_IFSOCK:
  36. fs.mode |= ModeSocket
  37. }
  38. if st.Mode&syscall.S_ISGID != 0 {
  39. fs.mode |= ModeSetgid
  40. }
  41. if st.Mode&syscall.S_ISUID != 0 {
  42. fs.mode |= ModeSetuid
  43. }
  44. if st.Mode&syscall.S_ISVTX != 0 {
  45. fs.mode |= ModeSticky
  46. }
  47. return fs
  48. }
  49. func timespecToTime(ts syscall.Timespec) time.Time {
  50. return time.Unix(int64(ts.Sec), int64(ts.Nsec))
  51. }
  52. // For testing.
  53. func atime(fi FileInfo) time.Time {
  54. return timespecToTime(fi.Sys().(*syscall.Stat_t).Atimespec)
  55. }