From e330772dfb182bd00605c45875ea97b6455728d0 Mon Sep 17 00:00:00 2001 From: Luca Guerra Date: Tue, 12 Dec 2023 11:48:43 +0000 Subject: [PATCH] update(libsinsp): add windows path workaround Signed-off-by: Luca Guerra --- .../libsinsp/sinsp_filtercheck_fspath.cpp | 4 +--- userspace/libsinsp/test/sinsp_utils.ut.cpp | 5 ++++ userspace/libsinsp/utils.cpp | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/userspace/libsinsp/sinsp_filtercheck_fspath.cpp b/userspace/libsinsp/sinsp_filtercheck_fspath.cpp index 91003a82db..d2da9aefbe 100644 --- a/userspace/libsinsp/sinsp_filtercheck_fspath.cpp +++ b/userspace/libsinsp/sinsp_filtercheck_fspath.cpp @@ -16,7 +16,6 @@ limitations under the License. */ -#include #include "sinsp_filtercheck_fspath.h" #include "sinsp_filtercheck_event.h" #include "sinsp_filtercheck_fd.h" @@ -350,8 +349,7 @@ uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, b return NULL; } - std::filesystem::path tstr = tinfo->get_cwd() + m_tstr; - m_tstr = std::filesystem::absolute(tstr).lexically_normal().string(); + m_tstr = sinsp_utils::concatenate_paths(tinfo->get_cwd(), m_tstr); } // If m_tstr ends in a c-style \0, remove it to be diff --git a/userspace/libsinsp/test/sinsp_utils.ut.cpp b/userspace/libsinsp/test/sinsp_utils.ut.cpp index 730ddfca8f..2f7ff5ea76 100644 --- a/userspace/libsinsp/test/sinsp_utils.ut.cpp +++ b/userspace/libsinsp/test/sinsp_utils.ut.cpp @@ -138,4 +138,9 @@ TEST(sinsp_utils_test, concatenate_paths) path2 = "../АБВЙЛж"; res = sinsp_utils::concatenate_paths(path1, path2); EXPECT_EQ("/АБВЙЛж", res); + + path1 = "/root"; + path2 = "c:/hello/world/"; + res = sinsp_utils::concatenate_paths(path1, path2); + EXPECT_EQ("/root/c:/hello/world", res); } diff --git a/userspace/libsinsp/utils.cpp b/userspace/libsinsp/utils.cpp index 2b92c88ed8..aa9075c533 100644 --- a/userspace/libsinsp/utils.cpp +++ b/userspace/libsinsp/utils.cpp @@ -596,11 +596,35 @@ bool sinsp_utils::sockinfo_to_str(sinsp_sockinfo* sinfo, scap_fd_type stype, cha return true; } +std::filesystem::path workaround_win_root_name(std::filesystem::path p) +{ + if (!p.has_root_name()) + { + return p; + } + + if (p.root_name().string().rfind("//", 0) == 0) + { + // this is something like //dir/hello. Add a leading slash to identify an absolute path rooted at / + return std::filesystem::path("/" + p.string()); + } + + // last case: this is a relative path, like c:/dir/hello. Add a leading ./ to identify a relative path + return std::filesystem::path("./" + p.string()); +} + std::string sinsp_utils::concatenate_paths(std::string_view path1, std::string_view path2, size_t max_len) { auto p1 = std::filesystem::path(path1, std::filesystem::path::format::generic_format); auto p2 = std::filesystem::path(path2, std::filesystem::path::format::generic_format); +#ifdef _WIN32 + // This is an ugly workaround to make sure we will not try to interpret root names (e.g. "c:/", "//?/") on Windows + // since this function only deals with unix-like paths + p1 = workaround_win_root_name(p1); + p2 = workaround_win_root_name(p2); +#endif // _WIN32 + // note: if p2 happens to be an absolute path, p1 / p2 == p2 auto path_concat = (p1 / p2).lexically_normal(); std::string result = path_concat.generic_string();