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 Ebad[] = "something bad happened";
|
||||||
static char Enomem[] = "no memory";
|
static char Enomem[] = "no memory";
|
||||||
|
|
||||||
typedef struct Ramfile Ramfile;
|
enum { CTL = 1, SLOT, CART, REALM, UNIVERSE };
|
||||||
struct Ramfile {
|
|
||||||
|
typedef struct Aux Aux;
|
||||||
|
struct Aux {
|
||||||
|
int type;
|
||||||
char* data;
|
char* data;
|
||||||
int ndata;
|
int count;
|
||||||
};
|
};
|
||||||
|
|
||||||
void fsread(Req* r) {
|
void fsread(Req* r) {
|
||||||
Ramfile* rf;
|
Aux* a;
|
||||||
vlong offset;
|
vlong offset;
|
||||||
long count;
|
long count;
|
||||||
|
|
||||||
rf = r->fid->file->aux;
|
a = r->fid->file->aux;
|
||||||
offset = r->ifcall.offset;
|
offset = r->ifcall.offset;
|
||||||
count = r->ifcall.count;
|
count = r->ifcall.count;
|
||||||
|
|
||||||
/*print("read %ld %lld\n", *count, offset); */
|
/*print("read %ld %lld\n", *count, offset); */
|
||||||
if (offset >= rf->ndata) {
|
if (offset >= a->count) {
|
||||||
r->ofcall.count = 0;
|
r->ofcall.count = 0;
|
||||||
respond(r, nil);
|
respond(r, nil);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset + count >= rf->ndata)
|
if (offset + count >= a->count)
|
||||||
count = rf->ndata - offset;
|
count = a->count - offset;
|
||||||
|
|
||||||
memmove(r->ofcall.data, rf->data + offset, count);
|
memmove(r->ofcall.data, a->data + offset, count);
|
||||||
r->ofcall.count = count;
|
r->ofcall.count = count;
|
||||||
respond(r, nil);
|
respond(r, nil);
|
||||||
}
|
}
|
||||||
|
@ -76,25 +79,25 @@ unsigned long long hash(char* str) {
|
||||||
|
|
||||||
void fswrite(Req* r) {
|
void fswrite(Req* r) {
|
||||||
void* v;
|
void* v;
|
||||||
Ramfile* rf;
|
Aux* a;
|
||||||
vlong offset;
|
vlong offset;
|
||||||
long count;
|
long count;
|
||||||
|
|
||||||
rf = r->fid->file->aux;
|
a = r->fid->file->aux;
|
||||||
offset = r->ifcall.offset;
|
offset = r->ifcall.offset;
|
||||||
count = r->ifcall.count;
|
count = r->ifcall.count;
|
||||||
|
|
||||||
if (offset + count >= rf->ndata) {
|
if (offset + count >= a->count) {
|
||||||
v = realloc(rf->data, offset + count);
|
v = realloc(a->data, offset + count);
|
||||||
if (v == nil) {
|
if (v == nil) {
|
||||||
respond(r, Enomem);
|
respond(r, Enomem);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rf->data = v;
|
a->data = v;
|
||||||
rf->ndata = offset + count;
|
a->count = offset + count;
|
||||||
r->fid->file->dir.length = rf->ndata;
|
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;
|
r->ofcall.count = count;
|
||||||
respond(r, nil);
|
respond(r, nil);
|
||||||
}
|
}
|
||||||
|
@ -102,87 +105,48 @@ void fswrite(Req* r) {
|
||||||
void handlectl(Req* r) {
|
void handlectl(Req* r) {
|
||||||
char cmd[16];
|
char cmd[16];
|
||||||
char* c = r->ifcall.data;
|
char* c = r->ifcall.data;
|
||||||
|
int i;
|
||||||
|
|
||||||
while (*c && *c != ' ') {
|
for (i = 0; i < r->ifcall.count; i++) {
|
||||||
ccat(cmd, *c);
|
ccat(cmd, *c++);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long long const cmd_hashv = hash(cmd);
|
unsigned long long const cmd_hashv = hash(cmd);
|
||||||
|
|
||||||
switch (cmd_hashv) {
|
switch (cmd_hashv) {
|
||||||
case SHUTDOWN_HASHV:
|
case SHUTDOWN_HASHV:
|
||||||
serving = 0;
|
printf("what");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r->ofcall.count = 16;
|
r->ofcall.count = r->ifcall.count;
|
||||||
r->fid->file->dir.length = 16;
|
r->fid->file->dir.length = r->ifcall.count;
|
||||||
respond(r, nil);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void xrxswrite(Req* r) {
|
void xrxswrite(Req* r) {
|
||||||
unsigned int filename_hashv = hash(r->ifcall.name);
|
Aux* a = r->fid->file->aux;
|
||||||
printf(r->ifcall.name);
|
switch (a->type) {
|
||||||
switch (filename_hashv) {
|
case CTL:
|
||||||
case CTL_HASHV:
|
|
||||||
handlectl(r);
|
handlectl(r);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fswrite(r);
|
|
||||||
break;
|
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);
|
respond(r, nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fsdestroyfile(File* f) {
|
void fsopen(Req* r) { respond(r, nil); }
|
||||||
Ramfile* rf;
|
|
||||||
|
|
||||||
/*fprint(2, "clunk\n"); */
|
void wstat(Req* r) { respond(r, nil); }
|
||||||
rf = f->aux;
|
|
||||||
if (rf) {
|
void fsdestroyfile(File* f) {
|
||||||
free(rf->data);
|
Aux* a = f->aux;
|
||||||
free(rf);
|
if (a && a->data) {
|
||||||
|
free(a->data);
|
||||||
|
free(a);
|
||||||
|
} else if (a) {
|
||||||
|
free(a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,36 +175,12 @@ String** listdir(char* path) {
|
||||||
return self;
|
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 = {
|
Srv fs = {
|
||||||
.open = fsopen,
|
.open = fsopen,
|
||||||
.read = fsread,
|
.read = fsread,
|
||||||
.write = xrxswrite,
|
.write = xrxswrite,
|
||||||
.create = xrxscreate,
|
.create = fsopen,
|
||||||
.wstat = xrxswstat
|
.wstat = wstat};
|
||||||
};
|
|
||||||
|
|
||||||
int threadmaybackground(void) { return 1; }
|
int threadmaybackground(void) { return 1; }
|
||||||
|
|
||||||
|
@ -250,9 +190,10 @@ void threadmain(int argc, char* argv[]) {
|
||||||
char* usocket = nil;
|
char* usocket = nil;
|
||||||
int i;
|
int i;
|
||||||
String** cart;
|
String** cart;
|
||||||
char cartsloc[256] = "./carts/";
|
Aux* ctlfile = malloc(sizeof(Aux));
|
||||||
char ctlbuf[128] = "some data";
|
ctlfile->type = CTL;
|
||||||
Ramfile ctlfile = {ctlbuf, 128};
|
ctlfile->data = malloc(256 * sizeof(char));
|
||||||
|
ctlfile->count = 0;
|
||||||
|
|
||||||
/* if -m PATH is supplied, mount on PATH */
|
/* if -m PATH is supplied, mount on PATH */
|
||||||
/* if -s NAME is supplied, create a socket for the namespace */
|
/* 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);
|
fs.tree = alloctree(nil, nil, DMDIR | 0777, fsdestroyfile);
|
||||||
tree = fs.tree;
|
tree = fs.tree;
|
||||||
createfile(tree->root, "carts", nil, DMDIR | 0555, nil);
|
closefile(createfile(tree->root, "carts", nil, DMDIR | 0555, nil));
|
||||||
createfile(tree->root, "ctl", nil, 0600, &ctlfile);
|
closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0300, ctlfile));
|
||||||
|
|
||||||
String** carts = listdir("carts/");
|
String** carts = listdir("carts/");
|
||||||
cart = carts;
|
cart = carts;
|
||||||
while (*cart) {
|
while (*cart) {
|
||||||
createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil);
|
closefile(
|
||||||
|
createfile(walkfile(tree->root, "carts"), (*cart)->base, nil, 0444, nil));
|
||||||
cart++;
|
cart++;
|
||||||
}
|
}
|
||||||
fs.foreground = 1;
|
|
||||||
|
|
||||||
while (serving) {
|
while (serving) {
|
||||||
if (argc >= 3) {
|
if (argc >= 3) {
|
||||||
|
|
Loading…
Reference in a new issue