From: Ross Lagerwall Date: Sun, 14 Sep 2014 14:41:04 +0000 (+0100) Subject: libnfs: Add access2() X-Git-Tag: upstream/1.9.6^2~2^2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=23b10e1f18c57e88ac4ef13b4dd08b976834260d;p=deb_libnfs.git libnfs: Add access2() Add nfs_access2(), like nfs_access() but it returns the individual statuses of R_OK, W_OK and X_OK rather than a single success or failure status. This saves the latency and overhead of multiple lookups if an application tries to determine the status of each of R_OK, W_OK and X_OK. Signed-off-by: Ross Lagerwall --- diff --git a/include/nfsc/libnfs.h b/include/nfsc/libnfs.h index e39e2e2..6707232 100644 --- a/include/nfsc/libnfs.h +++ b/include/nfsc/libnfs.h @@ -1270,6 +1270,36 @@ EXTERN int nfs_access(struct nfs_context *nfs, const char *path, int mode); + +/* + * ACCESS2() + */ +/* + * Async access2() + * Function returns + * 0 : The operation was initiated. Once the operation finishes, the callback will be invoked. + * <0 : An error occured when trying to set up the operation. The callback will not be invoked. + * + * When the callback is invoked, status indicates the result: + * >= 0 : A mask of R_OK, W_OK and X_OK indicating which permissions are + * available. + * data is NULL + * -errno : An error occured. + * data is the error string. + */ +EXTERN int nfs_access2_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data); +/* + * Sync access() + * Function returns + * >= 0 : A mask of R_OK, W_OK and X_OK indicating which permissions are + * available. + * -errno : The command failed. + */ +EXTERN int nfs_access2(struct nfs_context *nfs, const char *path); + + + + /* * SYMLINK() */ diff --git a/lib/libnfs-sync.c b/lib/libnfs-sync.c index 2851a91..59911fc 100644 --- a/lib/libnfs-sync.c +++ b/lib/libnfs-sync.c @@ -1277,6 +1277,40 @@ int nfs_access(struct nfs_context *nfs, const char *path, int mode) +/* + * access2() + */ +static void access2_cb(int status, struct nfs_context *nfs, void *data, void *private_data) +{ + struct sync_cb_data *cb_data = private_data; + + cb_data->is_finished = 1; + cb_data->status = status; + + if (status < 0) { + nfs_set_error(nfs, "access2 call failed with \"%s\"", (char *)data); + return; + } +} + +int nfs_access2(struct nfs_context *nfs, const char *path) +{ + struct sync_cb_data cb_data; + + cb_data.is_finished = 0; + + if (nfs_access2_async(nfs, path, access2_cb, &cb_data) != 0) { + nfs_set_error(nfs, "nfs_access2_async failed"); + return -1; + } + + wait_for_nfs_reply(nfs, &cb_data); + + return cb_data.status; +} + + + /* * symlink() */ diff --git a/lib/libnfs.c b/lib/libnfs.c index 566583c..2bacd91 100644 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -4404,6 +4404,80 @@ int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb +/* + * Async access2() + */ +static void nfs_access2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data) +{ + ACCESS3res *res; + struct nfs_cb_data *data = private_data; + struct nfs_context *nfs = data->nfs; + unsigned int result = 0; + + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + if (status == RPC_STATUS_ERROR) { + data->cb(-EFAULT, nfs, command_data, data->private_data); + free_nfs_cb_data(data); + return; + } + if (status == RPC_STATUS_CANCEL) { + data->cb(-EINTR, nfs, "Command was cancelled", data->private_data); + free_nfs_cb_data(data); + return; + } + + res = command_data; + if (res->status != NFS3_OK) { + rpc_set_error(nfs->rpc, "NFS: ACCESS of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status)); + data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data); + free_nfs_cb_data(data); + return; + } + + if (res->ACCESS3res_u.resok.access & ACCESS3_READ) { + result |= R_OK; + } + if (res->ACCESS3res_u.resok.access & (ACCESS3_MODIFY | ACCESS3_EXTEND | ACCESS3_DELETE)) { + result |= W_OK; + } + if (res->ACCESS3res_u.resok.access & (ACCESS3_LOOKUP | ACCESS3_EXECUTE)) { + result |= X_OK; + } + + data->cb(result, nfs, NULL, data->private_data); + free_nfs_cb_data(data); +} + +static int nfs_access2_continue_internal(struct nfs_context *nfs, fattr3 *attr _U_, struct nfs_cb_data *data) +{ + ACCESS3args args; + + memset(&args, 0, sizeof(ACCESS3args)); + args.object = data->fh; + args.access = ACCESS3_READ | ACCESS3_LOOKUP | ACCESS3_MODIFY | ACCESS3_EXTEND | ACCESS3_DELETE | ACCESS3_EXECUTE; + + if (rpc_nfs3_access_async(nfs->rpc, nfs_access2_cb, &args, data) != 0) { + rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path); + data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data); + free_nfs_cb_data(data); + return -1; + } + return 0; +} + +int nfs_access2_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data) +{ + if (nfs_lookuppath_async(nfs, path, 0, cb, private_data, nfs_access2_continue_internal, NULL, NULL, 0) != 0) { + rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components"); + return -1; + } + + return 0; +} + + + /* * Async symlink() */