void npc_fid_free(Npcfid *fid) { if (fid) { npc_put_id(fid->fsys->fidpool, fid->fid); npc_decref_fsys(fid->fsys); free(fid); } }
Npcfsys* npc_mount(int fd, char *aname, char *uname) { Npcfsys *fs; Npfcall *tc, *rc; fs = npc_create_fsys(fd, 8216); if (!fs) return NULL; tc = np_create_tversion(8216, "9P2000.u"); if (npc_rpc(fs, tc, &rc) < 0) goto error; if (rc->version.len==8 && !memcmp(rc->version.str, "9P2000.u", 8)) { fs->dotu = 1; } else if (rc->version.len==6 && !memcmp(rc->version.str, "9P2000", 6)) { fs->dotu = 0; } else { np_werror("unsupported 9P version", EIO); goto error; } free(tc); free(rc); tc = rc = NULL; fs->root = npc_fid_alloc(fs); if (!fs->root) goto error; tc = np_create_tattach(fs->root->fid, NOFID, uname, aname); if (npc_rpc(fs, tc, &rc) < 0) goto error; free(tc); free(rc); return fs; error: free(tc); free(rc); npc_disconnect_fsys(fs); npc_decref_fsys(fs); return NULL; }
Npcfsys * npc_create_fsys(int rfd, int wfd, int msize, int flags) { Npcfsys *fs; fs = malloc(sizeof(*fs)); if (!fs) { np_uerror(ENOMEM); return NULL; } np_uerror (0); pthread_mutex_init(&fs->lock, NULL); fs->msize = msize; fs->trans = NULL; fs->tagpool = NULL; fs->fidpool = NULL; fs->refcount = 1; fs->rpc = npc_rpc; fs->incref = npc_incref_fsys; fs->decref = npc_decref_fsys; fs->disconnect = NULL; fs->flags = flags; fs->trans = np_fdtrans_create(rfd, wfd); if (!fs->trans) goto error; fs->tagpool = npc_create_pool(P9_NOTAG); if (!fs->tagpool) goto error; fs->fidpool = npc_create_pool(P9_NOFID); if (!fs->fidpool) goto error; return fs; error: npc_decref_fsys(fs); /* will close fds if trans successfully created */ (void)close (rfd); /* close here anyway for consistancy */ if (rfd != wfd) (void)close (wfd); return NULL; }
void npc_umount(Npcfsys *fs) { npc_disconnect_fsys(fs); npc_decref_fsys(fs); }
Npcfsys* npc_mount(int fd, char *aname, Npuser *user, int (*auth)(Npcfid *afid, Npuser *user, void *aux), void *aux) { Npcfsys *fs; Npfcall *tc, *rc; fs = npc_create_fsys(fd, 8216); if (!fs) return NULL; tc = np_create_tversion(8216, "9P2000.u"); if (npc_rpc(fs, tc, &rc) < 0) goto error; if (rc->version.len==8 && !memcmp(rc->version.str, "9P2000.u", 8)) { fs->dotu = 1; } else if (rc->version.len==6 && !memcmp(rc->version.str, "9P2000", 6)) { fs->dotu = 0; } else { np_werror("unsupported 9P version", EIO); goto error; } free(tc); free(rc); tc = rc = NULL; if (auth) { fs->afid = npc_fid_alloc(fs); if (!fs->afid) goto error; tc = np_create_tauth(fs->afid->fid, user?user->uname:NULL, aname, user?user->uid:-1, fs->dotu); if (npc_rpc(fs, tc, &rc) < 0) { npc_fid_free(fs->afid); fs->afid = NULL; } else if ((*auth)(fs->afid, user, aux) < 0) goto error; free(tc); free(rc); tc = rc = NULL; } fs->root = npc_fid_alloc(fs); if (!fs->root) goto error; tc = np_create_tattach(fs->root->fid, fs->afid?fs->afid->fid:NOFID, user->uname, aname, user->uid, fs->dotu); if (npc_rpc(fs, tc, &rc) < 0) goto error; free(tc); free(rc); return fs; error: free(tc); free(rc); npc_disconnect_fsys(fs); npc_decref_fsys(fs); return NULL; }