commit 209564e16cebbd4dfd05f3cc910daf9b7242edf4
parent 730dc196e3b0141b935eff497609c53fd9b6ed39
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sun, 14 Mar 2021 19:13:01 +0000
fs: rebuild how server handles data
Diffstat:
M | fs.c | | | 88 | +++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ |
1 file changed, 55 insertions(+), 33 deletions(-)
diff --git a/fs.c b/fs.c
@@ -4,28 +4,42 @@
#include <thread.h>
#include <9p.h>
+#include "octree.h"
+#define GROWDELTA 8
+
/* TODO: possibly replace these with enum in file->aux and switch
statement instead of if/else in fsread/fswrite */
-
-struct Nmsg {
- u64int id;
- u64int size;
- char data;
-};
-
File *data;
File *stream;
-Channel *c;
-void
-procrespond(void*)
+Node *db = nil;
+long nodes = 0;
+long dbsize = 0;
+
+int
+pushnode(void *v)
{
- Req *r;
- r = nil;
- while (recv(c, &r) == 1) {
- assert(r != nil);
- respond(r, nil);
+ Node *n, *np;
+ n = v;
+ if (dbsize <= nodes * NODESIZE) {
+ dbsize = (nodes + GROWDELTA) * NODESIZE;
+ db = realloc(db, dbsize);
+ if (db == nil) sysfatal("%r");
+ }
+ if (nodes == 0) {
+ memmove(&db[0], n, NODESIZE);
+ nodes = 1;
+ } else {
+ for (np = db;
+ (np < db + nodes) && (np->id < n->id);
+ np++);
+ if (np->id != n->id) {
+ if (np != db+nodes) memmove(np + 1, np, NODESIZE * (nodes + np - db));
+ nodes++;
+ }
+ memmove(np, n, NODESIZE);
}
+ return 0;
}
void
@@ -34,16 +48,13 @@ fsread(Req *r)
File *f;
f = r->fid->file;
if (f == data) {
- readstr(r, "data\n");
+ readbuf(r, db, nodes * NODESIZE);
respond(r, nil);
} else if (f == stream) {
- fprint(2, "stream read\n");
- readstr(r, "stream\n");
- send(c, &r);
- }
- else {
- respond(r, "unidentified file");
+ readstr(r, r->fid->aux);
+ respond(r, nil);
}
+ else respond(r, "unidentified file");
}
void
@@ -52,14 +63,12 @@ fswrite(Req *r)
File *f;
f = r->fid->file;
if (f == stream) {
- struct Nmsg *msg;
- msg = malloc(r->ifcall.count);
- memcpy(r->ifcall.data, msg, r->ifcall.count);
- r->ofcall.count = r->ifcall.count;
- free(msg);
+ pushnode(r->ifcall.data);
+ r->ofcall.count = NODESIZE;
respond(r, nil);
}
else {
+ fprint(2, "tttt\n");
respond(r, "not implemented");
}
}
@@ -73,20 +82,35 @@ fsflush(Req *r)
}
void
+fsopen(Req *r)
+{
+ r->fid->aux = smprint("fid=%uld\n", r->fid->fid);
+ respond(r, nil);
+}
+
+void
+fsdestroyfid(Fid *fid)
+{
+ free(fid->aux);
+}
+
+void
usage(void)
{
fprint(2, "usage: %s [-D][-m /mnt/octree][-s service]\n", argv0);
- threadexits("usage");
+ exits("usage");
}
Srv fs = {
+ .open = fsopen,
+ .destroyfid = fsdestroyfid,
.read = fsread,
.write = fswrite,
.flush = fsflush,
};
void
-threadmain (int argc, char **argv)
+main (int argc, char **argv)
{
char *srv, *mtpt;
srv = nil;
@@ -108,11 +132,9 @@ threadmain (int argc, char **argv)
if (argc > 0) usage();
- c = chancreate(sizeof(Req*), 1024);
fs.tree = alloctree("octree", "octree", 0777|DMDIR, nil),
data = createfile(fs.tree->root, "data", "octree", 0666, nil);
stream = createfile(fs.tree->root, "stream", "octree", 0666, nil);
- fprint(2, "launching threadpostmountsrv\n");
- proccreate(procrespond, nil, 1024 * 8);
- threadpostmountsrv(&fs, srv, mtpt, MREPL);
+ postmountsrv(&fs, srv, mtpt, MREPL);
+ exits(nil);
}