]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/config: extract md_config_t::parse_buffer()
authorKefu Chai <kchai@redhat.com>
Fri, 7 Aug 2020 08:53:02 +0000 (16:53 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 10 Aug 2020 14:51:17 +0000 (22:51 +0800)
so we can reuse it in crimson.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/config.cc
src/common/config.h
src/test/cli/ceph-conf/env-vs-args.t

index 2a5eeed0f91369f0d9bdd8a996edb4ecc5ed75a5..04e5c54fc57c14ea9f80499bc7c45ce5b74e6a40 100644 (file)
@@ -352,7 +352,6 @@ int md_config_t::parse_config_files(ConfigValues& values,
                                    std::ostream *warnings,
                                    int flags)
 {
-
   if (safe_to_start_threads)
     return -ENOSYS;
 
@@ -362,42 +361,59 @@ int md_config_t::parse_config_files(ConfigValues& values,
   // open new conf
   string conffile;
   for (auto& fn : get_conffile_paths(values, conf_files_str, warnings, flags)) {
+    bufferlist bl;
+    std::string error;
+    if (bl.read_file(fn.c_str(), &error)) {
+      parse_error = error;
+      continue;
+    }
     ostringstream oss;
-    int ret = cf.parse_file(fn.c_str(), &oss);
+    int ret = parse_buffer(values, tracker, bl.c_str(), bl.length(), &oss);
     if (ret == 0) {
       parse_error.clear();
       conffile = fn;
       break;
-    } else {
-      parse_error = oss.str();
-      if (ret != -ENOENT) {
-       return ret;
-      }
+    }
+    parse_error = oss.str();
+    if (ret != -ENOENT) {
+      return ret;
     }
   }
   // it must have been all ENOENTs, that's the only way we got here
-  if (conffile.empty())
+  if (conffile.empty()) {
     return -ENOENT;
-
+  }
   if (values.cluster.empty()) {
     values.cluster = get_cluster_name(conffile.c_str());
   }
+  return 0;
+}
 
-  std::vector<std::string> my_sections = get_my_sections(values);
+int
+md_config_t::parse_buffer(ConfigValues& values,
+                         const ConfigTracker& tracker,
+                         const char* buf, size_t len,
+                         std::ostream* warnings)
+{
+  if (!cf.parse_buffer(string_view{buf, len}, warnings)) {
+    return -EINVAL;
+  }
+  const auto my_sections = get_my_sections(values);
   for (const auto &i : schema) {
     const auto &opt = i.second;
     std::string val;
-    int ret = _get_val_from_conf_file(my_sections, opt.name, val);
-    if (ret == 0) {
-      std::string error_message;
-      int r = _set_val(values, tracker, val, opt, CONF_FILE, &error_message);
-      if (warnings != nullptr && (r < 0 || !error_message.empty())) {
-        *warnings << "parse error setting '" << opt.name << "' to '" << val
-                  << "'";
+    if (_get_val_from_conf_file(my_sections, opt.name, val)) {
+      continue;
+    }
+    std::string error_message;
+    if (_set_val(values, tracker, val, opt, CONF_FILE, &error_message) < 0) {
+      if (warnings != nullptr) {
+        *warnings << "parse error setting " << std::quoted(opt.name)
+                  << " to " << std::quoted(val);
         if (!error_message.empty()) {
           *warnings << " (" << error_message << ")";
         }
-        *warnings << std::endl;
+        *warnings << '\n';
       }
     }
   }
index a3f54c0b65c2455ecb5a274476af1e2c1b749076..e71425292b444b8e8c29f262f802f4ed8ae4372c 100644 (file)
@@ -121,7 +121,9 @@ public:
   int parse_config_files(ConfigValues& values, const ConfigTracker& tracker,
                         const char *conf_files,
                         std::ostream *warnings, int flags);
-
+  int parse_buffer(ConfigValues& values, const ConfigTracker& tracker,
+                  const char* buf, size_t len,
+                  std::ostream *warnings);
   // Absorb config settings from the environment
   void parse_env(unsigned entity_type,
                 ConfigValues& values, const ConfigTracker& tracker,
@@ -312,11 +314,12 @@ public:  // for global_init
   // for those want to reexpand special meta, e.g, $pid
   bool finalize_reexpand_meta(ConfigValues& values,
                              const ConfigTracker& tracker);
-private:
+
   std::list<std::string> get_conffile_paths(const ConfigValues& values,
                                            const char *conf_files,
                                            std::ostream *warnings,
                                            int flags) const;
+private:
   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;
index 3e7f6edb6ffa65aaa5493d6fd9fd3f44a54bbc90..edb42ad6d064fc57bef8da6947388d0ec748961a 100644 (file)
@@ -2,9 +2,9 @@
   $ env CEPH_CONF=from-env ceph-conf -s foo bar
   did not load config file, using default settings.
   .* \-1 Errors while parsing config file! (re)
-  .* \-1 parse_file: filesystem error: .* file.size: (No such file or directory )?\[from-env\] (re)
+  .* \-1 can't open from-env: \(2\) (No such file or directory)? (re)
   .* \-1 Errors while parsing config file! (re)
-  .* \-1 parse_file: filesystem error: .* file.size: (No such file or directory )?\[from-env\] (re)
+  .* \-1 can't open from-env: \(2\) (No such file or directory)? (re)
   [1]
 
 # command-line arguments should override environment