From 0cf4b768768ce2703023ac59066b6ed79ce6f13e Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Tue, 10 Sep 2024 18:33:27 +0100 Subject: [PATCH] Common: Fix Recursive CreateDirectoryPath() on Windows --- common/FileSystem.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 5bcf03d4a3..826aee13ba 100644 --- a/common/FileSystem.cpp +++ b/common/FileSystem.cpp @@ -1732,8 +1732,26 @@ bool FileSystem::CreateDirectoryPath(const char* Path, bool Recursive, Error* er std::wstring tempPath; tempPath.reserve(pathLength); + // for absolute paths, we need to skip over the path root + size_t rootLength = 0; + if (Path::IsAbsolute(Path)) + { + const wchar_t* root_start = wpath.c_str(); + wchar_t* root_end; + const HRESULT hr = PathCchSkipRoot(const_cast(root_start), &root_end); + if (FAILED(hr)) + { + Error::SetHResult(error, "PathCchSkipRoot() failed: ", hr); + return false; + } + rootLength = static_cast(root_end - root_start); + + // copy path root + tempPath.append(wpath, 0, rootLength); + } + // create directories along the path - for (size_t i = 0; i < pathLength; i++) + for (size_t i = rootLength; i < pathLength; i++) { if (wpath[i] == L'\\' || wpath[i] == L'/') {