From e5a56174e235aa2c5719f02f687288cce0df5d02 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Tue, 9 Apr 2019 09:01:52 -0400 Subject: [PATCH] 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. --- src/boot/boot.janet | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 07446085..2a9268ec 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -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.