Update win32 readme to point to location for oncrpc library for windows
[deb_libnfs.git] / lib / libnfs.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
17/*
18 * High level api to nfs filesystems
19 */
a8a1b858
M
20#ifdef WIN32
21#include "win32_compat.h"
6874f61e 22#define DllExport
6874f61e
RS
23#else
24#include <strings.h>
25#include <sys/statvfs.h>
26#include <utime.h>
27#include <unistd.h>
a8a1b858
M
28#endif/*WIN32*/
29
30#define _GNU_SOURCE
6874f61e 31
84004dbf 32#include <stdio.h>
1896d37b 33#include <stdarg.h>
84004dbf
RS
34#include <stdlib.h>
35#include <string.h>
84004dbf
RS
36#include <errno.h>
37#include <sys/types.h>
38#include <sys/stat.h>
84004dbf
RS
39#include <fcntl.h>
40#include "libnfs.h"
41#include "libnfs-raw.h"
42#include "libnfs-raw-mount.h"
43#include "libnfs-raw-nfs.h"
1896d37b
RS
44#include "libnfs-private.h"
45
46struct nfsdir {
47 struct nfsdirent *entries;
48 struct nfsdirent *current;
49};
84004dbf
RS
50
51struct nfsfh {
52 struct nfs_fh3 fh;
53 int is_sync;
54 off_t offset;
55};
56
1896d37b
RS
57struct nfs_context {
58 struct rpc_context *rpc;
59 char *server;
60 char *export;
61 struct nfs_fh3 rootfh;
62 size_t readmax;
63 size_t writemax;
84004dbf
RS
64};
65
66void nfs_free_nfsdir(struct nfsdir *nfsdir)
67{
68 while (nfsdir->entries) {
69 struct nfsdirent *dirent = nfsdir->entries->next;
70 if (nfsdir->entries->name != NULL) {
71 free(nfsdir->entries->name);
72 }
73 free(nfsdir->entries);
74 nfsdir->entries = dirent;
75 }
76 free(nfsdir);
77}
78
84004dbf
RS
79struct nfs_cb_data;
80typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data);
81
82struct nfs_cb_data {
83 struct nfs_context *nfs;
84 struct nfsfh *nfsfh;
85 char *saved_path, *path;
86
87 nfs_cb cb;
88 void *private_data;
89
90 continue_func continue_cb;
91 void *continue_data;
92 void (*free_continue_data)(void *);
93 int continue_int;
94
95 struct nfs_fh3 fh;
921f877b
RS
96
97 /* for multi-read/write calls. */
98 int error;
99 int cancel;
100 int num_calls;
101 off_t start_offset, max_offset;
102 char *buffer;
103};
104
105struct nfs_mcb_data {
106 struct nfs_cb_data *data;
107 off_t offset;
108 size_t count;
84004dbf
RS
109};
110
111static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh);
112
113
114void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth)
115{
9896c715 116 rpc_set_auth(nfs->rpc, auth);
84004dbf
RS
117}
118
119int nfs_get_fd(struct nfs_context *nfs)
120{
121 return rpc_get_fd(nfs->rpc);
122}
123
124int nfs_which_events(struct nfs_context *nfs)
125{
126 return rpc_which_events(nfs->rpc);
127}
128
129int nfs_service(struct nfs_context *nfs, int revents)
130{
131 return rpc_service(nfs->rpc, revents);
132}
133
134char *nfs_get_error(struct nfs_context *nfs)
135{
136 return rpc_get_error(nfs->rpc);
137};
138
139struct nfs_context *nfs_init_context(void)
140{
141 struct nfs_context *nfs;
142
143 nfs = malloc(sizeof(struct nfs_context));
144 if (nfs == NULL) {
84004dbf
RS
145 return NULL;
146 }
147 nfs->rpc = rpc_init_context();
148 if (nfs->rpc == NULL) {
84004dbf
RS
149 free(nfs);
150 return NULL;
151 }
152
b077fdeb
RS
153 nfs->server = NULL;
154 nfs->export = NULL;
155
963c2f83
RS
156 nfs->rootfh.data.data_len = 0;
157 nfs->rootfh.data.data_val = NULL;
158
84004dbf
RS
159 return nfs;
160}
161
162void nfs_destroy_context(struct nfs_context *nfs)
163{
164 rpc_destroy_context(nfs->rpc);
165 nfs->rpc = NULL;
166
167 if (nfs->server) {
168 free(nfs->server);
169 nfs->server = NULL;
170 }
171
172 if (nfs->export) {
173 free(nfs->export);
174 nfs->export = NULL;
175 }
176
177 if (nfs->rootfh.data.data_val != NULL) {
178 free(nfs->rootfh.data.data_val);
179 nfs->rootfh.data.data_val = NULL;
180 }
181
182 free(nfs);
183}
184
185void free_nfs_cb_data(struct nfs_cb_data *data)
186{
187 if (data->saved_path != NULL) {
188 free(data->saved_path);
189 data->saved_path = NULL;
190 }
191
192 if (data->continue_data != NULL) {
193 data->free_continue_data(data->continue_data);
194 data->continue_data = NULL;
195 }
196
197 if (data->fh.data.data_val != NULL) {
198 free(data->fh.data.data_val);
199 data->fh.data.data_val = NULL;
200 }
201
921f877b
RS
202 if (data->buffer != NULL) {
203 free(data->buffer);
204 data->buffer = NULL;
205 }
206
84004dbf
RS
207 free(data);
208}
209
210
211
212
213
17ef62fa 214static void nfs_mount_10_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
84004dbf
RS
215{
216 struct nfs_cb_data *data = private_data;
217 struct nfs_context *nfs = data->nfs;
218
219 if (status == RPC_STATUS_ERROR) {
220 data->cb(-EFAULT, nfs, command_data, data->private_data);
221 free_nfs_cb_data(data);
222 return;
223 }
224 if (status == RPC_STATUS_CANCEL) {
225 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
226 free_nfs_cb_data(data);
227 return;
228 }
229
230 data->cb(0, nfs, NULL, data->private_data);
231 free_nfs_cb_data(data);
232}
233
17ef62fa 234static void nfs_mount_9_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
84004dbf
RS
235{
236 struct nfs_cb_data *data = private_data;
237 struct nfs_context *nfs = data->nfs;
17ef62fa 238 FSINFO3res *res = command_data;
84004dbf
RS
239
240 if (status == RPC_STATUS_ERROR) {
241 data->cb(-EFAULT, nfs, command_data, data->private_data);
242 free_nfs_cb_data(data);
243 return;
244 }
245 if (status == RPC_STATUS_CANCEL) {
246 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
247 free_nfs_cb_data(data);
248 return;
249 }
250
17ef62fa
RS
251 nfs->readmax = res->FSINFO3res_u.resok.rtmax;
252 nfs->writemax = res->FSINFO3res_u.resok.wtmax;
84004dbf 253
17ef62fa 254 if (rpc_nfs_getattr_async(rpc, nfs_mount_10_cb, &nfs->rootfh, data) != 0) {
84004dbf
RS
255 data->cb(-ENOMEM, nfs, command_data, data->private_data);
256 free_nfs_cb_data(data);
257 return;
258 }
259}
260
17ef62fa
RS
261static void nfs_mount_8_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
262{
263 struct nfs_cb_data *data = private_data;
264 struct nfs_context *nfs = data->nfs;
265
266 if (status == RPC_STATUS_ERROR) {
267 data->cb(-EFAULT, nfs, command_data, data->private_data);
268 free_nfs_cb_data(data);
269 return;
270 }
271 if (status == RPC_STATUS_CANCEL) {
272 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
273 free_nfs_cb_data(data);
274 return;
275 }
276
277 if (rpc_nfs_fsinfo_async(rpc, nfs_mount_9_cb, &nfs->rootfh, data) != 0) {
278 data->cb(-ENOMEM, nfs, command_data, data->private_data);
279 free_nfs_cb_data(data);
280 return;
281 }
282}
283
284
84004dbf
RS
285static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
286{
287 struct nfs_cb_data *data = private_data;
288 struct nfs_context *nfs = data->nfs;
289
290 if (status == RPC_STATUS_ERROR) {
291 data->cb(-EFAULT, nfs, command_data, data->private_data);
292 free_nfs_cb_data(data);
293 return;
294 }
295 if (status == RPC_STATUS_CANCEL) {
296 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
297 free_nfs_cb_data(data);
298 return;
299 }
300
301 if (rpc_nfs_null_async(rpc, nfs_mount_8_cb, data) != 0) {
302 data->cb(-ENOMEM, nfs, command_data, data->private_data);
303 free_nfs_cb_data(data);
304 return;
305 }
306}
307
308
309static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
310{
311 struct nfs_cb_data *data = private_data;
312 struct nfs_context *nfs = data->nfs;
313 mountres3 *res;
314
315 if (status == RPC_STATUS_ERROR) {
316 data->cb(-EFAULT, nfs, command_data, data->private_data);
317 free_nfs_cb_data(data);
318 return;
319 }
320 if (status == RPC_STATUS_CANCEL) {
321 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
322 free_nfs_cb_data(data);
323 return;
324 }
325
326 res = command_data;
327 if (res->fhs_status != MNT3_OK) {
328 rpc_set_error(rpc, "RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res->fhs_status), res->fhs_status, strerror(-mountstat3_to_errno(res->fhs_status)), -mountstat3_to_errno(res->fhs_status));
329 data->cb(mountstat3_to_errno(res->fhs_status), nfs, rpc_get_error(rpc), data->private_data);
330 free_nfs_cb_data(data);
331 return;
332 }
333
334 nfs->rootfh.data.data_len = res->mountres3_u.mountinfo.fhandle.fhandle3_len;
335 nfs->rootfh.data.data_val = malloc(nfs->rootfh.data.data_len);
336 if (nfs->rootfh.data.data_val == NULL) {
337 rpc_set_error(rpc, "Out of memory. Could not allocate memory to store root filehandle");
338 data->cb(-ENOMEM, nfs, rpc_get_error(rpc), data->private_data);
339 free_nfs_cb_data(data);
340 return;
341 }
342 memcpy(nfs->rootfh.data.data_val, res->mountres3_u.mountinfo.fhandle.fhandle3_val, nfs->rootfh.data.data_len);
343
344 rpc_disconnect(rpc, "normal disconnect");
345 if (rpc_connect_async(rpc, nfs->server, 2049, nfs_mount_7_cb, data) != 0) {
346 data->cb(-ENOMEM, nfs, command_data, data->private_data);
347 free_nfs_cb_data(data);
348 return;
349 }
350}
351
352
353static void nfs_mount_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
354{
355 struct nfs_cb_data *data = private_data;
356 struct nfs_context *nfs = data->nfs;
357
358 if (status == RPC_STATUS_ERROR) {
359 data->cb(-EFAULT, nfs, command_data, data->private_data);
360 free_nfs_cb_data(data);
361 return;
362 }
363 if (status == RPC_STATUS_CANCEL) {
364 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
365 free_nfs_cb_data(data);
366 return;
367 }
368
369 if (rpc_mount_mnt_async(rpc, nfs_mount_6_cb, nfs->export, data) != 0) {
370 data->cb(-ENOMEM, nfs, command_data, data->private_data);
371 free_nfs_cb_data(data);
372 return;
373 }
374}
375
376static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
377{
378 struct nfs_cb_data *data = private_data;
379 struct nfs_context *nfs = data->nfs;
380
381 if (status == RPC_STATUS_ERROR) {
382 data->cb(-EFAULT, nfs, command_data, data->private_data);
383 free_nfs_cb_data(data);
384 return;
385 }
386 if (status == RPC_STATUS_CANCEL) {
387 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
388 free_nfs_cb_data(data);
389 return;
390 }
391
392 if (rpc_mount_null_async(rpc, nfs_mount_5_cb, data) != 0) {
393 data->cb(-ENOMEM, nfs, command_data, data->private_data);
394 free_nfs_cb_data(data);
395 return;
396 }
397}
398
399static void nfs_mount_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
400{
401 struct nfs_cb_data *data = private_data;
402 struct nfs_context *nfs = data->nfs;
403 uint32_t mount_port;
404
405 if (status == RPC_STATUS_ERROR) {
406 data->cb(-EFAULT, nfs, command_data, data->private_data);
407 free_nfs_cb_data(data);
408 return;
409 }
410 if (status == RPC_STATUS_CANCEL) {
411 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
412 free_nfs_cb_data(data);
413 return;
414 }
415
416 mount_port = *(uint32_t *)command_data;
417 if (mount_port == 0) {
418 rpc_set_error(rpc, "RPC error. Mount program is not available on %s", nfs->server);
419 data->cb(-ENOENT, nfs, command_data, data->private_data);
420 free_nfs_cb_data(data);
421 return;
422 }
423
424 rpc_disconnect(rpc, "normal disconnect");
425 if (rpc_connect_async(rpc, nfs->server, mount_port, nfs_mount_4_cb, data) != 0) {
426 data->cb(-ENOMEM, nfs, command_data, data->private_data);
427 free_nfs_cb_data(data);
428 return;
429 }
430}
431
432
433static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
434{
435 struct nfs_cb_data *data = private_data;
436 struct nfs_context *nfs = data->nfs;
437
438 if (status == RPC_STATUS_ERROR) {
439 data->cb(-EFAULT, nfs, command_data, data->private_data);
440 free_nfs_cb_data(data);
441 return;
442 }
443 if (status == RPC_STATUS_CANCEL) {
444 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
445 free_nfs_cb_data(data);
446 return;
447 }
448
449 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, nfs_mount_3_cb, private_data) != 0) {
450 data->cb(-ENOMEM, nfs, command_data, data->private_data);
451 free_nfs_cb_data(data);
452 return;
453 }
454}
455
456static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
457{
458 struct nfs_cb_data *data = private_data;
459 struct nfs_context *nfs = data->nfs;
460
461 if (status == RPC_STATUS_ERROR) {
462 data->cb(-EFAULT, nfs, command_data, data->private_data);
463 free_nfs_cb_data(data);
464 return;
465 }
466 if (status == RPC_STATUS_CANCEL) {
467 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
468 free_nfs_cb_data(data);
469 return;
470 }
471
472 if (rpc_pmap_null_async(rpc, nfs_mount_2_cb, data) != 0) {
473 data->cb(-ENOMEM, nfs, command_data, data->private_data);
474 free_nfs_cb_data(data);
475 return;
476 }
477}
478
479/*
480 * Async call for mounting an nfs share and geting the root filehandle
481 */
482int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *export, nfs_cb cb, void *private_data)
483{
484 struct nfs_cb_data *data;
b077fdeb 485 char *new_server, *new_export;
84004dbf
RS
486
487 data = malloc(sizeof(struct nfs_cb_data));
488 if (data == NULL) {
cbbf9d3e 489 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
490 return -1;
491 }
ea98629a 492 memset(data, 0, sizeof(struct nfs_cb_data));
b077fdeb
RS
493 new_server = strdup(server);
494 new_export = strdup(export);
495 if (nfs->server != NULL) {
496 free(nfs->server);
497 nfs->server = NULL;
498 }
499 nfs->server = new_server;
500 if (nfs->export != NULL) {
501 free(nfs->export);
502 nfs->export = NULL;
503 }
504 nfs->export = new_export;
84004dbf
RS
505 data->nfs = nfs;
506 data->cb = cb;
507 data->private_data = private_data;
508
509 if (rpc_connect_async(nfs->rpc, server, 111, nfs_mount_1_cb, data) != 0) {
cbbf9d3e 510 rpc_set_error(nfs->rpc, "Failed to start connection");
84004dbf 511 free_nfs_cb_data(data);
cbbf9d3e 512 return -1;
84004dbf
RS
513 }
514
515 return 0;
516}
517
518
519
520/*
521 * Functions to first look up a path, component by component, and then finally call a specific function once
522 * the filehandle for the final component is found.
523 */
524static void nfs_lookup_path_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
525{
526 struct nfs_cb_data *data = private_data;
527 struct nfs_context *nfs = data->nfs;
528 LOOKUP3res *res;
529
530 if (status == RPC_STATUS_ERROR) {
531 data->cb(-EFAULT, nfs, command_data, data->private_data);
532 free_nfs_cb_data(data);
533 return;
534 }
535 if (status == RPC_STATUS_CANCEL) {
536 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
537 free_nfs_cb_data(data);
538 return;
539 }
540
541 res = command_data;
542 if (res->status != NFS3_OK) {
543 rpc_set_error(nfs->rpc, "NFS: Lookup of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
544 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
545 free_nfs_cb_data(data);
546 return;
547 }
548
549 if (nfs_lookup_path_async_internal(nfs, data, &res->LOOKUP3res_u.resok.object) != 0) {
550 rpc_set_error(nfs->rpc, "Failed to create lookup pdu");
551 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
552 free_nfs_cb_data(data);
553 return;
554 }
555}
556
557static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh)
558{
559 char *path, *str;
560
561 while (*data->path == '/') {
562 data->path++;
563 }
564
565 path = data->path;
93e9306f 566 str = strchr(path, '/');
84004dbf
RS
567 if (str != NULL) {
568 *str = 0;
569 data->path = str+1;
570 } else {
571 while (*data->path != 0) {
572 data->path++;
573 }
574 }
575
576 if (*path == 0) {
577 data->fh.data.data_len = fh->data.data_len;
578 data->fh.data.data_val = malloc(data->fh.data.data_len);
579 if (data->fh.data.data_val == NULL) {
580 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh for %s", data->path);
581 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
582 free_nfs_cb_data(data);
583 return -1;
584 }
585 memcpy(data->fh.data.data_val, fh->data.data_val, data->fh.data.data_len);
586 data->continue_cb(nfs, data);
587 return 0;
588 }
589
590 if (rpc_nfs_lookup_async(nfs->rpc, nfs_lookup_path_1_cb, fh, path, data) != 0) {
591 rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s", data->path);
592 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
593 free_nfs_cb_data(data);
594 return -1;
595 }
596 return 0;
597}
598
599static int nfs_lookuppath_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data, continue_func continue_cb, void *continue_data, void (*free_continue_data)(void *), int continue_int)
600{
601 struct nfs_cb_data *data;
602
603 if (path[0] != '/') {
604 rpc_set_error(nfs->rpc, "Pathname is not absulute %s", path);
605 return -1;
606 }
607
608 data = malloc(sizeof(struct nfs_cb_data));
609 if (data == NULL) {
610 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
cbbf9d3e 611 return -1;
84004dbf 612 }
ea98629a 613 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
614 data->nfs = nfs;
615 data->cb = cb;
616 data->continue_cb = continue_cb;
617 data->continue_data = continue_data;
618 data->free_continue_data = free_continue_data;
619 data->continue_int = continue_int;
620 data->private_data = private_data;
621 data->saved_path = strdup(path);
622 if (data->saved_path == NULL) {
623 rpc_set_error(nfs->rpc, "out of memory: failed to copy path string");
84004dbf 624 free_nfs_cb_data(data);
cbbf9d3e 625 return -1;
84004dbf
RS
626 }
627 data->path = data->saved_path;
628
629 if (nfs_lookup_path_async_internal(nfs, data, &nfs->rootfh) != 0) {
84004dbf
RS
630 /* return 0 here since the callback will be invoked if there is a failure */
631 return 0;
632 }
633 return 0;
634}
635
636
637
638
639
640/*
641 * Async stat()
642 */
643static void nfs_stat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
644{
645 GETATTR3res *res;
646 struct nfs_cb_data *data = private_data;
647 struct nfs_context *nfs = data->nfs;
648 struct stat st;
649
650 if (status == RPC_STATUS_ERROR) {
651 data->cb(-EFAULT, nfs, command_data, data->private_data);
652 free_nfs_cb_data(data);
653 return;
654 }
655 if (status == RPC_STATUS_CANCEL) {
656 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
657 free_nfs_cb_data(data);
658 return;
659 }
660
661 res = command_data;
662 if (res->status != NFS3_OK) {
663 rpc_set_error(nfs->rpc, "NFS: GETATTR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
664 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
665 free_nfs_cb_data(data);
666 return;
667 }
668
669 st.st_dev = -1;
670 st.st_ino = res->GETATTR3res_u.resok.obj_attributes.fileid;
671 st.st_mode = res->GETATTR3res_u.resok.obj_attributes.mode;
e8cab72a
RS
672 if (res->GETATTR3res_u.resok.obj_attributes.type == NF3DIR) {
673 st.st_mode |= S_IFDIR ;
674 }
84004dbf
RS
675 st.st_nlink = res->GETATTR3res_u.resok.obj_attributes.nlink;
676 st.st_uid = res->GETATTR3res_u.resok.obj_attributes.uid;
677 st.st_gid = res->GETATTR3res_u.resok.obj_attributes.gid;
678 st.st_rdev = 0;
679 st.st_size = res->GETATTR3res_u.resok.obj_attributes.size;
a8a1b858 680#ifndef WIN32
84004dbf
RS
681 st.st_blksize = 4096;
682 st.st_blocks = res->GETATTR3res_u.resok.obj_attributes.size / 4096;
a8a1b858 683#endif//WIN32
84004dbf
RS
684 st.st_atime = res->GETATTR3res_u.resok.obj_attributes.atime.seconds;
685 st.st_mtime = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds;
686 st.st_ctime = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds;
687
688 data->cb(0, nfs, &st, data->private_data);
689 free_nfs_cb_data(data);
690}
691
692static int nfs_stat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
693{
694 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &data->fh, data) != 0) {
695 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
696 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
697 free_nfs_cb_data(data);
698 return -1;
699 }
700 return 0;
701}
702
703int nfs_stat_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
704{
705 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_stat_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 706 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
707 return -1;
708 }
709
710 return 0;
711}
712
713
714
715
716
717/*
718 * Async open()
719 */
720static void nfs_open_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
721{
722 ACCESS3res *res;
723 struct nfs_cb_data *data = private_data;
724 struct nfs_context *nfs = data->nfs;
725 struct nfsfh *nfsfh;
726 unsigned int nfsmode = 0;
727
728 if (status == RPC_STATUS_ERROR) {
729 data->cb(-EFAULT, nfs, command_data, data->private_data);
730 free_nfs_cb_data(data);
731 return;
732 }
733 if (status == RPC_STATUS_CANCEL) {
734 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
735 free_nfs_cb_data(data);
736 return;
737 }
738
739 res = command_data;
740 if (res->status != NFS3_OK) {
741 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));
742 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
743 free_nfs_cb_data(data);
744 return;
745 }
746
747 if (data->continue_int & O_WRONLY) {
748 nfsmode |= ACCESS3_MODIFY;
749 }
750 if (data->continue_int & O_RDWR) {
751 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
752 }
753 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
754 nfsmode |= ACCESS3_READ;
755 }
756
757
758 if (res->ACCESS3res_u.resok.access != nfsmode) {
759 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
760 nfsmode&ACCESS3_READ?'r':'-',
761 nfsmode&ACCESS3_MODIFY?'w':'-',
762 nfsmode&ACCESS3_EXECUTE?'x':'-',
763 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
764 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
765 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
766 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
767 free_nfs_cb_data(data);
768 return;
769 }
770
771 nfsfh = malloc(sizeof(struct nfsfh));
772 if (nfsfh == NULL) {
773 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
774 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
775 free_nfs_cb_data(data);
776 return;
777 }
ea98629a 778 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
779
780 if (data->continue_int & O_SYNC) {
781 nfsfh->is_sync = 1;
782 }
783
784 /* steal the filehandle */
785 nfsfh->fh.data.data_len = data->fh.data.data_len;
786 nfsfh->fh.data.data_val = data->fh.data.data_val;
787 data->fh.data.data_val = NULL;
788
789 data->cb(0, nfs, nfsfh, data->private_data);
790 free_nfs_cb_data(data);
791}
792
793static int nfs_open_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
794{
795 int nfsmode = 0;
796
797 if (data->continue_int & O_WRONLY) {
798 nfsmode |= ACCESS3_MODIFY;
799 }
800 if (data->continue_int & O_RDWR) {
801 nfsmode |= ACCESS3_READ|ACCESS3_MODIFY;
802 }
803 if (!(data->continue_int & (O_WRONLY|O_RDWR))) {
804 nfsmode |= ACCESS3_READ;
805 }
806
807 if (rpc_nfs_access_async(nfs->rpc, nfs_open_cb, &data->fh, nfsmode, data) != 0) {
808 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
809 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
810 free_nfs_cb_data(data);
811 return -1;
812 }
813 return 0;
814}
815
816int nfs_open_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
817{
818 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_open_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
819 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
820 return -1;
84004dbf
RS
821 }
822
823 return 0;
824}
825
826
827
828
829
830/*
831 * Async pread()
832 */
833static void nfs_pread_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
834{
835 struct nfs_cb_data *data = private_data;
836 struct nfs_context *nfs = data->nfs;
837 READ3res *res;
838
839 if (status == RPC_STATUS_ERROR) {
840 data->cb(-EFAULT, nfs, command_data, data->private_data);
841 free_nfs_cb_data(data);
842 return;
843 }
844 if (status == RPC_STATUS_CANCEL) {
845 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
846 free_nfs_cb_data(data);
847 return;
848 }
849
850 res = command_data;
851 if (res->status != NFS3_OK) {
852 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
853 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
854 free_nfs_cb_data(data);
855 return;
856 }
857
858 data->nfsfh->offset += res->READ3res_u.resok.count;
859 data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data);
860 free_nfs_cb_data(data);
861}
862
921f877b
RS
863static void nfs_pread_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
864{
865 struct nfs_mcb_data *mdata = private_data;
866 struct nfs_cb_data *data = mdata->data;
867 struct nfs_context *nfs = data->nfs;
868 READ3res *res;
869
870 data->num_calls--;
871
872 if (status == RPC_STATUS_ERROR) {
873 /* flag the failure but do not invoke callback until we have received all responses */
874 data->error = 1;
875 }
876 if (status == RPC_STATUS_CANCEL) {
877 /* flag the cancellation but do not invoke callback until we have received all responses */
878 data->cancel = 1;
879 }
880
881 /* reassemble the data into the buffer */
882 if (status == RPC_STATUS_SUCCESS) {
883 res = command_data;
884 if (res->status != NFS3_OK) {
885 rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
886 data->error = 1;
e4a5ba42
RS
887 } else {
888 if (res->READ3res_u.resok.count > 0) {
889 memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count);
890 if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) {
891 data->max_offset = mdata->offset + res->READ3res_u.resok.count;
892 }
921f877b
RS
893 }
894 }
895 }
896
897 if (data->num_calls > 0) {
898 /* still waiting for more replies */
899 free(mdata);
900 return;
901 }
902
921f877b
RS
903 if (data->error != 0) {
904 data->cb(-EFAULT, nfs, command_data, data->private_data);
905 free_nfs_cb_data(data);
906 free(mdata);
907 return;
908 }
909 if (data->cancel != 0) {
910 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
911 free_nfs_cb_data(data);
912 free(mdata);
913 return;
914 }
915
916 data->nfsfh->offset = data->max_offset;
7ed9d87a
RS
917 data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data);
918
921f877b
RS
919 free_nfs_cb_data(data);
920 free(mdata);
921}
922
84004dbf
RS
923int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t count, nfs_cb cb, void *private_data)
924{
925 struct nfs_cb_data *data;
926
927 data = malloc(sizeof(struct nfs_cb_data));
928 if (data == NULL) {
929 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
930 return -1;
931 }
ea98629a 932 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
933 data->nfs = nfs;
934 data->cb = cb;
935 data->private_data = private_data;
936 data->nfsfh = nfsfh;
937
938 nfsfh->offset = offset;
921f877b
RS
939
940 if (count <= nfs_get_readmax(nfs)) {
941 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_cb, &nfsfh->fh, offset, count, data) != 0) {
942 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
943 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
944 free_nfs_cb_data(data);
945 return -1;
946 }
947 return 0;
948 }
949
950 /* trying to read more than maximum server read size, we has to chop it up into smaller
951 * reads and collect into a reassembly buffer.
952 * we send all reads in parallell so that performance is still good.
953 */
7ed9d87a 954 data->max_offset = offset;
921f877b
RS
955 data->start_offset = offset;
956
957 data->buffer = malloc(count);
958 if (data->buffer == NULL) {
959 rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count);
84004dbf
RS
960 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
961 free_nfs_cb_data(data);
962 return -1;
963 }
921f877b
RS
964
965 while (count > 0) {
966 size_t readcount = count;
967 struct nfs_mcb_data *mdata;
968
969 if (readcount > nfs_get_readmax(nfs)) {
970 readcount = nfs_get_readmax(nfs);
971 }
972
973 mdata = malloc(sizeof(struct nfs_mcb_data));
974 if (mdata == NULL) {
975 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
921f877b
RS
976 return -1;
977 }
ea98629a 978 memset(mdata, 0, sizeof(struct nfs_mcb_data));
921f877b
RS
979 mdata->data = data;
980 mdata->offset = offset;
981 mdata->count = readcount;
921f877b
RS
982 if (rpc_nfs_read_async(nfs->rpc, nfs_pread_mcb, &nfsfh->fh, offset, readcount, mdata) != 0) {
983 rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
984 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
985 free(mdata);
986 return -1;
987 }
988
989 count -= readcount;
990 offset += readcount;
991 data->num_calls++;
992 }
993
994 return 0;
84004dbf
RS
995}
996
997/*
998 * Async read()
999 */
1000int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, nfs_cb cb, void *private_data)
1001{
1002 return nfs_pread_async(nfs, nfsfh, nfsfh->offset, count, cb, private_data);
1003}
1004
1005
1006
1007/*
1008 * Async pwrite()
1009 */
1010static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1011{
1012 struct nfs_cb_data *data = private_data;
1013 struct nfs_context *nfs = data->nfs;
1014 WRITE3res *res;
1015
1016 if (status == RPC_STATUS_ERROR) {
1017 data->cb(-EFAULT, nfs, command_data, data->private_data);
1018 free_nfs_cb_data(data);
1019 return;
1020 }
1021 if (status == RPC_STATUS_CANCEL) {
1022 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1023 free_nfs_cb_data(data);
1024 return;
1025 }
1026
1027 res = command_data;
1028 if (res->status != NFS3_OK) {
1029 rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1030 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1031 free_nfs_cb_data(data);
1032 return;
1033 }
1034
1035 data->nfsfh->offset += res->WRITE3res_u.resok.count;
1036 data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data);
1037 free_nfs_cb_data(data);
1038}
1039
1040int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t count, char *buf, nfs_cb cb, void *private_data)
1041{
1042 struct nfs_cb_data *data;
1043
1044 data = malloc(sizeof(struct nfs_cb_data));
1045 if (data == NULL) {
1046 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1047 return -1;
1048 }
ea98629a 1049 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1050 data->nfs = nfs;
1051 data->cb = cb;
1052 data->private_data = private_data;
1053 data->nfsfh = nfsfh;
1054
1055 nfsfh->offset = offset;
1056 if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, data) != 0) {
1057 rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
1058 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1059 free_nfs_cb_data(data);
1060 return -1;
1061 }
1062 return 0;
1063}
1064
1065/*
1066 * Async write()
1067 */
1068int nfs_write_async(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, char *buf, nfs_cb cb, void *private_data)
1069{
1070 return nfs_pwrite_async(nfs, nfsfh, nfsfh->offset, count, buf, cb, private_data);
1071}
1072
1073
1074
1075
1076/*
1077 * close
1078 */
1079
1080int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1081{
1082 if (nfsfh->fh.data.data_val != NULL){
1083 free(nfsfh->fh.data.data_val);
1084 nfsfh->fh.data.data_val = NULL;
1085 }
1086 free(nfsfh);
1087
1088 cb(0, nfs, NULL, private_data);
1089 return 0;
1090};
1091
1092
1093
1094
1095
1096/*
1097 * Async fstat()
1098 */
1099int nfs_fstat_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1100{
1101 struct nfs_cb_data *data;
1102
1103 data = malloc(sizeof(struct nfs_cb_data));
1104 if (data == NULL) {
1105 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1106 return -1;
1107 }
ea98629a 1108 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1109 data->nfs = nfs;
1110 data->cb = cb;
1111 data->private_data = private_data;
1112
1113 if (rpc_nfs_getattr_async(nfs->rpc, nfs_stat_1_cb, &nfsfh->fh, data) != 0) {
1114 rpc_set_error(nfs->rpc, "RPC error: Failed to send STAT GETATTR call for %s", data->path);
1115 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1116 free_nfs_cb_data(data);
1117 return -1;
1118 }
1119 return 0;
1120}
1121
1122
1123
1124/*
1125 * Async fsync()
1126 */
1127static void nfs_fsync_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1128{
1129 struct nfs_cb_data *data = private_data;
1130 struct nfs_context *nfs = data->nfs;
1131 COMMIT3res *res;
1132
1133 if (status == RPC_STATUS_ERROR) {
1134 data->cb(-EFAULT, nfs, command_data, data->private_data);
1135 free_nfs_cb_data(data);
1136 return;
1137 }
1138 if (status == RPC_STATUS_CANCEL) {
1139 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1140 free_nfs_cb_data(data);
1141 return;
1142 }
1143
1144 res = command_data;
1145 if (res->status != NFS3_OK) {
1146 rpc_set_error(nfs->rpc, "NFS: Commit failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1147 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1148 free_nfs_cb_data(data);
1149 return;
1150 }
1151
1152 data->cb(0, nfs, NULL, data->private_data);
1153 free_nfs_cb_data(data);
1154}
1155
1156int nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb, void *private_data)
1157{
1158 struct nfs_cb_data *data;
1159
1160 data = malloc(sizeof(struct nfs_cb_data));
1161 if (data == NULL) {
1162 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1163 return -1;
1164 }
ea98629a 1165 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1166 data->nfs = nfs;
1167 data->cb = cb;
1168 data->private_data = private_data;
1169
1170 if (rpc_nfs_commit_async(nfs->rpc, nfs_fsync_cb, &nfsfh->fh, data) != 0) {
1171 rpc_set_error(nfs->rpc, "RPC error: Failed to send COMMIT call for %s", data->path);
1172 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1173 free_nfs_cb_data(data);
1174 return -1;
1175 }
1176 return 0;
1177}
1178
1179
1180
1181
1182/*
1183 * Async ftruncate()
1184 */
1185static void nfs_ftruncate_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1186{
1187 struct nfs_cb_data *data = private_data;
1188 struct nfs_context *nfs = data->nfs;
1189 SETATTR3res *res;
1190
1191 if (status == RPC_STATUS_ERROR) {
1192 data->cb(-EFAULT, nfs, command_data, data->private_data);
1193 free_nfs_cb_data(data);
1194 return;
1195 }
1196 if (status == RPC_STATUS_CANCEL) {
1197 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1198 free_nfs_cb_data(data);
1199 return;
1200 }
1201
1202 res = command_data;
1203 if (res->status != NFS3_OK) {
1204 rpc_set_error(nfs->rpc, "NFS: Setattr failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1205 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1206 free_nfs_cb_data(data);
1207 return;
1208 }
1209
1210 data->cb(0, nfs, NULL, data->private_data);
1211 free_nfs_cb_data(data);
1212}
1213
1214int nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t length, nfs_cb cb, void *private_data)
1215{
1216 struct nfs_cb_data *data;
1217 SETATTR3args args;
1218
1219 data = malloc(sizeof(struct nfs_cb_data));
1220 if (data == NULL) {
1221 rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_cb_data structure");
84004dbf
RS
1222 return -1;
1223 }
ea98629a 1224 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
1225 data->nfs = nfs;
1226 data->cb = cb;
1227 data->private_data = private_data;
1228
ea98629a 1229 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
1230 args.object.data.data_len = nfsfh->fh.data.data_len;
1231 args.object.data.data_val = nfsfh->fh.data.data_val;
1232 args.new_attributes.size.set_it = 1;
1233 args.new_attributes.size.set_size3_u.size = length;
1234
1235 if (rpc_nfs_setattr_async(nfs->rpc, nfs_ftruncate_cb, &args, data) != 0) {
1236 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1237 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1238 free_nfs_cb_data(data);
1239 return -1;
1240 }
1241 return 0;
1242}
1243
1244
1245/*
1246 * Async truncate()
1247 */
1248static int nfs_truncate_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1249{
1250 off_t offset = data->continue_int;
1251 struct nfsfh nfsfh;
1252
1253 nfsfh.fh.data.data_val = data->fh.data.data_val;
1254 nfsfh.fh.data.data_len = data->fh.data.data_len;
1255
1256 if (nfs_ftruncate_async(nfs, &nfsfh, offset, data->cb, data->private_data) != 0) {
1257 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
1258 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1259 free_nfs_cb_data(data);
1260 return -1;
1261 }
1262 free_nfs_cb_data(data);
1263 return 0;
1264}
1265
1266int nfs_truncate_async(struct nfs_context *nfs, const char *path, off_t length, nfs_cb cb, void *private_data)
1267{
1268 off_t offset;
1269
1270 offset = length;
1271
1272 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_truncate_continue_internal, NULL, NULL, offset) != 0) {
cbbf9d3e
RS
1273 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1274 return -1;
84004dbf
RS
1275 }
1276
1277 return 0;
1278}
1279
1280
1281
1282
1283/*
1284 * Async mkdir()
1285 */
1286static void nfs_mkdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1287{
1288 MKDIR3res *res;
1289 struct nfs_cb_data *data = private_data;
1290 struct nfs_context *nfs = data->nfs;
1291 char *str = data->continue_data;
1292
1293 str = &str[strlen(str) + 1];
1294
1295 if (status == RPC_STATUS_ERROR) {
1296 data->cb(-EFAULT, nfs, command_data, data->private_data);
1297 free_nfs_cb_data(data);
1298 return;
1299 }
1300 if (status == RPC_STATUS_CANCEL) {
1301 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1302 free_nfs_cb_data(data);
1303 return;
1304 }
1305
1306 res = command_data;
1307 if (res->status != NFS3_OK) {
1308 rpc_set_error(nfs->rpc, "NFS: MKDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1309 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1310 free_nfs_cb_data(data);
1311 return;
1312 }
1313
1314 data->cb(0, nfs, NULL, data->private_data);
1315 free_nfs_cb_data(data);
1316}
1317
1318static int nfs_mkdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1319{
1320 char *str = data->continue_data;
1321
1322 str = &str[strlen(str) + 1];
1323
1324 if (rpc_nfs_mkdir_async(nfs->rpc, nfs_mkdir_cb, &data->fh, str, data) != 0) {
1325 rpc_set_error(nfs->rpc, "RPC error: Failed to send MKDIR call for %s", data->path);
1326 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1327 free_nfs_cb_data(data);
1328 return -1;
1329 }
1330 return 0;
1331}
1332
1333int nfs_mkdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1334{
1335 char *new_path;
1336 char *ptr;
1337
1338 new_path = strdup(path);
1339 if (new_path == NULL) {
cbbf9d3e 1340 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1341 return -1;
1342 }
1343
93e9306f 1344 ptr = strrchr(new_path, '/');
84004dbf 1345 if (ptr == NULL) {
cbbf9d3e
RS
1346 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1347 return -1;
84004dbf
RS
1348 }
1349 *ptr = 0;
1350
1351 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1352 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_mkdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1353 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path component");
1354 return -1;
84004dbf
RS
1355 }
1356
1357 return 0;
1358}
1359
1360
1361
1362
1363
1364/*
1365 * Async rmdir()
1366 */
1367static void nfs_rmdir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1368{
1369 RMDIR3res *res;
1370 struct nfs_cb_data *data = private_data;
1371 struct nfs_context *nfs = data->nfs;
1372 char *str = data->continue_data;
1373
1374 str = &str[strlen(str) + 1];
1375
1376 if (status == RPC_STATUS_ERROR) {
1377 data->cb(-EFAULT, nfs, command_data, data->private_data);
1378 free_nfs_cb_data(data);
1379 return;
1380 }
1381 if (status == RPC_STATUS_CANCEL) {
1382 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1383 free_nfs_cb_data(data);
1384 return;
1385 }
1386
1387 res = command_data;
1388 if (res->status != NFS3_OK) {
1389 rpc_set_error(nfs->rpc, "NFS: RMDIR of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1390 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1391 free_nfs_cb_data(data);
1392 return;
1393 }
1394
1395 data->cb(0, nfs, NULL, data->private_data);
1396 free_nfs_cb_data(data);
1397}
1398
1399static int nfs_rmdir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1400{
1401 char *str = data->continue_data;
1402
1403 str = &str[strlen(str) + 1];
1404
1405 if (rpc_nfs_rmdir_async(nfs->rpc, nfs_rmdir_cb, &data->fh, str, data) != 0) {
1406 rpc_set_error(nfs->rpc, "RPC error: Failed to send RMDIR call for %s", data->path);
1407 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1408 free_nfs_cb_data(data);
1409 return -1;
1410 }
1411 return 0;
1412}
1413
1414int nfs_rmdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1415{
1416 char *new_path;
1417 char *ptr;
1418
1419 new_path = strdup(path);
1420 if (new_path == NULL) {
cbbf9d3e 1421 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1422 return -1;
1423 }
1424
93e9306f 1425 ptr = strrchr(new_path, '/');
84004dbf 1426 if (ptr == NULL) {
cbbf9d3e
RS
1427 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1428 return -1;
84004dbf
RS
1429 }
1430 *ptr = 0;
1431
1432 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1433 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_rmdir_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1434 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1435 return -1;
84004dbf
RS
1436 }
1437
1438 return 0;
1439}
1440
1441
1442
1443
1444/*
1445 * Async creat()
1446 */
1447static void nfs_create_2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1448{
1449 LOOKUP3res *res;
1450 struct nfs_cb_data *data = private_data;
1451 struct nfs_context *nfs = data->nfs;
1452 struct nfsfh *nfsfh;
1453 char *str = data->continue_data;
1454
1455 if (status == RPC_STATUS_ERROR) {
1456 data->cb(-EFAULT, nfs, command_data, data->private_data);
1457 free_nfs_cb_data(data);
1458 return;
1459 }
1460 if (status == RPC_STATUS_CANCEL) {
1461 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1462 free_nfs_cb_data(data);
1463 return;
1464 }
1465
1466 str = &str[strlen(str) + 1];
1467 res = command_data;
1468 if (res->status != NFS3_OK) {
1469 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));
1470 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1471
1472 return;
1473 }
1474
1475 nfsfh = malloc(sizeof(struct nfsfh));
1476 if (nfsfh == NULL) {
1477 rpc_set_error(nfs->rpc, "NFS: Failed to allocate nfsfh structure");
1478 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1479 free_nfs_cb_data(data);
1480 return;
1481 }
ea98629a 1482 memset(nfsfh, 0, sizeof(struct nfsfh));
84004dbf
RS
1483
1484 /* steal the filehandle */
1485 nfsfh->fh.data.data_len = data->fh.data.data_len;
1486 nfsfh->fh.data.data_val = data->fh.data.data_val;
1487 data->fh.data.data_val = NULL;
1488
1489 data->cb(0, nfs, nfsfh, data->private_data);
1490 free_nfs_cb_data(data);
1491}
1492
1493
1494
1495static void nfs_creat_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1496{
1497 CREATE3res *res;
1498 struct nfs_cb_data *data = private_data;
1499 struct nfs_context *nfs = data->nfs;
1500 char *str = data->continue_data;
1501
1502 if (status == RPC_STATUS_ERROR) {
1503 data->cb(-EFAULT, nfs, command_data, data->private_data);
1504 free_nfs_cb_data(data);
1505 return;
1506 }
1507 if (status == RPC_STATUS_CANCEL) {
1508 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1509 free_nfs_cb_data(data);
1510 return;
1511 }
1512
1513 str = &str[strlen(str) + 1];
1514 res = command_data;
1515 if (res->status != NFS3_OK) {
1516 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));
1517 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1518
1519 return;
1520 }
1521
1522 if (rpc_nfs_lookup_async(nfs->rpc, nfs_create_2_cb, &data->fh, str, data) != 0) {
1523 rpc_set_error(nfs->rpc, "RPC error: Failed to send lookup call for %s/%s", data->saved_path, str);
1524 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1525 free_nfs_cb_data(data);
1526 return;
1527 }
1528 return;
1529}
1530
1531static int nfs_creat_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1532{
1533 char *str = data->continue_data;
1534
1535 str = &str[strlen(str) + 1];
1536
1537 if (rpc_nfs_create_async(nfs->rpc, nfs_creat_1_cb, &data->fh, str, data->continue_int, data) != 0) {
1538 rpc_set_error(nfs->rpc, "RPC error: Failed to send CREATE call for %s/%s", data->path, str);
1539 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1540 free_nfs_cb_data(data);
1541 return -1;
1542 }
1543 return 0;
1544}
1545
1546int nfs_creat_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
1547{
1548 char *new_path;
1549 char *ptr;
1550
1551 new_path = strdup(path);
1552 if (new_path == NULL) {
cbbf9d3e 1553 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1554 return -1;
1555 }
1556
93e9306f 1557 ptr = strrchr(new_path, '/');
84004dbf 1558 if (ptr == NULL) {
cbbf9d3e
RS
1559 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1560 return -1;
84004dbf
RS
1561 }
1562 *ptr = 0;
1563
1564 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1565 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_creat_continue_internal, new_path, free, mode) != 0) {
cbbf9d3e
RS
1566 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1567 return -1;
84004dbf
RS
1568 }
1569
1570 return 0;
1571}
1572
1573
1574
1575
1576/*
1577 * Async unlink()
1578 */
1579static void nfs_unlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1580{
1581 REMOVE3res *res;
1582 struct nfs_cb_data *data = private_data;
1583 struct nfs_context *nfs = data->nfs;
1584 char *str = data->continue_data;
1585
1586 str = &str[strlen(str) + 1];
1587
1588 if (status == RPC_STATUS_ERROR) {
1589 data->cb(-EFAULT, nfs, command_data, data->private_data);
1590 free_nfs_cb_data(data);
1591 return;
1592 }
1593 if (status == RPC_STATUS_CANCEL) {
1594 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1595 free_nfs_cb_data(data);
1596 return;
1597 }
1598
1599 res = command_data;
1600 if (res->status != NFS3_OK) {
1601 rpc_set_error(nfs->rpc, "NFS: REMOVE of %s/%s failed with %s(%d)", data->saved_path, str, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1602 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1603 free_nfs_cb_data(data);
1604 return;
1605 }
1606
1607 data->cb(0, nfs, NULL, data->private_data);
1608 free_nfs_cb_data(data);
1609}
1610
1611static int nfs_unlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1612{
1613 char *str = data->continue_data;
1614
1615 str = &str[strlen(str) + 1];
1616
1617 if (rpc_nfs_remove_async(nfs->rpc, nfs_unlink_cb, &data->fh, str, data) != 0) {
1618 rpc_set_error(nfs->rpc, "RPC error: Failed to send REMOVE call for %s", data->path);
1619 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1620 free_nfs_cb_data(data);
1621 return -1;
1622 }
1623 return 0;
1624}
1625
1626int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1627{
1628 char *new_path;
1629 char *ptr;
1630
1631 new_path = strdup(path);
1632 if (new_path == NULL) {
cbbf9d3e 1633 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
84004dbf
RS
1634 return -1;
1635 }
1636
93e9306f 1637 ptr = strrchr(new_path, '/');
84004dbf 1638 if (ptr == NULL) {
cbbf9d3e
RS
1639 rpc_set_error(nfs->rpc, "Invalid path %s", path);
1640 return -1;
84004dbf
RS
1641 }
1642 *ptr = 0;
1643
1644 /* new_path now points to the parent directory, and beyond the nul terminateor is the new directory to create */
1645 if (nfs_lookuppath_async(nfs, new_path, cb, private_data, nfs_unlink_continue_internal, new_path, free, 0) != 0) {
cbbf9d3e
RS
1646 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1647 return -1;
84004dbf
RS
1648 }
1649
1650 return 0;
1651}
1652
1653
1654
1655
1656
1657/*
1658 * Async opendir()
1659 */
1660static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1661{
f390f181 1662 READDIRPLUS3res *res;
84004dbf
RS
1663 struct nfs_cb_data *data = private_data;
1664 struct nfs_context *nfs = data->nfs;
fb6510bb 1665 struct nfsdir *nfsdir = data->continue_data;
f390f181 1666 struct entryplus3 *entry;
84004dbf 1667 uint64_t cookie;
f390f181 1668
84004dbf
RS
1669 if (status == RPC_STATUS_ERROR) {
1670 data->cb(-EFAULT, nfs, command_data, data->private_data);
1671 nfs_free_nfsdir(nfsdir);
1672 data->continue_data = NULL;
1673 free_nfs_cb_data(data);
1674 return;
1675 }
1676 if (status == RPC_STATUS_CANCEL) {
1677 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1678 nfs_free_nfsdir(nfsdir);
1679 data->continue_data = NULL;
1680 free_nfs_cb_data(data);
1681 return;
1682 }
1683
1684 res = command_data;
1685 if (res->status != NFS3_OK) {
1686 rpc_set_error(nfs->rpc, "NFS: READDIR of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1687 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1688 nfs_free_nfsdir(nfsdir);
1689 data->continue_data = NULL;
1690 free_nfs_cb_data(data);
1691 return;
1692 }
1693
f390f181 1694 entry =res->READDIRPLUS3res_u.resok.reply.entries;
84004dbf
RS
1695 while (entry != NULL) {
1696 struct nfsdirent *nfsdirent;
1697
1698 nfsdirent = malloc(sizeof(struct nfsdirent));
1699 if (nfsdirent == NULL) {
1700 data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
1701 nfs_free_nfsdir(nfsdir);
1702 data->continue_data = NULL;
1703 free_nfs_cb_data(data);
1704 return;
1705 }
ea98629a 1706 memset(nfsdirent, 0, sizeof(struct nfsdirent));
84004dbf
RS
1707 nfsdirent->name = strdup(entry->name);
1708 if (nfsdirent->name == NULL) {
1709 data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
1710 nfs_free_nfsdir(nfsdir);
1711 data->continue_data = NULL;
1712 free_nfs_cb_data(data);
1713 return;
1714 }
1715 nfsdirent->inode = entry->fileid;
0804e67d
RS
1716 if (entry->name_attributes.attributes_follow) {
1717 nfsdirent->type = entry->name_attributes.post_op_attr_u.attributes.type;
1718 nfsdirent->mode = entry->name_attributes.post_op_attr_u.attributes.mode;
1719 nfsdirent->size = entry->name_attributes.post_op_attr_u.attributes.size;
1720
1721 nfsdirent->atime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.atime.seconds;
1722 nfsdirent->atime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.atime.nseconds/1000;
1723 nfsdirent->mtime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.mtime.seconds;
1724 nfsdirent->mtime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.mtime.nseconds/1000;
1725 nfsdirent->ctime.tv_sec = entry->name_attributes.post_op_attr_u.attributes.ctime.seconds;
1726 nfsdirent->ctime.tv_usec = entry->name_attributes.post_op_attr_u.attributes.ctime.nseconds/1000;
1727 }
1728
84004dbf
RS
1729 nfsdirent->next = nfsdir->entries;
1730 nfsdir->entries = nfsdirent;
1731
1732 cookie = entry->cookie;
1733 entry = entry->nextentry;
1734 }
1735
f390f181
RS
1736 if (res->READDIRPLUS3res_u.resok.reply.eof == 0) {
1737 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, cookie, res->READDIRPLUS3res_u.resok.cookieverf, 8192, data) != 0) {
1738 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
1739 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1740 nfs_free_nfsdir(nfsdir);
1741 data->continue_data = NULL;
1742 free_nfs_cb_data(data);
1743 return;
1744 }
1745 return;
1746 }
1747
1748 /* steal the dirhandle */
1749 data->continue_data = NULL;
1750 nfsdir->current = nfsdir->entries;
1751
1752 data->cb(0, nfs, nfsdir, data->private_data);
1753 free_nfs_cb_data(data);
1754}
1755
1756static int nfs_opendir_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1757{
1758 cookieverf3 cv;
1759
ea98629a 1760 memset(cv, 0, sizeof(cookieverf3));
f390f181
RS
1761 if (rpc_nfs_readdirplus_async(nfs->rpc, nfs_opendir_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
1762 rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIRPLUS call for %s", data->path);
84004dbf
RS
1763 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1764 free_nfs_cb_data(data);
1765 return -1;
1766 }
1767 return 0;
1768}
1769
1770int nfs_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1771{
1772 struct nfsdir *nfsdir;
1773
1774 nfsdir = malloc(sizeof(struct nfsdir));
1775 if (nfsdir == NULL) {
cbbf9d3e 1776 rpc_set_error(nfs->rpc, "failed to allocate buffer for nfsdir");
84004dbf
RS
1777 return -1;
1778 }
ea98629a 1779 memset(nfsdir, 0, sizeof(struct nfsdir));
84004dbf
RS
1780
1781 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_opendir_continue_internal, nfsdir, free, 0) != 0) {
cbbf9d3e
RS
1782 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
1783 return -1;
84004dbf
RS
1784 }
1785
1786 return 0;
1787}
1788
1789
1790struct nfsdirent *nfs_readdir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
1791{
1792 struct nfsdirent *nfsdirent = nfsdir->current;
1793
1794 if (nfsdir->current != NULL) {
1795 nfsdir->current = nfsdir->current->next;
1796 }
1797 return nfsdirent;
1798}
1799
1800
1801void nfs_closedir(struct nfs_context *nfs _U_, struct nfsdir *nfsdir)
1802{
1803 nfs_free_nfsdir(nfsdir);
1804}
1805
1806
1807
1808
1809
1810
1811
1812/*
1813 * Async lseek()
1814 */
1815struct lseek_cb_data {
1816 struct nfs_context *nfs;
1817 struct nfsfh *nfsfh;
1818 off_t offset;
1819 nfs_cb cb;
1820 void *private_data;
1821};
1822
1823static void nfs_lseek_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1824{
1825 GETATTR3res *res;
1826 struct lseek_cb_data *data = private_data;
1827 struct nfs_context *nfs = data->nfs;
1828
1829 if (status == RPC_STATUS_ERROR) {
1830 data->cb(-EFAULT, nfs, command_data, data->private_data);
1831 free(data);
1832 return;
1833 }
1834 if (status == RPC_STATUS_CANCEL) {
1835 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1836 free(data);
1837 return;
1838 }
1839
1840 res = command_data;
1841 if (res->status != NFS3_OK) {
1842 rpc_set_error(nfs->rpc, "NFS: GETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1843 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1844 free(data);
1845 return;
1846 }
1847
1848 data->nfsfh->offset = data->offset + res->GETATTR3res_u.resok.obj_attributes.size;
1849 data->cb(0, nfs, &data->nfsfh->offset, data->private_data);
1850 free(data);
1851}
1852
1853int nfs_lseek_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, int whence, nfs_cb cb, void *private_data)
1854{
1855 struct lseek_cb_data *data;
1856
1857 if (whence == SEEK_SET) {
1858 nfsfh->offset = offset;
1859 cb(0, nfs, &nfsfh->offset, private_data);
1860 return 0;
1861 }
1862 if (whence == SEEK_CUR) {
1863 nfsfh->offset += offset;
1864 cb(0, nfs, &nfsfh->offset, private_data);
1865 return 0;
1866 }
1867
1868 data = malloc(sizeof(struct lseek_cb_data));
1869 if (data == NULL) {
1870 rpc_set_error(nfs->rpc, "Out Of Memory: Failed to malloc lseek cb data");
1871 return -1;
1872 }
1873
1874 data->nfs = nfs;
1875 data->nfsfh = nfsfh;
1876 data->offset = offset;
1877 data->cb = cb;
1878 data->private_data = private_data;
1879
1880 if (rpc_nfs_getattr_async(nfs->rpc, nfs_lseek_1_cb, &nfsfh->fh, data) != 0) {
1881 rpc_set_error(nfs->rpc, "RPC error: Failed to send LSEEK GETATTR call");
1882 free(data);
cbbf9d3e 1883 return -1;
84004dbf
RS
1884 }
1885 return 0;
1886}
1887
1888
1889
1890
1891/*
1892 * Async statvfs()
1893 */
1894static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1895{
1896 FSSTAT3res *res;
1897 struct nfs_cb_data *data = private_data;
1898 struct nfs_context *nfs = data->nfs;
a8a1b858 1899#ifndef WIN32
84004dbf 1900 struct statvfs svfs;
a8a1b858 1901#endif/*WIN32*/
84004dbf
RS
1902
1903 if (status == RPC_STATUS_ERROR) {
1904 data->cb(-EFAULT, nfs, command_data, data->private_data);
1905 free_nfs_cb_data(data);
1906 return;
1907 }
1908 if (status == RPC_STATUS_CANCEL) {
1909 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1910 free_nfs_cb_data(data);
1911 return;
1912 }
1913
1914 res = command_data;
1915 if (res->status != NFS3_OK) {
1916 rpc_set_error(nfs->rpc, "NFS: FSSTAT of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1917 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1918 free_nfs_cb_data(data);
1919 return;
1920 }
1921
a8a1b858 1922#ifndef WIN32
84004dbf
RS
1923 svfs.f_bsize = 4096;
1924 svfs.f_frsize = 4096;
1925 svfs.f_blocks = res->FSSTAT3res_u.resok.tbytes/4096;
1926 svfs.f_bfree = res->FSSTAT3res_u.resok.fbytes/4096;
1927 svfs.f_bavail = res->FSSTAT3res_u.resok.abytes/4096;
1928 svfs.f_files = res->FSSTAT3res_u.resok.tfiles;
1929 svfs.f_ffree = res->FSSTAT3res_u.resok.ffiles;
1930 svfs.f_favail = res->FSSTAT3res_u.resok.afiles;
1931 svfs.f_fsid = 0;
1932 svfs.f_flag = 0;
1933 svfs.f_namemax = 256;
1934
1935 data->cb(0, nfs, &svfs, data->private_data);
a8a1b858
M
1936#else
1937 data->cb(0, nfs,NULL, data->private_data);
1938#endif/*WIN32*/
84004dbf
RS
1939 free_nfs_cb_data(data);
1940}
1941
1942static int nfs_statvfs_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
1943{
1944 if (rpc_nfs_fsstat_async(nfs->rpc, nfs_statvfs_1_cb, &data->fh, data) != 0) {
1945 rpc_set_error(nfs->rpc, "RPC error: Failed to send FSSTAT call for %s", data->path);
1946 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
1947 free_nfs_cb_data(data);
1948 return -1;
1949 }
1950 return 0;
1951}
1952
1953int nfs_statvfs_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
1954{
1955 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_statvfs_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 1956 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
1957 return -1;
1958 }
1959
1960 return 0;
1961}
1962
1963
1964
1965
1966/*
1967 * Async readlink()
1968 */
1969static void nfs_readlink_1_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
1970{
1971 READLINK3res *res;
1972 struct nfs_cb_data *data = private_data;
1973 struct nfs_context *nfs = data->nfs;
1974
1975 if (status == RPC_STATUS_ERROR) {
1976 data->cb(-EFAULT, nfs, command_data, data->private_data);
1977 free_nfs_cb_data(data);
1978 return;
1979 }
1980 if (status == RPC_STATUS_CANCEL) {
1981 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
1982 free_nfs_cb_data(data);
1983 return;
1984 }
1985
1986 res = command_data;
1987 if (res->status != NFS3_OK) {
1988 rpc_set_error(nfs->rpc, "NFS: READLINK of %s failed with %s(%d)", data->saved_path, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
1989 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
1990 free_nfs_cb_data(data);
1991 return;
1992 }
1993
1994
1995 data->cb(0, nfs, res->READLINK3res_u.resok.data, data->private_data);
1996 free_nfs_cb_data(data);
1997}
1998
1999static int nfs_readlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2000{
2001 if (rpc_nfs_readlink_async(nfs->rpc, nfs_readlink_1_cb, &data->fh, data) != 0) {
2002 rpc_set_error(nfs->rpc, "RPC error: Failed to send READLINK call for %s", data->path);
2003 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2004 free_nfs_cb_data(data);
2005 return -1;
2006 }
2007 return 0;
2008}
2009
2010int nfs_readlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *private_data)
2011{
2012 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_readlink_continue_internal, NULL, NULL, 0) != 0) {
cbbf9d3e 2013 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2014 return -1;
2015 }
2016
2017 return 0;
2018}
2019
2020
2021
2022
2023/*
2024 * Async chmod()
2025 */
2026static void nfs_chmod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2027{
2028 struct nfs_cb_data *data = private_data;
2029 struct nfs_context *nfs = data->nfs;
2030 SETATTR3res *res;
2031
2032 if (status == RPC_STATUS_ERROR) {
2033 data->cb(-EFAULT, nfs, command_data, data->private_data);
2034 free_nfs_cb_data(data);
2035 return;
2036 }
2037 if (status == RPC_STATUS_CANCEL) {
2038 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2039 free_nfs_cb_data(data);
2040 return;
2041 }
2042
2043 res = command_data;
2044 if (res->status != NFS3_OK) {
2045 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2046 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2047 free_nfs_cb_data(data);
2048 return;
2049 }
2050
2051 data->cb(0, nfs, NULL, data->private_data);
2052 free_nfs_cb_data(data);
2053}
2054
2055static int nfs_chmod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2056{
2057 SETATTR3args args;
2058
ea98629a 2059 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2060 args.object.data.data_len = data->fh.data.data_len;
2061 args.object.data.data_val = data->fh.data.data_val;
2062 args.new_attributes.mode.set_it = 1;
2063 args.new_attributes.mode.set_mode3_u.mode = data->continue_int;
2064
2065 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chmod_cb, &args, data) != 0) {
2066 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2067 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2068 free_nfs_cb_data(data);
2069 return -1;
2070 }
2071 return 0;
2072}
2073
2074
2075int nfs_chmod_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2076{
2077 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chmod_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e 2078 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2079 return -1;
2080 }
2081
2082 return 0;
2083}
2084
2085/*
2086 * Async fchmod()
2087 */
2088int nfs_fchmod_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode, nfs_cb cb, void *private_data)
2089{
2090 struct nfs_cb_data *data;
2091
2092 data = malloc(sizeof(struct nfs_cb_data));
2093 if (data == NULL) {
cbbf9d3e 2094 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for nfs mount data");
84004dbf
RS
2095 return -1;
2096 }
ea98629a 2097 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2098 data->nfs = nfs;
2099 data->cb = cb;
2100 data->private_data = private_data;
2101 data->continue_int = mode;
2102 data->fh.data.data_len = nfsfh->fh.data.data_len;
2103 data->fh.data.data_val = malloc(data->fh.data.data_len);
2104 if (data->fh.data.data_val == NULL) {
2105 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2106 free_nfs_cb_data(data);
2107 return -1;
2108 }
2109 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2110
2111 if (nfs_chmod_continue_internal(nfs, data) != 0) {
2112 free_nfs_cb_data(data);
2113 return -1;
2114 }
2115
2116 return 0;
2117}
2118
2119
2120
2121/*
2122 * Async chown()
2123 */
2124static void nfs_chown_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2125{
2126 struct nfs_cb_data *data = private_data;
2127 struct nfs_context *nfs = data->nfs;
2128 SETATTR3res *res;
2129
2130 if (status == RPC_STATUS_ERROR) {
2131 data->cb(-EFAULT, nfs, command_data, data->private_data);
2132 free_nfs_cb_data(data);
2133 return;
2134 }
2135 if (status == RPC_STATUS_CANCEL) {
2136 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2137 free_nfs_cb_data(data);
2138 return;
2139 }
2140
2141 res = command_data;
2142 if (res->status != NFS3_OK) {
2143 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2144 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2145 free_nfs_cb_data(data);
2146 return;
2147 }
2148
2149 data->cb(0, nfs, NULL, data->private_data);
2150 free_nfs_cb_data(data);
2151}
2152
2153struct nfs_chown_data {
2154 uid_t uid;
2155 gid_t gid;
2156};
2157
2158static int nfs_chown_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2159{
2160 SETATTR3args args;
2161 struct nfs_chown_data *chown_data = data->continue_data;
2162
ea98629a 2163 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2164 args.object.data.data_len = data->fh.data.data_len;
2165 args.object.data.data_val = data->fh.data.data_val;
2166 if (chown_data->uid != (uid_t)-1) {
2167 args.new_attributes.uid.set_it = 1;
2168 args.new_attributes.uid.set_uid3_u.uid = chown_data->uid;
2169 }
2170 if (chown_data->gid != (gid_t)-1) {
2171 args.new_attributes.gid.set_it = 1;
2172 args.new_attributes.gid.set_gid3_u.gid = chown_data->gid;
2173 }
2174
2175 if (rpc_nfs_setattr_async(nfs->rpc, nfs_chown_cb, &args, data) != 0) {
2176 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2177 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2178 free_nfs_cb_data(data);
2179 return -1;
2180 }
2181 return 0;
2182}
2183
2184
2185int nfs_chown_async(struct nfs_context *nfs, const char *path, int uid, int gid, nfs_cb cb, void *private_data)
2186{
2187 struct nfs_chown_data *chown_data;
2188
2189 chown_data = malloc(sizeof(struct nfs_chown_data));
2190 if (chown_data == NULL) {
cbbf9d3e 2191 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2192 return -1;
2193 }
2194
2195 chown_data->uid = uid;
2196 chown_data->gid = gid;
2197
2198 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_chown_continue_internal, chown_data, free, 0) != 0) {
cbbf9d3e 2199 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2200 return -1;
2201 }
2202
2203 return 0;
2204}
2205
2206
2207/*
2208 * Async fchown()
2209 */
2210int nfs_fchown_async(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid, nfs_cb cb, void *private_data)
2211{
2212 struct nfs_cb_data *data;
2213 struct nfs_chown_data *chown_data;
2214
2215 chown_data = malloc(sizeof(struct nfs_chown_data));
2216 if (chown_data == NULL) {
cbbf9d3e 2217 rpc_set_error(nfs->rpc, "Failed to allocate memory for chown data structure");
84004dbf
RS
2218 return -1;
2219 }
2220
2221 chown_data->uid = uid;
2222 chown_data->gid = gid;
2223
2224
2225 data = malloc(sizeof(struct nfs_cb_data));
2226 if (data == NULL) {
cbbf9d3e 2227 rpc_set_error(nfs->rpc, "out of memory. failed to allocate memory for fchown data");
84004dbf
RS
2228 return -1;
2229 }
ea98629a 2230 memset(data, 0, sizeof(struct nfs_cb_data));
84004dbf
RS
2231 data->nfs = nfs;
2232 data->cb = cb;
2233 data->private_data = private_data;
2234 data->continue_data = chown_data;
2235 data->fh.data.data_len = nfsfh->fh.data.data_len;
2236 data->fh.data.data_val = malloc(data->fh.data.data_len);
2237 if (data->fh.data.data_val == NULL) {
2238 rpc_set_error(nfs->rpc, "Out of memory: Failed to allocate fh");
2239 free_nfs_cb_data(data);
2240 return -1;
2241 }
2242 memcpy(data->fh.data.data_val, nfsfh->fh.data.data_val, data->fh.data.data_len);
2243
2244
2245 if (nfs_chown_continue_internal(nfs, data) != 0) {
2246 free_nfs_cb_data(data);
2247 return -1;
2248 }
2249
2250 return 0;
2251}
2252
2253
2254
2255
2256
2257/*
2258 * Async utimes()
2259 */
2260static void nfs_utimes_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2261{
2262 struct nfs_cb_data *data = private_data;
2263 struct nfs_context *nfs = data->nfs;
2264 SETATTR3res *res;
2265
2266 if (status == RPC_STATUS_ERROR) {
2267 data->cb(-EFAULT, nfs, command_data, data->private_data);
2268 free_nfs_cb_data(data);
2269 return;
2270 }
2271 if (status == RPC_STATUS_CANCEL) {
2272 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2273 free_nfs_cb_data(data);
2274 return;
2275 }
2276
2277 res = command_data;
2278 if (res->status != NFS3_OK) {
2279 rpc_set_error(nfs->rpc, "NFS: SETATTR failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2280 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2281 free_nfs_cb_data(data);
2282 return;
2283 }
2284
2285 data->cb(0, nfs, NULL, data->private_data);
2286 free_nfs_cb_data(data);
2287}
2288
2289static int nfs_utimes_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2290{
2291 SETATTR3args args;
2292 struct timeval *utimes_data = data->continue_data;
2293
ea98629a 2294 memset(&args, 0, sizeof(SETATTR3args));
84004dbf
RS
2295 args.object.data.data_len = data->fh.data.data_len;
2296 args.object.data.data_val = data->fh.data.data_val;
2297 if (utimes_data != NULL) {
2298 args.new_attributes.atime.set_it = SET_TO_CLIENT_TIME;
2299 args.new_attributes.atime.set_atime_u.atime.seconds = utimes_data[0].tv_sec;
2300 args.new_attributes.atime.set_atime_u.atime.nseconds = utimes_data[0].tv_usec * 1000;
2301 args.new_attributes.mtime.set_it = SET_TO_CLIENT_TIME;
2302 args.new_attributes.mtime.set_mtime_u.mtime.seconds = utimes_data[1].tv_sec;
2303 args.new_attributes.mtime.set_mtime_u.mtime.nseconds = utimes_data[1].tv_usec * 1000;
2304 } else {
2305 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2306 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2307 }
2308
2309 if (rpc_nfs_setattr_async(nfs->rpc, nfs_utimes_cb, &args, data) != 0) {
2310 rpc_set_error(nfs->rpc, "RPC error: Failed to send SETATTR call for %s", data->path);
2311 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2312 free_nfs_cb_data(data);
2313 return -1;
2314 }
2315 return 0;
2316}
2317
2318
2319int nfs_utimes_async(struct nfs_context *nfs, const char *path, struct timeval *times, nfs_cb cb, void *private_data)
2320{
2321 struct timeval *new_times = NULL;
2322
2323 if (times != NULL) {
2324 new_times = malloc(sizeof(struct timeval)*2);
2325 if (new_times == NULL) {
cbbf9d3e 2326 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2327 return -1;
2328 }
2329
2330 memcpy(new_times, times, sizeof(struct timeval)*2);
2331 }
2332
2333 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2334 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2335 return -1;
2336 }
2337
2338 return 0;
2339}
2340
2341/*
2342 * Async utime()
2343 */
2344int nfs_utime_async(struct nfs_context *nfs, const char *path, struct utimbuf *times, nfs_cb cb, void *private_data)
2345{
2346 struct timeval *new_times = NULL;
2347
2348 if (times != NULL) {
2349 new_times = malloc(sizeof(struct timeval)*2);
2350 if (new_times == NULL) {
cbbf9d3e 2351 rpc_set_error(nfs->rpc, "Failed to allocate memory for timeval structure");
84004dbf
RS
2352 return -1;
2353 }
2354
2355 new_times[0].tv_sec = times->actime;
2356 new_times[0].tv_usec = 0;
2357 new_times[1].tv_sec = times->modtime;
2358 new_times[1].tv_usec = 0;
2359 }
2360
2361 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_utimes_continue_internal, new_times, free, 0) != 0) {
cbbf9d3e 2362 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
84004dbf
RS
2363 return -1;
2364 }
2365
2366 return 0;
2367}
2368
2369
2370
2371
2372
2373/*
2374 * Async access()
2375 */
2376static void nfs_access_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2377{
2378 ACCESS3res *res;
2379 struct nfs_cb_data *data = private_data;
2380 struct nfs_context *nfs = data->nfs;
2381 unsigned int nfsmode = 0;
2382
2383 if (status == RPC_STATUS_ERROR) {
2384 data->cb(-EFAULT, nfs, command_data, data->private_data);
2385 free_nfs_cb_data(data);
2386 return;
2387 }
2388 if (status == RPC_STATUS_CANCEL) {
2389 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2390 free_nfs_cb_data(data);
2391 return;
2392 }
2393
2394 res = command_data;
2395 if (res->status != NFS3_OK) {
2396 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));
2397 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2398 free_nfs_cb_data(data);
2399 return;
2400 }
2401
2402 if (data->continue_int & R_OK) {
2403 nfsmode |= ACCESS3_READ;
2404 }
2405 if (data->continue_int & W_OK) {
2406 nfsmode |= ACCESS3_MODIFY;
2407 }
2408 if (data->continue_int & X_OK) {
2409 nfsmode |= ACCESS3_EXECUTE;
2410 }
2411
2412 if (res->ACCESS3res_u.resok.access != nfsmode) {
2413 rpc_set_error(nfs->rpc, "NFS: ACCESS denied. Required access %c%c%c. Allowed access %c%c%c",
2414 nfsmode&ACCESS3_READ?'r':'-',
2415 nfsmode&ACCESS3_MODIFY?'w':'-',
2416 nfsmode&ACCESS3_EXECUTE?'x':'-',
2417 res->ACCESS3res_u.resok.access&ACCESS3_READ?'r':'-',
2418 res->ACCESS3res_u.resok.access&ACCESS3_MODIFY?'w':'-',
2419 res->ACCESS3res_u.resok.access&ACCESS3_EXECUTE?'x':'-');
2420 data->cb(-EACCES, nfs, rpc_get_error(nfs->rpc), data->private_data);
2421 free_nfs_cb_data(data);
2422 return;
2423 }
2424
2425 data->cb(0, nfs, NULL, data->private_data);
2426 free_nfs_cb_data(data);
2427}
2428
2429static int nfs_access_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2430{
2431 int nfsmode = 0;
2432
2433 if (data->continue_int & R_OK) {
2434 nfsmode |= ACCESS3_READ;
2435 }
2436 if (data->continue_int & W_OK) {
2437 nfsmode |= ACCESS3_MODIFY;
2438 }
2439 if (data->continue_int & X_OK) {
2440 nfsmode |= ACCESS3_EXECUTE;
2441 }
2442
2443 if (rpc_nfs_access_async(nfs->rpc, nfs_access_cb, &data->fh, nfsmode, data) != 0) {
2444 rpc_set_error(nfs->rpc, "RPC error: Failed to send OPEN ACCESS call for %s", data->path);
2445 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2446 free_nfs_cb_data(data);
2447 return -1;
2448 }
2449 return 0;
2450}
2451
2452int nfs_access_async(struct nfs_context *nfs, const char *path, int mode, nfs_cb cb, void *private_data)
2453{
2454 if (nfs_lookuppath_async(nfs, path, cb, private_data, nfs_access_continue_internal, NULL, NULL, mode) != 0) {
cbbf9d3e
RS
2455 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2456 return -1;
84004dbf
RS
2457 }
2458
2459 return 0;
2460}
2461
2462
2463
2464/*
2465 * Async symlink()
2466 */
2467struct nfs_symlink_data {
2468 char *oldpath;
2469 char *newpathparent;
2470 char *newpathobject;
2471};
2472
2473static void free_nfs_symlink_data(void *mem)
2474{
2475 struct nfs_symlink_data *data = mem;
2476
2477 if (data->oldpath != NULL) {
2478 free(data->oldpath);
2479 }
2480 if (data->newpathparent != NULL) {
2481 free(data->newpathparent);
2482 }
2483 if (data->newpathobject != NULL) {
2484 free(data->newpathobject);
2485 }
2486 free(data);
2487}
2488
2489static void nfs_symlink_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2490{
2491 SYMLINK3res *res;
2492 struct nfs_cb_data *data = private_data;
2493 struct nfs_context *nfs = data->nfs;
2494 struct nfs_symlink_data *symlink_data = data->continue_data;
2495
2496 if (status == RPC_STATUS_ERROR) {
2497 data->cb(-EFAULT, nfs, command_data, data->private_data);
2498 free_nfs_cb_data(data);
2499 return;
2500 }
2501 if (status == RPC_STATUS_CANCEL) {
2502 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2503 free_nfs_cb_data(data);
2504 return;
2505 }
2506
2507 res = command_data;
2508 if (res->status != NFS3_OK) {
2509 rpc_set_error(nfs->rpc, "NFS: SYMLINK %s/%s -> %s failed with %s(%d)", symlink_data->newpathparent, symlink_data->newpathobject, symlink_data->oldpath, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2510 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2511 free_nfs_cb_data(data);
2512 return;
2513 }
2514
2515 data->cb(0, nfs, NULL, data->private_data);
2516 free_nfs_cb_data(data);
2517}
2518
2519static int nfs_symlink_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2520{
2521 struct nfs_symlink_data *symlink_data = data->continue_data;
2522
2523 if (rpc_nfs_symlink_async(nfs->rpc, nfs_symlink_cb, &data->fh, symlink_data->newpathobject, symlink_data->oldpath, data) != 0) {
2524 rpc_set_error(nfs->rpc, "RPC error: Failed to send SYMLINK call for %s", data->path);
2525 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2526 free_nfs_cb_data(data);
2527 return -1;
2528 }
2529 return 0;
2530}
2531
2532int nfs_symlink_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
2533{
2534 char *ptr;
2535 struct nfs_symlink_data *symlink_data;
2536
2537 symlink_data = malloc(sizeof(struct nfs_symlink_data));
2538 if (symlink_data == NULL) {
cbbf9d3e 2539 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for symlink data");
84004dbf
RS
2540 return -1;
2541 }
ea98629a 2542 memset(symlink_data, 0, sizeof(struct nfs_symlink_data));
84004dbf
RS
2543
2544 symlink_data->oldpath = strdup(oldpath);
2545 if (symlink_data->oldpath == NULL) {
cbbf9d3e 2546 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 2547 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2548 return -1;
84004dbf
RS
2549 }
2550
2551 symlink_data->newpathparent = strdup(newpath);
2552 if (symlink_data->newpathparent == NULL) {
cbbf9d3e 2553 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 2554 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2555 return -1;
84004dbf
RS
2556 }
2557
93e9306f 2558 ptr = strrchr(symlink_data->newpathparent, '/');
84004dbf 2559 if (ptr == NULL) {
cbbf9d3e 2560 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 2561 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2562 return -1;
84004dbf
RS
2563 }
2564 *ptr = 0;
2565 ptr++;
2566
2567 symlink_data->newpathobject = strdup(ptr);
2568 if (symlink_data->newpathobject == NULL) {
cbbf9d3e 2569 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for new path");
84004dbf 2570 free_nfs_symlink_data(symlink_data);
cbbf9d3e 2571 return -1;
84004dbf
RS
2572 }
2573
2574 if (nfs_lookuppath_async(nfs, symlink_data->newpathparent, cb, private_data, nfs_symlink_continue_internal, symlink_data, free_nfs_symlink_data, 0) != 0) {
cbbf9d3e
RS
2575 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2576 return -1;
84004dbf
RS
2577 }
2578
2579 return 0;
2580}
2581
2582
2583
2584/*
2585 * Async rename()
2586 */
2587struct nfs_rename_data {
2588 char *oldpath;
2589 char *oldobject;
2590 struct nfs_fh3 olddir;
2591 char *newpath;
2592 char *newobject;
2593 struct nfs_fh3 newdir;
2594};
2595
2596static void free_nfs_rename_data(void *mem)
2597{
2598 struct nfs_rename_data *data = mem;
2599
2600 if (data->oldpath != NULL) {
2601 free(data->oldpath);
2602 }
2603 if (data->olddir.data.data_val != NULL) {
2604 free(data->olddir.data.data_val);
2605 }
2606 if (data->newpath != NULL) {
2607 free(data->newpath);
2608 }
2609 if (data->newdir.data.data_val != NULL) {
2610 free(data->newdir.data.data_val);
2611 }
2612 free(data);
2613}
2614
2615static void nfs_rename_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2616{
2617 RENAME3res *res;
2618 struct nfs_cb_data *data = private_data;
2619 struct nfs_context *nfs = data->nfs;
2620 struct nfs_rename_data *rename_data = data->continue_data;
2621
2622 if (status == RPC_STATUS_ERROR) {
2623 data->cb(-EFAULT, nfs, command_data, data->private_data);
2624 free_nfs_cb_data(data);
2625 return;
2626 }
2627 if (status == RPC_STATUS_CANCEL) {
2628 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2629 free_nfs_cb_data(data);
2630 return;
2631 }
2632
2633 res = command_data;
2634 if (res->status != NFS3_OK) {
2635 rpc_set_error(nfs->rpc, "NFS: RENAME %s/%s -> %s/%s failed with %s(%d)", rename_data->oldpath, rename_data->oldobject, rename_data->newpath, rename_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2636 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2637 free_nfs_cb_data(data);
2638 return;
2639 }
2640
2641 data->cb(0, nfs, NULL, data->private_data);
2642 free_nfs_cb_data(data);
2643}
2644
2645static int nfs_rename_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2646{
2647 struct nfs_rename_data *rename_data = data->continue_data;
2648
2649 /* steal the filehandle */
2650 rename_data->newdir.data.data_len = data->fh.data.data_len;
2651 rename_data->newdir.data.data_val = data->fh.data.data_val;
2652 data->fh.data.data_val = NULL;
2653
2654 if (rpc_nfs_rename_async(nfs->rpc, nfs_rename_cb, &rename_data->olddir, rename_data->oldobject, &rename_data->newdir, rename_data->newobject, data) != 0) {
2655 rpc_set_error(nfs->rpc, "RPC error: Failed to send RENAME call for %s", data->path);
2656 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2657 free_nfs_cb_data(data);
2658 return -1;
2659 }
2660 return 0;
2661}
2662
2663
2664static int nfs_rename_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2665{
2666 struct nfs_rename_data *rename_data = data->continue_data;
2667
2668 /* steal the filehandle */
2669 rename_data->olddir.data.data_len = data->fh.data.data_len;
2670 rename_data->olddir.data.data_val = data->fh.data.data_val;
2671 data->fh.data.data_val = NULL;
2672
2673 if (nfs_lookuppath_async(nfs, rename_data->newpath, data->cb, data->private_data, nfs_rename_continue_2_internal, rename_data, free_nfs_rename_data, 0) != 0) {
2674 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", rename_data->newpath);
2675 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2676 free_nfs_cb_data(data);
2677 return -1;
2678 }
2679 data->continue_data = NULL;
2680 free_nfs_cb_data(data);
2681
2682 return 0;
2683}
2684
2685
2686int nfs_rename_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
2687{
2688 char *ptr;
2689 struct nfs_rename_data *rename_data;
2690
2691 rename_data = malloc(sizeof(struct nfs_rename_data));
2692 if (rename_data == NULL) {
cbbf9d3e 2693 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for rename data");
84004dbf
RS
2694 return -1;
2695 }
ea98629a 2696 memset(rename_data, 0, sizeof(struct nfs_rename_data));
84004dbf
RS
2697
2698 rename_data->oldpath = strdup(oldpath);
2699 if (rename_data->oldpath == NULL) {
cbbf9d3e 2700 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 2701 free_nfs_rename_data(rename_data);
cbbf9d3e 2702 return -1;
84004dbf 2703 }
93e9306f 2704 ptr = strrchr(rename_data->oldpath, '/');
84004dbf 2705 if (ptr == NULL) {
cbbf9d3e 2706 rpc_set_error(nfs->rpc, "Invalid path %s", oldpath);
84004dbf 2707 free_nfs_rename_data(rename_data);
cbbf9d3e 2708 return -1;
84004dbf
RS
2709 }
2710 *ptr = 0;
2711 ptr++;
2712 rename_data->oldobject = ptr;
2713
2714
2715 rename_data->newpath = strdup(newpath);
2716 if (rename_data->newpath == NULL) {
cbbf9d3e 2717 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 2718 free_nfs_rename_data(rename_data);
cbbf9d3e 2719 return -1;
84004dbf 2720 }
93e9306f 2721 ptr = strrchr(rename_data->newpath, '/');
84004dbf 2722 if (ptr == NULL) {
cbbf9d3e 2723 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 2724 free_nfs_rename_data(rename_data);
cbbf9d3e 2725 return -1;
84004dbf
RS
2726 }
2727 *ptr = 0;
2728 ptr++;
2729 rename_data->newobject = ptr;
2730
2731
2732 if (nfs_lookuppath_async(nfs, rename_data->oldpath, cb, private_data, nfs_rename_continue_1_internal, rename_data, free_nfs_rename_data, 0) != 0) {
cbbf9d3e
RS
2733 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2734 return -1;
84004dbf
RS
2735 }
2736
2737 return 0;
2738}
2739
2740
2741/*
2742 * Async link()
2743 */
2744struct nfs_link_data {
2745 char *oldpath;
2746 struct nfs_fh3 oldfh;
2747 char *newpath;
2748 char *newobject;
2749 struct nfs_fh3 newdir;
2750};
2751
2752static void free_nfs_link_data(void *mem)
2753{
2754 struct nfs_link_data *data = mem;
2755
2756 if (data->oldpath != NULL) {
2757 free(data->oldpath);
2758 }
2759 if (data->oldfh.data.data_val != NULL) {
2760 free(data->oldfh.data.data_val);
2761 }
2762 if (data->newpath != NULL) {
2763 free(data->newpath);
2764 }
2765 if (data->newdir.data.data_val != NULL) {
2766 free(data->newdir.data.data_val);
2767 }
2768 free(data);
2769}
2770
2771static void nfs_link_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
2772{
2773 LINK3res *res;
2774 struct nfs_cb_data *data = private_data;
2775 struct nfs_context *nfs = data->nfs;
2776 struct nfs_link_data *link_data = data->continue_data;
2777
2778 if (status == RPC_STATUS_ERROR) {
2779 data->cb(-EFAULT, nfs, command_data, data->private_data);
2780 free_nfs_cb_data(data);
2781 return;
2782 }
2783 if (status == RPC_STATUS_CANCEL) {
2784 data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
2785 free_nfs_cb_data(data);
2786 return;
2787 }
2788
2789 res = command_data;
2790 if (res->status != NFS3_OK) {
2791 rpc_set_error(nfs->rpc, "NFS: LINK %s -> %s/%s failed with %s(%d)", link_data->oldpath, link_data->newpath, link_data->newobject, nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
2792 data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
2793 free_nfs_cb_data(data);
2794 return;
2795 }
2796
2797 data->cb(0, nfs, NULL, data->private_data);
2798 free_nfs_cb_data(data);
2799}
2800
2801static int nfs_link_continue_2_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2802{
2803 struct nfs_link_data *link_data = data->continue_data;
2804
2805 /* steal the filehandle */
2806 link_data->newdir.data.data_len = data->fh.data.data_len;
2807 link_data->newdir.data.data_val = data->fh.data.data_val;
2808 data->fh.data.data_val = NULL;
2809
2810 if (rpc_nfs_link_async(nfs->rpc, nfs_link_cb, &link_data->oldfh, &link_data->newdir, link_data->newobject, data) != 0) {
2811 rpc_set_error(nfs->rpc, "RPC error: Failed to send LINK call for %s", data->path);
2812 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2813 free_nfs_cb_data(data);
2814 return -1;
2815 }
2816 return 0;
2817}
2818
2819
2820static int nfs_link_continue_1_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
2821{
2822 struct nfs_link_data *link_data = data->continue_data;
2823
2824 /* steal the filehandle */
2825 link_data->oldfh.data.data_len = data->fh.data.data_len;
2826 link_data->oldfh.data.data_val = data->fh.data.data_val;
2827 data->fh.data.data_val = NULL;
2828
2829 if (nfs_lookuppath_async(nfs, link_data->newpath, data->cb, data->private_data, nfs_link_continue_2_internal, link_data, free_nfs_link_data, 0) != 0) {
2830 rpc_set_error(nfs->rpc, "RPC error: Failed to send LOOKUP call for %s", link_data->newpath);
2831 data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
2832 free_nfs_cb_data(data);
2833 return -1;
2834 }
2835 data->continue_data = NULL;
2836 free_nfs_cb_data(data);
2837
2838 return 0;
2839}
2840
2841
2842int nfs_link_async(struct nfs_context *nfs, const char *oldpath, const char *newpath, nfs_cb cb, void *private_data)
2843{
2844 char *ptr;
2845 struct nfs_link_data *link_data;
2846
2847 link_data = malloc(sizeof(struct nfs_link_data));
2848 if (link_data == NULL) {
cbbf9d3e 2849 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for link data");
84004dbf
RS
2850 return -1;
2851 }
ea98629a 2852 memset(link_data, 0, sizeof(struct nfs_link_data));
84004dbf
RS
2853
2854 link_data->oldpath = strdup(oldpath);
2855 if (link_data->oldpath == NULL) {
cbbf9d3e 2856 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for oldpath");
84004dbf 2857 free_nfs_link_data(link_data);
cbbf9d3e 2858 return -1;
84004dbf
RS
2859 }
2860
2861 link_data->newpath = strdup(newpath);
2862 if (link_data->newpath == NULL) {
cbbf9d3e 2863 rpc_set_error(nfs->rpc, "Out of memory, failed to allocate buffer for newpath");
84004dbf 2864 free_nfs_link_data(link_data);
cbbf9d3e 2865 return -1;
84004dbf 2866 }
93e9306f 2867 ptr = strrchr(link_data->newpath, '/');
84004dbf 2868 if (ptr == NULL) {
cbbf9d3e 2869 rpc_set_error(nfs->rpc, "Invalid path %s", newpath);
84004dbf 2870 free_nfs_link_data(link_data);
cbbf9d3e 2871 return -1;
84004dbf
RS
2872 }
2873 *ptr = 0;
2874 ptr++;
2875 link_data->newobject = ptr;
2876
2877
2878 if (nfs_lookuppath_async(nfs, link_data->oldpath, cb, private_data, nfs_link_continue_1_internal, link_data, free_nfs_link_data, 0) != 0) {
cbbf9d3e
RS
2879 rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
2880 return -1;
84004dbf
RS
2881 }
2882
2883 return 0;
2884}
2885
2886
2887//qqq replace later with lseek()
2888off_t nfs_get_current_offset(struct nfsfh *nfsfh)
2889{
2890 return nfsfh->offset;
2891}
2892
17ef62fa
RS
2893
2894
2895/*
2896 * Get the maximum supported READ3 size by the server
2897 */
2898size_t nfs_get_readmax(struct nfs_context *nfs)
2899{
2900 return nfs->readmax;
2901}
2902
2903/*
2904 * Get the maximum supported WRITE3 size by the server
2905 */
2906size_t nfs_get_writemax(struct nfs_context *nfs)
2907{
2908 return nfs->writemax;
2909}
1896d37b
RS
2910
2911void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
2912{
2913 va_list ap;
1e8994af 2914 char *str = NULL;
1896d37b 2915
1e8994af 2916 va_start(ap, error_string);
2606f9bb
RS
2917 str = malloc(1024);
2918 vsnprintf(str, 1024, error_string, ap);
1896d37b
RS
2919 if (nfs->rpc->error_string != NULL) {
2920 free(nfs->rpc->error_string);
2921 }
1896d37b 2922 nfs->rpc->error_string = str;
a8a1b858 2923 va_end(ap);
1896d37b 2924}
7f0242ca
RS
2925
2926
2927
2928struct mount_cb_data {
2929 rpc_cb cb;
2930 void *private_data;
2931 char *server;
2932};
2933
2934static void free_mount_cb_data(struct mount_cb_data *data)
2935{
2936 if (data->server != NULL) {
2937 free(data->server);
2938 data->server = NULL;
2939 }
2940
2941 free(data);
2942}
2943
2944static void mount_export_5_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
2945{
2946 struct mount_cb_data *data = private_data;
2947
2948 if (status == RPC_STATUS_ERROR) {
2949 data->cb(rpc, -EFAULT, command_data, data->private_data);
2950 free_mount_cb_data(data);
2951 return;
2952 }
2953 if (status == RPC_STATUS_CANCEL) {
2954 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
2955 free_mount_cb_data(data);
2956 return;
2957 }
2958
2959 data->cb(rpc, 0, command_data, data->private_data);
2960 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
2961 rpc_set_error(rpc, "Failed to disconnect\n");
2962 }
2963 free_mount_cb_data(data);
2964}
2965
2966static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
2967{
2968 struct mount_cb_data *data = private_data;
2969
2970 if (status == RPC_STATUS_ERROR) {
2971 data->cb(rpc, -EFAULT, command_data, data->private_data);
2972 free_mount_cb_data(data);
2973 return;
2974 }
2975 if (status == RPC_STATUS_CANCEL) {
2976 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
2977 free_mount_cb_data(data);
2978 return;
2979 }
2980
2981 if (rpc_mount_export_async(rpc, mount_export_5_cb, data) != 0) {
2982 data->cb(rpc, -ENOMEM, command_data, data->private_data);
2983 free_mount_cb_data(data);
2984 return;
2985 }
2986}
2987
2988static void mount_export_3_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
2989{
2990 struct mount_cb_data *data = private_data;
2991 uint32_t mount_port;
2992
2993 if (status == RPC_STATUS_ERROR) {
2994 data->cb(rpc, -EFAULT, command_data, data->private_data);
2995 free_mount_cb_data(data);
2996 return;
2997 }
2998 if (status == RPC_STATUS_CANCEL) {
2999 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3000 free_mount_cb_data(data);
3001 return;
3002 }
3003
3004 mount_port = *(uint32_t *)command_data;
3005 if (mount_port == 0) {
3006 rpc_set_error(rpc, "RPC error. Mount program is not available");
3007 data->cb(rpc, -ENOENT, command_data, data->private_data);
3008 free_mount_cb_data(data);
3009 return;
3010 }
3011
3012 rpc_disconnect(rpc, "normal disconnect");
3013 if (rpc_connect_async(rpc, data->server, mount_port, mount_export_4_cb, data) != 0) {
3014 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3015 free_mount_cb_data(data);
3016 return;
3017 }
3018}
3019
3020static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3021{
3022 struct mount_cb_data *data = private_data;
3023
3024 if (status == RPC_STATUS_ERROR) {
3025 data->cb(rpc, -EFAULT, command_data, data->private_data);
3026 free_mount_cb_data(data);
3027 return;
3028 }
3029 if (status == RPC_STATUS_CANCEL) {
3030 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3031 free_mount_cb_data(data);
3032 return;
3033 }
3034
3035 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, mount_export_3_cb, private_data) != 0) {
3036 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3037 free_mount_cb_data(data);
3038 return;
3039 }
3040}
3041
3042static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
3043{
3044 struct mount_cb_data *data = private_data;
3045
3046 if (status == RPC_STATUS_ERROR) {
3047 data->cb(rpc, -EFAULT, command_data, data->private_data);
3048 free_mount_cb_data(data);
3049 return;
3050 }
3051 if (status == RPC_STATUS_CANCEL) {
3052 data->cb(rpc, -EINTR, "Command was cancelled", data->private_data);
3053 free_mount_cb_data(data);
3054 return;
3055 }
3056
3057 if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
3058 data->cb(rpc, -ENOMEM, command_data, data->private_data);
3059 free_mount_cb_data(data);
3060 return;
3061 }
3062}
3063
3064int mount_getexports_async(struct rpc_context *rpc, const char *server, rpc_cb cb, void *private_data)
3065{
3066 struct mount_cb_data *data;
3067
3068 data = malloc(sizeof(struct mount_cb_data));
3069 if (data == NULL) {
3070 return -1;
3071 }
ea98629a 3072 memset(data, 0, sizeof(struct mount_cb_data));
7f0242ca
RS
3073 data->cb = cb;
3074 data->private_data = private_data;
3075 data->server = strdup(server);
3076 if (data->server == NULL) {
3077 free_mount_cb_data(data);
3078 return -1;
3079 }
3080 if (rpc_connect_async(rpc, data->server, 111, mount_export_1_cb, data) != 0) {
3081 free_mount_cb_data(data);
3082 return -1;
3083 }
3084
3085 return 0;
3086}
3087
df5af25f
RS
3088struct rpc_context *nfs_get_rpc_context(struct nfs_context *nfs)
3089{
3090 return nfs->rpc;
3091}
3092
b077fdeb
RS
3093const char *nfs_get_server(struct nfs_context *nfs) {
3094 return nfs->server;
3095}
3096
3097const char *nfs_get_export(struct nfs_context *nfs) {
3098 return nfs->export;
3099}
0ad9a1f1
RS
3100
3101
3102#if defined(WIN32)
3103int poll(struct pollfd *fds, int nfsd, int timeout)
3104{
3105 fd_set rfds, wfds, efds;
3106 int ret;
3107
3108 FD_ZERO(&rfds);
3109 FD_ZERO(&wfds);
3110 FD_ZERO(&efds);
3111 if (fds->events & POLLIN) {
3112 FD_SET(fds->fd, &rfds);
3113 }
3114 if (fds->events & POLLOUT) {
3115 FD_SET(fds->fd, &wfds);
3116 }
3117 FD_SET(fds->fd, &efds);
3118 select(fds->fd + 1, &rfds, &wfds, &efds, NULL);
3119 fds->revents = 0;
3120 if (FD_ISSET(fds->fd, &rfds)) {
3121 fds->revents |= POLLIN;
3122 }
3123 if (FD_ISSET(fds->fd, &wfds)) {
3124 fds->revents |= POLLOUT;
3125 }
3126 if (FD_ISSET(fds->fd, &efds)) {
3127 fds->revents |= POLLHUP;
3128 }
41d82d7d 3129 return 1;
0ad9a1f1
RS
3130}
3131#endif
3132