diff options
| author | Mai <mai.iam2048@gmail.com> | 2023-03-02 15:08:18 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-02 15:08:18 -0500 |
| commit | ab2f8d3475fa7dd1f1bad911bf70eb9c85eeb0da (patch) | |
| tree | 30ca35c74e9084574f127ae6961ff884129052ab /Source/Core/Common/FatFsUtil.cpp | |
| parent | 6fcec80eb0067e720a665644e89ccd741ff365c5 (diff) | |
| parent | e2bd7d1d95b221406ee401114fcd10b2c3e27e4c (diff) | |
Merge pull request #11607 from AdmiralCurtiss/sd-progress
Qt/WiiPane: Add progress window for SD card conversion.
Diffstat (limited to 'Source/Core/Common/FatFsUtil.cpp')
| -rw-r--r-- | Source/Core/Common/FatFsUtil.cpp | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/Source/Core/Common/FatFsUtil.cpp b/Source/Core/Common/FatFsUtil.cpp index 2c88b56101..f194dc22a3 100644 --- a/Source/Core/Common/FatFsUtil.cpp +++ b/Source/Core/Common/FatFsUtil.cpp @@ -353,8 +353,12 @@ static u64 GetSize(const File::FSTEntry& entry) return size; } -static bool Pack(const File::FSTEntry& entry, bool is_root, std::vector<u8>& tmp_buffer) +static bool Pack(const std::function<bool()>& cancelled, const File::FSTEntry& entry, bool is_root, + std::vector<u8>& tmp_buffer) { + if (cancelled()) + return false; + if (!entry.isDirectory) { File::IOFile src(entry.physicalName, "rb"); @@ -392,6 +396,9 @@ static bool Pack(const File::FSTEntry& entry, bool is_root, std::vector<u8>& tmp u64 size = entry.size; while (size > 0) { + if (cancelled()) + return false; + u32 chunk_size = static_cast<u32>(std::min(size, static_cast<u64>(tmp_buffer.size()))); if (!src.ReadBytes(tmp_buffer.data(), chunk_size)) { @@ -456,7 +463,7 @@ static bool Pack(const File::FSTEntry& entry, bool is_root, std::vector<u8>& tmp for (const File::FSTEntry& child : entry.children) { - if (!Pack(child, false, tmp_buffer)) + if (!Pack(cancelled, child, false, tmp_buffer)) return false; } @@ -484,7 +491,7 @@ static void SortFST(File::FSTEntry* root) SortFST(&child); } -bool SyncSDFolderToSDImage(bool deterministic) +bool SyncSDFolderToSDImage(const std::function<bool()>& cancelled, bool deterministic) { const std::string source_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX); const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX); @@ -528,7 +535,10 @@ bool SyncSDFolderToSDImage(bool deterministic) } // delete temp file in failure case - Common::ScopeGuard image_delete_guard{[&] { File::Delete(temp_image_path); }}; + Common::ScopeGuard image_delete_guard{[&] { + image.Close(); + File::Delete(temp_image_path); + }}; if (!image.Resize(size)) { @@ -563,7 +573,7 @@ bool SyncSDFolderToSDImage(bool deterministic) } Common::ScopeGuard unmount_guard{[] { f_unmount(""); }}; - if (!Pack(root, true, tmp_buffer)) + if (!Pack(cancelled, root, true, tmp_buffer)) { ERROR_LOG_FMT(COMMON, "Failed to pack folder {} to SD image at {}", source_dir, temp_image_path); @@ -590,9 +600,12 @@ bool SyncSDFolderToSDImage(bool deterministic) return true; } -static bool Unpack(const std::string path, bool is_directory, const char* name, - std::vector<u8>& tmp_buffer) +static bool Unpack(const std::function<bool()>& cancelled, const std::string path, + bool is_directory, const char* name, std::vector<u8>& tmp_buffer) { + if (cancelled()) + return false; + if (!is_directory) { FIL src{}; @@ -614,6 +627,9 @@ static bool Unpack(const std::string path, bool is_directory, const char* name, u32 size = f_size(&src); while (size > 0) { + if (cancelled()) + return false; + u32 chunk_size = std::min(size, static_cast<u32>(tmp_buffer.size())); u32 read_size; const auto read_error_code = f_read(&src, tmp_buffer.data(), chunk_size, &read_size); @@ -717,8 +733,8 @@ static bool Unpack(const std::string path, bool is_directory, const char* name, return false; } - if (!Unpack(fmt::format("{}/{}", path, childname), entry.fattrib & AM_DIR, entry.fname, - tmp_buffer)) + if (!Unpack(cancelled, fmt::format("{}/{}", path, childname), entry.fattrib & AM_DIR, + entry.fname, tmp_buffer)) { return false; } @@ -743,7 +759,7 @@ static bool Unpack(const std::string path, bool is_directory, const char* name, return true; } -bool SyncSDImageToSDFolder() +bool SyncSDImageToSDFolder(const std::function<bool()>& cancelled) { const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX); const std::string target_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX); @@ -800,7 +816,7 @@ bool SyncSDImageToSDFolder() } std::vector<u8> tmp_buffer(MAX_CLUSTER_SIZE); - if (!Unpack(target_dir_without_slash, true, "", tmp_buffer)) + if (!Unpack(cancelled, target_dir_without_slash, true, "", tmp_buffer)) { ERROR_LOG_FMT(COMMON, "Failed to unpack SD image {} to {}", image_path, target_dir); File::DeleteDirRecursively(target_dir_without_slash); |
