From d7466c73b29a8a28dbebdfb69c132c39546e5442 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Thu, 24 Jun 2021 01:17:34 -0600 Subject: [PATCH] serve simple file directories over 9p --- carts/another | 0 carts/other | 0 carts/test | 0 xrxs.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 carts/another create mode 100644 carts/other create mode 100644 carts/test diff --git a/carts/another b/carts/another new file mode 100644 index 0000000..e69de29 diff --git a/carts/other b/carts/other new file mode 100644 index 0000000..e69de29 diff --git a/carts/test b/carts/test new file mode 100644 index 0000000..e69de29 diff --git a/xrxs.c b/xrxs.c index 6a13ad0..afe6c80 100644 --- a/xrxs.c +++ b/xrxs.c @@ -4,6 +4,10 @@ #include #include <9p.h> #include +#include +#include + +#define CARTSLOC "./carts/" /* clang-format off */ @@ -119,6 +123,40 @@ void fsdestroyfile(File* f) { } } +String** listdir(char* path) { + String** self = malloc(128 * sizeof(String*)); + DIR* dir; + struct dirent* ent; + int i = 0; + char* c; + if ((dir = opendir(path)) != NULL) { + while ((ent = readdir(dir)) != NULL) { + c = ent->d_name; + if (scmp(c, ".") || scmp(c, "..")) { + continue; + } + self[i] = s_new(); + + while (*c) { + s_putc(self[i], *c++); + } + s_terminate(self[i++]); + } + closedir(dir); + } + self[i] = nil; + return self; +} + +Ramfile* createrf(Tree* t, char* path, char* filename, int perm) { + Ramfile* rf; + File* f = walkfile(t->root, path); + createfile(f, filename, nil, perm, nil); + rf = (Ramfile*)emalloc9p(sizeof *rf); + f->aux = rf; + return rf; +} + void writerf(Tree* t, char* path, char* data) { Ramfile* rf; File* f = walkfile(t->root, path); @@ -142,6 +180,8 @@ void threadmain(int argc, char* argv[]) { char* mtpt = nil; char* usocket = nil; int i; + String** cart; + char cartsloc[256] = "./carts/"; /* if -m PATH is supplied, mount on PATH */ /* if -s NAME is supplied, create a socket for the namespace */ @@ -161,8 +201,13 @@ void threadmain(int argc, char* argv[]) { fs.tree = alloctree(nil, nil, DMDIR | 0777, fsdestroyfile); tree = fs.tree; - createfile(tree->root, "carts", nil, 0444, nil); - writerf(tree, "carts", "uStrat\nkatbug\nSoulGrind\n"); + createfile(tree->root, "carts", nil, DMDIR | 0555, nil); + String** carts = listdir("carts/"); + cart = carts; + while (*cart) { + createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil); + cart++; + } fs.foreground = 1; if (argc >= 3) {