From 9c63a4ed2281dfa0ae56e6725748e09656011bcc Mon Sep 17 00:00:00 2001 From: ddddhm1234 <113791864+ddddhm1234@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:37:12 +0800 Subject: [PATCH] Update ImGuiFileDialog.cpp The bug manifests as a freeze or crash of DPG when attempting to access a directory that the current user does not have permission to access on windows using file_dialog. This commit fixes the issue by adding a code snippet that checks the permission to access the target directory before attempting to do so. If the user does not have permission, the program will not access the target directory and stay in the current path. --- .../ImGuiFileDialog/ImGuiFileDialog.cpp | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/thirdparty/ImGuiFileDialog/ImGuiFileDialog.cpp b/thirdparty/ImGuiFileDialog/ImGuiFileDialog.cpp index 3abad221a..571031b39 100644 --- a/thirdparty/ImGuiFileDialog/ImGuiFileDialog.cpp +++ b/thirdparty/ImGuiFileDialog/ImGuiFileDialog.cpp @@ -309,6 +309,27 @@ namespace IGFD //// INLINE FUNCTIONS /////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// +#ifdef USE_STD_FILESYSTEM +inline bool isDirectoryAccessible(const std::string &path) +{ + try + { + const std::filesystem::path fspath(path); + const auto dir_iter = std::filesystem::directory_iterator(fspath); + return true; + } + catch (...) + { + return false; + } +} +#else +inline bool isDirectoryAccessible(const std::string& path) +{ + return access(path.c_str(), R_OK | F_OK) == 0; +} +#endif + #ifndef USE_STD_FILESYSTEM inline int inAlphaSort(const struct dirent** a, const struct dirent** b) { @@ -1738,7 +1759,7 @@ namespace IGFD newPath = prCurrentPath + std::string(1u, PATH_SEP) + vInfos->fileName; } - if (IGFD::Utils::IsDirectoryExist(newPath)) + if (IGFD::Utils::IsDirectoryExist(newPath) && isDirectoryAccessible(newPath)) { if (puShowDrives) { @@ -2107,8 +2128,11 @@ namespace IGFD auto gio = ImGui::GetIO(); if (ImGui::IsKeyReleased(gio.KeyMap[ImGuiKey_Enter])) { - puFileManager.SetCurrentPath(std::string(puFileManager.puInputPathBuffer)); - puFileManager.OpenCurrentPath(*this); + if (isDirectoryAccessible(std::string(puFileManager.puInputPathBuffer))) + { + puFileManager.SetCurrentPath(std::string(puFileManager.puInputPathBuffer)); + puFileManager.OpenCurrentPath(*this); + } puFileManager.puInputPathActivated = false; } if (ImGui::IsKeyReleased(gio.KeyMap[ImGuiKey_Escape]))