diff --git a/otp b/otp new file mode 120000 index 0000000..981fa96 --- /dev/null +++ b/otp @@ -0,0 +1 @@ +./build/otp-x86_64 \ No newline at end of file diff --git a/src/pads.c b/src/pads.c index e31ed3c..d8ce78d 100644 --- a/src/pads.c +++ b/src/pads.c @@ -89,7 +89,10 @@ int generate_pad(uint64_t size_bytes, int display_progress) { const char* pads_dir = get_current_pads_dir(); struct statvfs stat; if (statvfs(pads_dir, &stat) == 0) { - uint64_t available_bytes = stat.f_bavail * stat.f_frsize; + // Use f_bfree (total free blocks) instead of f_bavail (available to non-root) + // This gives the actual free space on the filesystem, which is more accurate + // for removable media and user-owned directories + uint64_t available_bytes = stat.f_bfree * stat.f_frsize; double available_gb = (double)available_bytes / (1024.0 * 1024.0 * 1024.0); double required_gb = (double)size_bytes / (1024.0 * 1024.0 * 1024.0); @@ -233,19 +236,41 @@ int read_state_offset(const char* pad_chksum, uint64_t* offset) { const char* pads_dir = get_current_pads_dir(); snprintf(state_filename, sizeof(state_filename), "%s/%s.state", pads_dir, pad_chksum); - FILE* state_file = fopen(state_filename, "rb"); + FILE* state_file = fopen(state_filename, "r"); if (!state_file) { *offset = 0; return 0; } - if (fread(offset, sizeof(uint64_t), 1, state_file) != 1) { + // Try to read as text format first (new format) + char line[128]; + if (fgets(line, sizeof(line), state_file)) { + // Check if it's text format (starts with "offset=") + if (strncmp(line, "offset=", 7) == 0) { + *offset = strtoull(line + 7, NULL, 10); + fclose(state_file); + return 0; + } + + // Not text format, try binary format (legacy) + fclose(state_file); + state_file = fopen(state_filename, "rb"); + if (!state_file) { + *offset = 0; + return 0; + } + + if (fread(offset, sizeof(uint64_t), 1, state_file) != 1) { + fclose(state_file); + *offset = 0; + return 0; + } fclose(state_file); - *offset = 0; return 0; } fclose(state_file); + *offset = 0; return 0; } @@ -254,12 +279,13 @@ int write_state_offset(const char* pad_chksum, uint64_t offset) { const char* pads_dir = get_current_pads_dir(); snprintf(state_filename, sizeof(state_filename), "%s/%s.state", pads_dir, pad_chksum); - FILE* state_file = fopen(state_filename, "wb"); + FILE* state_file = fopen(state_filename, "w"); if (!state_file) { return 1; } - if (fwrite(&offset, sizeof(uint64_t), 1, state_file) != 1) { + // Write in text format for human readability + if (fprintf(state_file, "offset=%lu\n", offset) < 0) { fclose(state_file); return 1; }