stat.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.Mtim),
  19. sys: st,
  20. }
  21. fs.mode = FileMode(st.Mode & 0777)
  22. switch st.Mode & syscall.S_IFMT {
  23. case syscall.S_IFBLK, syscall.S_IFCHR:
  24. fs.mode |= ModeDevice
  25. case syscall.S_IFDIR:
  26. fs.mode |= ModeDir
  27. case syscall.S_IFIFO:
  28. fs.mode |= ModeNamedPipe
  29. case syscall.S_IFLNK:
  30. fs.mode |= ModeSymlink
  31. case syscall.S_IFREG:
  32. // nothing to do
  33. case syscall.S_IFSOCK:
  34. fs.mode |= ModeSocket
  35. }
  36. if st.Mode&syscall.S_ISGID != 0 {
  37. fs.mode |= ModeSetgid
  38. }
  39. if st.Mode&syscall.S_ISUID != 0 {
  40. fs.mode |= ModeSetuid
  41. }
  42. return fs
  43. }
  44. func timespecToTime(ts syscall.Timespec) time.Time {
  45. return time.Unix(int64(ts.Sec), int64(ts.Nsec))
  46. }
  47. // For testing.
  48. func atime(fi FileInfo) time.Time {
  49. return timespecToTime(fi.Sys().(*syscall.Stat_t).Atim)
  50. }