7 typedef struct Fid Fid;
8 typedef struct DirBuf DirBuf;
12 OPERM = 0x3, /* mask of all permission types in open mode */
37 VacDir buf[DirBufSize];
56 uchar mdata[8192+IOHDRSZ];
57 int messagesize = sizeof mdata;
62 // VtSession *session;
71 int permf(VacFile*, char*, int);
73 void init(char*, char*, long, int);
74 DirBuf *dirBufAlloc(VacFile*);
75 VacDir *dirBufGet(DirBuf*);
76 int dirBufUnget(DirBuf*);
77 void dirBufFree(DirBuf*);
78 int vacdirread(Fid *f, char *p, long off, long cnt);
79 int vdStat(VacDir *vd, uchar *p, int np);
83 char *rflush(Fid*), *rversion(Fid*),
84 *rauth(Fid*), *rattach(Fid*), *rwalk(Fid*),
85 *ropen(Fid*), *rcreate(Fid*),
86 *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
87 *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
89 char *(*fcalls[])(Fid*) = {
105 char Eperm[] = "permission denied";
106 char Enotdir[] = "not a directory";
107 char Enotexist[] = "file does not exist";
108 char Einuse[] = "file in use";
109 char Eexist[] = "file exists";
110 char Enotowner[] = "not owner";
111 char Eisopen[] = "file already open for I/O";
112 char Excl[] = "exclusive use file already open";
113 char Ename[] = "illegal name";
114 char Erdonly[] = "read only file system";
115 char Eio[] = "i/o error";
116 char Eempty[] = "directory is not empty";
117 char Emode[] = "illegal mode";
122 notifyf(void *a, char *s)
125 if(strncmp(s, "interrupt", 9) == 0)
131 threadmain(int argc, char *argv[])
143 fmtinstall('F', fcallfmt);
147 ncache = atoi(EARGF(usage()));
155 host = EARGF(usage());
158 defsrv = EARGF(usage());
170 init(argv[0], host, ncache, readOnly);
173 sysfatal("pipe failed: %r");
177 proccreate(srv, 0, 32 * 1024);
180 q = strrchr(argv[0], '/');
185 defsrv = vtmalloc(6+strlen(q)+1);
186 strcpy(defsrv, "vacfs.");
189 if(strcmp(defsrv+l-4, ".vac") == 0)
193 if(post9pservice(p[1], defsrv) != 0)
194 sysfatal("post9pservice");
208 fprint(2, "usage: %s [-sd] [-h host] [-c ncache] [-m mountpoint] vacfile\n", argv0);
213 rversion(Fid *unused)
219 for(f = fids; f; f = f->next)
224 return vtstrdup("version: message size too small");
225 messagesize = rhdr.msize;
226 if(messagesize > sizeof mdata)
227 messagesize = sizeof mdata;
228 thdr.msize = messagesize;
229 if(strncmp(rhdr.version, "9P2000", 6) != 0)
230 return vtstrdup("unrecognized 9P version");
231 thdr.version = "9P2000";
246 return vtstrdup("vacfs: authentication not required");
252 /* no authentication for the momment */
256 file = vacfsgetroot(fs);
258 rerrstr(err, sizeof err);
259 return vtstrdup(err);
264 f->qid = (Qid){vacfilegetid(f->file), 0, QTDIR};
267 f->user = vtstrdup(rhdr.uname);
274 _vfWalk(VacFile *file, char *name)
278 n = vacfilewalk(file, name);
281 if(strcmp(name, "SLASH") == 0)
282 return vacfilewalk(file, "/");
289 VacFile *file, *nfile;
298 if(rhdr.fid != rhdr.newfid){
300 return vtstrdup(Eisopen);
302 return vtstrdup(Enotexist);
303 nf = newfid(rhdr.newfid);
305 return vtstrdup(Eisopen);
309 nf->file = vacfileincref(f->file);
310 nf->user = vtstrdup(f->user);
314 nwname = rhdr.nwname;
326 for(nqid = 0; nqid < nwname; nqid++){
327 if((qid.type & QTDIR) == 0){
331 if(!permf(file, f->user, Pexec)) {
335 nfile = _vfWalk(file, rhdr.wname[nqid]);
341 if(vacfileisdir(file))
343 qid.vers = vacfilegetmcount(file);
344 qid.path = vacfilegetid(file);
345 thdr.wqid[nqid] = qid;
352 f->qid = thdr.wqid[nqid-1];
353 vacfiledecref(f->file);
362 /* only error on the first element */
364 return vtstrdup(err);
375 return vtstrdup(Eisopen);
377 return vtstrdup(Enotexist);
380 thdr.iounit = messagesize - IOHDRSZ;
381 if(f->qid.type & QTDIR){
383 return vtstrdup(Eperm);
385 return vtstrdup(Eperm);
392 return vtstrdup(Erdonly);
393 trunc = mode & OTRUNC;
395 if(mode==OWRITE || mode==ORDWR || trunc)
397 return vtstrdup(Eperm);
398 if(mode==OREAD || mode==ORDWR)
400 return vtstrdup(Eperm);
403 return vtstrdup(Eperm);
405 thdr.iounit = messagesize - IOHDRSZ;
417 return vtstrdup(Eisopen);
419 return vtstrdup(Enotexist);
420 if(fs->mode & ModeSnapshot)
421 return vtstrdup(Erdonly);
423 if(!vacfileisdir(vf))
424 return vtstrdup(Enotdir);
425 if(!permf(vf, fid->user, Pwrite))
426 return vtstrdup(Eperm);
428 mode = rhdr.perm & 0777;
430 if(rhdr.perm & DMDIR){
431 if((rhdr.mode & OTRUNC) || (rhdr.perm & DMAPPEND))
432 return vtstrdup(Emode);
433 switch(rhdr.mode & OPERM){
435 return vtstrdup(Emode);
441 return vtstrdup(Eperm);
445 vf = vacfilecreate(vf, rhdr.name, mode, "none");
448 rerrstr(err, sizeof err);
450 return vtstrdup(err);
453 vacfiledecref(fid->file);
456 fid->qid.type = QTFILE;
458 fid->qid.type = QTDIR;
459 fid->qid.vers = vacfilegetmcount(vf);
460 fid->qid.path = vacfilegetid(vf);
463 thdr.iounit = messagesize - IOHDRSZ;
479 return vtstrdup(Enotexist);
485 if(f->qid.type & QTDIR)
486 n = vacdirread(f, buf, off, cnt);
488 n = vacfileread(vf, buf, cnt, off);
490 rerrstr(err, sizeof err);
491 return vtstrdup(err);
506 return vtstrdup(Enotexist);
512 if(f->qid.type & QTDIR)
513 return "file is a directory";
514 thdr.count = vacfilewrite(vf, buf, cnt, off, "none");
518 rerrstr(err, sizeof err);
519 fprint(2, "write failed: %s\n", err);
520 return vtstrdup(err);
532 vacfiledecref(f->file);
547 return vtstrdup(Enotexist);
549 vfp = vacfilegetparent(vf);
551 if(!permf(vfp, f->user, Pwrite)) {
556 if(!vacfileremove(vf, "none")) {
557 rerrstr(errbuf, sizeof errbuf);
558 print("vfRemove failed: %s\n", errbuf);
566 return vtstrdup(err);
573 static uchar statbuf[1024];
576 return vtstrdup(Enotexist);
577 vacfilegetdir(f->file, &dir);
579 thdr.nstat = vdStat(&dir, thdr.stat, sizeof statbuf);
588 return vtstrdup(Enotexist);
589 return vtstrdup(Erdonly);
593 vdStat(VacDir *vd, uchar *p, int np)
597 memset(&dir, 0, sizeof(dir));
600 * Where do path and version come from
602 dir.qid.path = vd->qid;
603 dir.qid.vers = vd->mcount;
604 dir.mode = vd->mode & 0777;
605 if(vd->mode & ModeAppend){
606 dir.qid.type |= QTAPPEND;
607 dir.mode |= DMAPPEND;
609 if(vd->mode & ModeExclusive){
610 dir.qid.type |= QTEXCL;
613 if(vd->mode & ModeDir){
614 dir.qid.type |= QTDIR;
618 dir.atime = vd->atime;
619 dir.mtime = vd->mtime;
620 dir.length = vd->size;
627 return convD2M(&dir, p, np);
631 dirBufAlloc(VacFile *vf)
635 db = vtmallocz(sizeof(DirBuf));
636 db->vde = vdeopen(vf);
641 dirBufGet(DirBuf *db)
650 n = vderead(db->vde, db->buf);
661 vd = db->buf + db->i;
668 dirBufUnget(DirBuf *db)
676 dirBufFree(DirBuf *db)
683 for(i=db->i; i<db->n; i++)
684 vdcleanup(db->buf + i);
690 vacdirread(Fid *f, char *p, long off, long cnt)
696 * special case of rewinding a directory
697 * otherwise ignore the offset
699 if(off == 0 && f->db) {
705 f->db = dirBufAlloc(f->file);
707 for(nb = 0; nb < cnt; nb += n) {
708 vd = dirBufGet(f->db);
714 n = vdStat(vd, (uchar*)p, cnt-nb);
731 for(f = fids; f; f = f->next)
734 else if(!ff && !f->busy)
740 f = vtmallocz(sizeof *f);
755 * reading from a pipe or a network device
756 * will give an error after a few eof reads
757 * however, we cannot tell the difference
758 * between a zero-length read and an interrupt
759 * on the processes writing to us,
760 * so we wait for the error
762 n = read9pmsg(mfd[0], mdata, sizeof mdata);
767 if(convM2S(mdata, n, &rhdr) != n)
768 sysfatal("convM2S conversion error");
771 fprint(2, "vacfs:<-%F\n", &rhdr);
773 thdr.data = (char*)mdata + IOHDRSZ;
774 if(!fcalls[rhdr.type])
775 err = "bad fcall type";
777 err = (*fcalls[rhdr.type])(newfid(rhdr.fid));
782 thdr.type = rhdr.type + 1;
787 fprint(2, "vacfs:->%F\n", &thdr);
788 n = convS2M(&thdr, mdata, messagesize);
792 if(write(mfd[1], mdata, n) != n)
793 sysfatal("mount write: %r");
798 permf(VacFile *vf, char *user, int p)
803 if(vacfilegetdir(vf, &dir))
805 perm = dir.mode & 0777;
809 if((p*Pother) & perm)
811 if(strcmp(user, dir.gid)==0 && ((p*Pgroup) & perm))
813 if(strcmp(user, dir.uid)==0 && ((p*Powner) & perm))
825 return permf(f->file, f->user, p);
829 init(char *file, char *host, long ncache, int readOnly)
834 fmtinstall('V', vtscorefmt);
835 // fmtinstall('R', vtErrFmt);
839 sysfatal("could not connect to server: %r");
841 if(vtconnect(conn) < 0)
842 sysfatal("vtconnect: %r");
844 fs = vacfsopen(conn, file, /*readOnly ? ModeSnapshot :*/ VtOREAD, ncache);
846 sysfatal("vfsOpen: %r");
854 for(f = fids; f; f = f->next) {
857 fprint(2, "open fid: %d\n", f->fid);