From ee70b55e8cc5dc8c9c2db41f7855064f035a7764 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Jul 2020 19:00:00 +0800 Subject: [PATCH] common/config: extract get_cluster_name() for two reasons * to consolidate the rules for figuring out the cluster name * to reuse it in another implementation of parse_config_files(), crimson will need to restructure the existing parse_config_files(), and implement an async version of it. Signed-off-by: Kefu Chai --- src/common/config.cc | 52 ++++++++++++++++++++++++-------------------- src/common/config.h | 2 +- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/common/config.cc b/src/common/config.cc index f53d453825f73..fc52433be40a0 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -13,7 +13,13 @@ */ #include - +#if __has_include() +#include +namespace fs = std::filesystem; +#else +#include +namespace fs = std::experimental::filesystem; +#endif #include "common/ceph_argparse.h" #include "common/common_init.h" #include "common/config.h" @@ -351,11 +357,7 @@ int md_config_t::parse_config_files(ConfigValues& values, return -ENOSYS; if (values.cluster.empty() && !conf_files_str) { - /* - * set the cluster name to 'ceph' when neither cluster name nor - * configuration file are specified. - */ - values.cluster = "ceph"; + values.cluster = get_cluster_name(nullptr); } // open new conf string conffile; @@ -377,23 +379,8 @@ int md_config_t::parse_config_files(ConfigValues& values, if (conffile.empty()) return -ENOENT; - if (values.cluster.size() == 0) { - /* - * If cluster name is not set yet, use the prefix of the - * basename of configuration file as cluster name. - */ - auto start = conffile.rfind('/') + 1; - auto end = conffile.find(".conf", start); - if (end == conffile.npos) { - /* - * If the configuration file does not follow $cluster.conf - * convention, we do the last try and assign the cluster to - * 'ceph'. - */ - values.cluster = "ceph"; - } else { - values.cluster = conffile.substr(start, end - start); - } + if (values.cluster.empty()) { + values.cluster = get_cluster_name(conffile.c_str()); } std::vector my_sections = get_my_sections(values); @@ -473,6 +460,25 @@ md_config_t::get_conffile_paths(const ConfigValues& values, return paths; } +std::string md_config_t::get_cluster_name(const char* conffile) +{ + if (conffile) { + // If cluster name is not set yet, use the prefix of the + // basename of configuration file as cluster name. + if (fs::path path{conffile}; path.extension() == ".conf") { + return path.stem(); + } else { + // If the configuration file does not follow $cluster.conf + // convention, we do the last try and assign the cluster to + // 'ceph'. + return "ceph"; + } + } else { + // set the cluster name to 'ceph' when configuration file is not specified. + return "ceph"; + } +} + void md_config_t::parse_env(unsigned entity_type, ConfigValues& values, const ConfigTracker& tracker, diff --git a/src/common/config.h b/src/common/config.h index 0fa89382b8a25..a3f54c0b65c24 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -317,7 +317,7 @@ private: const char *conf_files, std::ostream *warnings, int flags) const; - + static std::string get_cluster_name(const char* conffile_path); // The configuration file we read, or NULL if we haven't read one. ConfFile cf; public: -- 2.39.5