From: Kefu Chai Date: Sat, 11 Jul 2020 12:33:44 +0000 (+0800) Subject: kv: replace compat_mkdir with fs::create_directory X-Git-Tag: v16.1.0~1591^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=26f35dd7ad6bae98733bb4b5ed2f4368cd2263b3;p=ceph.git kv: replace compat_mkdir with fs::create_directory so no need to use a wrapper maintained by ourselves. Signed-off-by: Kefu Chai --- diff --git a/src/include/compat.h b/src/include/compat.h index 8018bd0a8730..7043bf3149f9 100644 --- a/src/include/compat.h +++ b/src/include/compat.h @@ -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 */ diff --git a/src/kv/MemDB.cc b/src/kv/MemDB.cc index 1618b2551acc..4846042e1fe3 100644 --- a/src/kv/MemDB.cc +++ b/src/kv/MemDB.cc @@ -10,6 +10,13 @@ #include #include #include +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#endif #include #include #include @@ -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(); diff --git a/src/kv/RocksDBStore.cc b/src/kv/RocksDBStore.cc index 86f7f40aba9d..a392d13bccf4 100644 --- a/src/kv/RocksDBStore.cc +++ b/src/kv/RocksDBStore.cc @@ -5,6 +5,13 @@ #include #include #include +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#endif #include #include #include @@ -320,13 +327,17 @@ int RocksDBStore::create_db_dir() unique_ptr 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;