From c600c1efbfae6dc893be9203b9756ecf320de7e5 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 28 Dec 2025 03:07:43 -0600 Subject: Finish _cryptscan__process_scandir impl --- src/encryption.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 12 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 9df5206..23c8a4d 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -458,37 +458,94 @@ static int _cryptscan__selector(const struct dirent *de) { return 1; } -/// helper function to deduplicate code for dealing with scandir +// TODO: Implement +int _cryptscan__crypt(void *data) { + +} + +// TODO: Implement +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 res = 0; struct dirent **namelist = NULL; int entries = scandir(folder, &namelist, _cryptscan__selector, alphasort); - if(!toscan || !tocrypt || entries < 0) goto _cryptscan__process_scandir_ERR; + if(!toscan || !tocrypt || entries < 0) {res = -1; goto _cryptscan__process_scandir_CLEANUP;} + struct stat sb; char *tmpname = NULL; task *tmptsk = NULL; char tflag = 0; for(int i = 0; i < entries; i++) { + tmpname = strdup(namelist[i]->d_name); + if(!tmpname) {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) { // Try to stat the file if it's unknown case DT_UNKNOWN: + if(stat(namelist[i]->d_name, &sb) < 0) goto UNKNOWN; + if(S_ISDIR(sb.st_mode)) goto SCAN; + if(S_ISREG(sb.st_mode)) goto CRYPT; break; + // Add it to the ctq - case DT_REG: + case DT_REG: CRYPT: + tmptsk = task_new(_cryptscan__crypt, free, tmpname); + if(!tmptsk) { + if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not generate crypt task for \"%s\"",, tmpname); + break; + } + + if(ctqueue_waitpush(tocrypt, tmptsk) < 0) { + if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not add crypt task to ctq",); + task_free(tmptsk); + break; + } + + res++; + tflag++; break; + // Add it to the scanlist - case DT_DIR: + case DT_DIR: SCAN: + tmptsk = task_new(_cryptscan__scan, free, tmpname); + if(!tmptsk) { + if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not generate scan task for \"%s\"",, tmpname); + break; + } + + if(taskqueue_push(toscan, tmptsk) < 0) { + if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not add scan task to scanlist",); + task_free(tmptsk); + break; + } + + res++; + tflag++; break; + // Ignore or spit out a warning - default: + default: UNKNOWN: + if(___VXGG___VERBOSE_ERRORS___) + WARN(ENOSYS, "<_cryptscan__process_scandir> Info: file \"%s\"s type is unsupported",, namelist[i]->d_name); break; } + if(!tflag) free(tmpname); + tflag = 0; } -_cryptscan__process_scandir_ERR: +_cryptscan__process_scandir_CLEANUP: + for(int i = 0; i < entries; i++) { + free(namelist[i]); + } + free(namelist); - return -1; + return res; } // 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 @@ -504,6 +561,10 @@ ctqueue * cryptscan(int threads, const char * const start) { // Initialize the lists if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; + // Loop through the scanlist until it's empty + while(taskqueue_size(toscan) > 0) { + task_fired(taskqueue_pop(toscan)); + } taskqueue_free(toscan); return tocrypt; @@ -512,11 +573,6 @@ cryptscan_ERR: taskqueue_free(toscan); ctqueue_free(tocrypt); - // for(int i = 0; i < entries; i++) { - // free(namelist[i]); - // } - // free(namelist); - return NULL; } -- cgit v1.2.3