summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrey Makarov <ph.makarov@gmail.com>2020-12-03 19:09:54 +0300
committerGitHub <noreply@github.com>2020-12-03 17:09:54 +0100
commite223a05123144ae8a030b2c22f4b7d8b23784314 (patch)
tree6d50f8156de6e5395d45bf31a52f06667795c97f
parentc731f7ab149a539782b0b000853ecef058bc3dde (diff)
downloadNim-e223a05123144ae8a030b2c22f4b7d8b23784314.tar.gz
minor fix to Posix part of walkDir (#16234)
* change break->continue and rewrite one-yield-style

* use case statement for clarity

* Tiny nit

Co-authored-by: Clyybber <darkmine956@gmail.com>
-rw-r--r--lib/pure/os.nim32
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index aa2a1aaa8..77499deea 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -2120,21 +2120,27 @@ iterator walkDir*(dir: string; relative = false, checkDir = false):
               y = path
             var k = pcFile
 
+            template kSetGeneric() =  # pure Posix component `k` resolution
+              if lstat(path, s) < 0'i32: continue  # don't yield
+              elif S_ISDIR(s.st_mode):
+                k = pcDir
+              elif S_ISLNK(s.st_mode):
+                k = getSymlinkFileKind(path)
+
             when defined(linux) or defined(macosx) or
                  defined(bsd) or defined(genode) or defined(nintendoswitch):
-              if x.d_type != DT_UNKNOWN:
-                if x.d_type == DT_DIR: k = pcDir
-                if x.d_type == DT_LNK:
-                  if dirExists(path): k = pcLinkToDir
-                  else: k = pcLinkToFile
-                yield (k, y)
-                continue
-
-            if lstat(path, s) < 0'i32: break
-            if S_ISDIR(s.st_mode):
-              k = pcDir
-            elif S_ISLNK(s.st_mode):
-              k = getSymlinkFileKind(path)
+              case x.d_type
+              of DT_DIR: k = pcDir
+              of DT_LNK:
+                if dirExists(path): k = pcLinkToDir
+                else: k = pcLinkToFile
+              of DT_UNKNOWN:
+                kSetGeneric()
+              else: # e.g. DT_REG etc
+                discard # leave it as pcFile
+            else:  # assuming that field `d_type` is not present
+              kSetGeneric()
+
             yield (k, y)
 
 iterator walkDirRec*(dir: string,