From 36dc7e18b66bb374d4c67a7f526c088636eaf9a2 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 2 Jan 2025 23:59:40 -0600 Subject: Filter out . and .. folders from scan results --- notes.txt | 4 ++-- src/main.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/notes.txt b/notes.txt index fb6f283..66af513 100644 --- a/notes.txt +++ b/notes.txt @@ -5,9 +5,9 @@ also encrypt the /root dir as well. Running as a daemon and setting things up in a good idea as well vxgg was not atomic in its encryption, in that if the encryption was in progress and interrupted for whatever reason, the original -file would be "irrecoverably" (as in vxgg had no error checking and couldn't tell the difference between an encrypted and +file would be "irrecoverably" fucked (as in vxgg had no error checking and couldn't tell the difference between an encrypted and unencrypted file other than through filename. You could manually unfuck the file yourself if you had the key, but it would be a -long and tedious process) fucked. I can't possibly make the entire operation of reading and writing to a file atomic, but I can +long and tedious process). I can't possibly make the entire operation of reading and writing to a file atomic, but I can make the action of "encrpytion" atomic; or, at least, I can make the encryption seem atomic through a cheat: hard linking. Turns out that you can make a file in /tmp, fill it with a whole bunch of shit, then hardlink it to somewhere else on the drive, and the file's contents will persist after a reboot. The idea here is to open a file in /tmp, write the encrypted contents to it, and then diff --git a/src/main.c b/src/main.c index bf110c2..4ecf545 100644 --- a/src/main.c +++ b/src/main.c @@ -4,15 +4,41 @@ #include #include -#include +#include +#include +#include #include +#include + +#include + +#include + +regex_t* initfilterregex() { + static regex_t *reg = NULL; + + if(reg != NULL) + return reg; + + reg = xcalloc(1, sizeof(*reg)); + if(regcomp(reg, "^(.{1,2})$", REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) + return NULL; + + return reg; +} int testfilter(const struct dirent *node) { + regex_t *reg = initfilterregex(); + + if(regexec(reg, node->d_name, 0, NULL, 0) != (REG_NOMATCH | REG_NOERROR) && S_ISDIR(DTTOIF(node->d_type))) + return 0; + return 1; } int main() { - // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion. No reason to do things in O(n2) time if I can do it in O(n) + // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion + // No reason to do things in O(n2) time if I can do it in O(n) int nnodes = -1; struct dirent **nodes = NULL; @@ -20,9 +46,22 @@ int main() { error(1, errno, "scandir broke"); for(int i = 0; i < nnodes; i++) { - printf("%s\n", nodes[i]->d_name); + printf("\"%s\"", nodes[i]->d_name); + #if defined _DIRENT_HAVE_D_TYPE + int mode = DTTOIF(nodes[i]->d_type); + printf(", Type: %s%s%s", + S_ISREG(mode) ? "Regular file" : "",\ + S_ISDIR(mode) ? "Directory" : "",\ + (!S_ISREG(mode) && !S_ISDIR(mode)) ? "Unknown" : ""); + + #endif + printf("\n"); } + // Ok so scandir is annoying and doesn't create a linked list, but rather an array of dirent objects. This means that if I want + // to iterate over the list myself and it'll be a pain in the ass to do so. That, or I can implement a linked list, which would + // also be a pain in the ass. I'm starting to remember why I finished vxgg the way I did + return 0; } \ No newline at end of file -- cgit v1.2.3