Common/FatFsUtil: Add callback for cancelling SD card conversion.

This commit is contained in:
Admiral H. Curtiss 2023-02-26 20:18:37 +01:00
parent 73aaa01648
commit 435d8c39ee
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
5 changed files with 34 additions and 18 deletions

View File

@ -191,12 +191,12 @@ Java_org_dolphinemu_dolphinemu_utils_WiiUtils_getSystemMenuVersion(JNIEnv* env,
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_utils_WiiUtils_syncSdFolderToSdImage(JNIEnv* env, jclass) Java_org_dolphinemu_dolphinemu_utils_WiiUtils_syncSdFolderToSdImage(JNIEnv* env, jclass)
{ {
return static_cast<jboolean>(Common::SyncSDFolderToSDImage(false)); return static_cast<jboolean>(Common::SyncSDFolderToSDImage([]() { return false; }, false));
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_utils_WiiUtils_syncSdImageToSdFolder(JNIEnv* env, jclass) Java_org_dolphinemu_dolphinemu_utils_WiiUtils_syncSdImageToSdFolder(JNIEnv* env, jclass)
{ {
return static_cast<jboolean>(Common::SyncSDImageToSDFolder()); return static_cast<jboolean>(Common::SyncSDImageToSDFolder([]() { return false; }));
} }
} }

View File

@ -353,8 +353,12 @@ static u64 GetSize(const File::FSTEntry& entry)
return size; 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) if (!entry.isDirectory)
{ {
File::IOFile src(entry.physicalName, "rb"); 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; u64 size = entry.size;
while (size > 0) while (size > 0)
{ {
if (cancelled())
return false;
u32 chunk_size = static_cast<u32>(std::min(size, static_cast<u64>(tmp_buffer.size()))); u32 chunk_size = static_cast<u32>(std::min(size, static_cast<u64>(tmp_buffer.size())));
if (!src.ReadBytes(tmp_buffer.data(), chunk_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) for (const File::FSTEntry& child : entry.children)
{ {
if (!Pack(child, false, tmp_buffer)) if (!Pack(cancelled, child, false, tmp_buffer))
return false; return false;
} }
@ -484,7 +491,7 @@ static void SortFST(File::FSTEntry* root)
SortFST(&child); 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 source_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX);
const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX); const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX);
@ -563,7 +570,7 @@ bool SyncSDFolderToSDImage(bool deterministic)
} }
Common::ScopeGuard unmount_guard{[] { f_unmount(""); }}; 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, ERROR_LOG_FMT(COMMON, "Failed to pack folder {} to SD image at {}", source_dir,
temp_image_path); temp_image_path);
@ -590,9 +597,12 @@ bool SyncSDFolderToSDImage(bool deterministic)
return true; return true;
} }
static bool Unpack(const std::string path, bool is_directory, const char* name, static bool Unpack(const std::function<bool()>& cancelled, const std::string path,
std::vector<u8>& tmp_buffer) bool is_directory, const char* name, std::vector<u8>& tmp_buffer)
{ {
if (cancelled())
return false;
if (!is_directory) if (!is_directory)
{ {
FIL src{}; FIL src{};
@ -614,6 +624,9 @@ static bool Unpack(const std::string path, bool is_directory, const char* name,
u32 size = f_size(&src); u32 size = f_size(&src);
while (size > 0) while (size > 0)
{ {
if (cancelled())
return false;
u32 chunk_size = std::min(size, static_cast<u32>(tmp_buffer.size())); u32 chunk_size = std::min(size, static_cast<u32>(tmp_buffer.size()));
u32 read_size; u32 read_size;
const auto read_error_code = f_read(&src, tmp_buffer.data(), chunk_size, &read_size); const auto read_error_code = f_read(&src, tmp_buffer.data(), chunk_size, &read_size);
@ -717,8 +730,8 @@ static bool Unpack(const std::string path, bool is_directory, const char* name,
return false; return false;
} }
if (!Unpack(fmt::format("{}/{}", path, childname), entry.fattrib & AM_DIR, entry.fname, if (!Unpack(cancelled, fmt::format("{}/{}", path, childname), entry.fattrib & AM_DIR,
tmp_buffer)) entry.fname, tmp_buffer))
{ {
return false; return false;
} }
@ -743,7 +756,7 @@ static bool Unpack(const std::string path, bool is_directory, const char* name,
return true; return true;
} }
bool SyncSDImageToSDFolder() bool SyncSDImageToSDFolder(const std::function<bool()>& cancelled)
{ {
const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX); const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX);
const std::string target_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX); const std::string target_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX);
@ -800,7 +813,7 @@ bool SyncSDImageToSDFolder()
} }
std::vector<u8> tmp_buffer(MAX_CLUSTER_SIZE); 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); ERROR_LOG_FMT(COMMON, "Failed to unpack SD image {} to {}", image_path, target_dir);
File::DeleteDirRecursively(target_dir_without_slash); File::DeleteDirRecursively(target_dir_without_slash);

