diff options
Diffstat (limited to 'lib9p/srv.c')
-rw-r--r-- | lib9p/srv.c | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/lib9p/srv.c b/lib9p/srv.c index e1a1d84..7e2bab0 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -70,6 +70,8 @@ typedef typeof( ((struct lib9p_qid){}).path ) srv_path_t; struct srv_pathinfo { lo_interface lib9p_srv_file file; enum srv_filetype type; + /* .parent_dir is used for (1) Twalk(".."), and (2) for checking + * permissions on the parent directory for remove(). */ srv_path_t parent_dir; /* References from other srv_pathinfos (via .parent_dir) or @@ -273,7 +275,7 @@ static inline void srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, bool remove struct lib9p_stat parent_stat = LO_CALL(parent->file, stat, ctx); if (!srv_check_perm(ctx, &parent_stat, 0b010)) { lib9p_error(&ctx->basectx, - LIB9P_ERRNO_L_EACCES, "remove: you do not have write permission on the parent directory"); + LIB9P_ERRNO_L_EACCES, "you do not have write permission on the parent directory"); goto clunk; } LO_CALL(pathinfo->file, remove, ctx); @@ -304,16 +306,26 @@ static inline void srv_fid_del(struct srv_req *ctx, lib9p_fid_t fid, bool remove * pathinfo->gc_refcount has already been incremented; does *not* * decrement it on failure. */ -static struct srv_fidinfo *srv_fid_store(struct srv_req *ctx, lib9p_fid_t fid, struct srv_pathinfo *pathinfo, bool overwrite) { +static inline struct srv_fidinfo *srv_fid_store(struct srv_req *ctx, lib9p_fid_t fid, struct srv_pathinfo *pathinfo, bool overwrite) { assert(ctx); assert(fid != LIB9P_FID_NOFID); assert(pathinfo); struct lib9p_qid qid = LO_CALL(pathinfo->file, qid); - if (map_load(&ctx->parent_sess->fids, fid)) { + struct srv_fidinfo *old_fidinfo = map_load(&ctx->parent_sess->fids, fid); + if (old_fidinfo) { if (overwrite) { - srv_fid_del(ctx, fid, false); + /* This should only happen from Twalk; because + * directories cannot be RCLOSE and Twalk cannot walk on + * FIDs open for I/O, we can skip most of + * srv_fid_del(). */ + assert(old_fidinfo->type == SRV_FILETYPE_DIR); + assert(old_fidinfo->flags == 0); + + old_fidinfo->authinfo = srv_authinfo_decref(old_fidinfo->authinfo); + srv_path_decref(ctx, old_fidinfo->path); + map_del(&ctx->parent_sess->fids, fid); } else { lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EBADF, "FID already in use"); @@ -876,6 +888,11 @@ static void handle_Twalk(struct srv_req *ctx, LIB9P_ERRNO_L_EBADF, "bad file number %"PRIu32, req->fid); return; } + if (fidinfo->flags & FIDFLAG_OPEN) { + lib9p_error(&ctx->basectx, + LIB9P_ERRNO_L_EALREADY, "cannot walk on FID open for I/O"); + return; + } ctx->authinfo = srv_authinfo_incref(fidinfo->authinfo); struct srv_pathinfo *pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path); @@ -884,23 +901,24 @@ static void handle_Twalk(struct srv_req *ctx, resp->wqid = (struct lib9p_qid *)(&resp[1]); for (resp->nwqid = 0; resp->nwqid < req->nwname; resp->nwqid++) { + if (pathinfo->type != SRV_FILETYPE_DIR) { + lib9p_error(&ctx->basectx, + LIB9P_ERRNO_L_ENOTDIR, "not a directory"); + break; + } + struct srv_pathinfo *new_pathinfo; if (lib9p_str_eq(req->wname[resp->nwqid], lib9p_str(".."))) { new_pathinfo = map_load(&ctx->parent_sess->paths, pathinfo->parent_dir); assert(new_pathinfo); new_pathinfo->gc_refcount++; } else { - if (pathinfo->type != SRV_FILETYPE_DIR) { - lib9p_error(&ctx->basectx, - LIB9P_ERRNO_L_ENOTDIR, "not a directory"); - break; - } - lo_interface lib9p_srv_file member_file = LO_CALL(pathinfo->file, dwalk, ctx, req->wname[resp->nwqid]); assert(LO_IS_NULL(member_file) == lib9p_ctx_has_error(&ctx->basectx)); if (lib9p_ctx_has_error(&ctx->basectx)) break; new_pathinfo = srv_path_save(ctx, member_file, LO_CALL(pathinfo->file, qid).path); + assert(new_pathinfo); } if (new_pathinfo->type == SRV_FILETYPE_DIR) { |