dirent.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package godirwalk
  2. import (
  3. "os"
  4. "path/filepath"
  5. )
  6. // Dirent stores the name and file system mode type of discovered file system
  7. // entries.
  8. type Dirent struct {
  9. name string // base name of the file system entry.
  10. path string // path name of the file system entry.
  11. modeType os.FileMode // modeType is the type of file system entry.
  12. }
  13. // NewDirent returns a newly initialized Dirent structure, or an error. This
  14. // function does not follow symbolic links.
  15. //
  16. // This function is rarely used, as Dirent structures are provided by other
  17. // functions in this library that read and walk directories, but is provided,
  18. // however, for the occasion when a program needs to create a Dirent.
  19. func NewDirent(osPathname string) (*Dirent, error) {
  20. modeType, err := modeType(osPathname)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &Dirent{
  25. name: filepath.Base(osPathname),
  26. path: filepath.Dir(osPathname),
  27. modeType: modeType,
  28. }, nil
  29. }
  30. // IsDir returns true if and only if the Dirent represents a file system
  31. // directory. Note that on some operating systems, more than one file mode bit
  32. // may be set for a node. For instance, on Windows, a symbolic link that points
  33. // to a directory will have both the directory and the symbolic link bits set.
  34. func (de Dirent) IsDir() bool { return de.modeType&os.ModeDir != 0 }
  35. // IsDirOrSymlinkToDir returns true if and only if the Dirent represents a file
  36. // system directory, or a symbolic link to a directory. Note that if the Dirent
  37. // is not a directory but is a symbolic link, this method will resolve by
  38. // sending a request to the operating system to follow the symbolic link.
  39. func (de Dirent) IsDirOrSymlinkToDir() (bool, error) {
  40. if de.IsDir() {
  41. return true, nil
  42. }
  43. if !de.IsSymlink() {
  44. return false, nil
  45. }
  46. // Does this symlink point to a directory?
  47. info, err := os.Stat(filepath.Join(de.path, de.name))
  48. if err != nil {
  49. return false, err
  50. }
  51. return info.IsDir(), nil
  52. }
  53. // IsRegular returns true if and only if the Dirent represents a regular file.
  54. // That is, it ensures that no mode type bits are set.
  55. func (de Dirent) IsRegular() bool { return de.modeType&os.ModeType == 0 }
  56. // IsSymlink returns true if and only if the Dirent represents a file system
  57. // symbolic link. Note that on some operating systems, more than one file mode
  58. // bit may be set for a node. For instance, on Windows, a symbolic link that
  59. // points to a directory will have both the directory and the symbolic link bits
  60. // set.
  61. func (de Dirent) IsSymlink() bool { return de.modeType&os.ModeSymlink != 0 }
  62. // IsDevice returns true if and only if the Dirent represents a device file.
  63. func (de Dirent) IsDevice() bool { return de.modeType&os.ModeDevice != 0 }
  64. // ModeType returns the mode bits that specify the file system node type. We
  65. // could make our own enum-like data type for encoding the file type, but Go's
  66. // runtime already gives us architecture independent file modes, as discussed in
  67. // `os/types.go`:
  68. //
  69. // Go's runtime FileMode type has same definition on all systems, so that
  70. // information about files can be moved from one system to another portably.
  71. func (de Dirent) ModeType() os.FileMode { return de.modeType }
  72. // Name returns the base name of the file system entry.
  73. func (de Dirent) Name() string { return de.name }
  74. // reset releases memory held by entry err and name, and resets mode type to 0.
  75. func (de *Dirent) reset() {
  76. de.name = ""
  77. de.path = ""
  78. de.modeType = 0
  79. }
  80. // Dirents represents a slice of Dirent pointers, which are sortable by base
  81. // name. This type satisfies the `sort.Interface` interface.
  82. type Dirents []*Dirent
  83. // Len returns the count of Dirent structures in the slice.
  84. func (l Dirents) Len() int { return len(l) }
  85. // Less returns true if and only if the base name of the element specified by
  86. // the first index is lexicographically less than that of the second index.
  87. func (l Dirents) Less(i, j int) bool { return l[i].name < l[j].name }
  88. // Swap exchanges the two Dirent entries specified by the two provided indexes.
  89. func (l Dirents) Swap(i, j int) { l[i], l[j] = l[j], l[i] }