summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoralaviss <alaviss@users.noreply.github.com>2018-12-21 20:12:48 +0700
committerAndreas Rumpf <rumpf_a@web.de>2018-12-21 14:12:48 +0100
commitb6257f3f212036216fb4aeb521a3d120222b237c (patch)
tree37738cd9a0ed5f080c1b0e607c9a0f209c5cc84e
parent52e36a19a902edcc533c0e0ffd0eff8c83158fef (diff)
downloadNim-b6257f3f212036216fb4aeb521a3d120222b237c.tar.gz
os.walkDir: correctly evaluate paths when relative = true (#10057) [backport]
-rw-r--r--lib/pure/os.nim9
-rw-r--r--tests/stdlib/tos.nim8
2 files changed, 13 insertions, 4 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 2919b39f0..330d2d6b8 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -1512,8 +1512,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
@@ -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_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,
diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim
index 577baea87..a7cf5d5b6 100644
--- a/tests/stdlib/tos.nim
+++ b/tests/stdlib/tos.nim
@@ -189,6 +189,14 @@ block walkDirRec:
 
   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:
   doAssert normalizedPath("") == ""
   block relative: