all repos — xrxs @ 36c208cb8895e59fd94e362abb5eda9fc0e4a4ba

experimental networked application/game server with 9p

playing with ramfs implemenatation
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmDJkEcACgkQO3+8IhRO
Y5j8RRAAh+3RcVn6vNCJVNoMhmPNTiJgE7MO5OVSu0PI+hy4j5u66+X0cKBZrZOJ
lb3nYfu4bWINNXuuwvBMMFdY5addIDx0Oo2FsSVMhwTHdtV6V4UvCZv1KoQvpOFL
ZO/VUGmfvOSxcbytrMJ8vSV5LITZhUEePxg6jyRxrvRqCkN3gWnEKyXK5rhE14r9
B7GpNCpQB3PoH6OV1dwAG4fvK/YRRqw0Ah3U/l45LjK9Qv861O0nwVWARpAifaz8
pchlygTregsHVvTH9uwY7QAcrlU5xlNdSD8GFAGeX/1uGqpDmYSClRIT4XzRjr6V
/ONQ2sGnV9sZ7dYYjcjFvFnn8xgdeSVFZc9/NK9b4U/DaW4xlp3qECeUHa7duXEy
A63z40XySrKmyrPbRZNj4zb8qnGHTt3u/AphHzbaCDVXcKGJFouZSaqi1PPQP6vc
duDHY/ug9rMLrSxxp/FeVSVCmMSS80juTpmAk0kX6yMVG/cBHQ0XP3ebPO6WvQUH
Szd9D6UNQLtSogHz0i3Wsd43DFr+4jHoWvj31JUc2Wbo8Wx8TcpqoWeFGWKtOx+n
1eP52vkEU1sNlGXgeACGC3riZSU5gSLUj/2K0LjxabGW8qIFtIDguh2yiof7+vsu
zgS8NEH8YDIkshCYjw8ad+IKHJdTIWh5wu++a6v6OU8Sk1vufUU=
=gDJC
-----END PGP SIGNATURE-----
commit

36c208cb8895e59fd94e362abb5eda9fc0e4a4ba

parent

b738d29f389d11012b9f4b82164422f487659bb2

1 files changed, 107 insertions(+), 9 deletions(-)

jump to
M xrxs.cxrxs.c

@@ -22,19 +22,119 @@ char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; }

/* clang-format on */ -int i = 0; +static char Ebad[] = "something bad happened"; +static char Enomem[] = "no memory"; + +typedef struct Ramfile Ramfile; +struct Ramfile { + char* data; + int ndata; +}; void fsread(Req* r) { - char numReads[32]; - i++; - sprintf(numReads, "Hello from 9p!\n%d reads\n", i); - readstr(r, numReads); + Ramfile* rf; + vlong offset; + long count; + + rf = r->fid->file->aux; + offset = r->ifcall.offset; + count = r->ifcall.count; + + /*print("read %ld %lld\n", *count, offset); */ + if (offset >= rf->ndata) { + r->ofcall.count = 0; + respond(r, nil); + return; + } + + if (offset + count >= rf->ndata) + count = rf->ndata - offset; + + memmove(r->ofcall.data, rf->data + offset, count); + r->ofcall.count = count; + respond(r, nil); +} + +void fswrite(Req* r) { + void* v; + Ramfile* rf; + vlong offset; + long count; + + rf = r->fid->file->aux; + offset = r->ifcall.offset; + count = r->ifcall.count; + + if (offset + count >= rf->ndata) { + v = realloc(rf->data, offset + count); + if (v == nil) { + respond(r, Enomem); + return; + } + rf->data = v; + rf->ndata = offset + count; + r->fid->file->dir.length = rf->ndata; + } + memmove(rf->data + offset, r->ifcall.data, count); + r->ofcall.count = count; respond(r, nil); } +void fscreate(Req* r) { + Ramfile* rf; + File* f; + + if ( + f = createfile( + r->fid->file, + r->ifcall.name, + nil, + r->ifcall.perm, + nil)) { + rf = emalloc9p(sizeof *rf); + f->aux = rf; + r->fid->file = f; + r->ofcall.qid = f->dir.qid; + respond(r, nil); + return; + } + respond(r, Ebad); +} + +void fsopen(Req* r) { + Ramfile* rf; + + rf = r->fid->file->aux; + + if (rf && (r->ifcall.mode & OTRUNC)) { + rf->ndata = 0; + r->fid->file->dir.length = 0; + } + + respond(r, nil); +} + +void fsdestroyfile(File* f) { + Ramfile* rf; + + /*fprint(2, "clunk\n"); */ + rf = f->aux; + if (rf) { + free(rf->data); + free(rf); + } +} + Srv fs = { + .open = fsopen, .read = fsread, + .write = fswrite, + .create = fscreate, }; + +int threadmaybackground(void) { + return 1; +} void threadmain(int argc, char* argv[]) { Tree* tree;

@@ -60,10 +160,8 @@ }

/* create a single file called 'hello'. reading it calls fsread() */ - tree = alloctree(nil, nil, DMDIR | 0555, nil); - fs.tree = tree; - fs.foreground = 1; - createfile(tree->root, "hello", nil, 0555, nil); + fs.tree = alloctree(nil, nil, DMDIR|0777, fsdestroyfile); + fs.foreground = 1; if (argc >= 3) { if (mtpt != nil && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0)