readdir.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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
  9. // system. If you plan on calling this function in a loop, you will have
  10. // significantly better performance if you allocate a scratch buffer and use it
  11. // each time you call this function.
  12. //
  13. // children, err := godirwalk.ReadDirents(osDirname, nil)
  14. // if err != nil {
  15. // return nil, errors.Wrap(err, "cannot get list of directory children")
  16. // }
  17. // sort.Sort(children)
  18. // for _, child := range children {
  19. // fmt.Printf("%s %s\n", child.ModeType, child.Name)
  20. // }
  21. func ReadDirents(osDirname string, scratchBuffer []byte) (Dirents, error) {
  22. return readDirents(osDirname, scratchBuffer)
  23. }
  24. // ReadDirnames returns a slice of strings, representing the immediate
  25. // descendants of the specified directory. If the specified directory is a
  26. // symbolic link, it will be resolved.
  27. //
  28. // If an optional scratch buffer is provided that is at least one page of
  29. // memory, it will be used when reading directory entries from the file
  30. // system. If you plan on calling this function in a loop, you will have
  31. // significantly better performance if you allocate a scratch buffer and use it
  32. // each time you call this function.
  33. //
  34. // Note that this function, depending on operating system, may or may not invoke
  35. // the ReadDirents function, in order to prepare the list of immediate
  36. // descendants. Therefore, if your program needs both the names and the file
  37. // system mode types of descendants, it will always be faster to invoke
  38. // ReadDirents directly, rather than calling this function, then looping over
  39. // the results and calling os.Stat or os.LStat for each entry.
  40. //
  41. // children, err := godirwalk.ReadDirnames(osDirname, nil)
  42. // if err != nil {
  43. // return nil, errors.Wrap(err, "cannot get list of directory children")
  44. // }
  45. // sort.Strings(children)
  46. // for _, child := range children {
  47. // fmt.Printf("%s\n", child)
  48. // }
  49. func ReadDirnames(osDirname string, scratchBuffer []byte) ([]string, error) {
  50. return readDirnames(osDirname, scratchBuffer)
  51. }