Skip to content

Commit

Permalink
update(libsinsp): add windows path workaround
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Dec 12, 2023
1 parent 45cdc28 commit e330772
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 1 addition & 3 deletions userspace/libsinsp/sinsp_filtercheck_fspath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
*/

#include <filesystem>
#include "sinsp_filtercheck_fspath.h"
#include "sinsp_filtercheck_event.h"
#include "sinsp_filtercheck_fd.h"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions userspace/libsinsp/test/sinsp_utils.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
24 changes: 24 additions & 0 deletions userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e330772

Please sign in to comment.