From 0cdc9292c8790da2cf5a5974a2f7a55ea24c9d68 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 4 Jun 2022 01:01:24 +0300 Subject: [PATCH] Fix true/false parsing in json configs like no_recovery/no_rebalance --- src/http_client.cpp | 18 ++++++++++++++++++ src/http_client.h | 3 +++ src/nfs_proxy.cpp | 5 +---- src/osd.cpp | 12 ++++++------ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/http_client.cpp b/src/http_client.cpp index ffd36ed4..1bd61e90 100644 --- a/src/http_client.cpp +++ b/src/http_client.cpp @@ -758,3 +758,21 @@ static std::string trim(const std::string & in) int end = in.find_last_not_of(" \n\r\t"); return in.substr(begin, end+1-begin); } + +bool json_is_true(const json11::Json & val) +{ + if (val.is_string()) + return val == "true" || val == "yes" || val == "1"; + return val.bool_value(); +} + +bool json_is_false(const json11::Json & val) +{ + if (val.is_string()) + return val.string_value() == "false" || val.string_value() == "no" || val.string_value() == "0"; + if (val.is_number()) + return val.number_value() == 0; + if (val.is_bool()) + return !val.bool_value(); + return false; +} diff --git a/src/http_client.h b/src/http_client.h index f5eb5d19..8fc11b4a 100644 --- a/src/http_client.h +++ b/src/http_client.h @@ -52,3 +52,6 @@ void http_close(http_co_t *co); // Utils uint64_t stoull_full(const std::string & str, int base = 10); std::string strtolower(const std::string & in); +// FIXME: move to json11 +bool json_is_true(const json11::Json & val); +bool json_is_false(const json11::Json & val); diff --git a/src/nfs_proxy.cpp b/src/nfs_proxy.cpp index deaaa559..0da1fa67 100644 --- a/src/nfs_proxy.cpp +++ b/src/nfs_proxy.cpp @@ -92,10 +92,7 @@ void nfs_proxy_t::run(json11::Json cfg) if (bind_address == "") bind_address = "0.0.0.0"; default_pool = cfg["pool"].as_string(); - portmap_enabled = cfg.object_items().find("portmap") == cfg.object_items().end() || - cfg["portmap"].uint64_value() || - cfg["portmap"].string_value() == "yes" || - cfg["portmap"].string_value() == "true"; + portmap_enabled = !json_is_false(cfg["portmap"]); nfs_port = cfg["port"].uint64_value() & 0xffff; if (!nfs_port) nfs_port = 2049; diff --git a/src/osd.cpp b/src/osd.cpp index a615040b..3e6f29c1 100644 --- a/src/osd.cpp +++ b/src/osd.cpp @@ -54,7 +54,7 @@ osd_t::osd_t(const json11::Json & config, ring_loop_t *ringloop) autosync_writes = max_autosync; } - if (this->config["osd_memlock"] == "true" || this->config["osd_memlock"] == "1" || this->config["osd_memlock"] == "yes") + if (json_is_true(this->config["osd_memlock"])) { // Lock all OSD memory if requested if (mlockall(MCL_CURRENT|MCL_FUTURE @@ -127,11 +127,11 @@ void osd_t::parse_config(const json11::Json & config) etcd_report_interval = config["etcd_report_interval"].uint64_value(); if (etcd_report_interval <= 0) etcd_report_interval = 5; - readonly = config["readonly"] == "true" || config["readonly"] == "1" || config["readonly"] == "yes"; - run_primary = config["run_primary"] != "false" && config["run_primary"] != "0" && config["run_primary"] != "no"; - no_rebalance = config["no_rebalance"] == "true" || config["no_rebalance"] == "1" || config["no_rebalance"] == "yes"; - no_recovery = config["no_recovery"] == "true" || config["no_recovery"] == "1" || config["no_recovery"] == "yes"; - allow_test_ops = config["allow_test_ops"] == "true" || config["allow_test_ops"] == "1" || config["allow_test_ops"] == "yes"; + readonly = json_is_true(config["readonly"]); + run_primary = !json_is_false(config["run_primary"]); + no_rebalance = json_is_true(config["no_rebalance"]); + no_recovery = json_is_true(config["no_recovery"]); + allow_test_ops = json_is_true(config["allow_test_ops"]); if (config["immediate_commit"] == "all") immediate_commit = IMMEDIATE_ALL; else if (config["immediate_commit"] == "small")