From 1c9e2ee527462e555d8f42e802075e960a9ab0e1 Mon Sep 17 00:00:00 2001 From: Laan Tungir Date: Thu, 14 Aug 2025 10:05:33 -0400 Subject: [PATCH] Version v0.2.66 - Fixed cross-filesystem pad generation with copy fallback for USB drives --- otp.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/otp.c b/otp.c index 647ccc8..0fe4480 100644 --- a/otp.c +++ b/otp.c @@ -1127,10 +1127,39 @@ int generate_pad_with_entropy(uint64_t size_bytes, int display_progress, int use // Get final paths in pads directory get_pad_path(chksum_hex, pad_path, state_path); + // Try rename first (works for same filesystem) 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); + 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); - return 1; } // Set pad file to read-only