types.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Getpagesize returns the underlying system's memory page size.
  10. func Getpagesize() int { return syscall.Getpagesize() }
  11. // A FileInfo describes a file and is returned by Stat and Lstat.
  12. type FileInfo interface {
  13. Name() string // base name of the file
  14. Size() int64 // length in bytes for regular files; system-dependent for others
  15. Mode() FileMode // file mode bits
  16. ModTime() time.Time // modification time
  17. IsDir() bool // abbreviation for Mode().IsDir()
  18. Sys() interface{} // underlying data source (can return nil)
  19. }
  20. // A FileMode represents a file's mode and permission bits.
  21. // The bits have the same definition on all systems, so that
  22. // information about files can be moved from one system
  23. // to another portably. Not all bits apply to all systems.
  24. // The only required bit is ModeDir for directories.
  25. type FileMode uint32
  26. // The defined file mode bits are the most significant bits of the FileMode.
  27. // The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
  28. // The values of these bits should be considered part of the public API and
  29. // may be used in wire protocols or disk representations: they must not be
  30. // changed, although new bits might be added.
  31. const (
  32. // The single letters are the abbreviations
  33. // used by the String method's formatting.
  34. ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
  35. ModeAppend // a: append-only
  36. ModeExclusive // l: exclusive use
  37. ModeTemporary // T: temporary file (not backed up)
  38. ModeSymlink // L: symbolic link
  39. ModeDevice // D: device file
  40. ModeNamedPipe // p: named pipe (FIFO)
  41. ModeSocket // S: Unix domain socket
  42. ModeSetuid // u: setuid
  43. ModeSetgid // g: setgid
  44. ModeCharDevice // c: Unix character device, when ModeDevice is set
  45. ModeSticky // t: sticky
  46. // Mask for the type bits. For regular files, none will be set.
  47. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
  48. ModePerm FileMode = 0777 // permission bits
  49. )
  50. func (m FileMode) String() string {
  51. const str = "dalTLDpSugct"
  52. var buf [32]byte // Mode is uint32.
  53. w := 0
  54. for i, c := range str {
  55. if m&(1<<uint(32-1-i)) != 0 {
  56. buf[w] = byte(c)
  57. w++
  58. }
  59. }
  60. if w == 0 {
  61. buf[w] = '-'
  62. w++
  63. }
  64. const rwx = "rwxrwxrwx"
  65. for i, c := range rwx {
  66. if m&(1<<uint(9-1-i)) != 0 {
  67. buf[w] = byte(c)
  68. } else {
  69. buf[w] = '-'
  70. }
  71. w++
  72. }
  73. return string(buf[:w])
  74. }
  75. // IsDir reports whether m describes a directory.
  76. // That is, it tests for the ModeDir bit being set in m.
  77. func (m FileMode) IsDir() bool {
  78. return m&ModeDir != 0
  79. }
  80. // IsRegular reports whether m describes a regular file.
  81. // That is, it tests that no mode type bits are set.
  82. func (m FileMode) IsRegular() bool {
  83. return m&ModeType == 0
  84. }
  85. // Perm returns the Unix permission bits in m.
  86. func (m FileMode) Perm() FileMode {
  87. return m & ModePerm
  88. }
  89. func (fs *fileStat) Name() string { return fs.name }
  90. func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }
  91. // SameFile reports whether fi1 and fi2 describe the same file.
  92. // For example, on Unix this means that the device and inode fields
  93. // of the two underlying structures are identical; on other systems
  94. // the decision may be based on the path names.
  95. // SameFile only applies to results returned by this package's Stat.
  96. // It returns false in other cases.
  97. func SameFile(fi1, fi2 FileInfo) bool {
  98. fs1, ok1 := fi1.(*fileStat)
  99. fs2, ok2 := fi2.(*fileStat)
  100. if !ok1 || !ok2 {
  101. return false
  102. }
  103. return sameFile(fs1, fs2)
  104. }