|
6 | 6 |
|
7 | 7 | #include <filesystem> |
8 | 8 | #include <sstream> |
| 9 | +#include <stack> |
9 | 10 | #include <windows.h> |
10 | 11 | #include <winrt/Windows.Storage.FileProperties.h> |
11 | 12 | #include <winrt/Windows.Storage.Streams.h> |
@@ -189,16 +190,39 @@ try |
189 | 190 | size_t pathLength{ directory.length() }; |
190 | 191 |
|
191 | 192 | if (pathLength <= 0) { |
192 | | - promise.Reject("Invalid path."); |
| 193 | + promise.Reject("Invalid path length"); |
193 | 194 | } |
194 | 195 | else { |
195 | 196 | bool hasTrailingSlash{ directory[pathLength - 1] == '\\' || directory[pathLength - 1] == '/' }; |
196 | 197 | std::filesystem::path path(hasTrailingSlash ? directory.substr(0, pathLength - 1) : directory); |
197 | 198 | path.make_preferred(); |
198 | 199 |
|
199 | | - StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync(path.parent_path().wstring()) }; |
200 | | - co_await folder.CreateFolderAsync(path.filename().wstring(), CreationCollisionOption::FailIfExists); |
| 200 | + auto parentPath{ path.parent_path().wstring() }; |
| 201 | + std::stack<std::wstring> directoriesToMake; |
| 202 | + directoriesToMake.push(path.filename().wstring()); |
201 | 203 |
|
| 204 | + StorageFolder folder{ nullptr }; |
| 205 | + while (folder == nullptr) { |
| 206 | + try { |
| 207 | + folder = co_await StorageFolder::GetFolderFromPathAsync(parentPath); |
| 208 | + } |
| 209 | + catch (const hresult_error& ex) { |
| 210 | + hresult result{ ex.code() }; |
| 211 | + if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) { |
| 212 | + auto index{ parentPath.find_last_of('\\') }; |
| 213 | + directoriesToMake.push(parentPath.substr(index + 1)); |
| 214 | + parentPath = parentPath.substr(0, index); |
| 215 | + } |
| 216 | + else { |
| 217 | + promise.Reject(winrt::to_string(ex.message()).c_str()); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + while (!directoriesToMake.empty()) { |
| 223 | + folder = co_await folder.CreateFolderAsync(directoriesToMake.top(), CreationCollisionOption::OpenIfExists); |
| 224 | + directoriesToMake.pop(); |
| 225 | + } |
202 | 226 | promise.Resolve(); |
203 | 227 | } |
204 | 228 | } |
|
0 commit comments