From 0b924c5c23a89760ddecf8d8f6baa19333f9b667 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Tue, 30 May 2023 17:57:21 +0200 Subject: filesystem: Add logic for handling (non-upwards) symlinks --- src/utils/cpp/path.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/utils/cpp/path.hpp') diff --git a/src/utils/cpp/path.hpp b/src/utils/cpp/path.hpp index d872535e..a9fb08e5 100644 --- a/src/utils/cpp/path.hpp +++ b/src/utils/cpp/path.hpp @@ -16,6 +16,7 @@ #define INCLUDED_SRC_UTILS_CPP_PATH_HPP #include +#include [[nodiscard]] static inline auto ToNormalPath(std::filesystem::path const& p) -> std::filesystem::path { @@ -29,4 +30,16 @@ return n; } +/// \brief Perform a non-upwards condition check on the given path. +/// A path is non-upwards if it is relative and it never references any other +/// path on a higher level in the directory tree than itself. +[[nodiscard]] static auto PathIsNonUpwards( + std::filesystem::path const& path) noexcept -> bool { + if (path.is_absolute()) { + return false; + } + // check non-upwards condition + return *path.lexically_normal().begin() != ".."; +} + #endif -- cgit v1.2.3