Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f4a1fb90f | |||
| c7fae1ad1d | |||
| 37bcb6a6d2 |
176
otp.c
176
otp.c
@@ -39,29 +39,20 @@ static const int base64_decode_table[256] = {
|
||||
#define MAX_LINE_LENGTH 1024
|
||||
#define MAX_HASH_LENGTH 65
|
||||
#define PROGRESS_UPDATE_INTERVAL (64 * 1024 * 1024) // 64MB intervals
|
||||
#define PADS_DIR "pads"
|
||||
#define DEFAULT_PADS_DIR "pads"
|
||||
#define FILES_DIR "files"
|
||||
#define MAX_ENTROPY_BUFFER 32768 // 32KB entropy buffer
|
||||
|
||||
// USB Pad structure for external drive detection
|
||||
struct USBPadInfo {
|
||||
char chksum[65]; // 64-char checksum + null terminator
|
||||
char pad_path[1024]; // Full path to .pad file
|
||||
char state_path[1024]; // Full path to .state file
|
||||
char mount_point[1024]; // USB mount point for display
|
||||
uint64_t state_offset; // Current state offset
|
||||
};
|
||||
// Global variable for current pads directory (can be local or OTP thumb drive)
|
||||
static char current_pads_dir[512] = DEFAULT_PADS_DIR;
|
||||
|
||||
// Function prototypes
|
||||
int main(int argc, char* argv[]);
|
||||
int interactive_mode(void);
|
||||
int command_line_mode(int argc, char* argv[]);
|
||||
|
||||
// USB drive detection functions
|
||||
int scan_usb_drives_for_pads(struct USBPadInfo** usb_pads, int* usb_count);
|
||||
int resolve_state_conflicts(const char* chksum, struct USBPadInfo* usb_pads, int usb_count,
|
||||
uint64_t local_offset, uint64_t* resolved_offset, char** authoritative_path);
|
||||
void free_usb_pads(struct USBPadInfo* usb_pads, int count);
|
||||
// OTP thumb drive detection function
|
||||
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size);
|
||||
|
||||
// Editor and file manager functions
|
||||
char* get_preferred_editor(void);
|
||||
@@ -124,6 +115,15 @@ void get_directory_display(const char* file_path, char* result, size_t result_si
|
||||
void print_usage(const char* program_name);
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Check for OTP thumb drive on startup
|
||||
char otp_drive_path[512];
|
||||
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
|
||||
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
||||
printf("Using as default pads directory for this session.\n\n");
|
||||
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
|
||||
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
|
||||
}
|
||||
|
||||
if (argc == 1) {
|
||||
return interactive_mode();
|
||||
} else {
|
||||
@@ -647,25 +647,19 @@ uint64_t parse_size_string(const char* size_str) {
|
||||
}
|
||||
|
||||
char* find_pad_by_prefix(const char* prefix) {
|
||||
// First scan for USB pads
|
||||
struct USBPadInfo* usb_pads = NULL;
|
||||
int usb_count = 0;
|
||||
scan_usb_drives_for_pads(&usb_pads, &usb_count);
|
||||
|
||||
DIR* dir = opendir(PADS_DIR);
|
||||
DIR* dir = opendir(current_pads_dir);
|
||||
if (!dir) {
|
||||
printf("Error: Cannot open pads directory\n");
|
||||
free_usb_pads(usb_pads, usb_count);
|
||||
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct dirent* entry;
|
||||
char* matches[200]; // Store up to 200 matches (local + USB)
|
||||
char* matches[100]; // Store up to 100 matches
|
||||
int match_count = 0;
|
||||
|
||||
// Always try hex prefix matching first on local pads
|
||||
// Always try hex prefix matching first
|
||||
size_t prefix_len = strlen(prefix);
|
||||
while ((entry = readdir(dir)) != NULL && match_count < 200) {
|
||||
while ((entry = readdir(dir)) != NULL && match_count < 100) {
|
||||
// Skip . and .. entries, and only process .pad files
|
||||
if (entry->d_name[0] == '.') continue;
|
||||
if (!strstr(entry->d_name, ".pad")) continue;
|
||||
@@ -680,35 +674,13 @@ char* find_pad_by_prefix(const char* prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
// Also check USB pads for prefix matches
|
||||
for (int i = 0; i < usb_count && match_count < 200; i++) {
|
||||
if (strncmp(usb_pads[i].chksum, prefix, prefix_len) == 0) {
|
||||
// Check if we already have this checksum from local pads
|
||||
int is_duplicate = 0;
|
||||
for (int j = 0; j < match_count; j++) {
|
||||
if (strcmp(matches[j], usb_pads[i].chksum) == 0) {
|
||||
is_duplicate = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_duplicate) {
|
||||
matches[match_count] = malloc(65);
|
||||
strncpy(matches[match_count], usb_pads[i].chksum, 64);
|
||||
matches[match_count][64] = '\0';
|
||||
match_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no hex prefix matches and it looks like a small number, try number selection
|
||||
if (match_count == 0) {
|
||||
char* endptr;
|
||||
int selection = strtol(prefix, &endptr, 10);
|
||||
if (*endptr == '\0' && selection > 0 && selection <= 200) {
|
||||
// It's a number, find the nth pad (counting local + USB)
|
||||
if (*endptr == '\0' && selection > 0 && selection <= 100) {
|
||||
// It's a number, find the nth pad
|
||||
int current = 0;
|
||||
|
||||
// Count local pads first
|
||||
rewinddir(dir);
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (entry->d_name[0] == '.') continue;
|
||||
@@ -724,45 +696,10 @@ char* find_pad_by_prefix(const char* prefix) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If not found in local pads, check USB pads
|
||||
if (match_count == 0) {
|
||||
for (int i = 0; i < usb_count; i++) {
|
||||
// Skip if this USB pad is already counted in local pads
|
||||
int is_local = 0;
|
||||
rewinddir(dir);
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (entry->d_name[0] == '.') continue;
|
||||
if (!strstr(entry->d_name, ".pad")) continue;
|
||||
if (strlen(entry->d_name) != 68) continue;
|
||||
|
||||
char local_chksum[65];
|
||||
strncpy(local_chksum, entry->d_name, 64);
|
||||
local_chksum[64] = '\0';
|
||||
|
||||
if (strcmp(local_chksum, usb_pads[i].chksum) == 0) {
|
||||
is_local = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_local) {
|
||||
current++;
|
||||
if (current == selection) {
|
||||
matches[match_count] = malloc(65);
|
||||
strncpy(matches[match_count], usb_pads[i].chksum, 64);
|
||||
matches[match_count][64] = '\0';
|
||||
match_count = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
free_usb_pads(usb_pads, usb_count);
|
||||
|
||||
if (match_count == 0) {
|
||||
printf("No pads found matching '%s'\n", prefix);
|
||||
@@ -2352,8 +2289,71 @@ void get_default_file_path(const char* filename, char* result_path, size_t resul
|
||||
}
|
||||
|
||||
void get_pad_path(const char* chksum, char* pad_path, char* state_path) {
|
||||
snprintf(pad_path, MAX_HASH_LENGTH + 20, "%s/%s.pad", PADS_DIR, chksum);
|
||||
snprintf(state_path, MAX_HASH_LENGTH + 20, "%s/%s.state", PADS_DIR, chksum);
|
||||
snprintf(pad_path, MAX_HASH_LENGTH + 20, "%s/%s.pad", current_pads_dir, chksum);
|
||||
snprintf(state_path, MAX_HASH_LENGTH + 20, "%s/%s.state", current_pads_dir, chksum);
|
||||
}
|
||||
|
||||
// OTP thumb drive detection function implementation
|
||||
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
|
||||
const char* mount_dirs[] = {"/media", "/run/media", "/mnt", NULL};
|
||||
|
||||
for (int mount_idx = 0; mount_dirs[mount_idx] != NULL; mount_idx++) {
|
||||
DIR* mount_dir = opendir(mount_dirs[mount_idx]);
|
||||
if (!mount_dir) continue;
|
||||
|
||||
struct dirent* mount_entry;
|
||||
while ((mount_entry = readdir(mount_dir)) != NULL) {
|
||||
if (mount_entry->d_name[0] == '.') continue;
|
||||
|
||||
// Check if drive name starts with "OTP"
|
||||
if (strncmp(mount_entry->d_name, "OTP", 3) != 0) continue;
|
||||
|
||||
char mount_path[512];
|
||||
snprintf(mount_path, sizeof(mount_path), "%s/%s", mount_dirs[mount_idx], mount_entry->d_name);
|
||||
|
||||
// For /run/media, we need to go one level deeper (skip username)
|
||||
if (strcmp(mount_dirs[mount_idx], "/run/media") == 0) {
|
||||
DIR* user_dir = opendir(mount_path);
|
||||
if (!user_dir) continue;
|
||||
|
||||
struct dirent* user_entry;
|
||||
while ((user_entry = readdir(user_dir)) != NULL) {
|
||||
if (user_entry->d_name[0] == '.') continue;
|
||||
|
||||
// Check if drive name starts with "OTP"
|
||||
if (strncmp(user_entry->d_name, "OTP", 3) != 0) continue;
|
||||
|
||||
char user_mount_path[512];
|
||||
snprintf(user_mount_path, sizeof(user_mount_path), "%s/%s", mount_path, user_entry->d_name);
|
||||
|
||||
// Check if this is a readable directory
|
||||
DIR* drive_dir = opendir(user_mount_path);
|
||||
if (drive_dir) {
|
||||
closedir(drive_dir);
|
||||
strncpy(otp_drive_path, user_mount_path, path_size - 1);
|
||||
otp_drive_path[path_size - 1] = '\0';
|
||||
closedir(user_dir);
|
||||
closedir(mount_dir);
|
||||
return 1; // Found OTP drive
|
||||
}
|
||||
}
|
||||
closedir(user_dir);
|
||||
} else {
|
||||
// Direct mount point (like /media/OTP_DRIVE or /mnt/OTP_DRIVE)
|
||||
DIR* drive_dir = opendir(mount_path);
|
||||
if (drive_dir) {
|
||||
closedir(drive_dir);
|
||||
strncpy(otp_drive_path, mount_path, path_size - 1);
|
||||
otp_drive_path[path_size - 1] = '\0';
|
||||
closedir(mount_dir);
|
||||
return 1; // Found OTP drive
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(mount_dir);
|
||||
}
|
||||
|
||||
return 0; // No OTP drive found
|
||||
}
|
||||
|
||||
// USB drive detection functions implementation
|
||||
@@ -2388,7 +2388,7 @@ int scan_usb_drives_for_pads(struct USBPadInfo** usb_pads, int* usb_count) {
|
||||
while ((user_entry = readdir(user_dir)) != NULL && count < 100) {
|
||||
if (user_entry->d_name[0] == '.') continue;
|
||||
|
||||
char user_mount_path[1024];
|
||||
char user_mount_path[2048];
|
||||
snprintf(user_mount_path, sizeof(user_mount_path), "%s/%s", mount_path, user_entry->d_name);
|
||||
|
||||
// Scan this mount point for pads
|
||||
@@ -2399,7 +2399,7 @@ int scan_usb_drives_for_pads(struct USBPadInfo** usb_pads, int* usb_count) {
|
||||
while ((drive_entry = readdir(drive_dir)) != NULL && count < 100) {
|
||||
// Look for .pad files in root of drive
|
||||
if (strstr(drive_entry->d_name, ".pad") && strlen(drive_entry->d_name) == 68) {
|
||||
char pad_path[2048], state_path[2048];
|
||||
char pad_path[4096], state_path[4096];
|
||||
snprintf(pad_path, sizeof(pad_path), "%s/%s", user_mount_path, drive_entry->d_name);
|
||||
|
||||
// Extract checksum from filename
|
||||
|
||||
Reference in New Issue
Block a user