stat_nacl.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.Mtime, st.MtimeNsec),
  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(sec, nsec int64) time.Time {
  50. return time.Unix(sec, nsec)
  51. }
  52. // For testing.
  53. func atime(fi FileInfo) time.Time {
  54. st := fi.Sys().(*syscall.Stat_t)
  55. return timespecToTime(st.Atime, st.AtimeNsec)
  56. }