]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
conf: small code cleanups
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 5 Apr 2011 21:36:24 +0000 (14:36 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 5 Apr 2011 21:41:25 +0000 (14:41 -0700)
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 <colin.mccabe@dreamhost.com>
src/common/ConfUtils.cc
src/common/config.cc
src/common/config.h
src/mon/MonClient.cc

index a0878dfeb50e91e88b983da9b112c58f57c010b0..1a238ee5dcd23c9ae5870a2292ad856149ddff41 100644 (file)
@@ -96,7 +96,7 @@ parse_file(const std::string &fname, std::deque<std::string> *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<std::string> *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<std::string> *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);
index 8f51826d01de69b141bc0bb8c68443983e0c2429..1ecc7edde27ca9cf4f3d9a31cead09952ef160a1 100644 (file)
@@ -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<std::string> &conf_files,
                   std::deque<std::string> *parse_errors)
 {
   // open new conf
-  list<string>::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<string>::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 <std::string> my_sections;
   get_my_sections(my_sections);
@@ -564,12 +555,7 @@ parse_argv(std::vector<const char*>& args)
   std::string val;
   for (std::vector<const char*>::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 <std::string> &sections)
 int md_config_t::
 get_all_sections(std::vector <std::string> &sections)
 {
-  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 <std::string> &sections,
                    const char *key, std::string &out, bool emeta) const
 {
-  if (!cf)
-    return -EDOM;
   std::vector <std::string>::const_iterator s = sections.begin();
   std::vector <std::string>::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);
index 258ba7b2f60bd3ae8e012de48f9c9f958bb804a3..295b66cb6a44471560396474dbdc9956f25ea0ec 100644 (file)
@@ -20,18 +20,20 @@ extern struct ceph_file_layout g_default_file_layout;
 #include <vector>
 #include <map>
 
-#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 <std::string> &sections);
 
-  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 <std::string> &sections,
@@ -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;
index 53021573eeddefe49a5358731fa6b2e98bf898de..a1d6c43f58755e37fbb085fe06ba3e9678d9b931 100644 (file)
@@ -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 <std::string> sections;
   int ret = g_conf.get_all_sections(sections);