@@ -1800,7 +1800,7 @@ template defaultWalkFilter(item): bool =
1800
1800
# # files and directories
1801
1801
true
1802
1802
1803
- template walkCommon (pattern: string , filter) =
1803
+ template walkCommon (pattern: string , filter: typed , checkError: bool ) =
1804
1804
# # Common code for getting the files and directories with the
1805
1805
# # specified `pattern`
1806
1806
when defined (windows):
@@ -1824,63 +1824,79 @@ template walkCommon(pattern: string, filter) =
1824
1824
let errCode = getLastError ()
1825
1825
if errCode == ERROR_NO_MORE_FILES : break
1826
1826
else : raiseOSError (errCode.OSErrorCode )
1827
+ elif checkError:
1828
+ raiseOSError (osLastError (), pattern)
1827
1829
else : # here we use glob
1828
1830
var
1829
1831
f: Glob
1830
1832
res: int
1831
1833
f.gl_offs = 0
1832
1834
f.gl_pathc = 0
1833
1835
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))
1835
1837
defer : globfree (addr (f))
1836
1838
if res == 0 :
1837
1839
for i in 0 .. f.gl_pathc - 1 :
1838
1840
assert (f.gl_pathv[i] != nil )
1839
1841
let path = $ f.gl_pathv[i]
1840
1842
if filter (path):
1841
1843
yield path
1844
+ elif checkError:
1845
+ raiseOSError (osLastError (), pattern)
1842
1846
1843
- iterator walkPattern * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1847
+ iterator walkPattern * (pattern: string ; checkError = false ): string {.
1848
+ tags : [ReadDirEffect ], noNimScript .} =
1844
1849
# # Iterate over all the files and directories that match the `pattern`.
1845
1850
# #
1846
1851
# # On POSIX this uses the `glob`:idx: call.
1847
1852
# # `pattern` is OS dependent, but at least the `"\*.ext"`
1848
1853
# # notation is supported.
1849
1854
# #
1855
+ # # In case of an error, raises `OSError` if `checkError` is true,
1856
+ # # otherwise the error is ignored and yields nothing.
1857
+ # #
1850
1858
# # See also:
1851
1859
# # * `walkFiles iterator <#walkFiles.i,string>`_
1852
1860
# # * `walkDirs iterator <#walkDirs.i,string>`_
1853
1861
# # * `walkDir iterator <#walkDir.i,string>`_
1854
1862
# # * `walkDirRec iterator <#walkDirRec.i,string>`_
1855
- walkCommon (pattern, defaultWalkFilter)
1863
+ walkCommon (pattern, defaultWalkFilter, checkError )
1856
1864
1857
- iterator walkFiles * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1865
+ iterator walkFiles * (pattern: string ; checkError = false ): string {.
1866
+ tags : [ReadDirEffect ], noNimScript .} =
1858
1867
# # Iterate over all the files that match the `pattern`.
1859
1868
# #
1860
1869
# # On POSIX this uses the `glob`:idx: call.
1861
1870
# # `pattern` is OS dependent, but at least the `"\*.ext"`
1862
1871
# # notation is supported.
1863
1872
# #
1873
+ # # In case of an error, raises `OSError` if `checkError` is true,
1874
+ # # otherwise the error is ignored and yields nothing.
1875
+ # #
1864
1876
# # See also:
1865
1877
# # * `walkPattern iterator <#walkPattern.i,string>`_
1866
1878
# # * `walkDirs iterator <#walkDirs.i,string>`_
1867
1879
# # * `walkDir iterator <#walkDir.i,string>`_
1868
1880
# # * `walkDirRec iterator <#walkDirRec.i,string>`_
1869
- walkCommon (pattern, isFile)
1881
+ walkCommon (pattern, isFile, checkError )
1870
1882
1871
- iterator walkDirs * (pattern: string ): string {.tags : [ReadDirEffect ], noNimScript .} =
1883
+ iterator walkDirs * (pattern: string ; checkError = false ): string {.
1884
+ tags : [ReadDirEffect ], noNimScript .} =
1872
1885
# # Iterate over all the directories that match the `pattern`.
1873
1886
# #
1874
1887
# # On POSIX this uses the `glob`:idx: call.
1875
1888
# # `pattern` is OS dependent, but at least the `"\*.ext"`
1876
1889
# # notation is supported.
1877
1890
# #
1891
+ # # In case of an error, raises `OSError` if `checkError` is true,
1892
+ # # otherwise the error is ignored and yields nothing.
1893
+ # #
1878
1894
# # See also:
1879
1895
# # * `walkPattern iterator <#walkPattern.i,string>`_
1880
1896
# # * `walkFiles iterator <#walkFiles.i,string>`_
1881
1897
# # * `walkDir iterator <#walkDir.i,string>`_
1882
1898
# # * `walkDirRec iterator <#walkDirRec.i,string>`_
1883
- walkCommon (pattern, isDir)
1899
+ walkCommon (pattern, isDir, checkError )
1884
1900
1885
1901
proc expandFilename * (filename: string ): string {.rtl , extern : " nos$1" ,
1886
1902
tags : [ReadDirEffect ], noNimScript .} =
0 commit comments