Skip to content

Commit

Permalink
Improve (file-exists? string) with error handling (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Aug 5, 2024
1 parent 7852471 commit fde1907
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/goldfish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const std::string goldfish_version=

namespace goldfish {
using std::filesystem::exists;
using std::filesystem::filesystem_error;
using std::filesystem::path;

// Glues for Goldfish
Expand All @@ -51,7 +52,13 @@ static s7_pointer
f_file_exists (s7_scheme* sc, s7_pointer args) {
const char* path_c= s7_string (s7_car (args));
auto p = path (path_c);
bool ret = exists (p);
bool ret = false;
try {
ret= exists (p);
} catch (filesystem_error const& ex) {
return s7_error (sc, s7_make_symbol (sc, "read-error"),
s7_make_string (sc, ex.what ()));
}
return s7_make_boolean (sc, ret);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/test_all.scm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
(check (file-exists? "/tmp") => #t)
(check (file-exists? "/not_exists") => #f))

(when (os-linux?)
(check (file-exists? "/root") => #t)
(check
(catch 'read-error
(lambda () (file-exists? "/root/.bashrc"))
(lambda args #t))
=> #t))

(when (os-windows?)
(check (file-exists? "C:") => #t))

Expand Down

0 comments on commit fde1907

Please sign in to comment.