Skip to content

Commit f7b05de

Browse files
authored
Merge pull request #6 from avmoroz/dev
Update mkdir()
2 parents 9e1d2fc + 8ca8b26 commit f7b05de

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

windows/RNFS/RNFSManager.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <filesystem>
88
#include <sstream>
9+
#include <stack>
910
#include <windows.h>
1011
#include <winrt/Windows.Storage.FileProperties.h>
1112
#include <winrt/Windows.Storage.Streams.h>
@@ -189,16 +190,39 @@ try
189190
size_t pathLength{ directory.length() };
190191

191192
if (pathLength <= 0) {
192-
promise.Reject("Invalid path.");
193+
promise.Reject("Invalid path length");
193194
}
194195
else {
195196
bool hasTrailingSlash{ directory[pathLength - 1] == '\\' || directory[pathLength - 1] == '/' };
196197
std::filesystem::path path(hasTrailingSlash ? directory.substr(0, pathLength - 1) : directory);
197198
path.make_preferred();
198199

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());
201203

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+
}
202226
promise.Resolve();
203227
}
204228
}

0 commit comments

Comments
 (0)