shithub: namespace-example

ref: da0a87c6b0b0294175e5f084118c8d587c65a8f4
dir: /main.c/

View raw version
#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";
}

void
nread(Req *r)
{

}

void
nwrite(Req *r)
{
}

void 
nopen(Req *r)
{

}

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
threadmain(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);
}