void plumbthread(void *v) { CFid *fid; Plumbmsg *m; Timer *t; USED(v); threadsetname("plumbproc"); /* * Loop so that if plumber is restarted, acme need not be. */ for(;;){ /* * Connect to plumber. */ plumbunmount(); while((fid = plumbopenfid("edit", OREAD|OCEXEC)) == nil){ t = timerstart(2000); recv(t->c, nil); timerstop(t); } plumbeditfid = fid; plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC); /* * Relay messages. */ for(;;){ m = plumbrecvfid(plumbeditfid); if(m == nil) break; sendp(cplumb, m); } /* * Lost connection. */ fid = plumbsendfid; plumbsendfid = nil; fsclose(fid); fid = plumbeditfid; plumbeditfid = nil; fsclose(fid); } }
void xrdwr(int argc, char **argv) { char buf[4096]; int n; CFid *fid; ARGBEGIN{ default: usage(); }ARGEND if(argc != 1) usage(); fid = xopen(argv[0], ORDWR); for(;;){ fsseek(fid, 0, 0); if((n = fsread(fid, buf, sizeof buf)) < 0) fprint(2, "read: %r\n"); else{ if(write(1, buf, n) < 0 || write(1, "\n", 1) < 0) sysfatal("write error: %r"); } n = read(0, buf, sizeof buf); if(n <= 0) break; if(buf[n-1] == '\n') n--; if(fswrite(fid, buf, n) != n) fprint(2, "write: %r\n"); } fsclose(fid); threadexitsall(0); }
int main(int argc, char *argv[]) { struct filesystem *f; char path[1024]; char *image = getsetting("imagefile"); if (!image) { printf("Have you run ulogin?\n"); return 10; } char *cwd = getsetting("cwd"); if (argc == 2) { if (argv[1][0] == '/') { sprintf(path, "%s", argv[1]); } else { sprintf(path, "%s/%s", cwd, argv[1]); } } else { sprintf(path, "%s", cwd); } free(cwd); f = fsopen(image); if (!f) { printf("Unable to open %s\n", image); } else { if (!uls(f, path)) { printf("%s not found\n", path); } } fsclose(f); free(image); return 0; }
CFid* fscreate(CFsys *fs, char *name, int mode, ulong perm) { CFid *fid; char *p, *dir, *elem; p = strrchr(name, '/'); if(p == nil){ dir = ""; elem = name; }else{ dir = name; *p = 0; elem = p+1; } if((fid = fswalk(fs->root, dir)) == nil){ if(p) *p = '/'; return nil; } if(p) *p = '/'; if(fsfcreate(fid, elem, mode, perm) < 0){ fsclose(fid); return nil; } return fid; }
void xcreate(int argc, char **argv) { int i; CFsys *fs; CFid *fid; char *p; ARGBEGIN{ default: usage(); }ARGEND if(argc == 0) usage(); for(i=0; i<argc; i++){ fs = xparse(argv[i], &p); if((fid=fscreate(fs, p, OREAD, 0666)) == nil) fprint(2, "create %s: %r\n", argv[i]); else fsclose(fid); fsunmount(fs); } }
CFsys* fsamount(int fd, char *aname) { CFid *afid, *fid; AuthInfo *ai; CFsys *fs; fs = fsinit(fd); if(fs == nil) return nil; if((afid = fsauth(fs, getuser(), aname)) == nil) goto noauth; ai = fsauth_proxy(afid, amount_getkey, "proto=p9any role=client"); if(ai != nil) auth_freeAI(ai); noauth: fid = fsattach(fs, afid, getuser(), aname); fsclose(afid); if(fid == nil){ _fsunmount(fs); return nil; } fssetroot(fs, fid); return fs; }
void auth_freerpc(AuthRpc *rpc) { if(rpc == nil) return; if(rpc->afd >= 0) close(rpc->afd); if(rpc->afid != nil) fsclose(rpc->afid); free(rpc); }
void xwrite(int argc, char **argv) { char buf[4096]; int n, did; CFid *fid; Biobuf *b; char *p; int byline; byline = 0; ARGBEGIN{ case 'l': byline = 1; break; default: usage(); }ARGEND if(argc != 1) usage(); did = 0; fid = xopen(argv[0], OWRITE|OTRUNC); if(byline){ n = 0; b = malloc(sizeof *b); if(b == nil) sysfatal("out of memory"); Binit(b, 0, OREAD); while((p = Brdstr(b, '\n', 0)) != nil){ n = strlen(p); did = 1; if(fswrite(fid, p, n) != n) fprint(2, "write: %r\n"); } free(b); }else{ while((n = read(0, buf, sizeof buf)) > 0){ did = 1; if(fswrite(fid, buf, n) != n) sysfatal("write error: %r"); } } if(n == 0 && !did){ if(fswrite(fid, buf, 0) != 0) sysfatal("write error: %r"); } if(n < 0) sysfatal("read error: %r"); fsclose(fid); threadexitsall(0); }
int main(int argc, char *argv[]) { struct filesystem *f; int i; int mode; char temp[1024]; char *cwd; char *fsname = getsetting("imagefile");; if (!fsname) { printf("Have you run ulogin?\n"); return 10; } if (argc < 3) { printf("Usage: %s <uid> <file> ...\n", argv[0]); return 10; } sscanf(argv[1], "%o", &mode); f = fsopen(fsname); if (!f) { printf("Unable to open %s\n", fsname); return 10; } cwd = getsetting("cwd"); for (i = 2; i < argc; i++) { if (argv[i][0] == '/') { uchown(f, argv[i], mode); } else { if (!cwd) { sprintf(temp, "/%s", argv[i]); } else { sprintf(temp, "%s/%s", cwd, argv[i]); } compresspath(temp); uchown(f, temp, mode); } } fsclose(f); if (cwd) free(cwd); free(fsname); return 0; }
int plumbstart(void) { CFid *fid; plumbfd = plumbopen("send", OWRITE|OCEXEC); /* not open is ok */ fid = plumbopenfid("edit", OREAD|OCEXEC); if(fid == nil) return -1; plumbc = chancreate(sizeof(int), 0); chansetname(plumbc, "plumbc"); if(plumbc == nil){ fsclose(fid); return -1; } threadcreate(plumbproc, fid, STACK); return 1; }
void loadmboxfaces(char *maildir) { CFid *dirfd; Dir *d; int i, n; dirfd = fsopen(mailfs, maildir, OREAD); if(dirfd != nil){ while((n = fsdirread(dirfd, &d)) > 0){ for(i=0; i<n; i++) addface(dirface(maildir, d[i].name)); free(d); } fsclose(dirfd); }else sysfatal("open %s: %r", maildir); }
Face* dirface(char *dir, char *num) { Face *f; char buf[1024], *info, *p, *t, *s; int n; ulong len; CFid *fid; sprint(buf, "%s/%s/info", dir, num); len = fsdirlen(mailfs, buf); if(len <= 0) return nil; fid = fsopen(mailfs, buf, OREAD); if(fid == nil) return nil; info = emalloc(len+1); n = fsreadn(fid, info, len); fsclose(fid); if(n < 0){ free(info); return nil; } info[n] = '\0'; f = emalloc(sizeof(Face)); for(p=info; (s=iline(p, &p)) != nil; ){ t = strchr(s, ' '); if(t == nil) continue; *t++ = 0; if(strcmp(s, "unixdate") == 0){ f->time = atoi(t); f->tm = *localtime(f->time); } else if(strcmp(s, "from") == 0) setname(f, t); else if(strcmp(s, "digest") == 0) f->str[Sdigest] = estrdup(t); } sprint(buf, "%s/%s", dir, num); f->str[Sshow] = estrdup(buf); free(info); return f; }
void xcon(int argc, char **argv) { char buf[4096], *r, *w, *e; int n, nocr; CFid *fid; nocr = 1; ARGBEGIN{ case 'r': nocr = 0; break; default: usage(); }ARGEND if(argc != 1) usage(); fid = xopen(argv[0], ORDWR); proccreate(rdcon, fid, 32768); for(;;){ n = fsread(fid, buf, sizeof buf); if(n <= 0) threadexitsall(0); if(nocr){ for(r=w=buf, e=buf+n; r<e; r++) if(*r != '\r') *w++ = *r; n = w-buf; } if(write(1, buf, n) != n) threadexitsall(0); } fsclose(fid); threadexitsall(0); }
void xread(int argc, char **argv) { char buf[4096]; int n; CFid *fid; ARGBEGIN{ default: usage(); }ARGEND if(argc != 1) usage(); fid = xopen(argv[0], OREAD); while((n = fsread(fid, buf, sizeof buf)) > 0) if(write(1, buf, n) < 0) sysfatal("write error: %r"); fsclose(fid); if(n < 0) sysfatal("read error: %r"); threadexitsall(0); }
int mboxcommand(Window *w, char *s) { char *args[10], **targs, *save; Window *sbox; Message *m, *next; int ok, nargs, i, j; CFid *searchfd; char buf[128], *res; nargs = tokenize(s, args, nelem(args)); if(nargs == 0) return 0; if(strcmp(args[0], "Mail") == 0){ if(nargs == 1) mkreply(nil, "Mail", "", nil, nil); else mkreply(nil, "Mail", args[1], nil, nil); return 1; } if(strcmp(s, "Del") == 0){ if(mbox.dirty){ mbox.dirty = 0; fprint(2, "mail: mailbox not written\n"); return 1; } if(w != mbox.w){ windel(w, 1); return 1; } ok = 1; for(m=mbox.head; m!=nil; m=next){ next = m->next; if(m->w){ if(windel(m->w, 0)) m->w = nil; else ok = 0; } } for(m=replies.head; m!=nil; m=next){ next = m->next; if(m->w){ if(windel(m->w, 0)) m->w = nil; else ok = 0; } } if(ok){ windel(w, 1); removeupasfs(); threadexitsall(nil); } return 1; } if(strcmp(s, "Put") == 0){ rewritembox(wbox, &mbox); return 1; } if(strcmp(s, "Get") == 0){ fswrite(mbox.ctlfd, "refresh", 7); return 1; } if(strcmp(s, "Delmesg") == 0){ save = nil; if(nargs > 1) save = args[1]; s = winselection(w); if(s == nil) return 1; nargs = 1; for(i=0; s[i]; i++) if(s[i] == '\n') nargs++; targs = emalloc(nargs*sizeof(char*)); /* could be too many for a local array */ nargs = getfields(s, targs, nargs, 1, "\n"); for(i=0; i<nargs; i++){ if(!isdigit(targs[i][0])) continue; j = atoi(targs[i]); /* easy way to parse the number! */ if(j == 0) continue; snprint(buf, sizeof buf, "%s%d", mbox.name, j); delmesg(buf, nil, 1, save); } free(s); free(targs); return 1; } if(strcmp(s, "Search") == 0){ if(nargs <= 1) return 1; s = estrstrdup(mboxname, "/search"); searchfd = fsopen(mailfs, s, ORDWR); if(searchfd == nil) return 1; save = estrdup(args[1]); for(i=2; i<nargs; i++) save = eappend(save, " ", args[i]); fswrite(searchfd, save, strlen(save)); fsseek(searchfd, 0, 0); j = fsread(searchfd, buf, sizeof buf - 1); if(j == 0){ fprint(2, "[%s] search %s: no results found\n", mboxname, save); fsclose(searchfd); free(save); return 1; } free(save); buf[j] = '\0'; res = estrdup(buf); j = fsread(searchfd, buf, sizeof buf - 1); for(; j != 0; j = fsread(searchfd, buf, sizeof buf - 1), buf[j] = '\0') res = eappend(res, "", buf); fsclose(searchfd); sbox = newwindow(); winname(sbox, s); free(s); threadcreate(mainctl, sbox, STACK); winopenbody(sbox, OWRITE); /* show results in reverse order */ m = mbox.tail; save = nil; for(s=strrchr(res, ' '); s!=nil || save!=res; s=strrchr(res, ' ')){ if(s != nil){ save = s+1; *s = '\0'; } else save = res; save = estrstrdup(save, "/"); for(; m && strcmp(save, m->name) != 0; m=m->prev); free(save); if(m == nil) break; fsprint(sbox->body, "%s%s\n", m->name, info(m, 0, 0)); m = m->prev; } free(res); winclean(sbox); winclosebody(sbox); return 1; } return 0; }
CFid* fswalk(CFid *fid, char *oname) { char *freep, *name; int i, nwalk; char *p; CFid *wfid; Fcall tx, rx; freep = nil; name = oname; if(name){ freep = malloc(strlen(name)+1); if(freep == nil) return nil; strcpy(freep, name); name = freep; } if((wfid = _fsgetfid(fid->fs)) == nil){ free(freep); return nil; } nwalk = 0; do{ /* collect names */ for(i=0; name && *name && i < MAXWELEM; ){ p = name; name = strchr(name, '/'); if(name) *name++ = 0; if(*p == 0 || (*p == '.' && *(p+1) == 0)) continue; tx.wname[i++] = p; } /* do a walk */ tx.type = Twalk; tx.fid = nwalk ? wfid->fid : fid->fid; tx.newfid = wfid->fid; tx.nwname = i; if(_fsrpc(fid->fs, &tx, &rx, 0) < 0){ Error: free(freep); if(nwalk) fsclose(wfid); else _fsputfid(wfid); return nil; } if(rx.nwqid != tx.nwname){ /* XXX lame error */ werrstr("file '%s' not found", oname); goto Error; } if(rx.nwqid == 0) wfid->qid = fid->qid; else wfid->qid = rx.wqid[rx.nwqid-1]; nwalk++; }while(name && *name); return wfid; }
void xls(int argc, char **argv) { char *err, *name, *xname, *f[4], buf[4096]; int i, j, l, sort; int lflag, dflag, tflag, n, len[4]; Dir *d; CFid *fid; CFsys *fs; err = nil; sort = 1; lflag = dflag = tflag = 0; ARGBEGIN{ case 'n': sort = 0; break; case 'l': lflag = 1; break; case 'd': dflag = 1; break; case 't': tflag = 1; break; }ARGEND fmtinstall('D', dirfmt); fmtinstall('M', dirmodefmt); quotefmtinstall(); fmtinstall('T', timefmt); if(argc == 0){ argv = dot; argc = 1; } for(i=0; i<argc; i++){ name = argv[i]; fs = xparse(name, &xname); if((d = fsdirstat(fs, xname)) == nil){ fprint(2, "dirstat %s: %r\n", name); fsunmount(fs); err = "errors"; continue; } if((d->mode&DMDIR) && !dflag){ if((fid = fsopen(fs, xname, OREAD)) == nil){ fprint(2, "open %s: %r\n", name); fsunmount(fs); free(d); err = "errors"; continue; } free(d); n = fsdirreadall(fid, &d); fsclose(fid); if(n < 0){ fprint(2, "dirreadall %s: %r\n", name); fsunmount(fs); err = "errors"; continue; } if(sort){ if(tflag) qsort(d, n, sizeof d[0], timecmp); else qsort(d, n, sizeof d[0], dircmp); } for(j=0; j<4; j++) len[j] = 0; for(i=0; i<n; i++){ d[i].type = 'M'; d[i].dev = 0; snprint(buf, sizeof buf, "%d %s %s %lld", d[i].dev, d[i].uid, d[i].gid, d[i].length); getfields(buf, f, 4, 0, " "); for(j=0; j<4; j++){ l = strlen(f[j]); if(l > len[j]) len[j] = l; } } for(i=0; i<n; i++){ if(!lflag){ print("%q\n", d[i].name); continue; } print("%M %C %*d %*s %*s %*lld %T %q\n", d[i].mode, d[i].type, len[0], d[i].dev, -len[1], d[i].uid, -len[2], d[i].gid, len[3], d[i].length, d[i].mtime, d[i].name); } }else{ d->type = 'M'; d->dev = 0; if(lflag) print("%M %C %d %s %s %lld %T %q\n", d->mode, d->type, d->dev, d->uid, d->gid, d->length, d->mtime, d->name); else print("%q\n", d->name); } free(d); } threadexitsall(err); }