View File

@ -9,8 +9,8 @@
namespace Common namespace Common
{ {
bool SyncSDFolderToSDImage(bool deterministic); bool SyncSDFolderToSDImage(const std::function<bool()>& cancelled, bool deterministic);
bool SyncSDImageToSDFolder(); bool SyncSDImageToSDFolder(const std::function<bool()>& cancelled);
class FatFsCallbacks class FatFsCallbacks
{ {

View File

@ -503,11 +503,14 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
bool sync_sd_folder = core_parameter.bWii && Config::Get(Config::MAIN_WII_SD_CARD) && bool sync_sd_folder = core_parameter.bWii && Config::Get(Config::MAIN_WII_SD_CARD) &&
Config::Get(Config::MAIN_WII_SD_CARD_ENABLE_FOLDER_SYNC); Config::Get(Config::MAIN_WII_SD_CARD_ENABLE_FOLDER_SYNC);
if (sync_sd_folder) if (sync_sd_folder)
sync_sd_folder = Common::SyncSDFolderToSDImage(Core::WantsDeterminism()); {
sync_sd_folder =
Common::SyncSDFolderToSDImage([]() { return false; }, Core::WantsDeterminism());
}
Common::ScopeGuard sd_folder_sync_guard{[sync_sd_folder] { Common::ScopeGuard sd_folder_sync_guard{[sync_sd_folder] {
if (sync_sd_folder && Config::Get(Config::MAIN_ALLOW_SD_WRITES)) if (sync_sd_folder && Config::Get(Config::MAIN_ALLOW_SD_WRITES))
Common::SyncSDImageToSDFolder(); Common::SyncSDImageToSDFolder([]() { return false; });
}}; }};
// Load Wiimotes - only if we are booting in Wii mode // Load Wiimotes - only if we are booting in Wii mode

View File

@ -228,7 +228,7 @@ void WiiPane::CreateSDCard()
QMessageBox::Yes | QMessageBox::No); QMessageBox::Yes | QMessageBox::No);
if (result == QMessageBox::Yes) if (result == QMessageBox::Yes)
{ {
if (!Common::SyncSDFolderToSDImage(false)) if (!Common::SyncSDFolderToSDImage([]() { return false; }, false))
ModalMessageBox::warning(this, tr("Convert Folder to File Now"), tr("Conversion failed.")); ModalMessageBox::warning(this, tr("Convert Folder to File Now"), tr("Conversion failed."));
} }
}); });
@ -242,7 +242,7 @@ void WiiPane::CreateSDCard()
QMessageBox::Yes | QMessageBox::No); QMessageBox::Yes | QMessageBox::No);
if (result == QMessageBox::Yes) if (result == QMessageBox::Yes)
{ {
if (!Common::SyncSDImageToSDFolder()) if (!Common::SyncSDImageToSDFolder([]() { return false; }))
ModalMessageBox::warning(this, tr("Convert File to Folder Now"), tr("Conversion failed.")); ModalMessageBox::warning(this, tr("Convert File to Folder Now"), tr("Conversion failed."));
} }
}); });