1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Switch fexists to use os/stat when available.

When os/stat is not available, we first
try to read one byte from the file before
saying it is good. If that fails, it is not
a file that we can read from so it counts as not found.
This commit is contained in:
Calvin Rose 2019-04-09 09:01:52 -04:00
parent 6c68c7a35f
commit e5a56174e2

View File

@ -1588,9 +1588,19 @@
on Windows is C:/Janet/Library."
(or (process/opts "JANET_PATH") ""))
(defn- fexists [path]
(def f (file/open path :r+))
(if f (do (file/close f) path)))
# Version of fexisst that works even with a reduced OS
(if-let [has-stat (_env 'os/stat)]
(let [stat (has-stat :value)]
(defglobal "fexists" (fn fexists [path] (= :file (stat path :mode)))))
(defglobal "fexists"
(fn fexists [path]
(def f (file/open path))
(when f
(def res
(try (do (file/read f 1) true)
([err] nil)))
(file/close f)
res))))
(defn module/find
"Try to match a module or path name from the patterns in module/paths.