]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
kv: replace compat_mkdir with fs::create_directory 36053/head
authorKefu Chai <kchai@redhat.com>
Sat, 11 Jul 2020 12:33:44 +0000 (20:33 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 11 Jul 2020 12:34:53 +0000 (20:34 +0800)
so no need to use a wrapper maintained by ourselves.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/compat.h
src/kv/MemDB.cc
src/kv/RocksDBStore.cc

index 8018bd0a87309d39940e0866464ed14742be42ea..7043bf3149f99634e4c791b43d6fdba185ff4705 100644 (file)
@@ -293,9 +293,6 @@ int lchown(const char *path, uid_t owner, gid_t group);
 #ifdef __cplusplus
 }
 
-// Windows' mkdir doesn't accept a mode argument.
-#define compat_mkdir(pathname, mode) mkdir(pathname)
-
 #endif
 
 // O_CLOEXEC is not defined on Windows. Since handles aren't inherited
@@ -308,8 +305,6 @@ int lchown(const char *path, uid_t owner, gid_t group);
 
 #define SOCKOPT_VAL_TYPE void*
 
-#define compat_mkdir(pathname, mode) mkdir(pathname, mode)
-
 #endif /* WIN32 */
 
 #endif /* !CEPH_COMPAT_H */
index 1618b2551acc62e15ee560e3fbf12f63f343b928..4846042e1fe3a7093eadf4ce3a309d5793538a9c 100644 (file)
 #include <map>
 #include <string>
 #include <memory>
+#if __has_include(<filesystem>)
+#include <filesystem>
+namespace fs = std::filesystem;
+#elif __has_include(<experimental/filesystem>)
+#include <experimental/filesystem>
+namespace fs = std::experimental::filesystem;
+#endif
 #include <errno.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -147,14 +154,15 @@ int MemDB::_init(bool create)
   int r;
   dout(1) << __func__ << dendl;
   if (create) {
-    r = compat_mkdir(m_db_path.c_str(), 0700);
-    if (r < 0) {
-      r = -errno;
-      if (r != -EEXIST) {
-       derr << __func__ << " mkdir failed: " << cpp_strerror(r) << dendl;
-       return r;
-      }
+    if (fs::exists(m_db_path)) {
       r = 0; // ignore EEXIST
+    } else {
+      std::error_code ec;
+      if (!fs::create_directory(m_db_path, ec)) {
+       derr << __func__ << " mkdir failed: " << ec.message() << dendl;
+       return -ec.value();
+      }
+      fs::permissions(m_db_path, fs::perms::owner_all);
     }
   } else {
     r = _load();
index 86f7f40aba9dbf792b84cd32bd53c3292f4b0397..a392d13bccf4bb0024797c3081832ef05ceb1941 100644 (file)
@@ -5,6 +5,13 @@
 #include <map>
 #include <string>
 #include <memory>
+#if __has_include(<filesystem>)
+#include <filesystem>
+namespace fs = std::filesystem;
+#elif __has_include(<experimental/filesystem>)
+#include <experimental/filesystem>
+namespace fs = std::experimental::filesystem;
+#endif
 #include <errno.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -320,13 +327,17 @@ int RocksDBStore::create_db_dir()
     unique_ptr<rocksdb::Directory> dir;
     env->NewDirectory(path, &dir);
   } else {
-    int r = compat_mkdir(path.c_str(), 0755);
-    if (r < 0)
-      r = -errno;
-    if (r < 0 && r != -EEXIST) {
-      derr << __func__ << " failed to create " << path << ": " << cpp_strerror(r)
-          << dendl;
-      return r;
+    if (!fs::exists(path)) {
+      std::error_code ec;
+      if (!fs::create_directory(path, ec)) {
+       derr << __func__ << " failed to create " << path
+            << ": " << ec.message() << dendl;
+       return -ec.value();
+      }
+      fs::permissions(path,
+                     fs::perms::owner_all |
+                     fs::perms::group_read | fs::perms::group_exec |
+                     fs::perms::others_read | fs::perms::others_exec);
     }
   }
   return 0;