Modified behavior of walkDirRec (#6952)

This commit is contained in:
Dmitry Atamanov 2017-12-21 13:15:19 +03:00 • committed by Andreas Rumpf
commit 63403b31ad

View file

@ -816,32 +816,40 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
k = getSymlinkFileKind(y) k = getSymlinkFileKind(y)
yield (k, y) yield (k, y)
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string {. iterator walkDirRec*(dir: string, yieldFilter = {pcFile},
tags: [ReadDirEffect].} = followFilter = {pcDir}): string {.tags: [ReadDirEffect].} =
## Recursively walks over the directory `dir` and yields for each file in `dir`. ## Recursively walks over the directory `dir` and yields for each file
## The full path for each file is returned. Directories are not returned. ## or directory in `dir`.
## The full path for each file or directory is returned.
## **Warning**: ## **Warning**:
## Modifying the directory structure while the iterator ## Modifying the directory structure while the iterator
## is traversing may result in undefined behavior! ## is traversing may result in undefined behavior!
## ##
## Walking is recursive. `filter` controls the behaviour of the iterator: ## Walking is recursive. `filters` controls the behaviour of the iterator:
## ##
## --------------------- --------------------------------------------- ## --------------------- ---------------------------------------------
## filter meaning ## yieldFilter meaning
## --------------------- --------------------------------------------- ## --------------------- ---------------------------------------------
## ``pcFile`` yield real files ## ``pcFile`` yield real files
## ``pcLinkToFile`` yield symbolic links to files ## ``pcLinkToFile`` yield symbolic links to files
## ``pcDir`` yield real directories
## ``pcLinkToDir`` yield symbolic links to directories
## --------------------- ---------------------------------------------
##
## --------------------- ---------------------------------------------
## followFilter meaning
## --------------------- ---------------------------------------------
## ``pcDir`` follow real directories ## ``pcDir`` follow real directories
## ``pcLinkToDir`` follow symbolic links to directories ## ``pcLinkToDir`` follow symbolic links to directories
## --------------------- --------------------------------------------- ## --------------------- ---------------------------------------------
## ##
var stack = @[dir] var stack = @[dir]
while stack.len > 0: while stack.len > 0:
for k,p in walkDir(stack.pop()): for k, p in walkDir(stack.pop()):
if k in filter: if k in {pcDir, pcLinkToDir} and k in followFilter:
case k stack.add(p)
of pcFile, pcLinkToFile: yield p if k in yieldFilter:
of pcDir, pcLinkToDir: stack.add(p) yield p
proc rawRemoveDir(dir: string) = proc rawRemoveDir(dir: string) =
when defined(windows): when defined(windows):