Add exceptions for more file errors

This commit is contained in:
Nightkingale 2024-05-18 07:05:19 -06:00
parent bdf5724029
commit 5661991590
3 changed files with 32 additions and 6 deletions

View File

@ -70,9 +70,16 @@ write_backup(FILE* account, const std::string& backup_path, char* buffer)
// Open the backup file and write the account data to it.
rewind(account); // Move the file pointer to the beginning.
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0)
fwrite(buffer, 1, bytesRead, backup);
size_t bytes_read = 0;
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, account)) > 0)
fwrite(buffer, 1, bytes_read, backup);
// Check if there was an error when writing.
if (ferror(backup)) {
draw_error_menu("Error writing to backup account.dat file!");
handle_cleanup(account, backup, buffer, true);
return false;
}
// Close the backup file.
fclose(backup);

View File

@ -74,9 +74,21 @@ swap_account(const char* backup_file, account account_type)
return false;
}
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0)
fwrite(buffer, 1, bytesRead, account);
size_t bytes_read = 0;
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) {
if (ferror(backup)) {
draw_error_menu("Error reading from backup account.dat file!");
handle_cleanup(backup, account_type, buffer, true);
return false;
}
fwrite(buffer, 1, bytes_read, account);
if (ferror(account)) {
draw_error_menu("Error writing to system account.dat file!");
handle_cleanup(backup, account_type, buffer, true);
return false;
}
}
fclose(account);
// We'll attempt to automatically swap the network using Inkay's configuration.

View File

@ -125,6 +125,13 @@ unlink_account()
// Write the string back to the file.
std::ofstream account_output(ACCOUNT_FILE);
account_output << processed_contents; // Write processed_contents to the file.
if (!account_output.bad()) {
draw_error_menu("Error writing to system account.dat file!");
OSEnableHomeButtonMenu(1);
return false;
}
account_output.close();
draw_success_menu(success::unlink); // Draw the success menu.