mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-02 12:04:44 +00:00
* Fix #20628 for Windows * Move isRegular - !isSpecial and onlyRegular - skipSpecial * Forgot to change it in 1 more place
This commit is contained in:
@@ -86,16 +86,16 @@ type
|
||||
|
||||
when defined(posix) and not weirdTarget:
|
||||
proc getSymlinkFileKind*(path: string):
|
||||
tuple[pc: PathComponent, isRegular: bool] =
|
||||
tuple[pc: PathComponent, isSpecial: bool] =
|
||||
# Helper function.
|
||||
var s: Stat
|
||||
assert(path != "")
|
||||
result = (pcLinkToFile, true)
|
||||
result = (pcLinkToFile, false)
|
||||
if stat(path, s) == 0'i32:
|
||||
if S_ISDIR(s.st_mode):
|
||||
result = (pcLinkToDir, true)
|
||||
result = (pcLinkToDir, false)
|
||||
elif not S_ISREG(s.st_mode):
|
||||
result = (pcLinkToFile, false)
|
||||
result = (pcLinkToFile, true)
|
||||
|
||||
proc tryMoveFSObject*(source, dest: string, isDir: bool): bool {.noWeirdTarget.} =
|
||||
## Moves a file (or directory if `isDir` is true) from `source` to `dest`.
|
||||
|
||||
@@ -155,7 +155,7 @@ proc staticWalkDir(dir: string; relative: bool): seq[
|
||||
discard
|
||||
|
||||
iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
onlyRegular = false):
|
||||
skipSpecial = false):
|
||||
tuple[kind: PathComponent, path: string] {.tags: [ReadDirEffect].} =
|
||||
## Walks over the directory `dir` and yields for each directory or file in
|
||||
## `dir`. The component type and full path for each item are returned.
|
||||
@@ -166,7 +166,7 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
## otherwise the full path is returned.
|
||||
## * If `checkDir` is true, `OSError` is raised when `dir`
|
||||
## doesn't exist.
|
||||
## * If `onlyRegular` is true, then (besides all directories) only *regular*
|
||||
## * If `skipSpecial` is true, then (besides all directories) only *regular*
|
||||
## files (**without** special "file" objects like FIFOs, device files,
|
||||
## etc) will be yielded on Unix.
|
||||
##
|
||||
@@ -240,9 +240,9 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
var k = pcFile
|
||||
|
||||
template resolveSymlink() =
|
||||
var isRegular: bool
|
||||
(k, isRegular) = getSymlinkFileKind(path)
|
||||
if onlyRegular and not isRegular: continue
|
||||
var isSpecial: bool
|
||||
(k, isSpecial) = getSymlinkFileKind(path)
|
||||
if skipSpecial and isSpecial: continue
|
||||
|
||||
template kSetGeneric() = # pure Posix component `k` resolution
|
||||
if lstat(path.cstring, s) < 0'i32: continue # don't yield
|
||||
@@ -250,7 +250,7 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
k = pcDir
|
||||
elif S_ISLNK(s.st_mode):
|
||||
resolveSymlink()
|
||||
elif onlyRegular and not S_ISREG(s.st_mode): continue
|
||||
elif skipSpecial and not S_ISREG(s.st_mode): continue
|
||||
|
||||
when defined(linux) or defined(macosx) or
|
||||
defined(bsd) or defined(genode) or defined(nintendoswitch):
|
||||
@@ -261,7 +261,7 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
of DT_UNKNOWN:
|
||||
kSetGeneric()
|
||||
else: # DT_REG or special "files" like FIFOs
|
||||
if onlyRegular and x.d_type != DT_REG: continue
|
||||
if skipSpecial and x.d_type != DT_REG: continue
|
||||
else: discard # leave it as pcFile
|
||||
else: # assuming that field `d_type` is not present
|
||||
kSetGeneric()
|
||||
@@ -270,12 +270,12 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||
|
||||
iterator walkDirRec*(dir: string,
|
||||
yieldFilter = {pcFile}, followFilter = {pcDir},
|
||||
relative = false, checkDir = false, onlyRegular = false):
|
||||
relative = false, checkDir = false, skipSpecial = false):
|
||||
string {.tags: [ReadDirEffect].} =
|
||||
## Recursively walks over the directory `dir` and yields for each file
|
||||
## or directory in `dir`.
|
||||
##
|
||||
## Options `relative`, `checkdir`, `onlyRegular` are explained in
|
||||
## Options `relative`, `checkdir`, `skipSpecial` are explained in
|
||||
## [walkDir iterator] description.
|
||||
##
|
||||
## .. warning:: Modifying the directory structure while the iterator
|
||||
@@ -311,7 +311,7 @@ iterator walkDirRec*(dir: string,
|
||||
while stack.len > 0:
|
||||
let d = stack.pop()
|
||||
for k, p in walkDir(dir / d, relative = true, checkDir = checkDir,
|
||||
onlyRegular = onlyRegular):
|
||||
skipSpecial = skipSpecial):
|
||||
let rel = d / p
|
||||
if k in {pcDir, pcLinkToDir} and k in followFilter:
|
||||
stack.add rel
|
||||
|
||||
Reference in New Issue
Block a user