From 3df21ef899aafd2461570687ef3b9b1c9f9a555e Mon Sep 17 00:00:00 2001 From: kaa Date: Sun, 19 Nov 2023 11:32:23 -0800 Subject: Dump Canvas account directory structure. Start of directory parsing. --- canls.c | 99 +++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 43 deletions(-) (limited to 'canls.c') diff --git a/canls.c b/canls.c index f47656e..7907046 100644 --- a/canls.c +++ b/canls.c @@ -1,55 +1,68 @@ #include -#include -#include +#include /* calloc */ +#include /* memmove */ +#include /* chdir */ +#include "shared.h" -enum { - bufsize = 16, -}; - -typedef char byte; - -struct listing { - long id, folder_id, size; - char *uuid, *filename, *upload_status, *content_type, *url; - char *created_at, *updated_at, *unlock_at, *lock_at, thumbnail_url; - char *modified_at, *mime_class, *media_entry_id, *category; - byte locked, hidden, hidden_for_user, locked_for_user; -}; - -typedef struct listing Listing; - -int -main() +static char *read_dir(FILE *dir_dump, char *buf) { - FILE* infile = fopen("dump", "r"); - if (infile == NULL) { - fprintf(stderr, "No dump provided. Run dump.sh.\n"); - } - - char buf[bufsize]; - char *contents = NULL; + static char *contents = NULL; int read, size = 0; - while ((read = fread(buf, 1, bufsize, infile)) > 0) { + while ((read = fread(buf, 1, bufsize, dir_dump)) > 0) { size += read; contents = realloc(contents, size); memmove(&contents[size-read], buf, read); } + return contents; +} + +const char *key_delim = "\":"; +/* Return character pointer to beginning of value. */ +char * +getval(char *str, char *key) +{ + key = realloc(key, strlen(key) + strlen(key_delim) + 1); + strcat(key, key_delim); + char *rt = strstr(str, key); + if (rt == NULL) { + fprintf(stderr, "%s\n", "Invalid key.\n"); + } + return rt; +} + +/* Count directories. */ +int +count_dir(char *contents) +{ + int count = 0; + char *p = contents; + while ((p = strstr(p, "\n")) != NULL) { + count++; + p = &p[1]; /* Increment p past the current '\n'. */ + } + return count; +} + +int +main(int argc, char **argv) +{ + + char *path = calloc(pathsize, sizeof(char)); + path[0] = '/'; - int i, j = 0; - char ch; - Listing *l = calloc(1, sizeof(Listing)); - int l_size = sizeof(Listing); - for (i = 0; i < size; i++) { - ch = contents[i]; - switch (ch) { - case '\n': - j++; - l_size += sizeof(Listing); - l = realloc(l, l_size); - break; - default: - } + chdir("dump"); + FILE *root_stream = fopen("folders", "r"); + if (root_stream == NULL) { + fprintf(stderr, "Generate a dump of your Canvas" + " account directory structure" + " using dump.sh.\n"); + return 1; } - printf("%d\n", l_size); + + char buf[bufsize]; + char *root_contents = read_dir(root_stream, buf); + int root_count = count_dir(root_contents); + printf("%d\n", root_count); + return 0; } -- cgit v1.2.3