> Ahoi all...
> Im like lets starts something basic (basic might be a relative term), 
> like a server that exports a ctl and a data file,
> every line written to ctl can be read from data with the letters sorted.


I did something like this some long time ago, I was told that using 
9p(2)/9pfile(2) for such tiny things, which file structure does not change much 
is not suggested - I actually did not get that.

Anyway, this may not even compile, I copied out of a source-tree. You want to 
read it and understand every line, anyway, do you? ;)

#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

Srv *daefs;

/*
our file layout. Note that the enum Qstatus is
used in daefsfiles_create() to determine the size
of the array. Either fix the function or add files 
(in any) before Qstatus.
*/
enum {
        Qctl,
        Qout,
        Qstatus,
};
struct {
        char name[20]; /* name of the file */
        ulong mode;     /* permission mode */
        File *file;             /* File-struct used by createfile() */
        char *data;     /* The data this file contains */
} daefsfiles[] = {
        "ctl",          0666, nil, nil,
        "output",       0444, nil, nil,
        "status",       0444, nil, nil,
};



void daefs_write(Req *r) {
        int i, myQid = (int)r->fid->file->qid.path;

        if(myQid > Qstatus)
                        sysfatal("unknown Qid %d in write-call\n",myQid);

        for(i=0;i+r->ifcall.offset < FILESIZE && i < r->ifcall.count;i++) {
                daefsfiles[myQid].data[i+r->ifcall.offset] = r->ifcall.data[i];
                r->ofcall.count++;
        }
        daefsfiles[myQid].file->length = r->ifcall.offset + r->ifcall.count;

        
        if(myQid == Qctl) {
                char *msg = daefsfiles[Qctl].data;
                // Do your processing...

                // Copy the string
                strcpy(daefsfiles[Qstatus].data,msg);
                // Do not forget to set the new length of the file, for stat(5) 
etc.
                daefsfiles[Qstatus].file->length =  strlen(msg);
        }

        respond(r,nil);
}

void daefs_read(Req *r) {
        readstr(r,daefsfiles[(int)r->fid->file->qid.path].data);
        respond(r,nil); 
}


void daefsfiles_create(void) {
        int i;

        if((daefs->tree = alloctree("daefs","daefs",DMDIR|0777,nil)) == nil)
                sysfatal("daefs: daefsfiles_create(): alloctree() == nil");

        for(i=0;i<=Qstatus;i++) {

                daefsfiles[i].data = emalloc9p(FILESIZE);

                if((daefsfiles[i].file = createfile(daefs->tree->root, 
daefsfiles[i].name,"daefs", daefsfiles[i].mode,(void *)daefsfiles[i].data)) == 
nil)
                        sysfatal("daefs: daefsfiles_create() for file \"%s\": 
createfile() == nil\n",daefsfiles[i].name);

        }

        daefs_status("ready\n");
}

void main(int argc, char **argv) {
        char *mtpt = "/n/daefs/";
        daefs = emalloc9p(sizeof(Srv));
        daefs->write = daefs_write;
        daefs->read = daefs_read;
        daefsfiles_create();
        threadpostmountsrv(daefs,"daefs",mtpt,0);
}

Reply via email to