serve simple file directories over 9p

This commit is contained in:
Iris Lightshard 2021-06-24 01:17:34 -06:00
parent 6688673fd6
commit d7466c73b2
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
4 changed files with 47 additions and 2 deletions

0
carts/another Normal file
View file

0
carts/other Normal file
View file

0
carts/test Normal file
View file

49
xrxs.c
View file

@ -4,6 +4,10 @@
#include <thread.h>
#include <9p.h>
#include <stdio.h>
#include <dirent.h>
#include <libString.h>
#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) {