Npcfid* npc_open(Npcfsys *fs, char *path, int mode) { Npfcall *tc, *rc; Npcfid *fid; fid = npc_walk(fs, path); if (!fid) return NULL; tc = np_create_topen(fid->fid, mode); if (npc_rpc(fs, tc, &rc) < 0) { npc_clunk(fid); free(tc); return NULL; } fid->iounit = rc->iounit; if (!fid->iounit || fid->iounit>fid->fsys->msize-IOHDRSZ) fid->iounit = fid->fsys->msize-IOHDRSZ; free(tc); free(rc); return fid; }
ssize_t npc_listxattr (Npcfid *root, char *path, char *buf, size_t size) { Npcfid *fid = NULL; Npcfid *attrfid = NULL; ssize_t ret = -1; size_t n, tot; if (!(fid = npc_walk (root, path))) goto done; if (!(attrfid = npc_fid_alloc(fid->fsys))) goto done; ret = npc_xattrwalk (fid, attrfid, NULL); if (ret < 0) goto done; if (buf == NULL || size == 0) goto done; if (ret > size) { np_uerror (ERANGE); goto done; } for (tot = 0; tot < size; ) { n = npc_read (attrfid, buf + tot, size - tot); if (n <= 0) break; tot += n; } ret = tot; done: if (fid) (void)npc_clunk (fid); if (attrfid) (void)npc_clunk (attrfid); return ret; }
Npcfid* npc_create(Npcfsys *fs, char *path, u32 perm, int mode) { char *fname, *pname; Npfcall *tc, *rc; Npcfid *fid; pname = strdup(path); if (!pname) return NULL; fname = strrchr(pname, '/'); if (fname) { *fname = '\0'; fname++; } else { fname = pname; pname = NULL; } // fprintf(stderr, "path %s dir %s fname %s\n", path, pname, fname); tc = NULL; fid = npc_walk(fs, pname); if (!fid) goto error; tc = np_create_tcreate(fid->fid, fname, perm, mode); if (npc_rpc(fs, tc, &rc) < 0) goto error; fid->iounit = rc->iounit; if (!fid->iounit || fid->iounit>fid->fsys->msize-IOHDRSZ) fid->iounit = fid->fsys->msize-IOHDRSZ; free(tc); free(rc); if (pname) free(pname); else free(fname); return fid; error: npc_clunk(fid); free(tc); if (pname) free(pname); else free(fname); return NULL; }
int npc_remove_bypath (Npcfid *root, char *path) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_remove (fid) < 0) return -1; return 0; }
int npc_chmod (Npcfid *root, char *path, mode_t mode) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_fchmod (fid, mode) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
int npc_utime(Npcfid *root, char *path, const struct utimbuf *times) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_futime (fid, times) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
int npc_truncate (Npcfid *root, char *path, off_t length) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_ftruncate (fid, length) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
int npc_chown (Npcfid *root, char *path, uid_t owner, gid_t group) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_fchown (fid, owner, group) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
static void lsfile_l (Npcfid *dir, char *name) { Npcfid *fid = NULL; struct stat sb; struct passwd *pw; struct group *gr; char uid[16], gid[16]; char *mtime; if (!(fid = npc_walk (dir, name))) { errn (np_rerror (), "npc_walk %s\n", name); goto error; } if (npc_fstat (fid, &sb) < 0) { errn (np_rerror (), "npc_fstat %s\n", name); goto error; } if (npc_clunk (fid) < 0) { errn (np_rerror (), "npc_clunk %s\n", name); goto error; } if (!(pw = getpwuid (sb.st_uid))) snprintf (uid, sizeof (uid), "%d", sb.st_uid); if (!(gr = getgrgid (sb.st_gid))) snprintf (gid, sizeof (gid), "%d", sb.st_gid); mtime = ctime( &sb.st_mtime); printf ("%10s %4lu %s %s %12lu %.*s %s\n", mode2str (sb.st_mode), sb.st_nlink, pw ? pw->pw_name : uid, gr ? gr->gr_name : gid, sb.st_size, (int)strlen (mtime) - 13, mtime + 4, name); return; error: if (fid) (void)npc_clunk (fid); }
int npc_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size, int flags) { Npcfid *fid = NULL; int n, tot = 0; int ret = -1; if (!(fid = npc_walk (root, path))) goto done; if (npc_xattrcreate (fid, name, size, flags) < 0) goto done; for (tot = 0; tot < size; tot += n) { n = npc_write(fid, val + tot, size - tot); if (n < 0) goto done; } ret = 0; done: if (fid) ret = npc_clunk(fid); return ret; }
int npc_mkdir_bypath (Npcfid *root, char *path, u32 mode) { Npcfid *fid; char *cpy, *dname, *fname; /* walk to the parent */ if (!(cpy = strdup (path))) { np_uerror (ENOMEM); return -1; } dname = dirname (cpy); fid = npc_walk (root, dname); free (cpy); if (!fid) return -1; /* create the child */ if (!(cpy = strdup (path))) { (void)npc_clunk (fid); np_uerror (ENOMEM); return -1; } fname = basename (cpy); if (npc_mkdir (fid, fname, mode) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); free (cpy); np_uerror (saved_err); return -1; } free (cpy); if (npc_clunk (fid) < 0) return -1; return 0; }
static void * loadgen (void *arg) { thd_t *t = (thd_t *)arg; uid_t uid = geteuid (); void *ret = NULL; int n, loops = 0; if ((t->fd = diod_sock_connect (t->host, t->port, 0)) < 0) goto done; if (!(t->fs = npc_start (t->fd, t->fd, t->msize, 0))) { errn (np_rerror (), "error negotiating protocol with server"); goto done; } if (!(t->afid = npc_auth (t->fs, "ctl", uid, diod_auth)) && np_rerror () != 0) { errn (np_rerror (), "error authenticating to server"); goto done; } if (!(t->root = npc_attach (t->fs, t->afid, "ctl", uid))) { errn (np_rerror (), "error attaching to aname=ctl"); goto done; } if (t->loadtype == LOAD_IO) { if (!(t->infile = npc_open_bypath (t->root, "zero", O_RDONLY))) { errn (np_rerror (), "open zero"); goto done; } if (!(t->outfile = npc_open_bypath (t->root, "null", O_WRONLY))) { errn (np_rerror (), "open null"); goto done; } do { if ((n = npc_pread (t->infile, t->buf, t->msize, 0)) <= 0) { errn (np_rerror (), "read zero"); break; } t->readbytes += n; if ((n = npc_pwrite (t->outfile, t->buf, t->msize, 0)) < 0) { errn (np_rerror (), "write null"); break; } t->writebytes += n; t->opcount++; loops++; } while ((loops % 100 != 0) || time (NULL) < t->stoptime); } else if (t->loadtype == LOAD_GETATTR) { if (!(t->infile = npc_walk (t->root, "null"))) { errn (np_rerror (), "walk null"); goto done; } do { struct stat sb; if (npc_fstat (t->infile, &sb) < 0) { errn (np_rerror (), "walk null"); break; } t->opcount++; loops++; } while ((loops % 100 != 0) || time (NULL) < t->stoptime); } done: if (t->outfile && npc_clunk (t->outfile) < 0) errn (np_rerror (), "error clunking null"); if (t->infile && npc_clunk (t->infile) < 0) errn (np_rerror (), "error clunking zero"); if (t->root && npc_clunk (t->root) < 0) errn (np_rerror (), "error clunking ctl"); if (t->afid && npc_clunk (t->afid) < 0) errn (np_rerror (), "error clunking afid"); if (t->fs) { npc_finish (t->fs); /* closes fd */ t->fd = -1; } if (t->fd != -1) close (t->fd); return ret; }