karrick / godirwalk

Fast directory traversal for Golang
BSD 2-Clause "Simplified" License
706 stars 70 forks source link

Scanner struct allows fast and lazy enumeration of directory contents #38

Closed karrick closed 5 years ago

karrick commented 5 years ago

Scanner is a new struct that allows opening a file system directory, and lazily enumerating its contents using a similar API to bufio.Scanner allows for enumeration of lines in a io.Reader.

Reader has been updated to use the new Scanner struct for reading entire directory contents. The API is not changed, but the provided scratch buffer to ReadDirents and ReadDirnames is ignored. All of the Windows vs non-Windows code has been moved to the Scanner struct. The ReadDirents and ReadDirnames code is now the same for all operating systems.

Walk has been updated to use the new Scanner struct for lazily reading directory contents. The API is not changed, other than the Config.ScratchBuffer field is ignored, and its documentation is also updated. Some details of Walk's use of Scanner is warranted. If Walk is told to enumerate the directory contents unsorted, then it will use the Scanner to lazily read and process each child entry in the directory. If Walk is not given the ability to enumerate unsorted, it will read the contents of the entire directory, building up a slice of Dirent, sorting them, then enumerating them. This is important because how Walk is required to stop processing if it receives a special filepath.SkipDir error return value from visiting a node. In order to ensure determinate results when that happens, and the same files are visited each time, the directory must be enumerated in sorted order. Unless of course the caller specifically requested unsorted enumeration.

Advantages

Disadvantages

Closes #34