@@ -1800,7 +1800,7 @@ template defaultWalkFilter(item): bool =
18001800 # # files and directories
18011801 true
18021802
1803- template walkCommon (pattern: string , filter) =
1803+ template walkCommon (pattern: string , filter: typed , checkError: bool ) =
18041804 # # Common code for getting the files and directories with the
18051805 # # specified `pattern`
18061806 when defined (windows):
@@ -1824,63 +1824,79 @@ template walkCommon(pattern: string, filter) =
18241824 let errCode = getLastError ()
18251825 if errCode == ERROR_NO_MORE_FILES : break
18261826 else : raiseOSError (errCode.OSErrorCode )
1827+ elif checkError:
1828+ raiseOSError (osLastError (), pattern)
18271829 else : # here we use glob
18281830 var
18291831 f: Glob
18301832 res: int
18311833 f.gl_offs = 0
18321834 f.gl_pathc = 0
18331835 f.gl_pathv = nil
1834- res = glob (pattern, 0 , nil , addr (f))
1836+ res = glob (pattern, if checkError: GLOB_ERR else : 0 , nil , addr (f))
18351837 defer : globfree (addr (f))
18361838 if res == 0 :
18371839 for i in 0 .. f.gl_pathc - 1 :
18381840 assert (f.gl_pathv[i] != nil )
18391841 let path = $ f.gl_pathv[i]
18401842 if filter (path):
18411843 yield path
1844+ elif checkError:
1845+ raiseOSError (osLastError (), pattern)
18421846
1843- iterator walkPattern * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1847+ iterator walkPattern * (pattern: string ; checkError = false ): string {.
1848+ tags : [ReadDirEffect ], noNimScript .} =
18441849 # # Iterate over all the files and directories that match the `pattern`.
18451850 # #
18461851 # # On POSIX this uses the `glob`:idx: call.
18471852 # # `pattern` is OS dependent, but at least the `"\*.ext"`
18481853 # # notation is supported.
18491854 # #
1855+ # # In case of an error, raises `OSError` if `checkError` is true,
1856+ # # otherwise the error is ignored and yields nothing.
1857+ # #
18501858 # # See also:
18511859 # # * `walkFiles iterator <#walkFiles.i,string>`_
18521860 # # * `walkDirs iterator <#walkDirs.i,string>`_
18531861 # # * `walkDir iterator <#walkDir.i,string>`_
18541862 # # * `walkDirRec iterator <#walkDirRec.i,string>`_
1855- walkCommon (pattern, defaultWalkFilter)
1863+ walkCommon (pattern, defaultWalkFilter, checkError )
18561864
1857- iterator walkFiles * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1865+ iterator walkFiles * (pattern: string ; checkError = false ): string {.
1866+ tags : [ReadDirEffect ], noNimScript .} =
18581867 # # Iterate over all the files that match the `pattern`.
18591868 # #
18601869 # # On POSIX this uses the `glob`:idx: call.
18611870 # # `pattern` is OS dependent, but at least the `"\*.ext"`
18621871 # # notation is supported.
18631872 # #
1873+ # # In case of an error, raises `OSError` if `checkError` is true,
1874+ # # otherwise the error is ignored and yields nothing.
1875+ # #
18641876 # # See also:
18651877 # # * `walkPattern iterator <#walkPattern.i,string>`_
18661878 # # * `walkDirs iterator <#walkDirs.i,string>`_
18671879 # # * `walkDir iterator <#walkDir.i,string>`_
18681880 # # * `walkDirRec iterator <#walkDirRec.i,string>`_
1869- walkCommon (pattern, isFile)
1881+ walkCommon (pattern, isFile, checkError )
18701882
1871- iterator walkDirs * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1883+ iterator walkDirs * (pattern: string ; checkError = false ): string {.
1884+ tags : [ReadDirEffect ], noNimScript .} =
18721885 # # Iterate over all the directories that match the `pattern`.
18731886 # #
18741887 # # On POSIX this uses the `glob`:idx: call.
18751888 # # `pattern` is OS dependent, but at least the `"\*.ext"`
18761889 # # notation is supported.
18771890 # #
1891+ # # In case of an error, raises `OSError` if `checkError` is true,
1892+ # # otherwise the error is ignored and yields nothing.
1893+ # #
18781894 # # See also:
18791895 # # * `walkPattern iterator <#walkPattern.i,string>`_
18801896 # # * `walkFiles iterator <#walkFiles.i,string>`_
18811897 # # * `walkDir iterator <#walkDir.i,string>`_
18821898 # # * `walkDirRec iterator <#walkDirRec.i,string>`_
1883- walkCommon (pattern, isDir)
1899+ walkCommon (pattern, isDir, checkError )
18841900
18851901proc expandFilename * (filename: string ): string {.rtl , extern : " nos$1" ,
18861902 tags : [ReadDirEffect ], noNimScript .} =
0 commit comments