stat_unix.go 1.3 KB

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