From d5de3e243a8481ad879dd8effc5759d406dc90b7 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 14 Jun 2025 18:16:03 -0500 Subject: Work on implementing getfilelist function --- src/encryption.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 17 deletions(-) diff --git a/src/encryption.c b/src/encryption.c index 02abccd..65b8843 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -222,8 +222,8 @@ int encryptviatmp(const char * const target, const char * const output, const un return 0; } -int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { - if(!encrypted || !target || !key) ERRRET(EINVAL, -1); +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); #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif @@ -233,15 +233,15 @@ int decryptto(const char * const encrypted, const char * const target, const uns int fdst; // Open the source file - if(!(src = fopen(encrypted, "rb"))) { - WARN(errno, " Could not open \"%s\" for decryption", , encrypted); + if(!(src = fopen(target, "rb"))) { + WARN(errno, " Could not open \"%s\" for decryption", , target); return -1; } cleanup_REGISTER(__ucl_fclose, src); // Get a temp descriptor for the temp file cleanup_CNDEXEC( - fdst = maketmp(target); + fdst = maketmp(output); if(!fdst) { WARN(errno, " Could not get temp file for decryption", ); cleanup_MARK(); @@ -252,7 +252,7 @@ int decryptto(const char * const encrypted, const char * const target, const uns // Open a FILE* version of the temp file cleanup_CNDEXEC( if(!(dst = fdopen(fdst, "wb"))) { - WARN(errno, " Could not open \"%s\" for writing decrypted data", , target); + WARN(errno, " Could not open \"%s\" for writing decrypted data", , output); cleanup_MARK(); } cleanup_CNDREGISTER(__ucl_fclose, dst); @@ -264,8 +264,8 @@ int decryptto(const char * const encrypted, const char * const target, const uns ERROR(1, errno, " How did you even cause an error?",); // Link temp into system - if(linkto(target, fdst) < 0) - WARN(errno, " Could not link \"%s\" into system", , target); + if(linkto(output, fdst) < 0) + WARN(errno, " Could not link \"%s\" into system", , output); fclose(dst); fclose(src); @@ -461,6 +461,56 @@ void* xsodium_malloc(size_t size) { +struct __FLC_FKP { + char *target; + char *output; + const unsigned char *key; +}; + +struct __FLC_FKP * fkp_init(char * const target, char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(!target || !output || !key) ERRRET(EINVAL, NULL); + struct __FLC_FKP *fkp = calloc(1, sizeof(*fkp)); + if(!fkp) + return NULL; + + fkp->key = key; + fkp->output = output; + fkp->target = target; + + return fkp; +} + +void fkp_free(void *fkp) { + if(!fkp) return; + struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; + + free(real->output); + free(real->target); + free(real); + + return; +} + +static int __FLC_TASK_ENCRYPT(void *fkp) { + if(!fkp) + return -1; + struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; + return encryptviatmp(real->target, real->output, real->key); +} +static int __FLC_TASK_DECRYPT(void *fkp) { + if(!fkp) + return -1; + struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; + return decryptto(real->target, real->output, real->key); +} + +enum VXGG_FLC { + VXGG_FLC__INVALID, + VXGG_FLC__ENCRYPT, + VXGG_FLC__DECRYPT, + VXGG_FLC__TOOBIG +}; + struct __ucl_namelist_vals { struct dirent **namelist; int entries; @@ -475,15 +525,16 @@ static void __ucl_namelist(void *namelist) { return; } -enum VXGG_FLC { - VXGG_FLC__INVALID, - VXGG_FLC__ENCRYPT, - VXGG_FLC__DECRYPT, - VXGG_FLC__TOOBIG -}; +// TODO: Write these +static int encryption_filter(const struct dirent *de) { -ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !filter || !compar) ERRRET(EINVAL, NULL); +} +static int decryption_filter(const struct dirent *de) { + +} + +ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !key) ERRRET(EINVAL, NULL); cleanup_CREATE(10); ctqueue *ctq = NULL; @@ -497,12 +548,36 @@ ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, // Get the scandir list struct dirent **namelist; - if((files = scandir(dir, &namelist, filter, compar)) < 0) + if((files = scandir(dir, &namelist, ((mode == VXGG_FLC__ENCRYPT) ? encryption_filter : decryption_filter), alphasort)) < 0) cleanup_MARK(); cleanup_CNDREGISTER(__ucl_namelist, (void*)(&(struct __ucl_namelist_vals){.namelist = namelist, .entries = files})); // Push everything onto the ctqueue // TODO: Write task* compatible callbacks for encryption and decryption, then populate the ctqueue based on the specified mode + cleanup_CNDEXEC( + for(int i = 0; i < files; i++) { + + // Target is either "filename" or "filename.vxggr" + // Output is either "filename.vxggr" or "filename" + + struct __FLC_FKP *fkp = fkp_init(target, output, key); + if(!fkp) { + WARN(errno, " Could not create file-key pair for \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de")); + free(target); + free(output); + } + task *ctask = task_init((mode == VXGG_FLC__ENCRYPT) ? __FLC_TASK_ENCRYPT : __FLC_TASK_DECRYPT, fkp_free, fkp); + if(!ctask) { + WARN(errno, " Could not push \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de")); + free(namelist[i]); + continue; + } + ctqueue_waitpush(ctq, ctask); + } + + free(namelist); + // namelist is the array that holds each pointer to each dirent, so it needs to be free'd separately from the other elements + ); cleanup_CNDFIRE(); if(cleanup_ERRORFLAGGED) -- cgit v1.2.3