From 06b553ab3f6355e643049dad0ccb570a0bc0d613 Mon Sep 17 00:00:00 2001
From: kaa <kaa@disroot.org>
Date: Sat, 25 Nov 2023 12:40:23 -0800
Subject: WIP Directory traversal using go.

---
 old/canls.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 old/dump.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+)
 create mode 100644 old/canls.c
 create mode 100755 old/dump.sh

(limited to 'old')

diff --git a/old/canls.c b/old/canls.c
new file mode 100644
index 0000000..02a41e2
--- /dev/null
+++ b/old/canls.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>		/* calloc */
+#include <string.h>		/* memmove */
+#include <unistd.h>	/* chdir */
+#include "shared.h"
+
+static char *read_dir(FILE *dir_dump, char *buf)
+{
+	static char *contents = NULL;
+	int read, size = 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 = NULL;
+	if (argc > 1) {
+		path = argv[1];
+	}
+
+	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;
+	}
+
+	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;
+}
diff --git a/old/dump.sh b/old/dump.sh
new file mode 100755
index 0000000..bc50cfb
--- /dev/null
+++ b/old/dump.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+if [ -z $CANKEY ]
+then
+	echo Provide an API key in the environment variable CANKEY.
+	return 1
+fi
+if ! [ -d dump ]
+then
+	mkdir dump
+fi
+cd dump
+base=https://sdccd.instructure.com/api/v1/
+process_json()
+{
+	jq -M
+}
+get()
+{
+	url=$base$1
+	curl -s -X GET "$url" -H "Authorization: Bearer $CANKEY"  | jq -M
+}
+
+get users/self/folders > folders
+get users/self/files > files
+
+get_id()
+{
+	< folders sed -e 's/,.*//g' -e 's/.*://g'
+}
+get_name()
+{
+	< folders grep id.:$1 | sed -e 's/.*name":"//' -e 's/",.*//'
+}
+
+# Recursively resolve directories and files.
+# Empty response: []
+get_dir()
+{
+	id=$1
+	name=$2
+	echo $id
+	mkdir $id
+	cd $id
+	echo $name > name
+	get folders/$id/files > files
+	get folders/$id/folders > folders
+	for i in $(get_id)
+	do
+		get_dir $i "$(get_name $i)"
+	done
+	cd ..
+}
+
+for i in $(get_id)
+do
+	get_dir $i
+done
-- 
cgit v1.2.3