stat_unix.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // +build !windows
  2. package system
  3. import (
  4. "syscall"
  5. )
  6. // StatT type contains status of a file. It contains metadata
  7. // like permission, owner, group, size, etc about a file.
  8. type StatT struct {
  9. mode uint32
  10. uid uint32
  11. gid uint32
  12. rdev uint64
  13. size int64
  14. mtim syscall.Timespec
  15. }
  16. // Mode returns file's permission mode.
  17. func (s StatT) Mode() uint32 {
  18. return s.mode
  19. }
  20. // UID returns file's user id of owner.
  21. func (s StatT) UID() uint32 {
  22. return s.uid
  23. }
  24. // GID returns file's group id of owner.
  25. func (s StatT) GID() uint32 {
  26. return s.gid
  27. }
  28. // Rdev returns file's device ID (if it's special file).
  29. func (s StatT) Rdev() uint64 {
  30. return s.rdev
  31. }
  32. // Size returns file's size.
  33. func (s StatT) Size() int64 {
  34. return s.size
  35. }
  36. // Mtim returns file's last modification time.
  37. func (s StatT) Mtim() syscall.Timespec {
  38. return s.mtim
  39. }
  40. // IsDir reports whether s describes a directory.
  41. func (s StatT) IsDir() bool {
  42. return s.mode&syscall.S_IFDIR != 0
  43. }
  44. // Stat takes a path to a file and returns
  45. // a system.StatT type pertaining to that file.
  46. //
  47. // Throws an error if the file does not exist
  48. func Stat(path string) (*StatT, error) {
  49. s := &syscall.Stat_t{}
  50. if err := syscall.Stat(path, s); err != nil {
  51. return nil, err
  52. }
  53. return fromStatT(s)
  54. }