]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
config: normalize key names, cleanup
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 15 Apr 2011 19:03:12 +0000 (12:03 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 15 Apr 2011 20:18:38 +0000 (13:18 -0700)
Normalize key names in md_config_t::get_val and md_config_t::set_val

Remove unused fields from struct config_option.

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/auth/KeyRing.cc
src/common/ConfUtils.cc
src/common/ConfUtils.h
src/common/config.cc
src/common/config.h

index c904b6da787f9a59c919774a1b615ab6f175c2f5..c1bbe5ee3bf3c2c69e6b7071cd9e1903470f8a83 100644 (file)
@@ -117,9 +117,11 @@ void KeyRing::decode_plaintext(bufferlist::iterator& bli)
         l != s->second.lines.end(); ++l) {
       if (l->key.empty())
         continue;
-      ret = set_modifier(l->key.c_str(), l->val.c_str(), ename, caps);
+      string k(l->key);
+      std::replace(k.begin(), k.end(), '_', ' ');
+      ret = set_modifier(k.c_str(), l->val.c_str(), ename, caps);
       if (ret < 0) {
-        derr << "error setting modifier for [" << name << "] type=" << l->key
+        derr << "error setting modifier for [" << name << "] type=" << k
             << " val=" << l->val << dendl;
         goto done_err;
       }
index e753598932c184bfc0a818176af3841fb9b6d0bf..d08f447755a581468b152db6e572af4f170747a7 100644 (file)
@@ -12,6 +12,7 @@
  *
  */
 
+#include <algorithm>
 #include <errno.h>
 #include <list>
 #include <map>
@@ -166,10 +167,12 @@ parse_bufferlist(ceph::bufferlist *bl, std::deque<std::string> *errors)
 int ConfFile::
 read(const std::string &section, const std::string &key, std::string &val) const
 {
+  string k(normalize_key_name(key));
+
   const_section_iter_t s = sections.find(section);
   if (s == sections.end())
     return -ENOENT;
-  ConfLine exemplar(key, "", "", "", 0);
+  ConfLine exemplar(k, "", "", "", 0);
   ConfSection::const_line_iter_t l = s->second.lines.find(exemplar);
   if (l == s->second.lines.end())
     return -ENOENT;
@@ -241,6 +244,22 @@ trim_whitespace(std::string &str, bool strip_internal)
   str.assign(output2);
 }
 
+/* Normalize a key name.
+ *
+ * Normalized key names have no leading or trailing whitespace, and all
+ * whitespace is stored as underscores.  The main reason for selecting this
+ * normal form is so that in common/config.cc, we can use a macro to stringify
+ * the field names of md_config_t and get a key in normal form.
+ */
+std::string ConfFile::
+normalize_key_name(const std::string &key)
+{
+  string k(key);
+  ConfFile::trim_whitespace(k, true);
+  std::replace(k.begin(), k.end(), ' ', '_');
+  return k;
+}
+
 std::ostream &operator<<(std::ostream &oss, const ConfFile &cf)
 {
   for (ConfFile::const_section_iter_t s = cf.sections_begin();
@@ -443,7 +462,7 @@ process_line(int line_no, const char *line, std::deque<std::string> *errors)
          return NULL;
        }
        else if ((c == '=') && (!escaping)) {
-         trim_whitespace(key, true);
+         key = normalize_key_name(key);
          if (key.empty()) {
            ostringstream oss;
            oss << "error parsing key name: no key name found? "
index a21a3d778de4a77c860a8b5b9b551739ceb094fd..b3d11696a2c089977da0d865f79fe6ec6dacf419 100644 (file)
@@ -71,6 +71,7 @@ public:
   const_section_iter_t sections_end() const;
 
   static void trim_whitespace(std::string &str, bool strip_internal);
+  static std::string normalize_key_name(const std::string &key);
   friend std::ostream &operator<<(std::ostream &oss, const ConfFile &cf);
 
 private:
index 5b678b75d3c3eab80c0b1677cd92050f1b5fc5f6..9e60be48173b6cd2cb30332c81d841e0acd63c47 100644 (file)
@@ -70,52 +70,43 @@ struct ceph_file_layout g_default_file_layout = {
 
 #define TYCHECK(x, ty) STATIC_ASSERT(sizeof(x) == sizeof(ty))
 
-#define OPTION_OPT_STR(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, std::string), \
-         NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), def_val, 0, 0, type }
-
-#define OPTION_OPT_ADDR(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, entity_addr_t), \
-        NULL, STRINGIFY(name), \
-        offsetof(struct md_config_t, name), def_val, 0, 0, type }
-
-#define OPTION_OPT_LONGLONG(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, long long), \
-        NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), 0, def_val, 0, type }
-
-#define OPTION_OPT_INT(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, int), \
-        NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), 0, def_val, 0, type }
-
-#define OPTION_OPT_BOOL(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, bool), \
-        NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), 0, def_val, 0, type }
-
-#define OPTION_OPT_U32(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, uint32_t), \
-        NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), 0, def_val, 0, type }
-
-#define OPTION_OPT_U64(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, uint64_t), \
-        NULL, STRINGIFY(name), \
-         offsetof(struct md_config_t, name), 0, def_val, 0, type }
-
-#define OPTION_OPT_DOUBLE(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, double), \
-        NULL, STRINGIFY(name), \
-        offsetof(struct md_config_t, name), 0, 0, def_val, type }
-
-#define OPTION_OPT_FLOAT(section, name, type, def_val) \
-       { STRINGIFY(section) + TYCHECK(g_conf.name, float), \
-        NULL, STRINGIFY(name), \
-        offsetof(struct md_config_t, name), 0, 0, def_val, type }
-
-#define OPTION(name, type, def_val) OPTION_##type("global", name, type, def_val)
+#define OPTION_OPT_STR(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, std::string), \
+         offsetof(struct md_config_t, name), def_val, 0, 0, OPT_STR }
+
+#define OPTION_OPT_ADDR(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, entity_addr_t), \
+        offsetof(struct md_config_t, name), def_val, 0, 0, OPT_ADDR }
+
+#define OPTION_OPT_LONGLONG(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, long long), \
+         offsetof(struct md_config_t, name), 0, def_val, 0, OPT_LONGLONG }
+
+#define OPTION_OPT_INT(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, int), \
+         offsetof(struct md_config_t, name), 0, def_val, 0, OPT_INT }
+
+#define OPTION_OPT_BOOL(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, bool), \
+         offsetof(struct md_config_t, name), 0, def_val, 0, OPT_BOOL }
+
+#define OPTION_OPT_U32(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, uint32_t), \
+         offsetof(struct md_config_t, name), 0, def_val, 0, OPT_U32 }
+
+#define OPTION_OPT_U64(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, uint64_t), \
+         offsetof(struct md_config_t, name), 0, def_val, 0, OPT_U64 }
+
+#define OPTION_OPT_DOUBLE(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, double), \
+        offsetof(struct md_config_t, name), 0, 0, def_val, OPT_DOUBLE }
+
+#define OPTION_OPT_FLOAT(name, def_val) \
+       { STRINGIFY(name) + TYCHECK(g_conf.name, float), \
+        offsetof(struct md_config_t, name), 0, 0, def_val, OPT_FLOAT }
+
+#define OPTION(name, type, def_val) OPTION_##type(name, def_val)
 
 void *config_option::conf_ptr(md_config_t *conf) const
 {
@@ -423,46 +414,6 @@ struct config_option config_optionsp[] = {
 
 const int NUM_CONFIG_OPTIONS = sizeof(config_optionsp) / sizeof(config_option);
 
-static void set_conf_name(config_option *opt)
-{
-  char *newsection = (char *)opt->section;
-  char *newconf = (char *)opt->name;
-  int i;
-
-  if (opt->section[0] == 0) {
-    newsection = strdup("global");
-  }
-
-  if (strncmp(newsection, opt->name, strlen(newsection)) == 0) {
-    /* if key starts with the name of the section, remove name of the section
-       unless key equals to it */
-
-    if (strcmp(newsection, opt->name) == 0)
-      goto done;
-
-    newconf = strdup(&opt->name[strlen(newsection)+1]);
-  } else {
-    newconf = strdup(opt->name);
-  }
-
-  i = 0;
-  while (newconf[i]) {
-    if (newconf[i] == '_')
-      newconf[i] = ' ';
-
-    ++i;
-  }
-
-  done:
-    opt->section = newsection;
-    opt->conf_name = (const char *)newconf;
-}
-
-bool is_bool_param(const char *param)
-{
-  return ((strcasecmp(param, "true")==0) || (strcasecmp(param, "false")==0));
-}
-
 bool ceph_resolve_file_search(const std::string& filename_list,
                              std::string& result)
 {
@@ -497,7 +448,6 @@ md_config_t()
   for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
     config_option *opt = config_optionsp + i;
     set_val_from_default(opt);
-    set_conf_name(opt);
   }
 }
 
@@ -528,7 +478,7 @@ parse_config_files(const std::list<std::string> &conf_files,
   for (int i = 0; i < NUM_CONFIG_OPTIONS; i++) {
     config_option *opt = &config_optionsp[i];
     std::string val;
-    int ret = get_val_from_conf_file(my_sections, opt->conf_name, val, false);
+    int ret = get_val_from_conf_file(my_sections, opt->name, val, false);
     if (ret == 0) {
       set_val_impl(val.c_str(), opt);
     }
@@ -630,11 +580,15 @@ set_val(const char *key, const char *val)
     return -EINVAL;
   if (!val)
     return -EINVAL;
+
   std::string v(val);
   expand_meta(v);
+
+  string k(ConfFile::normalize_key_name(key));
+
   for (int i = 0; i < NUM_CONFIG_OPTIONS; ++i) {
     config_option *opt = &config_optionsp[i];
-    if (strcmp(opt->conf_name, key) == 0)
+    if (strcmp(opt->name, k.c_str()) == 0)
       return set_val_impl(v.c_str(), opt);
   }
 
@@ -647,9 +601,14 @@ get_val(const char *key, char **buf, int len) const
 {
   if (!key)
     return -EINVAL;
+
+  // In key names, leading and trailing whitespace are not significant.
+  string k(ConfFile::normalize_key_name(key));
+
+  cout << "get_val(key='" << key << "'): k = '" << k << "'" << std::endl;
   for (int i = 0; i < NUM_CONFIG_OPTIONS; ++i) {
     const config_option *opt = &config_optionsp[i];
-    if (strcmp(opt->conf_name, key))
+    if (strcmp(opt->name, k.c_str()))
       continue;
 
     ostringstream oss;
@@ -695,7 +654,7 @@ get_val(const char *key, char **buf, int len) const
     snprintf(*buf, len, "%s", str.c_str());
     return (l > len) ? -ENAMETOOLONG : 0;
   }
-  // couldn't find a configuration option with key 'key'
+  // couldn't find a configuration option with key 'k'
   return -ENOENT;
 }
 
@@ -801,7 +760,7 @@ set_val_from_default(const config_option *opt)
       entity_addr_t *addr = (entity_addr_t*)opt->conf_ptr(this);
       if (!addr->parse(opt->def_str)) {
        ostringstream oss;
-       oss << "Default value for " << opt->conf_name << " cannot be parsed."
+       oss << "Default value for " << opt->name << " cannot be parsed."
            << std::endl;
        assert(oss.str() == 0);
       }
index fae40acb66d34f18bf3e3257c8dffdf6f8e99ba3..282fa9a947750a81c12835c708abe5926f8c17e5 100644 (file)
@@ -486,8 +486,6 @@ bool ceph_resolve_file_search(const std::string& filename_list,
                              std::string& result);
 
 struct config_option {
-  const char *section;
-  const char *conf_name;
   const char *name;
   size_t md_conf_off;