From b9225dfbc36ae56c7b967e5bf78b20f5c3e44965 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 5 Dec 2019 15:34:12 +0800 Subject: [PATCH] common/pick_address.cc: silence GCC warning MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * use `string_view` instead of string * use `auto` instead of `int` to silence the warning * use `enum class` instead of plain `enum` * do not specify the value of enum, as we don't care about it this change silences following warning ``` ../src/common/pick_address.cc: In function ‘int get_iface_numa_node(const string&, int*)’: ../src/common/pick_address.cc:517:11: warning: comparison of integer expressions of different signedness: ‘int’ and ‘const size_type’ {aka ‘const long unsigned int’} [-Wsign-com\ pare] 517 | if (pos != string::npos ) { | ~~~~^~~~~~~~~~~~~~~ ``` Signed-off-by: Kefu Chai --- src/CMakeLists.txt | 1 + src/common/pick_address.cc | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 83cb101da77..8b363766b90 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -387,6 +387,7 @@ set(ceph_common_deps Boost::date_time Boost::iostreams StdFilesystem::filesystem + fmt::fmt ${BLKID_LIBRARIES} ${Backtrace_LIBRARIES} ${BLKIN_LIBRARIES} diff --git a/src/common/pick_address.cc b/src/common/pick_address.cc index 7c38de659c8..ec464c1b8e1 100644 --- a/src/common/pick_address.cc +++ b/src/common/pick_address.cc @@ -13,6 +13,14 @@ */ #include "common/pick_address.h" + +#include +#include +#include +#include + +#include + #include "include/ipaddr.h" #include "include/str_list.h" #include "common/ceph_context.h" @@ -24,11 +32,6 @@ #include "common/errno.h" #include "common/numa.h" -#include -#include -#include -#include - #define dout_subsys ceph_subsys_ const struct sockaddr *find_ip_in_subnet_list( @@ -511,21 +514,23 @@ int get_iface_numa_node( const std::string& iface, int *node) { - enum { IFACE_PHY_PORT = 1, IFACE_BOND_PORT = 2} ifatype = IFACE_PHY_PORT; - string ifa = iface; - int pos = ifa.find(":"); - if (pos != string::npos) { - ifa.erase(pos); + enum class iface_t { + PHY_PORT, + BOND_PORT + } ifatype = iface_t::PHY_PORT; + string_view ifa{iface}; + if (auto pos = ifa.find(":"); pos != ifa.npos) { + ifa.remove_suffix(ifa.size() - pos); } - string fn = std::string("/sys/class/net/") + ifa + "/device/numa_node"; + string fn = fmt::format("/sys/class/net/{}/device/numa_node", ifa); int fd = ::open(fn.c_str(), O_RDONLY); if (fd < 0) { - fn = std::string("/sys/class/net/") + ifa + "/bonding/slaves"; + fn = fmt::format("/sys/class/net/{}/bonding/slaves", ifa); fd = ::open(fn.c_str(), O_RDONLY); if (fd < 0) { return -errno; } - ifatype = IFACE_BOND_PORT; + ifatype = iface_t::BOND_PORT; } int r = 0; @@ -541,7 +546,7 @@ int get_iface_numa_node( } switch (ifatype) { - case IFACE_PHY_PORT: + case iface_t::PHY_PORT: *node = strtoll(buf, &endptr, 10); if (endptr != buf + strlen(buf)) { r = -EINVAL; @@ -549,7 +554,7 @@ int get_iface_numa_node( } r = 0; break; - case IFACE_BOND_PORT: + case iface_t::BOND_PORT: int bond_node = -1; std::vector sv; std::string ifacestr = buf; -- 2.39.5