Skip to content

Commit 5661991

Browse files
committed
Add exceptions for more file errors
1 parent bdf5724 commit 5661991

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

source/backup.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ write_backup(FILE* account, const std::string& backup_path, char* buffer)
7070
// Open the backup file and write the account data to it.
7171
rewind(account); // Move the file pointer to the beginning.
7272

73-
size_t bytesRead = 0;
74-
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0)
75-
fwrite(buffer, 1, bytesRead, backup);
73+
size_t bytes_read = 0;
74+
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, account)) > 0)
75+
fwrite(buffer, 1, bytes_read, backup);
76+
77+
// Check if there was an error when writing.
78+
if (ferror(backup)) {
79+
draw_error_menu("Error writing to backup account.dat file!");
80+
handle_cleanup(account, backup, buffer, true);
81+
return false;
82+
}
7683

7784
// Close the backup file.
7885
fclose(backup);

source/swap.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,21 @@ swap_account(const char* backup_file, account account_type)
7474
return false;
7575
}
7676

77-
size_t bytesRead = 0;
78-
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0)
79-
fwrite(buffer, 1, bytesRead, account);
77+
size_t bytes_read = 0;
78+
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) {
79+
if (ferror(backup)) {
80+
draw_error_menu("Error reading from backup account.dat file!");
81+
handle_cleanup(backup, account_type, buffer, true);
82+
return false;
83+
}
84+
85+
fwrite(buffer, 1, bytes_read, account);
86+
if (ferror(account)) {
87+
draw_error_menu("Error writing to system account.dat file!");
88+
handle_cleanup(backup, account_type, buffer, true);
89+
return false;
90+
}
91+
}
8092
fclose(account);
8193

8294
// We'll attempt to automatically swap the network using Inkay's configuration.

source/unlink.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ unlink_account()
125125
// Write the string back to the file.
126126
std::ofstream account_output(ACCOUNT_FILE);
127127
account_output << processed_contents; // Write processed_contents to the file.
128+
129+
if (!account_output.bad()) {
130+
draw_error_menu("Error writing to system account.dat file!");
131+
OSEnableHomeButtonMenu(1);
132+
return false;
133+
}
134+
128135
account_output.close();
129136

130137
draw_success_menu(success::unlink); // Draw the success menu.

0 commit comments

Comments
 (0)