Skip to content

Commit

Permalink
fix: Language server crashes while trying to list inaccessible directory
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera committed Nov 4, 2021
1 parent c68270f commit 60db271
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions utils/src/native_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,27 @@ std::filesystem::path canonical(const std::filesystem::path& p, std::error_code&
}

bool equal(const std::filesystem::path& left, const std::filesystem::path& right) { return left == right; }
bool is_directory(const std::filesystem::path& p) { return std::filesystem::directory_entry(p).is_directory(); }
bool is_directory(const std::filesystem::path& p)
{
std::error_code ec;
std::filesystem::directory_entry d(p, ec);

return !ec && d.is_directory();
}

list_directory_rc list_directory_regular_files(
const std::filesystem::path& d, std::function<void(const std::filesystem::path&)> h)
{
std::filesystem::directory_entry dir(d);
try
{
std::filesystem::directory_entry dir(d);

if (!dir.exists())
return list_directory_rc::not_exists;
if (!dir.exists())
return list_directory_rc::not_exists;

if (!dir.is_directory())
return list_directory_rc::not_a_directory;
if (!dir.is_directory())
return list_directory_rc::not_a_directory;

try
{
std::filesystem::directory_iterator it(dir);

for (auto& p : it)
Expand All @@ -78,16 +84,16 @@ list_directory_rc list_directory_regular_files(
list_directory_rc list_directory_subdirs_and_symlinks(
const std::filesystem::path& d, std::function<void(const std::filesystem::path&)> h)
{
std::filesystem::directory_entry dir(d);
try
{
std::filesystem::directory_entry dir(d);

if (!dir.exists())
return list_directory_rc::not_exists;
if (!dir.exists())
return list_directory_rc::not_exists;

if (!dir.is_directory())
return list_directory_rc::not_a_directory;
if (!dir.is_directory())
return list_directory_rc::not_a_directory;

try
{
std::filesystem::directory_iterator it(dir);

for (auto& p : it)
Expand Down

0 comments on commit 60db271

Please sign in to comment.