From fe45265fcd7c74809fbeb8dd4b060f94f7bdfd88 Mon Sep 17 00:00:00 2001 From: Augusto Domingos Date: Sat, 19 Sep 2026 17:53:15 +0200 Subject: [PATCH] preserve native paths when creating directories --- src/common/file.cpp | 75 +++++++++++++-------------------- tests/KernelFileSystemTests.cpp | 11 ++--- 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/common/file.cpp b/src/common/file.cpp index 90bc6db6..4dfec8af 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -275,63 +275,48 @@ bool File::DeleteDirectory(const std::filesystem::path& path) { } bool File::CreateDirectories(const std::filesystem::path& path) { - std::string real_path = Common::ReplaceChar(PathToGenericString(path), '\\', '/'); + const auto normalized_path = + WithoutTrailingSeparator(path); - std::vector list = Common::Split(real_path, "/"); + auto current_path = normalized_path.root_path(); - std::string p; + for (const auto& component: + normalized_path.relative_path()) { + current_path /= component; - for (uint32_t si = 0; si < list.size(); si++) { - const std::string& s = list[si]; + if (IsDirectoryExisting(current_path)) { + continue; + } - if (si != 0 || real_path.starts_with("/")) { - p += "/"; - } + if (!CreateDirectory(current_path)) { + return false; + } + } - p += s; - - if (IsDirectoryExisting(p)) { - continue; - } - - if (!CreateDirectory(p)) // @suppress("Invalid arguments") - { - return false; - } - } - - return true; + return true; } bool File::DeleteDirectories(const std::filesystem::path& path) { - std::string real_path = Common::ReplaceChar(PathToGenericString(path), '\\', '/'); + const auto normalized_path = + WithoutTrailingSeparator(path); - std::vector list = Common::Split(real_path, "/"); + std::vector directories; + auto current_path = normalized_path.root_path(); - std::string p; - std::vector list2; + for (const auto& component: + normalized_path.relative_path()) { + current_path /= component; + directories.push_back(current_path); + } - for (uint32_t si = 0; si < list.size(); si++) { - const std::string& s = list[si]; + for (auto it = directories.rbegin(); + it != directories.rend(); ++it) { + if (!DeleteDirectory(*it)) { + return false; + } + } - if (si != 0 || real_path.starts_with("/")) { - p += "/"; - } - - p += s; - - list2.push_back(p); - } - - uint32_t num = list2.size(); - - for (uint32_t si = num - 1; si < num; si--) { - if (!DeleteDirectory(list2[si])) { - return false; - } - } - - return true; + return true; } bool File::DeleteFile( diff --git a/tests/KernelFileSystemTests.cpp b/tests/KernelFileSystemTests.cpp index 9b4bf142..b7cf5a26 100644 --- a/tests/KernelFileSystemTests.cpp +++ b/tests/KernelFileSystemTests.cpp @@ -137,20 +137,21 @@ void CheckUnicodePaths(const std::filesystem::path &root) { const auto unicode_root = root / Common::PathFromUtf8(HostDirectory); + const auto nested_root = unicode_root / "nested"; - Check(std::filesystem::create_directories(unicode_root), - "create Unicode host directory"); + Check(Common::File::CreateDirectories(nested_root), + "create nested Unicode host directory"); - CheckMountRoot(unicode_root); + CheckMountRoot(nested_root); const auto native_file = - unicode_root / Common::PathFromUtf8(GuestFilename); + nested_root / Common::PathFromUtf8(GuestFilename); Common::File fixture; Check(fixture.Create(native_file), "create Unicode filename"); fixture.Close(); - FileSystem::Mount(unicode_root, "/app0"); + FileSystem::Mount(nested_root, "/app0"); const auto guest_file = std::string("/app0/") + std::string(GuestFilename);