From 64185b6878daa6aded4e6c9d2d58402c909422c2 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Tue, 5 Apr 2011 14:36:24 -0700 Subject: [PATCH] conf: small code cleanups md_config_t: store a ConfFile by value rather than by pointer. ConfFile::parse_file: negate error when returning errors. Signed-off-by: Colin McCabe --- src/common/ConfUtils.cc | 6 ++--- src/common/config.cc | 50 +++++++++++------------------------------ src/common/config.h | 19 ++++++---------- src/mon/MonClient.cc | 8 ------- 4 files changed, 23 insertions(+), 60 deletions(-) diff --git a/src/common/ConfUtils.cc b/src/common/ConfUtils.cc index a0878dfeb50e9..1a238ee5dcd23 100644 --- a/src/common/ConfUtils.cc +++ b/src/common/ConfUtils.cc @@ -96,7 +96,7 @@ parse_file(const std::string &fname, std::deque *errors) char *buf = NULL; FILE *fp = fopen(fname.c_str(), "r"); if (!fp) { - ret = errno; + ret = -errno; ostringstream oss; oss << "read_conf: failed to open '" << fname << "': " << cpp_strerror(ret); errors->push_back(oss.str()); @@ -105,7 +105,7 @@ parse_file(const std::string &fname, std::deque *errors) struct stat st_buf; if (fstat(fileno(fp), &st_buf)) { - ret = errno; + ret = -errno; ostringstream oss; oss << "read_conf: failed to fstat '" << fname << "': " << cpp_strerror(ret); errors->push_back(oss.str()); @@ -126,7 +126,7 @@ parse_file(const std::string &fname, std::deque *errors) if (fread(buf, 1, sz, fp) != sz) { if (ferror(fp)) { - ret = errno; + ret = -errno; ostringstream oss; oss << "read_conf: fread error while reading '" << fname << "': " << cpp_strerror(ret); diff --git a/src/common/config.cc b/src/common/config.cc index 8f51826d01de6..1ecc7edde27ca 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -485,7 +485,6 @@ bool ceph_resolve_file_search(const std::string& filename_list, md_config_t:: md_config_t() - : cf(NULL) { // // Note: because our md_config_t structure is a global, the memory used to @@ -505,8 +504,6 @@ md_config_t() md_config_t:: ~md_config_t() { - delete cf; - cf = NULL; } int md_config_t:: @@ -514,23 +511,17 @@ parse_config_files(const std::list &conf_files, std::deque *parse_errors) { // open new conf - list::const_iterator c = conf_files.begin(); - if (c == conf_files.end()) - return -EINVAL; - while (true) { - if (c == conf_files.end()) - return -EINVAL; - ConfFile *cf_ = new ConfFile(); - int res = cf_->parse_file(c->c_str(), parse_errors); - if (res == 0) { - cf = cf_; + list::const_iterator c; + for (c = conf_files.begin(); c != conf_files.end(); ++c) { + cf.clear(); + int ret = cf.parse_file(c->c_str(), parse_errors); + if (ret == 0) break; - } - delete cf_; - if (res == -EDOM) - return -EDOM; - ++c; + else if (ret != -ENOENT) + return ret; } + if (c == conf_files.end()) + return -EINVAL; std::vector my_sections; get_my_sections(my_sections); @@ -564,12 +555,7 @@ parse_argv(std::vector& args) std::string val; for (std::vector::iterator i = args.begin(); i != args.end(); ) { if (ceph_argparse_flag(args, i, "--show_conf", (char*)NULL)) { - if (cf) { - cerr << *cf << std::endl; - } - else { - cerr << "(no conf loaded)" << std::endl; - } + cerr << cf << std::endl; _exit(0); } else if (ceph_argparse_flag(args, i, "--foreground", "-f", (char*)NULL)) { @@ -731,31 +717,21 @@ get_my_sections(std::vector §ions) int md_config_t:: get_all_sections(std::vector §ions) { - if (!cf) - return -EDOM; - for (ConfFile::const_section_iter_t s = cf->sections_begin(); - s != cf->sections_end(); ++s) { + for (ConfFile::const_section_iter_t s = cf.sections_begin(); + s != cf.sections_end(); ++s) { sections.push_back(s->first); } return 0; } -bool md_config_t:: -have_conf_file() const -{ - return !!cf; -} - int md_config_t:: get_val_from_conf_file(const std::vector §ions, const char *key, std::string &out, bool emeta) const { - if (!cf) - return -EDOM; std::vector ::const_iterator s = sections.begin(); std::vector ::const_iterator s_end = sections.end(); for (; s != s_end; ++s) { - int ret = cf->read(s->c_str(), key, out); + int ret = cf.read(s->c_str(), key, out); if (ret == 0) { if (emeta) expand_meta(out); diff --git a/src/common/config.h b/src/common/config.h index 258ba7b2f60bd..295b66cb6a444 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -20,18 +20,20 @@ extern struct ceph_file_layout g_default_file_layout; #include #include -#include "include/assert.h" - +#include "common/ConfUtils.h" #include "common/Mutex.h" +#include "include/assert.h" +#include "msg/msg_types.h" #define OSD_REP_PRIMARY 0 #define OSD_REP_SPLAY 1 #define OSD_REP_CHAIN 2 +struct EntityName; -#include "msg/msg_types.h" +class config_option; -struct EntityName; +extern const char *CEPH_CONF_FILE_DEFAULT; enum log_to_stderr_t { LOG_TO_STDERR_NONE = 0, @@ -39,11 +41,6 @@ enum log_to_stderr_t { LOG_TO_STDERR_ALL = 2, }; -struct ConfFile; -class config_option; - -extern const char *CEPH_CONF_FILE_DEFAULT; - struct md_config_t { public: @@ -75,8 +72,6 @@ public: // Return a list of all sections int get_all_sections(std::vector §ions); - bool have_conf_file() const; - // Get a value from the configuration file that we read earlier. // Metavariables will be expanded if emeta is true. int get_val_from_conf_file(const std::vector §ions, @@ -96,7 +91,7 @@ private: int set_val_impl(const char *val, const config_option *opt); // The configuration file we read, or NULL if we haven't read one. - ConfFile *cf; + ConfFile cf; public: std::string host; diff --git a/src/mon/MonClient.cc b/src/mon/MonClient.cc index 53021573eedde..a1d6c43f58755 100644 --- a/src/mon/MonClient.cc +++ b/src/mon/MonClient.cc @@ -108,14 +108,6 @@ int MonClient::build_initial_monmap() cerr << "unable to parse addrs in '" << g_conf.mon_host << "'" << std::endl; } - // config file? - if (!g_conf.have_conf_file()) { - cerr << "Unable to find any monitors in the configuration " - << "file, because there is no configuration file. " - << "Please specify monitors via -m monaddr or -c ceph.conf" << std::endl; - return -ENOENT; - } - // What monitors are in the config file? std::vector sections; int ret = g_conf.get_all_sections(sections); -- 2.39.5