From 73902dc1b3dad91f98b785f5a742e4e44c7e7d73 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 28 Dec 2025 01:00:13 -0600 Subject: Make some progress on cryptscan impl --- src/encryption.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/encryption.c b/src/encryption.c index e304e6e..9df5206 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -322,8 +322,8 @@ CLEANUP_encryptviatmp: int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!target || !output || !key) ERRRET(EINVAL, -1); - FILE *src, *dst; - int fdst, eflag = -1, res = -1; + FILE *src = NULL, *dst = NULL; + int fdst = -1, eflag = -1, res = -1; // Open the source file if(!(src = fopen(target, "rb"))) {eflag = 0; goto CLEANUP_decryptto;} @@ -450,16 +450,74 @@ static int _cryptscan__selector(const struct dirent *de) { if(!de) return 0; for(int i = 0; i < STATIC_ARRAY_LEN(EXCLUDED_ENTRIES); i++) { - if(strncmp(de->d_name, EXCLUDED_ENTRIES[i], sizeof(de->d_name)) == 0) + // Would use strncmp here but d_name doesn't have a fixed size and is supposedly guaranteed to have a null terminator so it shouldn't be a big deal + if(strcmp(de->d_name, EXCLUDED_ENTRIES[i]) == 0) return 0; } return 1; } +/// helper function to deduplicate code for dealing with scandir +int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { + if(!folder || !toscan || !tocrypt) return -1; + + struct dirent **namelist = NULL; + int entries = scandir(folder, &namelist, _cryptscan__selector, alphasort); + if(!toscan || !tocrypt || entries < 0) goto _cryptscan__process_scandir_ERR; + + for(int i = 0; i < entries; i++) { + switch(namelist[i]->d_type) { + // Try to stat the file if it's unknown + case DT_UNKNOWN: + break; + + // Add it to the ctq + case DT_REG: + break; + + // Add it to the scanlist + case DT_DIR: + break; + + // Ignore or spit out a warning + default: + break; + } + } + +_cryptscan__process_scandir_ERR: + + return -1; +} + +// 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; + taskqueue *toscan = taskqueue_new(); + ctqueue *tocrypt = ctqueue_init(threads); + + // Initialize the lists + if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; + + + taskqueue_free(toscan); + return tocrypt; + +cryptscan_ERR: + taskqueue_free(toscan); + ctqueue_free(tocrypt); + + // for(int i = 0; i < entries; i++) { + // free(namelist[i]); + // } + // free(namelist); + + return NULL; } #endif \ No newline at end of file -- cgit v1.2.3