diff options
| author | @syxhe <https://t.me/syxhe> | 2025-12-28 03:07:43 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-12-28 03:07:43 -0600 |
| commit | c600c1efbfae6dc893be9203b9756ecf320de7e5 (patch) | |
| tree | 4399dd3f84f3604d26231ffa377762dd47ed29d1 /src | |
| parent | 73902dc1b3dad91f98b785f5a742e4e44c7e7d73 (diff) | |
Finish _cryptscan__process_scandir impl
Diffstat (limited to 'src')
| -rw-r--r-- | src/encryption.c | 80 |
1 files changed, 68 insertions, 12 deletions
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) { | |||
| 458 | return 1; | 458 | return 1; |
| 459 | } | 459 | } |
| 460 | 460 | ||
| 461 | /// helper function to deduplicate code for dealing with scandir | 461 | // TODO: Implement |
| 462 | int _cryptscan__crypt(void *data) { | ||
| 463 | |||
| 464 | } | ||
| 465 | |||
| 466 | // TODO: Implement | ||
| 467 | int _cryptscan__scan(void *data) { | ||
| 468 | |||
| 469 | } | ||
| 470 | |||
| 471 | /// helper function to deduplicate code for dealing with scandir. Scans the directory at `folder` and places dirents into their respective lists | ||
| 462 | int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { | 472 | int _cryptscan__process_scandir(const char * const folder, taskqueue *toscan, ctqueue *tocrypt) { |
| 463 | if(!folder || !toscan || !tocrypt) return -1; | 473 | if(!folder || !toscan || !tocrypt) return -1; |
| 464 | 474 | ||
| 475 | int res = 0; | ||
| 465 | struct dirent **namelist = NULL; | 476 | struct dirent **namelist = NULL; |
| 466 | int entries = scandir(folder, &namelist, _cryptscan__selector, alphasort); | 477 | int entries = scandir(folder, &namelist, _cryptscan__selector, alphasort); |
| 467 | if(!toscan || !tocrypt || entries < 0) goto _cryptscan__process_scandir_ERR; | 478 | if(!toscan || !tocrypt || entries < 0) {res = -1; goto _cryptscan__process_scandir_CLEANUP;} |
| 468 | 479 | ||
| 480 | struct stat sb; char *tmpname = NULL; task *tmptsk = NULL; char tflag = 0; | ||
| 469 | for(int i = 0; i < entries; i++) { | 481 | for(int i = 0; i < entries; i++) { |
| 482 | tmpname = strdup(namelist[i]->d_name); | ||
| 483 | 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;} | ||
| 484 | |||
| 470 | switch(namelist[i]->d_type) { | 485 | switch(namelist[i]->d_type) { |
| 471 | // Try to stat the file if it's unknown | 486 | // Try to stat the file if it's unknown |
| 472 | case DT_UNKNOWN: | 487 | case DT_UNKNOWN: |
| 488 | if(stat(namelist[i]->d_name, &sb) < 0) goto UNKNOWN; | ||
| 489 | if(S_ISDIR(sb.st_mode)) goto SCAN; | ||
| 490 | if(S_ISREG(sb.st_mode)) goto CRYPT; | ||
| 473 | break; | 491 | break; |
| 474 | 492 | ||
| 493 | |||
| 475 | // Add it to the ctq | 494 | // Add it to the ctq |
| 476 | case DT_REG: | 495 | case DT_REG: CRYPT: |
| 496 | tmptsk = task_new(_cryptscan__crypt, free, tmpname); | ||
| 497 | if(!tmptsk) { | ||
| 498 | if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not generate crypt task for \"%s\"",, tmpname); | ||
| 499 | break; | ||
| 500 | } | ||
| 501 | |||
| 502 | if(ctqueue_waitpush(tocrypt, tmptsk) < 0) { | ||
| 503 | if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not add crypt task to ctq",); | ||
| 504 | task_free(tmptsk); | ||
| 505 | break; | ||
| 506 | } | ||
| 507 | |||
| 508 | res++; | ||
| 509 | tflag++; | ||
| 477 | break; | 510 | break; |
| 478 | 511 | ||
| 512 | |||
| 479 | // Add it to the scanlist | 513 | // Add it to the scanlist |
| 480 | case DT_DIR: | 514 | case DT_DIR: SCAN: |
| 515 | tmptsk = task_new(_cryptscan__scan, free, tmpname); | ||
| 516 | if(!tmptsk) { | ||
| 517 | if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not generate scan task for \"%s\"",, tmpname); | ||
| 518 | break; | ||
| 519 | } | ||
| 520 | |||
| 521 | if(taskqueue_push(toscan, tmptsk) < 0) { | ||
| 522 | if(___VXGG___VERBOSE_ERRORS___) WARN(errno, "<_cryptscan__process_scandir> Warning: Could not add scan task to scanlist",); | ||
| 523 | task_free(tmptsk); | ||
| 524 | break; | ||
| 525 | } | ||
| 526 | |||
| 527 | res++; | ||
| 528 | tflag++; | ||
| 481 | break; | 529 | break; |
| 482 | 530 | ||
| 531 | |||
| 483 | // Ignore or spit out a warning | 532 | // Ignore or spit out a warning |
| 484 | default: | 533 | default: UNKNOWN: |
| 534 | if(___VXGG___VERBOSE_ERRORS___) | ||
| 535 | WARN(ENOSYS, "<_cryptscan__process_scandir> Info: file \"%s\"s type is unsupported",, namelist[i]->d_name); | ||
| 485 | break; | 536 | break; |
| 486 | } | 537 | } |
| 538 | if(!tflag) free(tmpname); | ||
| 539 | tflag = 0; | ||
| 487 | } | 540 | } |
| 488 | 541 | ||
| 489 | _cryptscan__process_scandir_ERR: | 542 | _cryptscan__process_scandir_CLEANUP: |
| 543 | for(int i = 0; i < entries; i++) { | ||
| 544 | free(namelist[i]); | ||
| 545 | } | ||
| 546 | free(namelist); | ||
| 490 | 547 | ||
| 491 | return -1; | 548 | return res; |
| 492 | } | 549 | } |
| 493 | 550 | ||
| 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 | 551 | // 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) { | |||
| 504 | // Initialize the lists | 561 | // Initialize the lists |
| 505 | if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; | 562 | if(_cryptscan__process_scandir(start, toscan, tocrypt) < 1) goto cryptscan_ERR; |
| 506 | 563 | ||
| 564 | // Loop through the scanlist until it's empty | ||
| 565 | while(taskqueue_size(toscan) > 0) { | ||
| 566 | task_fired(taskqueue_pop(toscan)); | ||
| 567 | } | ||
| 507 | 568 | ||
| 508 | taskqueue_free(toscan); | 569 | taskqueue_free(toscan); |
| 509 | return tocrypt; | 570 | return tocrypt; |
| @@ -512,11 +573,6 @@ cryptscan_ERR: | |||
| 512 | taskqueue_free(toscan); | 573 | taskqueue_free(toscan); |
| 513 | ctqueue_free(tocrypt); | 574 | ctqueue_free(tocrypt); |
| 514 | 575 | ||
| 515 | // for(int i = 0; i < entries; i++) { | ||
| 516 | // free(namelist[i]); | ||
| 517 | // } | ||
| 518 | // free(namelist); | ||
| 519 | |||
| 520 | return NULL; | 576 | return NULL; |
| 521 | } | 577 | } |
| 522 | 578 | ||
