+ /* copy the filehandle */
+ nfsfh->fh.data.data_len = res->LOOKUP3res_u.resok.object.data.data_len;
+ nfsfh->fh.data.data_val = malloc(nfsfh->fh.data.data_len);
+ memcpy(nfsfh->fh.data.data_val, res->LOOKUP3res_u.resok.object.data.data_val, nfsfh->fh.data.data_len);
+
+ data->cb(0, nfs, nfsfh, data->private_data);
+ free_nfs_cb_data(data);
+}
+
+
+
+static void nfs_creat_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
+{
+ CREATE3res *res;
+ struct nfs_cb_data *data = private_data;
+ struct nfs_context *nfs = data->nfs;
+ char *str = data->continue_data;
+ LOOKUP3args args;
+
+ 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;
+ }
+
+ str = &str[strlen(str) + 1];
+ res = command_data;
+ if (res->status != NFS3_OK) {
+ rpc_set_error(nfs->rpc, "NFS: CREATE of %s/%s failed with %s(%d)", data->saved_path, str, 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;
+ }
+
+ memset(&args, 0, sizeof(LOOKUP3args));
+ args.what.dir = data->fh;
+ args.what.name = str;
+
+ if (rpc_nfs3_lookup_async(nfs->rpc, nfs_create_2_cb, &args, data) != 0) {
+ rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str);
+ data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+ free_nfs_cb_data(data);
+ return;
+ }
+ return;
+}
+
+static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
+{
+ char *str = data->continue_data;
+ CREATE3args args;
+
+ str = &str[strlen(str) + 1];
+
+ memset(&args, 0, sizeof(CREATE3args));
+ args.where.dir = data->fh;
+ args.where.name = str;
+ args.how.mode = UNCHECKED;
+ args.how.createhow3_u.obj_attributes.mode.set_it = 1;
+ args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = data->continue_int;
+
+ if (rpc_nfs3_create_async(nfs->rpc, nfs_creat_1_cb, &args, data) != 0) {
+ rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str);
+ data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+ free_nfs_cb_data(data);
+ return -1;
+ }
+ return 0;
+}
+
+int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
+{
+ char *new_path;
+ char *ptr;
+
+ new_path = strdup(path);
+ if (new_path == NULL) {
+ rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
+ return -1;
+ }
+
+ ptr = strrchr(new_path, '/');
+ if (ptr == NULL) {
+ rpc_set_error(nfs->rpc, "Invalid path %s", path);
+ free(new_path);
+ return -1;
+ }
+ *ptr = 0;
+
+ /* new_path now points to the parent directory, and beyond the nul terminator is the new directory to create */
+ if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) {
+ rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
+ return -1;
+ }