Skip to content

Commit c6c6b26

Browse files
authored
Improve Configuration::find to gracefully handle FUSE (#619)
See: sourcemeta/jsonschema#650 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent ce2165f commit c6c6b26

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/configuration/configuration.cc

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
#include <sourcemeta/blaze/configuration.h>
22
#include <sourcemeta/core/io.h>
33

4-
#include <algorithm> // std::ranges::any_of
5-
#include <cassert> // assert
6-
#include <string> // std::string
4+
#include <algorithm> // std::ranges::any_of
5+
#include <cassert> // assert
6+
#include <string> // std::string
7+
#include <system_error> // std::error_code
78

89
namespace sourcemeta::blaze {
910

1011
auto Configuration::find(const std::filesystem::path &path)
1112
-> std::optional<std::filesystem::path> {
12-
const auto canonical{sourcemeta::core::weakly_canonical(path)};
13+
// Note we use non-throwing overloads of filesystem functions to gracefully
14+
// handle I/O errors on FUSE and other unusual filesystems
15+
std::filesystem::path canonical;
16+
try {
17+
canonical = sourcemeta::core::weakly_canonical(path);
18+
} catch (const std::filesystem::filesystem_error &) {
19+
return std::nullopt;
20+
}
21+
1322
assert(canonical.is_absolute());
14-
auto current = std::filesystem::is_directory(canonical)
15-
? canonical
16-
: canonical.parent_path();
23+
std::error_code error;
24+
const auto is_directory = std::filesystem::is_directory(canonical, error);
25+
auto current = !error && !is_directory ? canonical.parent_path() : canonical;
1726

1827
while (!current.empty()) {
1928
auto candidate = current / "jsonschema.json";
20-
if (std::filesystem::exists(candidate) &&
21-
std::filesystem::is_regular_file(candidate)) {
29+
if (std::filesystem::exists(candidate, error) &&
30+
std::filesystem::is_regular_file(candidate, error)) {
2231
return candidate;
2332
}
2433

0 commit comments

Comments
 (0)