From: Kefu Chai Date: Fri, 7 Aug 2020 08:53:02 +0000 (+0800) Subject: common/config: extract md_config_t::parse_buffer() X-Git-Tag: v16.1.0~1468^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e7dcc403b20922ffeeca114899f90a385bf25f0e;p=ceph.git common/config: extract md_config_t::parse_buffer() so we can reuse it in crimson. Signed-off-by: Kefu Chai --- diff --git a/src/common/config.cc b/src/common/config.cc index 2a5eeed0f913..04e5c54fc57c 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -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 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'; } } } diff --git a/src/common/config.h b/src/common/config.h index a3f54c0b65c2..e71425292b44 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -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 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; diff --git a/src/test/cli/ceph-conf/env-vs-args.t b/src/test/cli/ceph-conf/env-vs-args.t index 3e7f6edb6ffa..edb42ad6d064 100644 --- a/src/test/cli/ceph-conf/env-vs-args.t +++ b/src/test/cli/ceph-conf/env-vs-args.t @@ -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