os.walkDir: correctly evaluate paths when relative = true [0.19] (#10173)

This commit is contained in:
alaviss
2019-01-03 22:35:14 +07:00
committed by Miran
parent 32d7fa35a9
commit 1134f85faa
2 changed files with 12 additions and 4 deletions

View File

@@ -875,8 +875,9 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
var y = $x.d_name.cstring
if y != "." and y != "..":
var s: Stat
let path = dir / y
if not relative:
y = dir / y
y = path
var k = pcFile
when defined(linux) or defined(macosx) or
@@ -884,16 +885,16 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path:
if x.d_type != DT_UNKNOWN:
if x.d_type == DT_DIR: k = pcDir
if x.d_type == DT_LNK:
if dirExists(y): k = pcLinkToDir
if dirExists(path): k = pcLinkToDir
else: k = pcLinkToFile
yield (k, y)
continue
if lstat(y, s) < 0'i32: break
if lstat(path, s) < 0'i32: break
if S_ISDIR(s.st_mode):
k = pcDir
elif S_ISLNK(s.st_mode):
k = getSymlinkFileKind(y)
k = getSymlinkFileKind(path)
yield (k, y)
iterator walkDirRec*(dir: string, yieldFilter = {pcFile},

View File

@@ -189,6 +189,13 @@ when defined(posix):
doAssert normalizedPath("/a/b/c/..") == "/a/b"
doAssert normalizedPath("/a/b/c/../") == "/a/b"
block walkDirRelative:
createDir("walkdir_test")
createSymlink(".", "walkdir_test/c")
for k, p in walkDir("walkdir_test", true):
doAssert k == pcLinkToDir
removeDir("walkdir_test")
else:
block normalizedPath: