* Fix #20628 for Windows * Move isRegular - !isSpecial and onlyRegular - skipSpecial * Forgot to change it in 1 more place
This commit is contained in:
parent
e68a6ea759
commit
779b1cc5be
7 changed files with 41 additions and 38 deletions
|
|
@ -1030,8 +1030,11 @@ type
|
||||||
creationTime*: times.Time ## Time file was created. Not supported on all systems!
|
creationTime*: times.Time ## Time file was created. Not supported on all systems!
|
||||||
blockSize*: int ## Preferred I/O block size for this object.
|
blockSize*: int ## Preferred I/O block size for this object.
|
||||||
## In some filesystems, this may vary from file to file.
|
## In some filesystems, this may vary from file to file.
|
||||||
isRegular*: bool ## Is file regular? (on Unix some "files"
|
isSpecial*: bool ## Is file special? (on Unix some "files"
|
||||||
## can be non-regular like FIFOs, devices)
|
## can be special=non-regular like FIFOs,
|
||||||
|
## devices); for directories `isSpecial`
|
||||||
|
## is always `false`, for symlinks it is
|
||||||
|
## the same as for the link's target.
|
||||||
|
|
||||||
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||||
## Transforms the native file info structure into the one nim uses.
|
## Transforms the native file info structure into the one nim uses.
|
||||||
|
|
@ -1092,14 +1095,14 @@ template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||||
checkAndIncludeMode(S_IWOTH, fpOthersWrite)
|
checkAndIncludeMode(S_IWOTH, fpOthersWrite)
|
||||||
checkAndIncludeMode(S_IXOTH, fpOthersExec)
|
checkAndIncludeMode(S_IXOTH, fpOthersExec)
|
||||||
|
|
||||||
(formalInfo.kind, formalInfo.isRegular) =
|
(formalInfo.kind, formalInfo.isSpecial) =
|
||||||
if S_ISDIR(rawInfo.st_mode):
|
if S_ISDIR(rawInfo.st_mode):
|
||||||
(pcDir, true)
|
(pcDir, false)
|
||||||
elif S_ISLNK(rawInfo.st_mode):
|
elif S_ISLNK(rawInfo.st_mode):
|
||||||
assert(path != "") # symlinks can't occur for file handles
|
assert(path != "") # symlinks can't occur for file handles
|
||||||
getSymlinkFileKind(path)
|
getSymlinkFileKind(path)
|
||||||
else:
|
else:
|
||||||
(pcFile, S_ISREG(rawInfo.st_mode))
|
(pcFile, not S_ISREG(rawInfo.st_mode))
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
when not declared(FileHandle):
|
when not declared(FileHandle):
|
||||||
|
|
@ -1153,7 +1156,7 @@ proc getFileInfo*(path: string, followSymlink = true): FileInfo {.noWeirdTarget.
|
||||||
## When `followSymlink` is true (default), symlinks are followed and the
|
## When `followSymlink` is true (default), symlinks are followed and the
|
||||||
## information retrieved is information related to the symlink's target.
|
## information retrieved is information related to the symlink's target.
|
||||||
## Otherwise, information on the symlink itself is retrieved (however,
|
## Otherwise, information on the symlink itself is retrieved (however,
|
||||||
## field `isRegular` is still determined from the target on Unix).
|
## field `isSpecial` is still determined from the target on Unix).
|
||||||
##
|
##
|
||||||
## If the information cannot be retrieved, such as when the path doesn't
|
## If the information cannot be retrieved, such as when the path doesn't
|
||||||
## exist, or when permission restrictions prevent the program from retrieving
|
## exist, or when permission restrictions prevent the program from retrieving
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ proc moveDir*(source, dest: Path) {.inline, tags: [ReadIOEffect, WriteIOEffect].
|
||||||
moveDir(source.string, dest.string)
|
moveDir(source.string, dest.string)
|
||||||
|
|
||||||
iterator walkDir*(dir: Path; relative = false, checkDir = false,
|
iterator walkDir*(dir: Path; relative = false, checkDir = false,
|
||||||
onlyRegular = false):
|
skipSpecial = false):
|
||||||
tuple[kind: PathComponent, path: Path] {.tags: [ReadDirEffect].} =
|
tuple[kind: PathComponent, path: Path] {.tags: [ReadDirEffect].} =
|
||||||
## Walks over the directory `dir` and yields for each directory or file in
|
## 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.
|
## `dir`. The component type and full path for each item are returned.
|
||||||
|
|
@ -80,20 +80,20 @@ iterator walkDir*(dir: Path; relative = false, checkDir = false,
|
||||||
## otherwise the full path is returned.
|
## otherwise the full path is returned.
|
||||||
## * If `checkDir` is true, `OSError` is raised when `dir`
|
## * If `checkDir` is true, `OSError` is raised when `dir`
|
||||||
## doesn't exist.
|
## 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,
|
## files (**without** special "file" objects like FIFOs, device files,
|
||||||
## etc) will be yielded on Unix.
|
## etc) will be yielded on Unix.
|
||||||
for (k, p) in walkDir(dir.string, relative, checkDir, onlyRegular):
|
for (k, p) in walkDir(dir.string, relative, checkDir, skipSpecial):
|
||||||
yield (k, Path(p))
|
yield (k, Path(p))
|
||||||
|
|
||||||
iterator walkDirRec*(dir: Path,
|
iterator walkDirRec*(dir: Path,
|
||||||
yieldFilter = {pcFile}, followFilter = {pcDir},
|
yieldFilter = {pcFile}, followFilter = {pcDir},
|
||||||
relative = false, checkDir = false, onlyRegular = false):
|
relative = false, checkDir = false, skipSpecial = false):
|
||||||
Path {.tags: [ReadDirEffect].} =
|
Path {.tags: [ReadDirEffect].} =
|
||||||
## Recursively walks over the directory `dir` and yields for each file
|
## Recursively walks over the directory `dir` and yields for each file
|
||||||
## or directory in `dir`.
|
## or directory in `dir`.
|
||||||
##
|
##
|
||||||
## Options `relative`, `checkdir`, `onlyRegular` are explained in
|
## Options `relative`, `checkdir`, `skipSpecial` are explained in
|
||||||
## [walkDir iterator] description.
|
## [walkDir iterator] description.
|
||||||
##
|
##
|
||||||
## .. warning:: Modifying the directory structure while the iterator
|
## .. warning:: Modifying the directory structure while the iterator
|
||||||
|
|
@ -121,7 +121,7 @@ iterator walkDirRec*(dir: Path,
|
||||||
## See also:
|
## See also:
|
||||||
## * `walkDir iterator`_
|
## * `walkDir iterator`_
|
||||||
for p in walkDirRec(dir.string, yieldFilter, followFilter, relative,
|
for p in walkDirRec(dir.string, yieldFilter, followFilter, relative,
|
||||||
checkDir, onlyRegular):
|
checkDir, skipSpecial):
|
||||||
yield Path(p)
|
yield Path(p)
|
||||||
|
|
||||||
proc setCurrentDir*(newDir: Path) {.inline, tags: [].} =
|
proc setCurrentDir*(newDir: Path) {.inline, tags: [].} =
|
||||||
|
|
|
||||||
|
|
@ -86,16 +86,16 @@ type
|
||||||
|
|
||||||
when defined(posix) and not weirdTarget:
|
when defined(posix) and not weirdTarget:
|
||||||
proc getSymlinkFileKind*(path: string):
|
proc getSymlinkFileKind*(path: string):
|
||||||
tuple[pc: PathComponent, isRegular: bool] =
|
tuple[pc: PathComponent, isSpecial: bool] =
|
||||||
# Helper function.
|
# Helper function.
|
||||||
var s: Stat
|
var s: Stat
|
||||||
assert(path != "")
|
assert(path != "")
|
||||||
result = (pcLinkToFile, true)
|
result = (pcLinkToFile, false)
|
||||||
if stat(path, s) == 0'i32:
|
if stat(path, s) == 0'i32:
|
||||||
if S_ISDIR(s.st_mode):
|
if S_ISDIR(s.st_mode):
|
||||||
result = (pcLinkToDir, true)
|
result = (pcLinkToDir, false)
|
||||||
elif not S_ISREG(s.st_mode):
|
elif not S_ISREG(s.st_mode):
|
||||||
result = (pcLinkToFile, false)
|
result = (pcLinkToFile, true)
|
||||||
|
|
||||||
proc tryMoveFSObject*(source, dest: string, isDir: bool): bool {.noWeirdTarget.} =
|
proc tryMoveFSObject*(source, dest: string, isDir: bool): bool {.noWeirdTarget.} =
|
||||||
## Moves a file (or directory if `isDir` is true) from `source` to `dest`.
|
## 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
|
discard
|
||||||
|
|
||||||
iterator walkDir*(dir: string; relative = false, checkDir = false,
|
iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||||
onlyRegular = false):
|
skipSpecial = false):
|
||||||
tuple[kind: PathComponent, path: string] {.tags: [ReadDirEffect].} =
|
tuple[kind: PathComponent, path: string] {.tags: [ReadDirEffect].} =
|
||||||
## Walks over the directory `dir` and yields for each directory or file in
|
## 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.
|
## `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.
|
## otherwise the full path is returned.
|
||||||
## * If `checkDir` is true, `OSError` is raised when `dir`
|
## * If `checkDir` is true, `OSError` is raised when `dir`
|
||||||
## doesn't exist.
|
## 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,
|
## files (**without** special "file" objects like FIFOs, device files,
|
||||||
## etc) will be yielded on Unix.
|
## etc) will be yielded on Unix.
|
||||||
##
|
##
|
||||||
|
|
@ -240,9 +240,9 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||||
var k = pcFile
|
var k = pcFile
|
||||||
|
|
||||||
template resolveSymlink() =
|
template resolveSymlink() =
|
||||||
var isRegular: bool
|
var isSpecial: bool
|
||||||
(k, isRegular) = getSymlinkFileKind(path)
|
(k, isSpecial) = getSymlinkFileKind(path)
|
||||||
if onlyRegular and not isRegular: continue
|
if skipSpecial and isSpecial: continue
|
||||||
|
|
||||||
template kSetGeneric() = # pure Posix component `k` resolution
|
template kSetGeneric() = # pure Posix component `k` resolution
|
||||||
if lstat(path.cstring, s) < 0'i32: continue # don't yield
|
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
|
k = pcDir
|
||||||
elif S_ISLNK(s.st_mode):
|
elif S_ISLNK(s.st_mode):
|
||||||
resolveSymlink()
|
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
|
when defined(linux) or defined(macosx) or
|
||||||
defined(bsd) or defined(genode) or defined(nintendoswitch):
|
defined(bsd) or defined(genode) or defined(nintendoswitch):
|
||||||
|
|
@ -261,7 +261,7 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||||
of DT_UNKNOWN:
|
of DT_UNKNOWN:
|
||||||
kSetGeneric()
|
kSetGeneric()
|
||||||
else: # DT_REG or special "files" like FIFOs
|
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: discard # leave it as pcFile
|
||||||
else: # assuming that field `d_type` is not present
|
else: # assuming that field `d_type` is not present
|
||||||
kSetGeneric()
|
kSetGeneric()
|
||||||
|
|
@ -270,12 +270,12 @@ iterator walkDir*(dir: string; relative = false, checkDir = false,
|
||||||
|
|
||||||
iterator walkDirRec*(dir: string,
|
iterator walkDirRec*(dir: string,
|
||||||
yieldFilter = {pcFile}, followFilter = {pcDir},
|
yieldFilter = {pcFile}, followFilter = {pcDir},
|
||||||
relative = false, checkDir = false, onlyRegular = false):
|
relative = false, checkDir = false, skipSpecial = false):
|
||||||
string {.tags: [ReadDirEffect].} =
|
string {.tags: [ReadDirEffect].} =
|
||||||
## Recursively walks over the directory `dir` and yields for each file
|
## Recursively walks over the directory `dir` and yields for each file
|
||||||
## or directory in `dir`.
|
## or directory in `dir`.
|
||||||
##
|
##
|
||||||
## Options `relative`, `checkdir`, `onlyRegular` are explained in
|
## Options `relative`, `checkdir`, `skipSpecial` are explained in
|
||||||
## [walkDir iterator] description.
|
## [walkDir iterator] description.
|
||||||
##
|
##
|
||||||
## .. warning:: Modifying the directory structure while the iterator
|
## .. warning:: Modifying the directory structure while the iterator
|
||||||
|
|
@ -311,7 +311,7 @@ iterator walkDirRec*(dir: string,
|
||||||
while stack.len > 0:
|
while stack.len > 0:
|
||||||
let d = stack.pop()
|
let d = stack.pop()
|
||||||
for k, p in walkDir(dir / d, relative = true, checkDir = checkDir,
|
for k, p in walkDir(dir / d, relative = true, checkDir = checkDir,
|
||||||
onlyRegular = onlyRegular):
|
skipSpecial = skipSpecial):
|
||||||
let rel = d / p
|
let rel = d / p
|
||||||
if k in {pcDir, pcLinkToDir} and k in followFilter:
|
if k in {pcDir, pcLinkToDir} and k in followFilter:
|
||||||
stack.add rel
|
stack.add rel
|
||||||
|
|
|
||||||
|
|
@ -127,11 +127,11 @@ proc testGetFileInfo =
|
||||||
echo pcLinkToDir
|
echo pcLinkToDir
|
||||||
echo pcLinkToFile
|
echo pcLinkToFile
|
||||||
|
|
||||||
doAssert dirInfo.isRegular == true
|
doAssert dirInfo.isSpecial == false
|
||||||
doAssert fileInfo.isRegular == true
|
doAssert fileInfo.isSpecial == false
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
doAssert linkDirInfo.isRegular == true
|
doAssert linkDirInfo.isSpecial == false
|
||||||
doAssert linkFileInfo.isRegular == true
|
doAssert linkFileInfo.isSpecial == false
|
||||||
|
|
||||||
removeDir(dirPath)
|
removeDir(dirPath)
|
||||||
removeFile(filePath)
|
removeFile(filePath)
|
||||||
|
|
@ -139,7 +139,7 @@ proc testGetFileInfo =
|
||||||
removeFile(linkDirPath)
|
removeFile(linkDirPath)
|
||||||
removeFile(linkFilePath)
|
removeFile(linkFilePath)
|
||||||
|
|
||||||
# Test that `isRegular` is set correctly
|
# Test that `isSpecial` is set correctly
|
||||||
block:
|
block:
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
let
|
let
|
||||||
|
|
@ -154,8 +154,8 @@ proc testGetFileInfo =
|
||||||
fifoInfo = getFileInfo(fifoPath)
|
fifoInfo = getFileInfo(fifoPath)
|
||||||
linkFifoInfo = getFileInfo(linkFifoPath)
|
linkFifoInfo = getFileInfo(linkFifoPath)
|
||||||
|
|
||||||
doAssert fifoInfo.isRegular == false
|
doAssert fifoInfo.isSpecial == true
|
||||||
doAssert linkFifoInfo.isRegular == false
|
doAssert linkFifoInfo.isSpecial == true
|
||||||
|
|
||||||
removeFile(fifoPath)
|
removeFile(fifoPath)
|
||||||
removeFile(linkFifoPath)
|
removeFile(linkFifoPath)
|
||||||
|
|
|
||||||
|
|
@ -361,7 +361,7 @@ block: # walkDir
|
||||||
removeDir("walkdir_test")
|
removeDir("walkdir_test")
|
||||||
|
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
block walkDirRegular:
|
block walkDirSpecial:
|
||||||
createDir("walkdir_test")
|
createDir("walkdir_test")
|
||||||
doAssert execShellCmd("mkfifo walkdir_test/fifo") == 0
|
doAssert execShellCmd("mkfifo walkdir_test/fifo") == 0
|
||||||
createSymlink("fifo", "walkdir_test/fifo_link")
|
createSymlink("fifo", "walkdir_test/fifo_link")
|
||||||
|
|
@ -370,9 +370,9 @@ block: # walkDir
|
||||||
(pcFile, "fifo") in withSpecialFiles and
|
(pcFile, "fifo") in withSpecialFiles and
|
||||||
(pcLinkToFile, "fifo_link") in withSpecialFiles)
|
(pcLinkToFile, "fifo_link") in withSpecialFiles)
|
||||||
# now Unix special files are excluded from walkdir output:
|
# now Unix special files are excluded from walkdir output:
|
||||||
let onlyRegularFiles = toSeq(walkDir("walkdir_test", relative = true,
|
let skipSpecialFiles = toSeq(walkDir("walkdir_test", relative = true,
|
||||||
onlyRegular = true))
|
skipSpecial = true))
|
||||||
doAssert onlyRegularFiles.len == 0
|
doAssert skipSpecialFiles.len == 0
|
||||||
removeDir("walkdir_test")
|
removeDir("walkdir_test")
|
||||||
|
|
||||||
block normalizedPath:
|
block normalizedPath:
|
||||||
|
|
|
||||||
|
|
@ -989,7 +989,7 @@ iterator walkDirBasic(dir: string, walkOptC: WalkOptComp[Pattern]): string
|
||||||
let rightDirForFiles = d.isRightDirectory(walkOptC)
|
let rightDirForFiles = d.isRightDirectory(walkOptC)
|
||||||
var files = newSeq[string]()
|
var files = newSeq[string]()
|
||||||
var dirs = newSeq[string]()
|
var dirs = newSeq[string]()
|
||||||
for kind, path in walkDir(d, onlyRegular = true):
|
for kind, path in walkDir(d, skipSpecial = true):
|
||||||
case kind
|
case kind
|
||||||
of pcFile:
|
of pcFile:
|
||||||
if path.hasRightPath(walkOptC) and rightDirForFiles:
|
if path.hasRightPath(walkOptC) and rightDirForFiles:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue