2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
37 #define statvfs statfs
44 #ifdef HAVE_SYS_STATVFS_H
45 #include <sys/statvfs.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
52 #ifdef HAVE_SYS_SOCKET_H
53 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 #include <netinet/in.h>
80 #include <sys/types.h>
85 #ifdef HAVE_SYS_SOCKIO_H
86 #include <sys/sockio.h>
89 #include "libnfs-zdr.h"
91 #include "libnfs-raw.h"
92 #include "libnfs-raw-mount.h"
93 #include "libnfs-raw-nfs.h"
94 #include "libnfs-private.h"
105 static void wait_for_reply(struct rpc_context
*rpc
, struct sync_cb_data
*cb_data
)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
111 while (!cb_data
->is_finished
) {
113 pfd
.fd
= rpc_get_fd(rpc
);
114 pfd
.events
= rpc_which_events(rpc
);
115 if (poll(&pfd
, 1, -1) < 0) {
116 rpc_set_error(rpc
, "Poll failed");
117 cb_data
->status
= -EIO
;
120 if (rpc_service(rpc
, pfd
.revents
) < 0) {
121 rpc_set_error(rpc
, "rpc_service failed");
122 cb_data
->status
= -EIO
;
125 if (rpc_get_fd(rpc
) == -1) {
126 rpc_set_error(rpc
, "Socket closed\n");
132 static void wait_for_nfs_reply(struct nfs_context
*nfs
, struct sync_cb_data
*cb_data
)
136 while (!cb_data
->is_finished
) {
138 pfd
.fd
= nfs_get_fd(nfs
);
139 pfd
.events
= nfs_which_events(nfs
);
140 if (poll(&pfd
, 1, -1) < 0) {
141 nfs_set_error(nfs
, "Poll failed");
142 cb_data
->status
= -EIO
;
145 if (nfs_service(nfs
, pfd
.revents
) < 0) {
146 nfs_set_error(nfs
, "nfs_service failed");
147 cb_data
->status
= -EIO
;
159 * connect to the server and mount the export
161 static void mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
163 struct sync_cb_data
*cb_data
= private_data
;
165 cb_data
->is_finished
= 1;
166 cb_data
->status
= status
;
169 nfs_set_error(nfs
, "mount/mnt call failed with \"%s\"", (char *)data
);
174 int nfs_mount(struct nfs_context
*nfs
, const char *server
, const char *export
)
176 struct sync_cb_data cb_data
;
177 struct rpc_context
*rpc
= nfs_get_rpc_context(nfs
);
179 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
181 cb_data
.is_finished
= 0;
183 if (nfs_mount_async(nfs
, server
, export
, mount_cb
, &cb_data
) != 0) {
184 nfs_set_error(nfs
, "nfs_mount_async failed");
188 wait_for_nfs_reply(nfs
, &cb_data
);
190 /* Dont want any more callbacks even if the socket is closed */
191 rpc
->connect_cb
= NULL
;
193 return cb_data
.status
;
200 static void stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
202 struct sync_cb_data
*cb_data
= private_data
;
204 cb_data
->is_finished
= 1;
205 cb_data
->status
= status
;
208 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
212 memcpy(cb_data
->return_data
, data
, sizeof(struct stat
));
215 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct stat
*st
)
217 struct sync_cb_data cb_data
;
219 cb_data
.is_finished
= 0;
220 cb_data
.return_data
= st
;
222 if (nfs_stat_async(nfs
, path
, stat_cb
, &cb_data
) != 0) {
223 nfs_set_error(nfs
, "nfs_stat_async failed");
227 wait_for_nfs_reply(nfs
, &cb_data
);
229 return cb_data
.status
;
238 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
240 struct sync_cb_data
*cb_data
= private_data
;
241 struct nfsfh
*fh
, **nfsfh
;
243 cb_data
->is_finished
= 1;
244 cb_data
->status
= status
;
247 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
252 nfsfh
= cb_data
->return_data
;
256 int nfs_open(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
258 struct sync_cb_data cb_data
;
260 cb_data
.is_finished
= 0;
261 cb_data
.return_data
= nfsfh
;
263 if (nfs_open_async(nfs
, path
, mode
, open_cb
, &cb_data
) != 0) {
264 nfs_set_error(nfs
, "nfs_open_async failed");
268 wait_for_nfs_reply(nfs
, &cb_data
);
270 return cb_data
.status
;
279 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
281 struct sync_cb_data
*cb_data
= private_data
;
283 cb_data
->is_finished
= 1;
284 cb_data
->status
= status
;
287 nfs_set_error(nfs
, "pread call failed with \"%s\"", (char *)data
);
291 buffer
= cb_data
->return_data
;
292 memcpy(buffer
, (char *)data
, status
);
295 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buffer
)
297 struct sync_cb_data cb_data
;
299 cb_data
.is_finished
= 0;
300 cb_data
.return_data
= buffer
;
302 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
303 nfs_set_error(nfs
, "nfs_pread_async failed");
307 wait_for_nfs_reply(nfs
, &cb_data
);
309 return cb_data
.status
;
315 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buffer
)
317 return nfs_pread(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buffer
);
323 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
325 struct sync_cb_data
*cb_data
= private_data
;
326 cb_data
->is_finished
= 1;
327 cb_data
->status
= status
;
330 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
335 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
337 struct sync_cb_data cb_data
;
339 cb_data
.is_finished
= 0;
341 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
342 nfs_set_error(nfs
, "nfs_close_async failed");
346 wait_for_nfs_reply(nfs
, &cb_data
);
348 return cb_data
.status
;
357 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
359 struct sync_cb_data cb_data
;
361 cb_data
.is_finished
= 0;
362 cb_data
.return_data
= st
;
364 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
365 nfs_set_error(nfs
, "nfs_fstat_async failed");
369 wait_for_nfs_reply(nfs
, &cb_data
);
371 return cb_data
.status
;
378 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
380 struct sync_cb_data
*cb_data
= private_data
;
381 cb_data
->is_finished
= 1;
382 cb_data
->status
= status
;
385 nfs_set_error(nfs
, "pwrite call failed with \"%s\"", (char *)data
);
390 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
)
392 struct sync_cb_data cb_data
;
394 cb_data
.is_finished
= 0;
396 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
397 nfs_set_error(nfs
, "nfs_pwrite_async failed");
401 wait_for_nfs_reply(nfs
, &cb_data
);
403 return cb_data
.status
;
409 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
)
411 return nfs_pwrite(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buf
);
418 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
420 struct sync_cb_data
*cb_data
= private_data
;
421 cb_data
->is_finished
= 1;
422 cb_data
->status
= status
;
425 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
430 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
432 struct sync_cb_data cb_data
;
434 cb_data
.is_finished
= 0;
436 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
437 nfs_set_error(nfs
, "nfs_fsync_async failed");
441 wait_for_nfs_reply(nfs
, &cb_data
);
443 return cb_data
.status
;
452 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
454 struct sync_cb_data
*cb_data
= private_data
;
455 cb_data
->is_finished
= 1;
456 cb_data
->status
= status
;
459 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
464 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
)
466 struct sync_cb_data cb_data
;
468 cb_data
.is_finished
= 0;
470 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
471 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
475 wait_for_nfs_reply(nfs
, &cb_data
);
477 return cb_data
.status
;
485 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
487 struct sync_cb_data
*cb_data
= private_data
;
488 cb_data
->is_finished
= 1;
489 cb_data
->status
= status
;
492 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
497 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, uint64_t length
)
499 struct sync_cb_data cb_data
;
501 cb_data
.is_finished
= 0;
503 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
504 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
508 wait_for_nfs_reply(nfs
, &cb_data
);
510 return cb_data
.status
;
520 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
522 struct sync_cb_data
*cb_data
= private_data
;
523 cb_data
->is_finished
= 1;
524 cb_data
->status
= status
;
527 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
532 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
534 struct sync_cb_data cb_data
;
536 cb_data
.is_finished
= 0;
538 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
539 nfs_set_error(nfs
, "nfs_mkdir_async failed");
543 wait_for_nfs_reply(nfs
, &cb_data
);
545 return cb_data
.status
;
555 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
557 struct sync_cb_data
*cb_data
= private_data
;
558 cb_data
->is_finished
= 1;
559 cb_data
->status
= status
;
562 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
567 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
569 struct sync_cb_data cb_data
;
571 cb_data
.is_finished
= 0;
573 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
574 nfs_set_error(nfs
, "nfs_rmdir_async failed");
578 wait_for_nfs_reply(nfs
, &cb_data
);
580 return cb_data
.status
;
588 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
590 struct sync_cb_data
*cb_data
= private_data
;
591 struct nfsfh
*fh
, **nfsfh
;
593 cb_data
->is_finished
= 1;
594 cb_data
->status
= status
;
597 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
602 nfsfh
= cb_data
->return_data
;
606 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
608 struct sync_cb_data cb_data
;
610 cb_data
.is_finished
= 0;
611 cb_data
.return_data
= nfsfh
;
613 if (nfs_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
614 nfs_set_error(nfs
, "nfs_creat_async failed");
618 wait_for_nfs_reply(nfs
, &cb_data
);
620 return cb_data
.status
;
626 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
628 struct sync_cb_data
*cb_data
= private_data
;
630 cb_data
->is_finished
= 1;
631 cb_data
->status
= status
;
634 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
639 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
641 struct sync_cb_data cb_data
;
643 cb_data
.is_finished
= 0;
645 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
646 nfs_set_error(nfs
, "nfs_creat_async failed");
650 wait_for_nfs_reply(nfs
, &cb_data
);
652 return cb_data
.status
;
659 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
661 struct sync_cb_data
*cb_data
= private_data
;
663 cb_data
->is_finished
= 1;
664 cb_data
->status
= status
;
667 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
672 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
674 struct sync_cb_data cb_data
;
676 cb_data
.is_finished
= 0;
678 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
679 nfs_set_error(nfs
, "nfs_unlink_async failed");
683 wait_for_nfs_reply(nfs
, &cb_data
);
685 return cb_data
.status
;
693 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
695 struct sync_cb_data
*cb_data
= private_data
;
696 struct nfsdir
*dir
, **nfsdir
;
698 cb_data
->is_finished
= 1;
699 cb_data
->status
= status
;
702 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
707 nfsdir
= cb_data
->return_data
;
711 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
713 struct sync_cb_data cb_data
;
715 cb_data
.is_finished
= 0;
716 cb_data
.return_data
= nfsdir
;
718 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
719 nfs_set_error(nfs
, "nfs_opendir_async failed");
723 wait_for_nfs_reply(nfs
, &cb_data
);
725 return cb_data
.status
;
732 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
734 struct sync_cb_data
*cb_data
= private_data
;
736 cb_data
->is_finished
= 1;
737 cb_data
->status
= status
;
740 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
744 if (cb_data
->return_data
!= NULL
) {
745 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
749 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, uint64_t *current_offset
)
751 struct sync_cb_data cb_data
;
753 cb_data
.is_finished
= 0;
754 cb_data
.return_data
= current_offset
;
756 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
757 nfs_set_error(nfs
, "nfs_lseek_async failed");
761 wait_for_nfs_reply(nfs
, &cb_data
);
763 return cb_data
.status
;
771 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
773 struct sync_cb_data
*cb_data
= private_data
;
775 cb_data
->is_finished
= 1;
776 cb_data
->status
= status
;
779 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
783 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
786 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
788 struct sync_cb_data cb_data
;
790 cb_data
.is_finished
= 0;
791 cb_data
.return_data
= svfs
;
793 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
794 nfs_set_error(nfs
, "nfs_statvfs_async failed");
798 wait_for_nfs_reply(nfs
, &cb_data
);
800 return cb_data
.status
;
810 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
812 struct sync_cb_data
*cb_data
= private_data
;
814 cb_data
->is_finished
= 1;
815 cb_data
->status
= status
;
818 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
822 if (strlen(data
) > (size_t)cb_data
->return_int
) {
823 nfs_set_error(nfs
, "Too small buffer for readlink");
824 cb_data
->status
= -ENAMETOOLONG
;
828 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
831 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
833 struct sync_cb_data cb_data
;
835 cb_data
.is_finished
= 0;
836 cb_data
.return_data
= buf
;
837 cb_data
.return_int
= bufsize
;
839 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
840 nfs_set_error(nfs
, "nfs_readlink_async failed");
844 wait_for_nfs_reply(nfs
, &cb_data
);
846 return cb_data
.status
;
854 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
856 struct sync_cb_data
*cb_data
= private_data
;
858 cb_data
->is_finished
= 1;
859 cb_data
->status
= status
;
862 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
867 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
869 struct sync_cb_data cb_data
;
871 cb_data
.is_finished
= 0;
873 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
874 nfs_set_error(nfs
, "nfs_chmod_async failed");
878 wait_for_nfs_reply(nfs
, &cb_data
);
880 return cb_data
.status
;
889 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
891 struct sync_cb_data
*cb_data
= private_data
;
893 cb_data
->is_finished
= 1;
894 cb_data
->status
= status
;
897 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
902 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
904 struct sync_cb_data cb_data
;
906 cb_data
.is_finished
= 0;
908 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
909 nfs_set_error(nfs
, "nfs_fchmod_async failed");
913 wait_for_nfs_reply(nfs
, &cb_data
);
915 return cb_data
.status
;
924 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
926 struct sync_cb_data
*cb_data
= private_data
;
928 cb_data
->is_finished
= 1;
929 cb_data
->status
= status
;
932 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
937 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
939 struct sync_cb_data cb_data
;
941 cb_data
.is_finished
= 0;
943 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
944 nfs_set_error(nfs
, "nfs_chown_async failed");
948 wait_for_nfs_reply(nfs
, &cb_data
);
950 return cb_data
.status
;
956 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
958 struct sync_cb_data
*cb_data
= private_data
;
960 cb_data
->is_finished
= 1;
961 cb_data
->status
= status
;
964 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
969 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
971 struct sync_cb_data cb_data
;
973 cb_data
.is_finished
= 0;
975 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
976 nfs_set_error(nfs
, "nfs_fchown_async failed");
980 wait_for_nfs_reply(nfs
, &cb_data
);
982 return cb_data
.status
;
990 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
992 struct sync_cb_data
*cb_data
= private_data
;
994 cb_data
->is_finished
= 1;
995 cb_data
->status
= status
;
998 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1003 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1005 struct sync_cb_data cb_data
;
1007 cb_data
.is_finished
= 0;
1009 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1010 nfs_set_error(nfs
, "nfs_utimes_async failed");
1014 wait_for_nfs_reply(nfs
, &cb_data
);
1016 return cb_data
.status
;
1024 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1026 struct sync_cb_data
*cb_data
= private_data
;
1028 cb_data
->is_finished
= 1;
1029 cb_data
->status
= status
;
1032 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1037 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1039 struct sync_cb_data cb_data
;
1041 cb_data
.is_finished
= 0;
1043 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1044 nfs_set_error(nfs
, "nfs_utimes_async failed");
1048 wait_for_nfs_reply(nfs
, &cb_data
);
1050 return cb_data
.status
;
1059 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1061 struct sync_cb_data
*cb_data
= private_data
;
1063 cb_data
->is_finished
= 1;
1064 cb_data
->status
= status
;
1067 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1072 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1074 struct sync_cb_data cb_data
;
1076 cb_data
.is_finished
= 0;
1078 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1079 nfs_set_error(nfs
, "nfs_access_async failed");
1083 wait_for_nfs_reply(nfs
, &cb_data
);
1085 return cb_data
.status
;
1093 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1095 struct sync_cb_data
*cb_data
= private_data
;
1097 cb_data
->is_finished
= 1;
1098 cb_data
->status
= status
;
1101 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1106 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1108 struct sync_cb_data cb_data
;
1110 cb_data
.is_finished
= 0;
1112 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1113 nfs_set_error(nfs
, "nfs_symlink_async failed");
1117 wait_for_nfs_reply(nfs
, &cb_data
);
1119 return cb_data
.status
;
1127 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1129 struct sync_cb_data
*cb_data
= private_data
;
1131 cb_data
->is_finished
= 1;
1132 cb_data
->status
= status
;
1135 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1140 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1142 struct sync_cb_data cb_data
;
1144 cb_data
.is_finished
= 0;
1146 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1147 nfs_set_error(nfs
, "nfs_rename_async failed");
1151 wait_for_nfs_reply(nfs
, &cb_data
);
1153 return cb_data
.status
;
1161 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1163 struct sync_cb_data
*cb_data
= private_data
;
1165 cb_data
->is_finished
= 1;
1166 cb_data
->status
= status
;
1169 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1174 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1176 struct sync_cb_data cb_data
;
1178 cb_data
.is_finished
= 0;
1180 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1181 nfs_set_error(nfs
, "nfs_link_async failed");
1185 wait_for_nfs_reply(nfs
, &cb_data
);
1187 return cb_data
.status
;
1190 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1192 struct sync_cb_data
*cb_data
= private_data
;
1195 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1197 cb_data
->is_finished
= 1;
1198 cb_data
->status
= status
;
1199 cb_data
->return_data
= NULL
;
1202 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1206 export
= *(exports
*)data
;
1207 while (export
!= NULL
) {
1210 new_export
= malloc(sizeof(*new_export
));
1211 memset(new_export
, 0, sizeof(*new_export
));
1212 new_export
->ex_dir
= strdup(export
->ex_dir
);
1213 new_export
->ex_next
= cb_data
->return_data
;
1215 cb_data
->return_data
= new_export
;
1217 export
= export
->ex_next
;
1221 struct exportnode
*mount_getexports(const char *server
)
1223 struct sync_cb_data cb_data
;
1224 struct rpc_context
*rpc
;
1227 cb_data
.is_finished
= 0;
1228 cb_data
.return_data
= NULL
;
1230 rpc
= rpc_init_context();
1231 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1232 rpc_destroy_context(rpc
);
1236 wait_for_reply(rpc
, &cb_data
);
1237 rpc_destroy_context(rpc
);
1239 return cb_data
.return_data
;
1242 void mount_free_export_list(struct exportnode
*exports
)
1244 struct exportnode
*tmp
;
1246 while ((tmp
= exports
)) {
1247 exports
= exports
->ex_next
;
1255 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1257 while (srv
!= NULL
) {
1258 struct nfs_server_list
*next
= srv
->next
;
1266 struct nfs_list_data
{
1268 struct nfs_server_list
*srvrs
;
1271 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1273 struct nfs_list_data
*srv_data
= private_data
;
1274 struct sockaddr
*sin
;
1276 struct nfs_server_list
*srvr
;
1278 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1280 if (status
== RPC_STATUS_CANCEL
) {
1284 srv_data
->status
= -1;
1288 sin
= rpc_get_recv_sockaddr(rpc
);
1290 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1291 srv_data
->status
= -1;
1295 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1296 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1297 srv_data
->status
= -1;
1301 /* check for dupes */
1302 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1303 if (!strcmp(hostdd
, srvr
->addr
)) {
1308 srvr
= malloc(sizeof(struct nfs_server_list
));
1310 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1311 srv_data
->status
= -1;
1315 srvr
->addr
= strdup(hostdd
);
1316 if (srvr
->addr
== NULL
) {
1317 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1319 srv_data
->status
= -1;
1323 srvr
->next
= srv_data
->srvrs
;
1324 srv_data
->srvrs
= srvr
;
1329 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1333 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1335 for(i
= 0; i
< numIfs
; i
++)
1339 unsigned long nFlags
= 0;
1341 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1343 if(pAddress
->sa_family
!= AF_INET
)
1346 nFlags
= InterfaceList
[i
].iiFlags
;
1348 if (!(nFlags
& IFF_UP
))
1353 if (nFlags
& IFF_LOOPBACK
)
1358 if (!(nFlags
& IFF_BROADCAST
))
1363 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1368 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1373 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1381 struct nfs_server_list
*nfs_find_local_servers(void)
1383 struct rpc_context
*rpc
;
1384 struct nfs_list_data data
= {0, NULL
};
1385 struct timeval tv_start
, tv_current
;
1388 INTERFACE_INFO InterfaceList
[20];
1389 unsigned long nBytesReturned
;
1390 int nNumInterfaces
= 0;
1392 rpc
= rpc_init_udp_context();
1398 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1400 rpc_destroy_context(rpc
);
1404 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1409 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1411 for (loop
=0; loop
<3; loop
++)
1413 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1415 rpc_destroy_context(rpc
);
1419 win32_gettimeofday(&tv_start
, NULL
);
1424 pfd
.fd
= rpc_get_fd(rpc
);
1425 pfd
.events
= rpc_which_events(rpc
);
1427 win32_gettimeofday(&tv_current
, NULL
);
1429 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1430 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1432 if (poll(&pfd
, 1, mpt
) < 0)
1434 free_nfs_srvr_list(data
.srvrs
);
1435 rpc_destroy_context(rpc
);
1438 if (pfd
.revents
== 0)
1443 if (rpc_service(rpc
, pfd
.revents
) < 0)
1450 rpc_destroy_context(rpc
);
1452 if (data
.status
!= 0)
1454 free_nfs_srvr_list(data
.srvrs
);
1461 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1465 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1467 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1471 ifr
= (struct ifreq
*)ptr
;
1472 #ifdef HAVE_SOCKADDR_LEN
1473 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1474 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1476 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1479 ptr
+= sizeof(struct ifreq
);
1482 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1485 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1488 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1491 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1494 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1497 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1500 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1503 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1507 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1515 struct nfs_server_list
*nfs_find_local_servers(void)
1517 struct rpc_context
*rpc
;
1518 struct nfs_list_data data
= {0, NULL
};
1519 struct timeval tv_start
, tv_current
;
1524 rpc
= rpc_init_udp_context();
1529 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1530 rpc_destroy_context(rpc
);
1535 /* get list of all interfaces */
1536 size
= sizeof(struct ifreq
);
1540 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1545 ifc
.ifc_buf
= malloc(size
);
1546 memset(ifc
.ifc_buf
, 0, size
);
1547 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1548 rpc_destroy_context(rpc
);
1554 for (loop
=0; loop
<3; loop
++) {
1555 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1556 rpc_destroy_context(rpc
);
1561 gettimeofday(&tv_start
, NULL
);
1565 pfd
.fd
= rpc_get_fd(rpc
);
1566 pfd
.events
= rpc_which_events(rpc
);
1568 gettimeofday(&tv_current
, NULL
);
1570 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1571 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1573 if (poll(&pfd
, 1, mpt
) < 0) {
1574 free_nfs_srvr_list(data
.srvrs
);
1575 rpc_destroy_context(rpc
);
1578 if (pfd
.revents
== 0) {
1582 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1589 rpc_destroy_context(rpc
);
1591 if (data
.status
!= 0) {
1592 free_nfs_srvr_list(data
.srvrs
);