we can write to ctl; thanks sigrid :)
This commit is contained in:
parent
28775d390e
commit
a0163016c2
2 changed files with 66 additions and 109 deletions
16
hash.c
Normal file
16
hash.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
|
||||
unsigned long long hash(char* str) {
|
||||
unsigned long long h;
|
||||
unsigned char* p;
|
||||
|
||||
h = 0;
|
||||
for (p = (unsigned char*)str; *p != '\0'; p++)
|
||||
h = 37 * h + *p;
|
||||
return h; // or, h % ARRAY_SIZE;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
printf("%llu\n", hash(argv[1]));
|
||||
return 0;
|
||||
}
|
159
xrxs.c
159
xrxs.c
|
@ -34,32 +34,35 @@ static int serving = 1;
|
|||
static char Ebad[] = "something bad happened";
|
||||
static char Enomem[] = "no memory";
|
||||
|
||||
typedef struct Ramfile Ramfile;
|
||||
struct Ramfile {
|
||||
enum { CTL = 1, SLOT, CART, REALM, UNIVERSE };
|
||||
|
||||
typedef struct Aux Aux;
|
||||
struct Aux {
|
||||
int type;
|
||||
char* data;
|
||||
int ndata;
|
||||
int count;
|
||||
};
|
||||
|
||||
void fsread(Req* r) {
|
||||
Ramfile* rf;
|
||||
Aux* a;
|
||||
vlong offset;
|
||||
long count;
|
||||
|
||||
rf = r->fid->file->aux;
|
||||
a = r->fid->file->aux;
|
||||
offset = r->ifcall.offset;
|
||||
count = r->ifcall.count;
|
||||
|
||||
/*print("read %ld %lld\n", *count, offset); */
|
||||
if (offset >= rf->ndata) {
|
||||
if (offset >= a->count) {
|
||||
r->ofcall.count = 0;
|
||||
respond(r, nil);
|
||||
return;
|
||||
}
|
||||
|
||||
if (offset + count >= rf->ndata)
|
||||
count = rf->ndata - offset;
|
||||
if (offset + count >= a->count)
|
||||
count = a->count - offset;
|
||||
|
||||
memmove(r->ofcall.data, rf->data + offset, count);
|
||||
memmove(r->ofcall.data, a->data + offset, count);
|
||||
r->ofcall.count = count;
|
||||
respond(r, nil);
|
||||
}
|
||||
|
@ -76,25 +79,25 @@ unsigned long long hash(char* str) {
|
|||
|
||||
void fswrite(Req* r) {
|
||||
void* v;
|
||||
Ramfile* rf;
|
||||
Aux* a;
|
||||
vlong offset;
|
||||
long count;
|
||||
|
||||
rf = r->fid->file->aux;
|
||||
a = r->fid->file->aux;
|
||||
offset = r->ifcall.offset;
|
||||
count = r->ifcall.count;
|
||||
|
||||
if (offset + count >= rf->ndata) {
|
||||
v = realloc(rf->data, offset + count);
|
||||
if (offset + count >= a->count) {
|
||||
v = realloc(a->data, offset + count);
|
||||
if (v == nil) {
|
||||
respond(r, Enomem);
|
||||
return;
|
||||
}
|
||||
rf->data = v;
|
||||
rf->ndata = offset + count;
|
||||
r->fid->file->dir.length = rf->ndata;
|
||||
a->data = v;
|
||||
a->count = offset + count;
|
||||
r->fid->file->dir.length = a->count;
|
||||
}
|
||||
memmove(rf->data + offset, r->ifcall.data, count);
|
||||
memmove(a->data + offset, r->ifcall.data, count);
|
||||
r->ofcall.count = count;
|
||||
respond(r, nil);
|
||||
}
|
||||
|
@ -102,87 +105,48 @@ void fswrite(Req* r) {
|
|||
void handlectl(Req* r) {
|
||||
char cmd[16];
|
||||
char* c = r->ifcall.data;
|
||||
int i;
|
||||
|
||||
while (*c && *c != ' ') {
|
||||
ccat(cmd, *c);
|
||||
for (i = 0; i < r->ifcall.count; i++) {
|
||||
ccat(cmd, *c++);
|
||||
}
|
||||
|
||||
unsigned long long const cmd_hashv = hash(cmd);
|
||||
|
||||
switch (cmd_hashv) {
|
||||
case SHUTDOWN_HASHV:
|
||||
serving = 0;
|
||||
printf("what");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
r->ofcall.count = 16;
|
||||
r->fid->file->dir.length = 16;
|
||||
respond(r, nil);
|
||||
r->ofcall.count = r->ifcall.count;
|
||||
r->fid->file->dir.length = r->ifcall.count;
|
||||
}
|
||||
|
||||
void xrxswrite(Req* r) {
|
||||
unsigned int filename_hashv = hash(r->ifcall.name);
|
||||
printf(r->ifcall.name);
|
||||
switch (filename_hashv) {
|
||||
case CTL_HASHV:
|
||||
Aux* a = r->fid->file->aux;
|
||||
switch (a->type) {
|
||||
case CTL:
|
||||
handlectl(r);
|
||||
break;
|
||||
default:
|
||||
fswrite(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void fscreate(Req* r) {
|
||||
Ramfile* rf;
|
||||
File* f;
|
||||
printf("eee");
|
||||
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 xrxscreate(Req* r) {
|
||||
unsigned int filename_hashv = hash(r->ifcall.name);
|
||||
printf("fff");
|
||||
switch (filename_hashv) {
|
||||
case CTL_HASHV:
|
||||
handlectl(r);
|
||||
break;
|
||||
default:
|
||||
fscreate(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void fsopen(Req* r) {
|
||||
Ramfile* rf;
|
||||
printf("ooo");
|
||||
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;
|
||||
void fsopen(Req* r) { respond(r, nil); }
|
||||
|
||||
/*fprint(2, "clunk\n"); */
|
||||
rf = f->aux;
|
||||
if (rf) {
|
||||
free(rf->data);
|
||||
free(rf);
|
||||
void wstat(Req* r) { respond(r, nil); }
|
||||
|
||||
void fsdestroyfile(File* f) {
|
||||
Aux* a = f->aux;
|
||||
if (a && a->data) {
|
||||
free(a->data);
|
||||
free(a);
|
||||
} else if (a) {
|
||||
free(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,36 +175,12 @@ String** listdir(char* path) {
|
|||
return self;
|
||||
}
|
||||
|
||||
Ramfile* createrf(Tree* t, char* path, char* filename, int perm) {
|
||||
Ramfile* rf;
|
||||
File* f = walkfile(t->root, path);
|
||||
createfile(f, filename, nil, perm, nil);
|
||||
rf = (Ramfile*)emalloc9p(sizeof *rf);
|
||||
f->aux = rf;
|
||||
return rf;
|
||||
}
|
||||
|
||||
void writerf(Tree* t, char* path, char* data) {
|
||||
Ramfile* rf;
|
||||
File* f = walkfile(t->root, path);
|
||||
rf = (Ramfile*)emalloc9p(sizeof *rf);
|
||||
rf->data = data;
|
||||
rf->ndata = slen(data);
|
||||
f->aux = rf;
|
||||
}
|
||||
|
||||
void xrxswstat(Req* r) {
|
||||
r->fid->omode = OWRITE;
|
||||
respond(r, nil);
|
||||
}
|
||||
|
||||
Srv fs = {
|
||||
.open = fsopen,
|
||||
.read = fsread,
|
||||
.write = xrxswrite,
|
||||
.create = xrxscreate,
|
||||
.wstat = xrxswstat
|
||||
};
|
||||
.create = fsopen,
|
||||
.wstat = wstat};
|
||||
|
||||
int threadmaybackground(void) { return 1; }
|
||||
|
||||
|
@ -250,9 +190,10 @@ void threadmain(int argc, char* argv[]) {
|
|||
char* usocket = nil;
|
||||
int i;
|
||||
String** cart;
|
||||
char cartsloc[256] = "./carts/";
|
||||
char ctlbuf[128] = "some data";
|
||||
Ramfile ctlfile = {ctlbuf, 128};
|
||||
Aux* ctlfile = malloc(sizeof(Aux));
|
||||
ctlfile->type = CTL;
|
||||
ctlfile->data = malloc(256 * sizeof(char));
|
||||
ctlfile->count = 0;
|
||||
|
||||
/* if -m PATH is supplied, mount on PATH */
|
||||
/* if -s NAME is supplied, create a socket for the namespace */
|
||||
|
@ -270,18 +211,18 @@ void threadmain(int argc, char* argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
fs.foreground = 1;
|
||||
fs.tree = alloctree(nil, nil, DMDIR | 0777, fsdestroyfile);
|
||||
tree = fs.tree;
|
||||
createfile(tree->root, "carts", nil, DMDIR | 0555, nil);
|
||||
createfile(tree->root, "ctl", nil, 0600, &ctlfile);
|
||||
|
||||
closefile(createfile(tree->root, "carts", nil, DMDIR | 0555, nil));
|
||||
closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0300, ctlfile));
|
||||
String** carts = listdir("carts/");
|
||||
cart = carts;
|
||||
while (*cart) {
|
||||
createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil);
|
||||
closefile(
|
||||
createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil));
|
||||
cart++;
|
||||
}
|
||||
fs.foreground = 1;
|
||||
|
||||
while (serving) {
|
||||
if (argc >= 3) {
|
||||
|
|
Loading…
Reference in a new issue