]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/PyModuleRegistry: probe modules using std::filesystem
authorKefu Chai <kchai@redhat.com>
Thu, 30 Apr 2020 02:43:01 +0000 (10:43 +0800)
committerCory Snyder <csnyder@iland.com>
Tue, 1 Jun 2021 11:38:47 +0000 (07:38 -0400)
for better readability

Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 40d06ce623f19172721a2f1d5b4f4f8b642a3077)

src/mgr/PyModuleRegistry.cc

index 42680e5f44b621a39d0b9a7b1d49eadac6d197ad..31fa24d8340d8e501bf78b3b2955bb740ea3eef3 100644 (file)
  * Foundation.  See file COPYING.
  */
 
+#include "PyModuleRegistry.h"
+
+#if __has_include(<filesystem>)
+#include <filesystem>
+namespace fs = std::filesystem;
+#elif __has_include(<experimental/filesystem>)
+#include <experimental/filesystem>
+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<std::string> PyModuleRegistry::probe_modules(const std::string &path) const
 {
-  DIR *dir = opendir(path.c_str());
-  if (!dir) {
-    return {};
-  }
-
-  std::set<std::string> 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<std::string> 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(