From d7c0f78d02ced81c7828453036e551f93c0d3b45 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Thu, 30 Apr 2026 19:28:31 -0400 Subject: [PATCH] core: Dump directory contents Signed-off-by: Chloe M. --- core/blobchain.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/core/blobchain.c b/core/blobchain.c index c593b52..60b3be0 100644 --- a/core/blobchain.c +++ b/core/blobchain.c @@ -3,9 +3,18 @@ * Provided under the BSD-3 clause */ +#define _DEFAULT_SOURCE +#include #include #include +#include +#include +#include #include "blobchain/common.h" +#include "blobchain/stream.h" + +/* Input directory path */ +static char *input_dir = NULL; static void help(void) @@ -13,6 +22,7 @@ help(void) printf("usage: ./blobchain \n"); printf("... [-h] Display this help menu\n"); printf("... [-v] Display the version\n"); + printf("... [-i] Input directory\n"); } static void @@ -21,6 +31,60 @@ version(void) printf("Version v%s\n", BLOBCHAIN_VERSION); } +static void +pack_foreach(const char *input_dir) +{ + char pathbuf[512]; + struct dirent *dirent; + DIR *dir; + + if (input_dir == NULL) { + return; + } + + if ((dir = opendir(input_dir)) == NULL) { + perror("opendir"); + return; + } + + while ((dirent = readdir(dir)) != NULL) { + if (dirent->d_name[0] == '.') { + continue; + } + + switch (dirent->d_type) { + case DT_DIR: + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); + pack_foreach(pathbuf); + printf("[d] %s\n", pathbuf); + break; + case DT_REG: + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); + printf("[r] %s\n", pathbuf); + break; + } + } +} + +static void +pack_dir(void) +{ + struct stat sb; + + if (stat(input_dir, &sb) < 0) { + perror("stat"); + return; + } + + /* Must be a directory */ + if (!S_ISDIR(sb.st_mode)) { + printf("fatal: given path is not a directory!\n"); + return; + } + + pack_foreach(input_dir); +} + int main(int argc, char **argv) { @@ -32,7 +96,7 @@ main(int argc, char **argv) return -1; } - while ((opt = getopt(argc, argv, "hv")) != -1) { + while ((opt = getopt(argc, argv, "hvi:")) != -1) { switch (opt) { case 'h': help(); @@ -40,8 +104,24 @@ main(int argc, char **argv) case 'v': version(); return -1; + case 'i': + input_dir = strdup(optarg); + if (input_dir == NULL) { + printf("fatal: out of memory\n"); + return -1; + } + + break; } } + if (input_dir == NULL) { + printf("fatal: expected input directory\n"); + help(); + return -1; + } + + pack_dir(); + free(input_dir); return 0; }