stat_unix.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // Stat takes a path to a file and returns
  41. // a system.StatT type pertaining to that file.
  42. //
  43. // Throws an error if the file does not exist
  44. func Stat(path string) (*StatT, error) {
  45. s := &syscall.Stat_t{}
  46. if err := syscall.Stat(path, s); err != nil {
  47. return nil, err
  48. }
  49. return fromStatT(s)
  50. }