From 8ad36e2526600541dd1e61463799f302a7a7f0a5 Mon Sep 17 00:00:00 2001 From: Eladash Date: Sat, 26 Sep 2020 21:18:32 +0300 Subject: [PATCH] Fix fs::delim type, fix "." and ".." processing in fs::get_parent_dir (#8975) * Fix fs::delim type * Fix fs::get_parent_dir: "." and ".." processing --- Utilities/File.cpp | 16 ++++------------ Utilities/File.h | 8 ++++---- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index f2612c9c2c..ca15bfac6b 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -322,20 +322,12 @@ std::shared_ptr fs::set_virtual_device(const std::string& name, std::string fs::get_parent_dir(const std::string& path) { - // Search upper bound (set to the last character, npos for empty string) - auto last = path.size() - 1; + // Get (basically) processed path + const auto real_path = fs::escape_path(path); - while (true) - { - const auto pos = path.find_last_of(delim, last, sizeof(delim) - 1); + const auto pos = real_path.find_last_of(delim); - // Contiguous slashes are ignored at the end - if (std::exchange(last, pos - 1) != pos) - { - // Return empty string if the path doesn't contain at least 2 elements - return path.substr(0, pos != umax && path.find_last_not_of(delim, pos, sizeof(delim) - 1) != umax ? pos : 0); - } - } + return real_path.substr(0, pos == umax ? 0 : pos); } bool fs::stat(const std::string& path, stat_t& info) diff --git a/Utilities/File.h b/Utilities/File.h index f297db1212..cdaa1085af 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -11,12 +11,12 @@ namespace fs { #ifdef _WIN32 - static constexpr auto delim = "/\\"; - static constexpr auto wdelim = L"/\\"; + static constexpr auto& delim = "/\\"; + static constexpr auto& wdelim = L"/\\"; using native_handle = void*; #else - static constexpr auto delim = "/"; - static constexpr auto wdelim = L"/"; + static constexpr auto& delim = "/"; + static constexpr auto& wdelim = L"/"; using native_handle = int; #endif