readdir.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package godirwalk
  2. // ReadDirents returns a sortable slice of pointers to Dirent structures, each
  3. // representing the file system name and mode type for one of the immediate
  4. // descendant of the specified directory. If the specified directory is a
  5. // symbolic link, it will be resolved.
  6. //
  7. // If an optional scratch buffer is provided that is at least one page of
  8. // memory, it will be used when reading directory entries from the file system.
  9. //
  10. // children, err := godirwalk.ReadDirents(osDirname, nil)
  11. // if err != nil {
  12. // return nil, errors.Wrap(err, "cannot get list of directory children")
  13. // }
  14. // sort.Sort(children)
  15. // for _, child := range children {
  16. // fmt.Printf("%s %s\n", child.ModeType, child.Name)
  17. // }
  18. func ReadDirents(osDirname string, scratchBuffer []byte) (Dirents, error) {
  19. return readdirents(osDirname, scratchBuffer)
  20. }
  21. // ReadDirnames returns a slice of strings, representing the immediate
  22. // descendants of the specified directory. If the specified directory is a
  23. // symbolic link, it will be resolved.
  24. //
  25. // If an optional scratch buffer is provided that is at least one page of
  26. // memory, it will be used when reading directory entries from the file system.
  27. //
  28. // Note that this function, depending on operating system, may or may not invoke
  29. // the ReadDirents function, in order to prepare the list of immediate
  30. // descendants. Therefore, if your program needs both the names and the file
  31. // system mode types of descendants, it will always be faster to invoke
  32. // ReadDirents directly, rather than calling this function, then looping over
  33. // the results and calling os.Stat for each child.
  34. //
  35. // children, err := godirwalk.ReadDirnames(osDirname, nil)
  36. // if err != nil {
  37. // return nil, errors.Wrap(err, "cannot get list of directory children")
  38. // }
  39. // sort.Strings(children)
  40. // for _, child := range children {
  41. // fmt.Printf("%s\n", child)
  42. // }
  43. func ReadDirnames(osDirname string, scratchBuffer []byte) ([]string, error) {
  44. return readdirnames(osDirname, scratchBuffer)
  45. }