ref: f86d02fc768075a4cfab130f340174fddbd69c2c
dir: /main.c/
#include <u.h> #include <libc.h> #include <fcall.h> #include <thread.h> #include <9p.h> enum { Qroot, Qitem1, Qitem2, }; typedef struct F { char *name; Qid qid; ulong mode; } F; F root = { "/", {Qroot, 0, QTDIR}, 0555|DMDIR }; F roottab[] = { "item1", {Qitem1, 0, QTFILE}, 0444, "item2", {Qitem2, 0, QTFILE}, 0444, }; enum {Cmdrange}; Cmdtab cmd[] = { Cmdrange, "range", 3, }; F* filebypath(uvlong path) { int i; if(path == Qroot) return &root; for(i = 0; i < nelem(roottab); i++) if(path == roottab[i].qid.path) return &roottab[i]; return nil; } void fillstat(Dir *d, uvlong path, char *user) { F *f; f = filebypath(path); d->name = estrdup9p(f->name); d->qid = f->qid; d->mode = f->mode; d->uid = estrdup9p(user); d->gid = estrdup9p(user); d->muid = estrdup9p(user); d->length = 0; d->atime = time(0); d->mtime = time(0); } void nattach(Req *r) { r->fid->qid = filebypath(Qroot)->qid; r->ofcall.qid = r->fid->qid; respond(r, nil); } char* nwalk1(Fid *fid, char *name, Qid *qid) { int i; switch(fid->qid.path){ case Qroot: for(i = 0; i < nelem(roottab); i++){ if(strcmp(roottab[i].name, name) == 0){ *qid = roottab[i].qid; fid->qid = *qid; return nil; } } if(strcmp("..", name) == 0){ *qid = root.qid; fid->qid = *qid; return nil; } break; } return "not found"; } int rootgen(int n, Dir *d, void *aux) { Req *r = aux; if(n >= nelem(roottab)) return -1; fillstat(d, roottab[n].qid.path, r->fid->uid); return 0; } void nread(Req *r) { char buf[128]; int n = 0; uvlong path = r->fid->qid.path; vlong offset; long count; offset = r->ifcall.offset; if(offset >= 1){ r->ofcall.count = 0; respond(r, nil); return; } if(path == Qroot){ dirread9p(r, rootgen, r); respond(r, nil); return; } switch(path){ case Qitem1: n = snprint(buf, sizeof buf, "%s\n", "test"); break; case Qitem2: n = snprint(buf, sizeof buf, "%s\n", "lol test again"); break; } if(r->ifcall.count < n) n = r->ifcall.count; r->ofcall.count = n; memmove(r->ofcall.data, buf, n); respond(r, nil); } void nwrite(Req *r) { respond(r, nil); } void nopen(Req *r) { respond(r, nil); } void nstat(Req *r) { fillstat(&r->d, r->fid->qid.path, r->fid->uid); respond(r, nil); return; } void ndestroyfid(Fid *fid) { if(fid->aux != nil) free(fid->aux); } Srv fs = { .attach = nattach, .walk1 = nwalk1, .open = nopen, .stat = nstat, .read = nread, .write = nwrite, .destroyfid = ndestroyfid, }; void main(int argc, char *argv[]) { print("This is a demo of namespace in plan9/9p \n"); print("This is not meant for real world use and is just a simple demo \n"); char *mtpt, *srvn; mtpt = "/mnt/namespace-test"; srvn = nil; postmountsrv(&fs, srvn, mtpt, MREPL); exits(nil); }