os.walkDir: correctly evaluate paths when relative = true (#10057) [backport]
This commit is contained in:
parent
52e36a19a9
commit
b6257f3f21
2 changed files with 13 additions and 4 deletions
|
|
@ -1512,8 +1512,9 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
var y = $x.d_name.cstring
|
var y = $x.d_name.cstring
|
||||||
if y != "." and y != "..":
|
if y != "." and y != "..":
|
||||||
var s: Stat
|
var s: Stat
|
||||||
|
let path = dir / y
|
||||||
if not relative:
|
if not relative:
|
||||||
y = dir / y
|
y = path
|
||||||
var k = pcFile
|
var k = pcFile
|
||||||
|
|
||||||
when defined(linux) or defined(macosx) or
|
when defined(linux) or defined(macosx) or
|
||||||
|
|
@ -1521,16 +1522,16 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
|
||||||
if x.d_type != DT_UNKNOWN:
|
if x.d_type != DT_UNKNOWN:
|
||||||
if x.d_type == DT_DIR: k = pcDir
|
if x.d_type == DT_DIR: k = pcDir
|
||||||
if x.d_type == DT_LNK:
|
if x.d_type == DT_LNK:
|
||||||
if dirExists(y): k = pcLinkToDir
|
if dirExists(path): k = pcLinkToDir
|
||||||
else: k = pcLinkToFile
|
else: k = pcLinkToFile
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if lstat(y, s) < 0'i32: break
|
if lstat(path, s) < 0'i32: break
|
||||||
if S_ISDIR(s.st_mode):
|
if S_ISDIR(s.st_mode):
|
||||||
k = pcDir
|
k = pcDir
|
||||||
elif S_ISLNK(s.st_mode):
|
elif S_ISLNK(s.st_mode):
|
||||||
k = getSymlinkFileKind(y)
|
k = getSymlinkFileKind(path)
|
||||||
yield (k, y)
|
yield (k, y)
|
||||||
|
|
||||||
iterator walkDirRec*(dir: string,
|
iterator walkDirRec*(dir: string,
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,14 @@ block walkDirRec:
|
||||||
|
|
||||||
removeDir("walkdir_test")
|
removeDir("walkdir_test")
|
||||||
|
|
||||||
|
when not defined(windows):
|
||||||
|
block walkDirRelative:
|
||||||
|
createDir("walkdir_test")
|
||||||
|
createSymlink(".", "walkdir_test/c")
|
||||||
|
for k, p in walkDir("walkdir_test", true):
|
||||||
|
doAssert k == pcLinkToDir
|
||||||
|
removeDir("walkdir_test")
|
||||||
|
|
||||||
block normalizedPath:
|
block normalizedPath:
|
||||||
doAssert normalizedPath("") == ""
|
doAssert normalizedPath("") == ""
|
||||||
block relative:
|
block relative:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue