Writes a DS cart save, already read into memory, as a new file in the backup folder the caller has just created. Creating that file has been seen to fail with FS NotFound (0xC8804478) right after the folder was made, and a plain retry by hand worked (#584). So the open is retried a few times, first putting the folder back if FS no longer has it. Each failed open is logged together with whether th
| 552 | // has it. Each failed open is logged together with whether the folder was still |
| 553 | // there, which is the part the #584 log could not tell. |
| 554 | static Result writeCardSave(const std::u16string& folder, const std::u16string& path, const u8* data, u32 size) |
| 555 | { |
| 556 | static constexpr int attempts = 4; |
| 557 | |
| 558 | for (int attempt = 1;; attempt++) { |
| 559 | FSStream stream(Archive::sdmc(), path, FS_OPEN_WRITE, size); |
| 560 | if (stream.good()) { |
| 561 | const u32 wt = stream.write(data, size); |
| 562 | const Result writeRes = stream.result(); |
| 563 | const Result closeRes = stream.close(); |
| 564 | if (R_FAILED(writeRes)) { |
| 565 | Logging::error("Write failure on {} with result 0x{:08X}.", StringUtils::UTF16toUTF8(path), (u32)writeRes); |
| 566 | return writeRes; |
| 567 | } |
| 568 | if (wt != size) { |
| 569 | Logging::error("Short write on {}: wrote {} of {} bytes.", StringUtils::UTF16toUTF8(path), wt, size); |
| 570 | return RES_SHORT_WRITE; |
| 571 | } |
| 572 | if (R_FAILED(closeRes)) { |
| 573 | Logging::error("Failed to close {} with result 0x{:08X}.", StringUtils::UTF16toUTF8(path), (u32)closeRes); |
| 574 | return closeRes; |
| 575 | } |
| 576 | if (attempt > 1) { |
| 577 | Logging::info("Created {} on attempt {} of {}.", StringUtils::UTF16toUTF8(path), attempt, attempts); |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | const Result openRes = stream.result(); |
| 583 | const bool folderPresent = io::directoryExists(Archive::sdmc(), folder); |
| 584 | Logging::warning("Failed to create {} with result 0x{:08X} (attempt {} of {}, backup folder {}).", StringUtils::UTF16toUTF8(path), |
| 585 | (u32)openRes, attempt, attempts, folderPresent ? "present" : "missing"); |
| 586 | if (attempt == attempts) { |
| 587 | return openRes; |
| 588 | } |
| 589 | if (!folderPresent) { |
| 590 | const Result dirRes = io::createDirectory(Archive::sdmc(), folder); |
| 591 | if (R_FAILED(dirRes) && (u32)dirRes != RES_FS_DIR_EXISTS) { |
| 592 | Logging::warning("Failed to recreate the backup folder with result 0x{:08X}.", (u32)dirRes); |
| 593 | } |
| 594 | } |
| 595 | svcSleepThread(250'000'000); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | io::IoOutcome io::backup(const BackupTarget& target, const std::u16string& dstPath, ProgressSink& sink) |
| 600 | { |