summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaa <kaa@disroot.org>2023-11-19 11:32:23 -0800
committerkaa <kaa@disroot.org>2023-11-19 11:32:23 -0800
commit3df21ef899aafd2461570687ef3b9b1c9f9a555e (patch)
tree91659e8964062709853460ad9ee80dbd1099edc9
parentc11b26e1f75b643997787b76f5181731058a28f9 (diff)
Dump Canvas account directory structure. Start of directory parsing.
-rw-r--r--canls.c99
-rw-r--r--canup.c2
-rw-r--r--dump10
-rwxr-xr-xdump.sh38
-rw-r--r--makefile3
-rw-r--r--shared.h28
6 files changed, 125 insertions, 55 deletions
diff --git a/canls.c b/canls.c
index f47656e..7907046 100644
--- a/canls.c
+++ b/canls.c
@@ -1,55 +1,68 @@
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <stdlib.h> /* calloc */
+#include <string.h> /* memmove */
+#include <unistd.h> /* 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;
}
diff --git a/canup.c b/canup.c
index ffb5ba1..a3ea0ee 100644
--- a/canup.c
+++ b/canup.c
@@ -4,7 +4,7 @@
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
-#include "config.h"
+#include "shared.h"
char *url = "https://sdccd.instructure.com/api/v1/users/self/files";
diff --git a/dump b/dump
deleted file mode 100644
index 116b04f..0000000
--- a/dump
+++ /dev/null
@@ -1,10 +0,0 @@
-"id":145759442,"uuid":"FBbPqRtIqzAsed4Nl1Huht1mWZCxauZfBQTYjbjd","folder_id":17503637,"display_name":"1-1.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759442/download?download_frd=1\u0026verifier=FBbPqRtIqzAsed4Nl1Huht1mWZCxauZfBQTYjbjd","size":457740,"created_at":"2023-10-10T18:22:45Z","updated_at":"2023-10-10T18:22:48Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:22:45Z","mime_class":"video","media_entry_id":"m-5VgVtDFKPLCjwPrGKvdEPJV7USYAS8p8","category":"uncategorized","locked_for_user":false
-"id":145759448,"uuid":"OFek0dF01u2BaEEBzV6VWsh7gipwrwd59ozmsMAR","folder_id":17503637,"display_name":"1-2.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759448/download?download_frd=1\u0026verifier=OFek0dF01u2BaEEBzV6VWsh7gipwrwd59ozmsMAR","size":698610,"created_at":"2023-10-10T18:23:48Z","updated_at":"2023-10-10T18:23:51Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:23:48Z","mime_class":"video","media_entry_id":"m-pv8VkiJkWJDxxkJfhoGs4dSYYLom4j6","category":"uncategorized","locked_for_user":false
-"id":145808071,"uuid":"FqiRoD9sO2WOJqzsDrQXNlW3YyDWv5J87ZoKwBwk","folder_id":17503637,"display_name":"1-3.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145808071/download?download_frd=1\u0026verifier=FqiRoD9sO2WOJqzsDrQXNlW3YyDWv5J87ZoKwBwk","size":7859303,"created_at":"2023-10-15T00:11:04Z","updated_at":"2023-10-15T00:16:10Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-15T00:11:04Z","mime_class":"video","media_entry_id":"m-2iJ8QpBNPNk9Dz9jxeE6iFgXtMCR3Pxi","category":"uncategorized","locked_for_user":false
-"id":145759343,"uuid":"bw5CbntslXR4hpfaE25sP8UwTSbUiwnleqCs7yL3","folder_id":17503637,"display_name":"1.mp4","filename":"1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759343/download?download_frd=1\u0026verifier=bw5CbntslXR4hpfaE25sP8UwTSbUiwnleqCs7yL3","size":394585,"created_at":"2023-10-10T18:13:47Z","updated_at":"2023-10-10T18:13:53Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:13:47Z","mime_class":"video","media_entry_id":"m-4AaUe8Wsar2aeUR2Gif6cuqTphvyoNqz","category":"uncategorized","locked_for_user":false
-"id":145759359,"uuid":"AClgbYCaPnUaXx7XoIi4Uqv95Mj6tboBTvbKVQhi","folder_id":17503637,"display_name":"2.mp4","filename":"2.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759359/download?download_frd=1\u0026verifier=AClgbYCaPnUaXx7XoIi4Uqv95Mj6tboBTvbKVQhi","size":311927,"created_at":"2023-10-10T18:16:38Z","updated_at":"2023-10-10T18:21:39Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:16:38Z","mime_class":"video","media_entry_id":"m-p3NrT3AXosPwAcX7f1PawKfEGYjsQRw","category":"uncategorized","locked_for_user":false
-"id":145759375,"uuid":"LfdmKamZskIckgWc9tF2Wky2R7AxO5K58v4nPdkI","folder_id":17503637,"display_name":"3.mp4","filename":"3.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759375/download?download_frd=1\u0026verifier=LfdmKamZskIckgWc9tF2Wky2R7AxO5K58v4nPdkI","size":null,"created_at":"2023-10-10T18:18:16Z","updated_at":"2023-10-10T18:18:16Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:18:16Z","mime_class":"video","media_entry_id":"m-2BwVBf9erBu5Q6tXn1knyxZ7FfeJDEQM","category":"uncategorized","locked_for_user":false
-"id":145759410,"uuid":"90CiCh9Dp1MHqA8xxpFaethz99Nf8LS4MysPtOZa","folder_id":17503637,"display_name":"4.mp4","filename":"4.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759410/download?download_frd=1\u0026verifier=90CiCh9Dp1MHqA8xxpFaethz99Nf8LS4MysPtOZa","size":513597,"created_at":"2023-10-10T18:20:22Z","updated_at":"2023-10-10T18:20:25Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:20:22Z","mime_class":"video","media_entry_id":"m-4a4DtDGce89fexDZPcZwWfvLtF3CLLbi","category":"uncategorized","locked_for_user":false
-"id":145759517,"uuid":"g0XdLJIZyfOYtj11QzKDu5dQWHmUUK2QxY8qinQu","folder_id":17503637,"display_name":"7_1-1.mp4","filename":"7_1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759517/download?download_frd=1\u0026verifier=g0XdLJIZyfOYtj11QzKDu5dQWHmUUK2QxY8qinQu","size":512562,"created_at":"2023-10-10T18:31:08Z","updated_at":"2023-10-10T18:31:09Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:31:08Z","mime_class":"video","media_entry_id":"m-xgCoyMC1GBbEsp7VQyVygzHT34dAovn","category":"uncategorized","locked_for_user":false
-"id":145759511,"uuid":"nXeUtVHYtGUarYFm1N8Wo1jXf6oryNiw36qAbVts","folder_id":17503637,"display_name":"7_1.mp4","filename":"7_1.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759511/download?download_frd=1\u0026verifier=nXeUtVHYtGUarYFm1N8Wo1jXf6oryNiw36qAbVts","size":512562,"created_at":"2023-10-10T18:30:37Z","updated_at":"2023-10-10T18:30:38Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:30:37Z","mime_class":"video","media_entry_id":"m-9VTHrTDPdeTL5CbshFAM7vFXwVTBa8R","category":"uncategorized","locked_for_user":false
-"id":145759527,"uuid":"YTRw1Fii0QqILHlmfhsnSJVYB9nDak9kc1EDMjcB","folder_id":17503637,"display_name":"7_2.mp4","filename":"7_2.mp4","upload_status":"success","content-type":"video/mp4","url":"https://sdccd.instructure.com/files/145759527/download?download_frd=1\u0026verifier=YTRw1Fii0QqILHlmfhsnSJVYB9nDak9kc1EDMjcB","size":311927,"created_at":"2023-10-10T18:32:22Z","updated_at":"2023-10-10T18:32:24Z","unlock_at":null,"locked":false,"hidden":true,"lock_at":null,"hidden_for_user":true,"thumbnail_url":null,"modified_at":"2023-10-10T18:32:22Z","mime_class":"video","media_entry_id":"m-2sf1gc4JAtY95rs67DvBJAbuAjw7JCCR","category":"uncategorized","locked_for_user":false
diff --git a/dump.sh b/dump.sh
index 21ae5da..18ed1f4 100755
--- a/dump.sh
+++ b/dump.sh
@@ -1,4 +1,40 @@
#!/bin/sh
-curl -s -X GET "https://sdccd.instructure.com/api/v1/users/self/files" -H "Authorization: Bearer $CANKEY" | tr '}' '\n' | sed -e 's/{//g' -e's/\[//g' -e 's/\]//g' -e 's/^,//g' > dump
+cd dump
+base=https://sdccd.instructure.com/api/v1/
+get()
+{
+ url=$base$1
+ curl -s -X GET "$url" -H "Authorization: Bearer $CANKEY" | tr '}' '\n' | sed -e 's/{//g' -e's/\[//g' -e 's/\]//g' -e 's/^,//g'
+}
+get users/self/folders > folders
+get users/self/files > files
+fi
+
+get_id()
+{
+ < folders sed -e 's/,.*//g' -e 's/.*://g'
+}
+
+# Recursively resolve directories and files.
+# Empty response: []
+get_dir()
+{
+ id=$1
+ echo $id
+ mkdir $id
+ cd $id
+ get folders/$id/files > files
+ get folders/$id/folders > folders
+ for i in $(get_id)
+ do
+ get_dir $i
+ done
+ cd ..
+}
+
+for i in $(get_id)
+do
+ get_dir $i
+done
diff --git a/makefile b/makefile
index 15425c0..e8e40e8 100644
--- a/makefile
+++ b/makefile
@@ -8,5 +8,8 @@ all: $(OUT)
.c:
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $<
+ strip $@
clean:
rm $(OUT)
+dist: clean
+ rm -rf dump/*
diff --git a/shared.h b/shared.h
new file mode 100644
index 0000000..618d3df
--- /dev/null
+++ b/shared.h
@@ -0,0 +1,28 @@
+enum {
+ cmdsize = 4096,
+ pathsize = 1024,
+ 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;
+
+struct directory {
+ char *context_type, updated_at, folders_url, files_url, full_name;
+ char *lock_at, name, *created_at, *unlock_at;
+ long context_id, files_count, position, id, folders_count;
+ long parent_folder_id;
+ byte hidden, hidden_for_user, locked, locked_for_users;
+ byte for_submissions;
+};
+typedef struct directory Directory;
+
+char *upload_key;