@@ -183,7 +183,7 @@ void RNFSManager::ConstantsViaConstantsProvider(RN::ReactConstantProvider& const
183183 constants.Add (L" RNFSFileTypeDirectory" , 1 );
184184}
185185
186- void RNFSManager::mkdir (std::string directory, RN::JSValueObject options, RN::ReactPromise<void > promise) noexcept
186+ winrt::fire_and_forget RNFSManager::mkdir (std::string directory, RN::JSValueObject options, RN::ReactPromise<void > promise) noexcept
187187try
188188{
189189 size_t pathLength{ directory.length () };
@@ -194,14 +194,12 @@ try
194194 else {
195195 bool hasTrailingSlash{ directory[pathLength - 1 ] == ' \\ ' || directory[pathLength - 1 ] == ' /' };
196196 std::filesystem::path path (hasTrailingSlash ? directory.substr (0 , pathLength - 1 ) : directory);
197- if (std::filesystem::create_directories (path) == false )
198- {
199- promise.Reject (" Failed to create directory. Directory may already exist." );
200- }
201- else
202- {
203- promise.Resolve ();
204- }
197+ path.make_preferred ();
198+
199+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (path.parent_path ().wstring ()) };
200+ co_await folder.CreateFolderAsync (path.filename ().wstring (), CreationCollisionOption::FailIfExists);
201+
202+ promise.Resolve ();
205203 }
206204}
207205catch (const hresult_error& ex)
@@ -265,7 +263,7 @@ winrt::fire_and_forget RNFSManager::copyFolder(
265263 RN::ReactPromise<void > promise) noexcept
266264try
267265{
268- std::filesystem::path srcPath{srcFolderPath};
266+ std::filesystem::path srcPath{ srcFolderPath };
269267 srcPath.make_preferred ();
270268 std::filesystem::path destPath{ destFolderPath };
271269 destPath.make_preferred ();
@@ -347,27 +345,27 @@ catch (const hresult_error& ex)
347345winrt::fire_and_forget RNFSManager::unlink (std::string filepath, RN::ReactPromise<void > promise) noexcept
348346try
349347{
350- if (std::filesystem::is_directory (filepath))
351- {
352- std::filesystem::path path (filepath);
353- path.make_preferred ();
354- StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (winrt::to_hstring (path.c_str ())) };
355- co_await folder.DeleteAsync ();
348+ size_t pathLength{ filepath.length () };
349+
350+ if (pathLength <= 0 ) {
351+ promise.Reject (" Invalid path." );
356352 }
357- else
358- {
359- winrt::hstring directoryPath, fileName;
360- splitPath (filepath, directoryPath, fileName);
361- StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
362- auto target{ co_await folder.GetItemAsync (fileName) };
353+ else {
354+ bool hasTrailingSlash{ filepath[pathLength - 1 ] == ' \\ ' || filepath[pathLength - 1 ] == ' /' };
355+ std::filesystem::path path (hasTrailingSlash ? filepath.substr (0 , pathLength - 1 ) : filepath);
356+ path.make_preferred ();
357+
358+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (path.parent_path ().wstring ()) };
359+ auto target{ co_await folder.GetItemAsync (path.filename ().wstring ()) };
363360 co_await target.DeleteAsync ();
361+
362+ promise.Resolve ();
364363 }
365- promise.Resolve ();
366364}
367365catch (const hresult_error& ex)
368366{
369367 hresult result{ ex.code () };
370- if (result == 0x80070002 ) // FileNotFoundException
368+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
371369 {
372370 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
373371 }
@@ -379,7 +377,7 @@ catch (const hresult_error& ex)
379377}
380378
381379
382- void RNFSManager::exists (std::string filepath, RN::ReactPromise<bool > promise) noexcept
380+ winrt::fire_and_forget RNFSManager::exists (std::string filepath, RN::ReactPromise<bool > promise) noexcept
383381try
384382{
385383 size_t fileLength{ filepath.length () };
@@ -390,11 +388,22 @@ try
390388 else {
391389 bool hasTrailingSlash{ filepath[fileLength - 1 ] == ' \\ ' || filepath[fileLength - 1 ] == ' /' };
392390 std::filesystem::path path (hasTrailingSlash ? filepath.substr (0 , fileLength - 1 ) : filepath);
393- promise.Resolve (std::filesystem::exists (path));
391+
392+ winrt::hstring directoryPath, fileName;
393+ splitPath (filepath, directoryPath, fileName);
394+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
395+ if (fileName.size () > 0 ) {
396+ co_await folder.GetItemAsync (fileName);
397+ }
398+ promise.Resolve (true );
394399 }
395400}
396401catch (const hresult_error& ex)
397402{
403+ hresult result{ ex.code () };
404+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND)) {
405+ promise.Resolve (false );
406+ }
398407 // "Failed to check if file or directory exists.
399408 promise.Reject (winrt::to_string (ex.message ()).c_str ());
400409}
@@ -428,11 +437,11 @@ try
428437catch (const hresult_error& ex)
429438{
430439 hresult result{ ex.code () };
431- if (result == 0x80070002 ) // FileNotFoundException
440+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
432441 {
433442 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
434443 }
435- else if (result == 0x80070005 ) // UnauthorizedAccessException
444+ else if (result == HRESULT_FROM_WIN32 (E_ACCESSDENIED) ) // UnauthorizedAccessException
436445 {
437446 promise.Reject (RN::ReactError{ " EISDIR" , " EISDIR: illegal operation on a directory, read" });
438447 }
@@ -447,29 +456,27 @@ catch (const hresult_error& ex)
447456winrt::fire_and_forget RNFSManager::stat (std::string filepath, RN::ReactPromise<RN::JSValueObject> promise) noexcept
448457try
449458{
450- std::filesystem::path path (filepath);
451- path.make_preferred ();
452-
453- std::string preliminaryPath{ winrt::to_string (path.c_str ()) };
454- bool isDirectory{ std::filesystem::is_directory (preliminaryPath) };
455- auto resultPath{ winrt::to_hstring (preliminaryPath) };
459+ size_t pathLength{ filepath.length () };
456460
457- IStorageItem item;
458- if (isDirectory) {
459- item = co_await StorageFolder::GetFolderFromPathAsync (resultPath);
461+ if (pathLength <= 0 ) {
462+ promise.Reject (" Invalid path." );
460463 }
461464 else {
462- item = co_await StorageFile::GetFileFromPathAsync (resultPath);
463- }
465+ bool hasTrailingSlash{ filepath[pathLength - 1 ] == ' \\ ' || filepath[pathLength - 1 ] == ' /' };
466+ std::filesystem::path path (hasTrailingSlash ? filepath.substr (0 , pathLength - 1 ) : filepath);
467+ path.make_preferred ();
464468
465- auto properties{ co_await item.GetBasicPropertiesAsync () };
466- RN::JSValueObject fileInfo;
467- fileInfo[" ctime" ] = winrt::clock::to_time_t (item.DateCreated ());
468- fileInfo[" mtime" ] = winrt::clock::to_time_t (properties.DateModified ());
469- fileInfo[" size" ] = std::to_string (properties.Size ());
470- fileInfo[" type" ] = isDirectory ? 1 : 0 ;
469+ StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (path.parent_path ().wstring ()) };
470+ IStorageItem item{ co_await folder.GetItemAsync (path.filename ().wstring ()) };
471471
472- promise.Resolve (fileInfo);
472+ auto properties{ co_await item.GetBasicPropertiesAsync () };
473+ RN::JSValueObject fileInfo;
474+ fileInfo[" ctime" ] = winrt::clock::to_time_t (item.DateCreated ());
475+ fileInfo[" mtime" ] = winrt::clock::to_time_t (properties.DateModified ());
476+ fileInfo[" size" ] = std::to_string (properties.Size ());
477+ fileInfo[" type" ] = item.IsOfType (StorageItemTypes::Folder) ? 1 : 0 ;
478+ promise.Resolve (fileInfo);
479+ }
473480}
474481catch (...)
475482{
@@ -533,11 +540,11 @@ try
533540catch (const hresult_error& ex)
534541{
535542 hresult result{ ex.code () };
536- if (result == 0x80070002 ) // FileNotFoundException
543+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
537544 {
538545 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
539546 }
540- else if (result == 0x80070005 ) // UnauthorizedAccessException
547+ else if (result == HRESULT_FROM_WIN32 (E_ACCESSDENIED) ) // UnauthorizedAccessException
541548 {
542549 promise.Reject (RN::ReactError{" EISDIR" , " EISDIR: Could not open file for reading" });
543550 }
@@ -583,11 +590,11 @@ try
583590catch (const hresult_error& ex)
584591{
585592 hresult result{ ex.code () };
586- if (result == 0x80070002 ) // FileNotFoundException
593+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
587594 {
588595 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
589596 }
590- else if (result == 0x80070005 ) // UnauthorizedAccessException
597+ else if (result == HRESULT_FROM_WIN32 (E_ACCESSDENIED) ) // UnauthorizedAccessException
591598 {
592599 promise.Reject (RN::ReactError{ " EISDIR" , " EISDIR: illegal operation on a directory, read" });
593600 }
619626catch (const hresult_error& ex)
620627{
621628 hresult result{ ex.code () };
622- if (result == 0x80070002 ) // FileNotFoundException
629+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
623630 {
624631 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
625632 }
@@ -634,11 +641,15 @@ catch (const hresult_error& ex)
634641winrt::fire_and_forget RNFSManager::appendFile (std::string filepath, std::string base64Content, RN::ReactPromise<void > promise) noexcept
635642try
636643{
644+ size_t fileLength = filepath.length ();
645+ bool hasTrailingSlash{ filepath[fileLength - 1 ] == ' \\ ' || filepath[fileLength - 1 ] == ' /' };
646+ std::filesystem::path path (hasTrailingSlash ? filepath.substr (0 , fileLength - 1 ) : filepath);
647+
637648 winrt::hstring directoryPath, fileName;
638649 splitPath (filepath, directoryPath, fileName);
639650
640651 StorageFolder folder{ co_await StorageFolder::GetFolderFromPathAsync (directoryPath) };
641- StorageFile file{ co_await folder.GetFileAsync (fileName) };
652+ StorageFile file{ co_await folder.CreateFileAsync (fileName, CreationCollisionOption::OpenIfExists ) };
642653
643654 winrt::hstring base64ContentStr{ winrt::to_hstring (base64Content) };
644655 Streams::IBuffer buffer{ Cryptography::CryptographicBuffer::DecodeFromBase64String (base64ContentStr) };
652663catch (const hresult_error& ex)
653664{
654665 hresult result{ ex.code () };
655- if (result == 0x80070002 ) // FileNotFoundException
666+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
656667 {
657668 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
658669 }
689700catch (const hresult_error& ex)
690701{
691702 hresult result{ ex.code () };
692- if (result == 0x80070002 ) // FileNotFoundException
703+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
693704 {
694705 promise.Reject (RN::ReactError{ " ENOENT" , " ENOENT: no such file or directory, open " + filepath });
695706 }
821832 std::filesystem::path path (filepath);
822833 path.make_preferred ();
823834 auto s_path{ path.c_str () };
824- LPCWSTR actual_path{ s_path };
835+ PCWSTR actual_path{ s_path };
825836 DWORD accessMode{ GENERIC_READ | GENERIC_WRITE };
826837 DWORD shareMode{ FILE_SHARE_WRITE };
827838 DWORD creationMode{ OPEN_EXISTING };
869880catch (const hresult_error& ex)
870881{
871882 hresult result{ ex.code () };
872- if (result == 0x80070002 ) // FileNotFoundException
883+ if (result == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND) ) // FileNotFoundException
873884 {
874885 promise.Reject (" ENOENT: no such file." );
875886 }
@@ -918,7 +929,7 @@ IAsyncAction RNFSManager::ProcessDownloadRequestAsync(RN::ReactPromise<RN::JSVal
918929
919930 std::filesystem::path fsFilePath{ filePath };
920931
921- StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (fsFilePath.parent_path ().wstring ()) };
932+ StorageFolder storageFolder{ co_await StorageFolder::GetFolderFromPathAsync (fsFilePath.parent_path ().wstring ()) };
922933 StorageFile storageFile{ co_await storageFolder.CreateFileAsync (fsFilePath.filename ().wstring (), CreationCollisionOption::ReplaceExisting) };
923934 IRandomAccessStream stream{ co_await storageFile.OpenAsync (FileAccessMode::ReadWrite) };
924935 IOutputStream outputStream{ stream.GetOutputStreamAt (0 ) };
0 commit comments