diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/encryption.c | 64 |
1 files 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: | |||
| 322 | int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 322 | int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 323 | if(!target || !output || !key) ERRRET(EINVAL, -1); | 323 | if(!target || !output || !key) ERRRET(EINVAL, -1); |
| 324 | 324 | ||
| 325 | FILE *src, *dst; | 325 | FILE *src = NULL, *dst = NULL; |
| 326 | int fdst, eflag = -1, res = -1; | 326 | int fdst = -1, eflag = -1, res = -1; |
| 327 | 327 | ||
| 328 | // Open the source file | 328 | // Open the source file |
| 329 | if(!(src = fopen(target, "rb"))) {eflag = 0; goto CLEANUP_decryptto;} | 329 | if(!(src = fopen(target, "rb"))) {eflag = 0; goto CLEANUP_decryptto;} |
| @@ -450,16 +450,74 @@ static int _cryptscan__selector(const struct dirent *de) { | |||
| 450 | if(!de) return 0; | 450 | if(!de) return 0; |
| 451 | 451 | ||
| 452 | for(int i = 0; i < STATIC_ARRAY_LEN(EXCLUDED_ENTRIES); i++) { | 452 | for(int i = 0; i < STATIC_ARRAY_LEN(EXCLUDED_ENTRIES); i++) { |
| 453 | if(strncmp(de->d_name, EXCLUDED_ENTRIES[i], sizeof(de->d_name)) == 0) | 453 | // 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 |
| 454 | if(strcmp(de->d_name, EXCLUDED_ENTRIES[i]) == 0) | ||
| 454 | return 0; | 455 | return 0; |
| 455 | } | 456 | } |
| 456 | 457 | ||
| 457 | return 1; | 458 | return 1; |
| 458 | } | 459 | } |
| 459 | 460 | ||
| 461 | /// helper function to deduplicate code for dealing with scandir | ||
| 462 | int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { | ||
| 463 | if(!folder || !toscan || !tocrypt) return -1; | ||
| 464 | |||
| 465 | struct dirent **namelist = NULL; | ||
| 466 | int entries = scandir(folder, &namelist, _cryptscan__selector, alphasort); | ||
| 467 | if(!toscan || !tocrypt || entries < 0) goto _cryptscan__process_scandir_ERR; | ||
| 468 | |||
| 469 | for(int i = 0; i < entries; i++) { | ||
| 470 | switch(namelist[i]->d_type) { | ||
| 471 | // Try to stat the file if it's unknown | ||
| 472 | case DT_UNKNOWN: | ||
| 473 | break; | ||
| 474 | |||
| 475 | // Add it to the ctq | ||
| 476 | case DT_REG: | ||
| 477 | break; | ||
| 478 | |||
| 479 | // Add it to the scanlist | ||
| 480 | case DT_DIR: | ||
| 481 | break; | ||
| 482 | |||
| 483 | // Ignore or spit out a warning | ||
| 484 | default: | ||
| 485 | break; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | _cryptscan__process_scandir_ERR: | ||
| 490 | |||
| 491 | return -1; | ||
| 492 | } | ||
| 493 | |||
| 494 | // 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 | ||
| 495 | // 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 | ||
| 496 | // execution efficient) | ||
| 497 | |||
| 460 | ctqueue * cryptscan(int threads, const char * const start) { | 498 | ctqueue * cryptscan(int threads, const char * const start) { |
| 461 | if(!start || threads < 1) return NULL; | 499 | if(!start || threads < 1) return NULL; |
| 462 | 500 | ||
| 501 | taskqueue *toscan = taskqueue_new(); | ||
| 502 | ctqueue *tocrypt = ctqueue_init(threads); | ||
| 503 | |||
| 504 | // Initialize the lists | ||
| 505 | if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; | ||
| 506 | |||
| 507 | |||
| 508 | taskqueue_free(toscan); | ||
| 509 | return tocrypt; | ||
| 510 | |||
| 511 | cryptscan_ERR: | ||
| 512 | taskqueue_free(toscan); | ||
| 513 | ctqueue_free(tocrypt); | ||
| 514 | |||
| 515 | // for(int i = 0; i < entries; i++) { | ||
| 516 | // free(namelist[i]); | ||
| 517 | // } | ||
| 518 | // free(namelist); | ||
| 519 | |||
| 520 | return NULL; | ||
| 463 | } | 521 | } |
| 464 | 522 | ||
| 465 | #endif \ No newline at end of file | 523 | #endif \ No newline at end of file |
