Compare commits

...

2 Commits

46
otp.c
View File

@@ -158,6 +158,7 @@ int interactive_mode(void) {
handle_pads_menu(); handle_pads_menu();
break; break;
case 'X': case 'X':
case 'Q':
printf("Goodbye!\n"); printf("Goodbye!\n");
return 0; return 0;
default: default:
@@ -1127,12 +1128,41 @@ int generate_pad_with_entropy(uint64_t size_bytes, int display_progress, int use
// Get final paths in pads directory // Get final paths in pads directory
get_pad_path(chksum_hex, pad_path, state_path); get_pad_path(chksum_hex, pad_path, state_path);
// Try rename first (works for same filesystem)
if (rename(temp_filename, pad_path) != 0) { if (rename(temp_filename, pad_path) != 0) {
printf("Error: Cannot move pad file to pads directory\n"); // If rename fails, try copy and delete (works across filesystems)
FILE* temp_file = fopen(temp_filename, "rb");
FILE* dest_file = fopen(pad_path, "wb");
if (!temp_file || !dest_file) {
printf("Error: Cannot copy pad file to pads directory\n");
if (temp_file) fclose(temp_file);
if (dest_file) fclose(dest_file);
unlink(temp_filename); unlink(temp_filename);
return 1; return 1;
} }
// Copy file in chunks
unsigned char copy_buffer[64 * 1024];
size_t bytes_read;
while ((bytes_read = fread(copy_buffer, 1, sizeof(copy_buffer), temp_file)) > 0) {
if (fwrite(copy_buffer, 1, bytes_read, dest_file) != bytes_read) {
printf("Error: Failed to copy pad file to pads directory\n");
fclose(temp_file);
fclose(dest_file);
unlink(temp_filename);
unlink(pad_path);
return 1;
}
}
fclose(temp_file);
fclose(dest_file);
// Remove temporary file after successful copy
unlink(temp_filename);
}
// Set pad file to read-only // Set pad file to read-only
if (chmod(pad_path, S_IRUSR) != 0) { if (chmod(pad_path, S_IRUSR) != 0) {
printf("Warning: Cannot set pad file to read-only\n"); printf("Warning: Cannot set pad file to read-only\n");
@@ -3018,7 +3048,12 @@ int handle_pads_menu(void) {
if (fgets(input, sizeof(input), stdin)) { if (fgets(input, sizeof(input), stdin)) {
char choice = toupper(input[0]); char choice = toupper(input[0]);
if (choice == 'G') { if (choice == 'G') {
return handle_generate_menu(); int result = handle_generate_menu();
if (result == 0) {
// After successful pad generation, return to pads menu
return handle_pads_menu();
}
return result;
} }
} }
return 0; return 0;
@@ -3088,7 +3123,12 @@ int handle_pads_menu(void) {
// Handle actions first // Handle actions first
if (toupper(input[0]) == 'G') { if (toupper(input[0]) == 'G') {
return handle_generate_menu(); int result = handle_generate_menu();
if (result == 0) {
// After successful pad generation, return to pads menu
return handle_pads_menu();
}
return result;
} else if (toupper(input[0]) == 'B') { } else if (toupper(input[0]) == 'B') {
return 0; // Back to main menu return 0; // Back to main menu
} }