commit 939aaf00a856fbab7a16933e13b8c41f211ddaa4
Author: glenda <glenda@device>
Date: Mon, 24 Oct 2022 20:58:48 +0000
add src/btr
Diffstat:
6 files changed, 255 insertions(+), 0 deletions(-)
diff --git a/src/btr/btr.h b/src/btr/btr.h
@@ -0,0 +1,34 @@
+typedef struct Btrmsgheader Btrmsgheader;
+struct Btrmsgheader {
+ int op;
+ vlong id;
+ long nbytes;
+ vlong offset;
+};
+
+enum {
+ BNone,
+ BTstat,
+ BRstat,
+ BRerror,
+};
+
+typedef struct Btrstat Btrstat;
+struct Btrstat {
+ vlong id;
+ int type;
+ vlong length;
+};
+
+typedef struct Btrctl Btrctl;
+struct Btrctl {
+ int fd;
+};
+
+Btrstat bstat(Btrctl *bc, vlong id);
+Btrctl* initbtr(char *path);
+long bpread(Btrctl *bc, vlong id, void *buf, long nbytes, vlong offset);
+long bpwrite(Btrctl *bc, vlong id, void *buf, long nbytes, vlong offset);
+
+//bsendmsg
+//brecvmsg
+\ No newline at end of file
diff --git a/src/btr/btrcon.c b/src/btr/btrcon.c
@@ -0,0 +1,21 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+#include <btr.h>
+
+void
+main(void)
+{
+ Biobuf *bp = Bfdopen(0, OREAD);
+ Btrctl *bc = initbtr("/mnt/btr/data");
+ for (;;) {
+ // char *in = Brdstr(bp, '\n', 1);
+ // parse
+ // print("got %s\n", in);
+ // free(in);
+
+ Btrstat st = bstat(bc, 0);
+ print("%lld %d %lld\n", st);
+ }
+}
diff --git a/src/btr/fs.c b/src/btr/fs.c
@@ -0,0 +1,159 @@
+#include <u.h>
+#include <libc.h>
+#include <fcall.h>
+#include <thread.h>
+#include <9p.h>
+
+#include <btr.h>
+
+typedef struct Snek Snek;
+struct Snek {
+ void *aux;
+ Snek* next;
+};
+
+Snek *head, *tail;
+
+
+typedef struct Cell Cell;
+struct Cell {
+ int type;
+ long n;
+ char *p;
+};
+
+char *mkmsg(int op, vlong id, long nbytes, vlong offset, char *data);
+void pushmsg(char *msg);
+char *pullmsg(void);
+void msgerror(vlong id, char *str);
+
+/*
+ Cell's type can be:
+ - Atom
+ arbitrary length bytestring
+ - NULL
+ technically equivalent to Atom of length 0
+ - Pair
+ required opeartions:
+ - fetch (id -> Cell *)
+ - store (id, Cell * -> _ )
+*/
+
+Cell *
+fetch(long id)
+{
+ return nil;
+}
+
+void
+store(long id, Cell *c)
+{
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s\n", argv0);
+ exits("usage");
+}
+
+void
+fs_write(Req *r)
+{
+ Btrmsgheader *m;
+ if (r->ifcall.count < sizeof(Btrmsgheader)) {
+ // msg too short, provide BTerror
+ msgerror(0, smprint("msg too short"));
+ r->ofcall.count = r->ifcall.count;
+ respond(r, nil);
+ return;
+ }
+ m = (Btrmsgheader *)r->ifcall.data;
+ switch (m->op) {
+ case BTstat:
+ // provide BRstat
+ msgerror(m->id, smprint("BTstat not implemented yet"));
+ r->ofcall.count = r->ifcall.count;
+ break;
+ default:
+ // unexpected op, provide BTerror
+ msgerror(m->id, smprint("unknown op %X", m->op));
+ r->ofcall.count = r->ifcall.count;
+ }
+ respond(r, nil);
+}
+
+void
+fs_read(Req *r)
+{
+ char *msg = pullmsg();
+ if (msg == nil) {
+ // TODO: should block instead of returning read of 0 length, probably?
+ respond(r, nil);
+ return;
+ }
+ Btrmsgheader *mh = (Btrmsgheader *)msg;
+ long count = mh->nbytes + sizeof(Btrmsgheader);
+ if (count > r->ifcall.count) {
+ // TODO: this should be an error, probably?
+ count = r->ifcall.count;
+ }
+ memcpy(r->ofcall.data, msg, count);
+ r->ofcall.count = count;
+ respond(r, nil);
+}
+
+void
+main(int argc, char **argv)
+{
+ ARGBEGIN{
+ default:
+ usage();
+ } ARGEND
+ if (argc > 0) usage();
+ Srv btrsrv = {
+ .read = fs_read,
+ .write = fs_write,
+ };
+ btrsrv.tree = alloctree("btr", "btr", DMDIR|0555, nil);
+ File *data = createfile(btrsrv.tree->root, "data", "btr", DMAPPEND|0666, nil);
+ postmountsrv(&btrsrv, "btr", "/mnt/btr", MREPL);
+}
+
+char *
+mkmsg(int op, vlong id, long nbytes, vlong offset, char *data)
+{
+ Btrmsgheader mh = {
+ .op = op, .id = id, .nbytes = nbytes, .offset = offset,
+ };
+ char *msg = malloc(sizeof(Btrmsgheader) + nbytes);
+ memcpy(msg, &mh, sizeof(Btrmsgheader));
+ memcpy(msg + sizeof(Btrmsgheader), data, nbytes);
+ return msg;
+}
+
+void
+pushmsg(char *msg)
+{
+ Snek *next = mallocz(sizeof(Snek), 1);
+ next->aux = msg;
+ if (head != nil) head->next = next;
+ else tail = next;
+ head = next;
+}
+
+char *
+pullmsg(void)
+{
+ if (tail == nil) return nil;
+ char *msg = tail->aux;
+ tail = tail->next;
+ if (tail == nil) head = nil;
+ return msg;
+}
+
+void
+msgerror(vlong id, char *str)
+{
+ pushmsg(mkmsg(BRerror, id, strlen(str), 0, str));
+}
diff --git a/src/btr/libbtr.c b/src/btr/libbtr.c
@@ -0,0 +1,33 @@
+#include <u.h>
+#include <libc.h>
+
+#include <btr.h>
+
+Btrctl *
+initbtr(char *path)
+{
+ Btrctl *bc;
+ int fd;
+ if (path == nil) path = "/mnt/btr/data";
+ fd = open(path, OREAD|OWRITE);
+ if (fd < 0) return nil;
+ bc = mallocz(sizeof(Btrctl), 1);
+ bc->fd = fd;
+ return bc;
+}
+
+Btrstat
+bstat(Btrctl *bc, vlong id)
+{
+ return (Btrstat){0, 0, 0};
+}
+
+long bpread(Btrctl *bc, vlong id, void *buf, long nbytes, vlong offset)
+{
+ return 0;
+}
+
+long bpwrite(Btrctl *bc, vlong id, void *buf, long nbytes, vlong offset)
+{
+ return 0;
+}
diff --git a/src/btr/mkfile b/src/btr/mkfile
@@ -0,0 +1,6 @@
+</$objtype/mkfile
+
+TARG=btrcon fs
+OFILES=libbtr.$O
+
+</sys/src/cmd/mkmany
diff --git a/src/btr/readme b/src/btr/readme
@@ -0,0 +1 @@
+BTR is a general purpose binary tree storage facility.