代码拉取完成,页面将自动刷新
From 8daafbc09ac2653a63c5a381e17e844207ea96c6 Mon Sep 17 00:00:00 2001
From: Robert Altnoeder <robert.altnoeder@linbit.com>
Date: Tue, 19 Mar 2024 16:00:22 +0100
Subject: [PATCH 082/100] DRBDmon: SystemApi methods update
- Modify node name query
- Add file name extraction from path
- Mark suitable methods const
---
user/drbdmon/platform/Linux/SystemApiImpl.cpp | 21 +++++++++++++---
user/drbdmon/platform/Linux/SystemApiImpl.h | 3 ++-
user/drbdmon/platform/NT/SystemApiImpl.cpp | 25 +++++++++++++++----
user/drbdmon/platform/NT/SystemApiImpl.h | 3 ++-
user/drbdmon/platform/SystemApi.h | 5 ++--
5 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/user/drbdmon/platform/Linux/SystemApiImpl.cpp b/user/drbdmon/platform/Linux/SystemApiImpl.cpp
index 85a31f3a..5017068c 100644
--- a/user/drbdmon/platform/Linux/SystemApiImpl.cpp
+++ b/user/drbdmon/platform/Linux/SystemApiImpl.cpp
@@ -48,7 +48,7 @@ std::unique_ptr<TerminalControl> LinuxApi::create_terminal_control()
return std::unique_ptr<TerminalControl>(dynamic_cast<TerminalControl*> (new TerminalControlImpl()));
}
-bool LinuxApi::is_file_accessible(const char* const file_path)
+bool LinuxApi::is_file_accessible(const char* const file_path) const
{
struct stat file_info;
std::memset(&file_info, 0, sizeof (file_info));
@@ -73,6 +73,21 @@ std::string LinuxApi::get_config_file_path()
return path;
}
+std::string LinuxApi::file_name_for_path(const std::string path) const
+{
+ std::string file_name;
+ const size_t split_idx = path.rfind('/');
+ if (split_idx == std::string::npos)
+ {
+ file_name = path;
+ }
+ else
+ {
+ file_name = path.substr(split_idx + 1);
+ }
+ return file_name;
+}
+
namespace system_api
{
std::unique_ptr<SystemApi> create_system_api()
@@ -192,13 +207,13 @@ namespace system_api
return ids_safe;
}
- void init_node_name(std::unique_ptr<std::string>& node_name_mgr)
+ void init_node_name(std::string& node_name)
{
std::unique_ptr<struct utsname> uname_buffer(new struct utsname);
int rc = uname(uname_buffer.get());
if (rc == 0)
{
- node_name_mgr = std::unique_ptr<std::string>(new std::string(uname_buffer->nodename));
+ node_name = uname_buffer->nodename;
}
}
}
diff --git a/user/drbdmon/platform/Linux/SystemApiImpl.h b/user/drbdmon/platform/Linux/SystemApiImpl.h
index 9bc6a8b0..c476c2e7 100644
--- a/user/drbdmon/platform/Linux/SystemApiImpl.h
+++ b/user/drbdmon/platform/Linux/SystemApiImpl.h
@@ -22,7 +22,8 @@ class LinuxApi : public SystemApi
virtual std::unique_ptr<SubProcess> create_subprocess_handler() override;
virtual std::unique_ptr<TerminalControl> create_terminal_control() override;
virtual std::string get_config_file_path() override;
- virtual bool is_file_accessible(const char* const file_path) override;
+ virtual std::string file_name_for_path(const std::string path) const override;
+ virtual bool is_file_accessible(const char* const file_path) const override;
private:
static const char* const CONFIG_FILE_NAME;
diff --git a/user/drbdmon/platform/NT/SystemApiImpl.cpp b/user/drbdmon/platform/NT/SystemApiImpl.cpp
index 2e9ad12e..d39d9b0f 100644
--- a/user/drbdmon/platform/NT/SystemApiImpl.cpp
+++ b/user/drbdmon/platform/NT/SystemApiImpl.cpp
@@ -51,7 +51,22 @@ std::string NtApi::get_config_file_path()
return path;
}
-bool NtApi::is_file_accessible(const char* const file_path)
+std::string NtApi::file_name_for_path(const std::string path) const
+{
+ std::string file_name;
+ const size_t split_idx = path.rfind('\\');
+ if (split_idx == std::string::npos)
+ {
+ file_name = path;
+ }
+ else
+ {
+ file_name = path.substr(split_idx + 1);
+ }
+ return file_name;
+}
+
+bool NtApi::is_file_accessible(const char* const file_path) const
{
return PathFileExistsA(file_path) != 0;
}
@@ -72,8 +87,9 @@ namespace system_api
return true;
}
- void init_node_name(std::unique_ptr<std::string>& node_name_mgr)
+ void init_node_name(std::string& node_name)
{
+ node_name.clear();
try
{
std::unique_ptr<char[]> name_buffer(new char[MAX_COMPUTER_NAME_LENGTH]);
@@ -83,14 +99,13 @@ namespace system_api
{
if (length >= 1 && length < MAX_COMPUTER_NAME_LENGTH)
{
- node_name_mgr = std::unique_ptr<std::string>(new std::string());
- node_name_mgr->append(name_buffer.get(), length);
+ node_name->append(name_buffer.get(), length);
}
}
}
catch (std::bad_alloc&)
{
- node_name_mgr = nullptr;
+ // no-op
}
}
}
diff --git a/user/drbdmon/platform/NT/SystemApiImpl.h b/user/drbdmon/platform/NT/SystemApiImpl.h
index e28d6a1b..251f1b48 100644
--- a/user/drbdmon/platform/NT/SystemApiImpl.h
+++ b/user/drbdmon/platform/NT/SystemApiImpl.h
@@ -13,7 +13,8 @@ class NtApi : public SystemApi
virtual std::unique_ptr<SubProcess> create_subprocess_handler() override;
virtual std::unique_ptr<TerminalControl> create_terminal_control() override;
virtual std::string get_config_file_path() override;
- virtual bool is_file_accessible(const char* const file_path) override;
+ virtual std::string file_name_for_path(const std::string path) const override;
+ virtual bool is_file_accessible(const char* const file_path) const override;
private:
static const char* const CONFIG_FILE_NAME;
diff --git a/user/drbdmon/platform/SystemApi.h b/user/drbdmon/platform/SystemApi.h
index 21414f4d..691968bd 100644
--- a/user/drbdmon/platform/SystemApi.h
+++ b/user/drbdmon/platform/SystemApi.h
@@ -26,14 +26,15 @@ class SystemApi
virtual std::unique_ptr<SubProcess> create_subprocess_handler() = 0;
virtual std::unique_ptr<TerminalControl> create_terminal_control() = 0;
virtual std::string get_config_file_path() = 0;
- virtual bool is_file_accessible(const char* const file_path) = 0;
+ virtual std::string file_name_for_path(const std::string path) const = 0;
+ virtual bool is_file_accessible(const char* const file_path) const = 0;
};
namespace system_api
{
std::unique_ptr<SystemApi> create_system_api();
bool init_security(MessageLog& log);
- void init_node_name(std::unique_ptr<std::string>& node_name_mgr);
+ void init_node_name(std::string& node_name);
}
#endif /* SYSTEMAPI_H */
--
2.33.1.windows.1
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。