static PyObject * Wmii_ls(Wmii *self, PyObject *args) { const char *file; char *buf; PyArg_ParseTuple(args, "s", &file); int count; PyObject *list; IxpCFid *fid; IxpStat stat; IxpMsg msg; fid = ixp_open(self->client, file, P9_OREAD); buf = malloc(fid->iounit); list = PyList_New(0); while( (count = ixp_read(fid, buf, fid->iounit)) > 0 ) { msg = ixp_message(buf, count, MsgUnpack); while(msg.pos < msg.end) { ixp_pstat(&msg, &stat); PyList_Append(list, PyString_FromString(stat.name)); } } ixp_close(fid); free(buf); return list; }
/* * Metadata Management */ void unpfs_stat(Ixp9Req *r) { int ret = 0; struct stat stbuf; struct unpfs_fid *fid = r->fid->aux; int size; IxpMsg m; struct IxpStat s; char *buf, *name = strdup(fid->path); unpfs_log(LOG_NOTICE, "%s: fid=%u real_path=%s\n", __func__, r->fid->fid, fid->real_path); if (!name || lstat(fid->real_path, &stbuf) < 0) { ret = errno; goto out; } stat_posix_to_9p(&s, basename(name), &stbuf); /* Pack the stat to the binary */ size = ixp_sizeof_stat(&s); buf = ixp_emallocz(size); m = ixp_message(buf, size, MsgPack); ixp_pstat(&m, &s); zfree(&name); r->fid->qid = s.qid; r->ofcall.rstat.nstat = size; r->ofcall.rstat.stat = (uint8_t *)m.data; out: respond(r, ret); }
void ixp_srv_readdir(Ixp9Req *req, IxpLookupFn lookup, void (*dostat)(IxpStat*, IxpFileId*)) { IxpMsg msg; IxpFileId *file, *tfile; IxpStat stat; char *buf; ulong size, n; uint64_t offset; file = req->fid->aux; size = req->ifcall.io.count; if(size > req->fid->iounit) size = req->fid->iounit; buf = emallocz(size); msg = ixp_message(buf, size, MsgPack); file = lookup(file, nil); tfile = file; /* Note: The first file is ".", so we skip it. */ offset = 0; for(file=file->next; file; file=file->next) { dostat(&stat, file); n = ixp_sizeof_stat(&stat); if(offset >= req->ifcall.io.offset) { if(size < n) break; ixp_pstat(&msg, &stat); size -= n; } offset += n; } while((file = tfile)) { tfile=tfile->next; ixp_srv_freefile(file); } req->ofcall.io.count = msg.pos - msg.data; req->ofcall.io.data = msg.data; ixp_respond(req, nil); }
static int xls(int argc, char *argv[]) { IxpMsg m; Stat *stat; IxpCFid *fid; char *file, *buf; int lflag, dflag, count, nstat, mstat, i; lflag = dflag = 0; ARGBEGIN{ case 'l': lflag++; break; case 'd': dflag++; break; default: usage(); }ARGEND; file = EARGF(usage()); stat = ixp_stat(client, file); if(stat == nil) fatal("cannot stat file '%s': %s\n", file, ixp_errbuf()); if(dflag || (stat->mode&P9_DMDIR) == 0) { print_stat(stat, lflag); ixp_freestat(stat); return 0; } ixp_freestat(stat); fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %s\n", file, ixp_errbuf()); nstat = 0; mstat = 16; stat = emalloc(sizeof(*stat) * mstat); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) { m = ixp_message(buf, count, MsgUnpack); while(m.pos < m.end) { if(nstat == mstat) { mstat <<= 1; stat = ixp_erealloc(stat, sizeof(*stat) * mstat); } ixp_pstat(&m, &stat[nstat++]); } } qsort(stat, nstat, sizeof(*stat), comp_stat); for(i = 0; i < nstat; i++) { print_stat(&stat[i], lflag); ixp_freestat(&stat[i]); } free(stat); if(count == -1) fatal("cannot read directory '%s': %s\n", file, ixp_errbuf()); return 0; }
void ixp_pfcall(IxpMsg *msg, IxpFcall *fcall) { ixp_pu8(msg, &fcall->hdr.type); ixp_pu16(msg, &fcall->hdr.tag); switch (fcall->hdr.type) { case TVersion: case RVersion: ixp_pu32(msg, &fcall->version.msize); ixp_pstring(msg, &fcall->version.version); break; case TAuth: ixp_pu32(msg, &fcall->tauth.afid); ixp_pstring(msg, &fcall->tauth.uname); ixp_pstring(msg, &fcall->tauth.aname); break; case RAuth: ixp_pqid(msg, &fcall->rauth.aqid); break; case RAttach: ixp_pqid(msg, &fcall->rattach.qid); break; case TAttach: ixp_pu32(msg, &fcall->hdr.fid); ixp_pu32(msg, &fcall->tattach.afid); ixp_pstring(msg, &fcall->tattach.uname); ixp_pstring(msg, &fcall->tattach.aname); break; case RError: ixp_pstring(msg, &fcall->error.ename); break; case TFlush: ixp_pu16(msg, &fcall->tflush.oldtag); break; case TWalk: ixp_pu32(msg, &fcall->hdr.fid); ixp_pu32(msg, &fcall->twalk.newfid); ixp_pstrings(msg, &fcall->twalk.nwname, fcall->twalk.wname, nelem(fcall->twalk.wname)); break; case RWalk: ixp_pqids(msg, &fcall->rwalk.nwqid, fcall->rwalk.wqid, nelem(fcall->rwalk.wqid)); break; case TOpen: ixp_pu32(msg, &fcall->hdr.fid); ixp_pu8(msg, &fcall->topen.mode); break; case ROpen: case RCreate: ixp_pqid(msg, &fcall->ropen.qid); ixp_pu32(msg, &fcall->ropen.iounit); break; case TCreate: ixp_pu32(msg, &fcall->hdr.fid); ixp_pstring(msg, &fcall->tcreate.name); ixp_pu32(msg, &fcall->tcreate.perm); ixp_pu8(msg, &fcall->tcreate.mode); break; case TRead: ixp_pu32(msg, &fcall->hdr.fid); ixp_pu64(msg, &fcall->tread.offset); ixp_pu32(msg, &fcall->tread.count); break; case RRead: ixp_pu32(msg, &fcall->rread.count); ixp_pdata(msg, &fcall->rread.data, fcall->rread.count); break; case TWrite: ixp_pu32(msg, &fcall->hdr.fid); ixp_pu64(msg, &fcall->twrite.offset); ixp_pu32(msg, &fcall->twrite.count); ixp_pdata(msg, &fcall->twrite.data, fcall->twrite.count); break; case RWrite: ixp_pu32(msg, &fcall->rwrite.count); break; case TClunk: case TRemove: case TStat: ixp_pu32(msg, &fcall->hdr.fid); break; case RStat: ixp_pu16(msg, &fcall->rstat.nstat); ixp_pdata(msg, (char**)&fcall->rstat.stat, fcall->rstat.nstat); break; case TWStat: { uint16_t size; ixp_pu32(msg, &fcall->hdr.fid); ixp_pu16(msg, &size); ixp_pstat(msg, &fcall->twstat.stat); break; } } }
static int xls(int argc, char *argv[]) { IxpMsg m; Stat *stat; IxpCFid *fid; char *file; char *buf; int lflag, dflag, pflag; int count, nstat, mstat, i; lflag = dflag = pflag = 0; ARGBEGIN{ case 'l': lflag++; break; case 'd': dflag++; break; case 'p': pflag++; break; default: usage(); }ARGEND; count = 0; file = EARGF(usage()); do { stat = ixp_stat(client, file); if(stat == nil) fatal("cannot stat file '%s': %r\n", file); i = strlen(file); if(file[i-1] == '/') { file[i-1] = '\0'; if(!(stat->mode&P9_DMDIR)) fatal("%s: not a directory", file); } if(dflag || (stat->mode&P9_DMDIR) == 0) { print_stat(stat, lflag, file, pflag); ixp_freestat(stat); continue; } ixp_freestat(stat); fid = ixp_open(client, file, P9_OREAD); if(fid == nil) fatal("Can't open file '%s': %r\n", file); nstat = 0; mstat = 16; stat = emalloc(mstat * sizeof *stat); buf = emalloc(fid->iounit); while((count = ixp_read(fid, buf, fid->iounit)) > 0) { m = ixp_message(buf, count, MsgUnpack); while(m.pos < m.end) { if(nstat == mstat) { mstat <<= 1; stat = erealloc(stat, mstat * sizeof *stat); } ixp_pstat(&m, &stat[nstat++]); } } ixp_close(fid); qsort(stat, nstat, sizeof *stat, comp_stat); for(i = 0; i < nstat; i++) { print_stat(&stat[i], lflag, file, pflag); ixp_freestat(&stat[i]); } free(stat); } while((file = ARGF())); if(count == -1) fatal("cannot read directory '%s': %r\n", file); return 0; }