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

This commit is contained in:
alaviss 2018-12-21 20:12:48 +07:00 • committed by Andreas Rumpf
commit b6257f3f21
2 changed files with 13 additions and 4 deletions

View file

@ -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,

View file

@ -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: