2 #include "../port/lib.h"
6 #include "../port/error.h"
11 mkqid(Qid *q, vlong path, ulong vers, int type)
19 devno(int c, int user)
23 for(i = 0; devtab[i] != nil; i++) {
24 if(devtab[i]->dc == c)
28 panic("devno %C %#ux", c, c);
34 devdir(Chan *c, Qid qid, char *n, vlong length, char *user, long perm, Dir *db)
40 db->type = devtab[c->type]->dc;
43 db->mode |= qid.type << 24;
44 db->atime = seconds();
53 * (here, Devgen is the prototype; devgen is the function in dev.c.)
55 * a Devgen is expected to return the directory entry for ".."
56 * if you pass it s==DEVDOTDOT (-1). otherwise...
58 * there are two contradictory rules.
60 * (i) if c is a directory, a Devgen is expected to list its children
63 * (ii) whether or not c is a directory, a Devgen is expected to list
64 * its siblings as you iterate s.
66 * devgen always returns the list of children in the root
67 * directory. thus it follows (i) when c is the root and (ii) otherwise.
68 * many other Devgens follow (i) when c is a directory and (ii) otherwise.
70 * devwalk assumes (i). it knows that devgen breaks (i)
71 * for children that are themselves directories, and explicitly catches them.
73 * devstat assumes (ii). if the Devgen in question follows (i)
74 * for this particular c, devstat will not find the necessary info.
75 * with our particular Devgen functions, this happens only for
76 * directories, so devstat makes something up, assuming
77 * c->name, c->qid, eve, DMDIR|0555.
79 * devdirread assumes (i). the callers have to make sure
80 * that the Devgen satisfies (i) for the chan being read.
83 * the zeroth element of the table MUST be the directory itself for ..
86 devgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
94 if(strcmp(tab[i].name, name) == 0)
100 /* skip over the first element, that for . itself */
106 devdir(c, tab->qid, tab->name, tab->length, eve, tab->perm, dp);
126 devattach(int tc, char *spec)
133 mkqid(&c->qid, 0, 0, QTDIR);
134 c->type = devno(tc, 0);
137 n = 1+UTFmax+strlen(spec)+1;
139 snprint(buf, n, "#%C%s", tc, spec);
140 c->path = newpath(buf);
152 panic("clone of open file type %C", devtab[c->type]->dc);
160 nc->offset = c->offset;
169 devwalk(Chan *c, Chan *nc, char **name, int nname, Dirtab *tab, int ntab, Devgen *gen)
180 wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
182 if(alloc && wq->clone!=nil)
189 nc->type = 0; /* device doesn't know about this channel yet */
194 for(j=0; j<nname; j++){
195 if(!(nc->qid.type&QTDIR)){
201 if(strcmp(n, ".") == 0){
203 wq->qid[wq->nqid++] = nc->qid;
206 if(strcmp(n, "..") == 0){
207 if((*gen)(nc, nil, tab, ntab, DEVDOTDOT, &dir) != 1){
208 print("devgen walk .. in dev%s %llux broken\n",
209 devtab[nc->type]->name, nc->qid.path);
210 error("broken devgen");
216 * Ugly problem: If we're using devgen, make sure we're
217 * walking the directory itself, represented by the first
218 * entry in the table, and not trying to step into a sub-
219 * directory of the table, e.g. /net/net. Devgen itself
220 * should take care of the problem, but it doesn't have
221 * the necessary information (that we're doing a walk).
223 if(gen==devgen && nc->qid.path!=tab[0].qid.path)
226 switch((*gen)(nc, n, tab, ntab, i, &dir)){
231 kstrcpy(up->errstr, Enonexist, ERRMAX);
236 if(strcmp(n, dir.name) == 0){
245 * We processed at least one name, so will return some data.
246 * If we didn't process all nname entries succesfully, we drop
247 * the cloned channel and return just the Qids of the walks.
251 if(wq->nqid < nname){
256 /* attach cloned channel to same device */
257 wq->clone->type = c->type;
263 devstat(Chan *c, uchar *db, int n, Dirtab *tab, int ntab, Devgen *gen)
270 switch((*gen)(c, nil, tab, ntab, i, &dir)){
272 if(c->qid.type & QTDIR){
275 else if(strcmp(c->path->s, "/") == 0)
278 for(elem=p=c->path->s; *p; p++)
281 devdir(c, c->qid, elem, 0, eve, DMDIR|0555, &dir);
282 n = convD2M(&dir, db, n);
292 if(c->qid.path == dir.qid.path) {
295 n = convD2M(&dir, db, n);
306 devdirread(Chan *c, char *d, long n, Dirtab *tab, int ntab, Devgen *gen)
311 for(m=0; m<n; c->dri++) {
312 switch((*gen)(c, nil, tab, ntab, c->dri, &dir)){
320 dsz = convD2M(&dir, (uchar*)d, n-m);
321 if(dsz <= BIT16SZ){ /* <= not < because this isn't stat; read is stuck */
336 * error(Eperm) if open permission not granted for up->user.
339 devpermcheck(char *fileuid, ulong perm, int omode)
342 static int access[] = { 0400, 0200, 0600, 0100 };
344 if(strcmp(up->user, fileuid) == 0)
347 if(strcmp(up->user, eve) == 0)
358 devopen(Chan *c, int omode, Dirtab *tab, int ntab, Devgen *gen)
364 switch((*gen)(c, nil, tab, ntab, i, &dir)){
370 if(c->qid.path == dir.qid.path) {
371 devpermcheck(dir.uid, dir.mode, omode);
379 if((c->qid.type&QTDIR) && omode!=OREAD)
381 c->mode = openmode(omode);
387 devcreate(Chan*, char*, int, ulong)
394 devbread(Chan *c, long n, ulong offset)
405 bp->wp += devtab[c->type]->read(c, bp->wp, n, offset);
411 devbwrite(Chan *c, Block *bp, ulong offset)
419 n = devtab[c->type]->write(c, bp->rp, BLEN(bp), offset);
433 devwstat(Chan*, uchar*, int)
446 devconfig(int, char *, DevConf *)