From: Kefu Chai Date: Thu, 30 Apr 2020 02:43:01 +0000 (+0800) Subject: mgr/PyModuleRegistry: probe modules using std::filesystem X-Git-Tag: v15.2.14~54^2~3 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=1c25484db43268b56be8e57f5627bfc752d02f47;p=ceph.git mgr/PyModuleRegistry: probe modules using std::filesystem for better readability Signed-off-by: Kefu Chai (cherry picked from commit 40d06ce623f19172721a2f1d5b4f4f8b642a3077) --- diff --git a/src/mgr/PyModuleRegistry.cc b/src/mgr/PyModuleRegistry.cc index 42680e5f44b62..31fa24d8340d8 100644 --- a/src/mgr/PyModuleRegistry.cc +++ b/src/mgr/PyModuleRegistry.cc @@ -11,6 +11,17 @@ * Foundation. See file COPYING. */ +#include "PyModuleRegistry.h" + +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#else +#error std::filesystem not available! +#endif #include "include/stringify.h" #include "common/errno.h" @@ -24,8 +35,6 @@ #include "ActivePyModules.h" -#include "PyModuleRegistry.h" - #define dout_context g_ceph_context #define dout_subsys ceph_subsys_mgr @@ -260,29 +269,17 @@ void PyModuleRegistry::shutdown() std::set PyModuleRegistry::probe_modules(const std::string &path) const { - DIR *dir = opendir(path.c_str()); - if (!dir) { - return {}; - } - - std::set modules_out; - struct dirent *entry = NULL; - while ((entry = readdir(dir)) != NULL) { - string n(entry->d_name); - string fn = path + "/" + n; - struct stat st; - int r = ::stat(fn.c_str(), &st); - if (r == 0 && S_ISDIR(st.st_mode)) { - string initfn = fn + "/module.py"; - r = ::stat(initfn.c_str(), &st); - if (r == 0) { - modules_out.insert(n); - } + std::set modules; + for (const auto& entry: fs::directory_iterator(path)) { + if (!fs::is_directory(entry)) { + continue; + } + auto module_path = entry.path() / "module.py"; + if (fs::exists(module_path)) { + modules.emplace(entry.path().filename()); } } - closedir(dir); - - return modules_out; + return modules; } int PyModuleRegistry::handle_command(