2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 /* Example program using the highlevel async interface.
25 #include "win32_compat.h"
38 #define SERVER "10.1.1.27"
39 #define EXPORT "/VIRTUAL"
40 #define NFSFILE "/BOOKS/Classics/Dracula.djvu"
41 #define NFSDIR "/BOOKS/Classics/"
46 #include <sys/types.h>
49 #include "libnfs-raw.h"
50 #include "libnfs-raw-mount.h"
52 struct rpc_context
*mount_context
;
62 void mount_export_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
64 struct client
*client
= private_data
;
65 exports export
= *(exports
*)data
;
68 printf("MOUNT/EXPORT failed with \"%s\"\n", rpc_get_error(mount_context
));
72 printf("Got exports list from server %s\n", client
->server
);
73 while (export
!= NULL
) {
74 printf("Export: %s\n", export
->ex_dir
);
75 export
= export
->ex_next
;
80 client
->is_finished
= 1;
83 void nfs_opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
85 struct client
*client
= private_data
;
86 struct nfsdir
*nfsdir
= data
;
87 struct nfsdirent
*nfsdirent
;
90 printf("opendir failed with \"%s\"\n", (char *)data
);
94 printf("opendir successful\n");
95 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
96 printf("Inode:%d Name:%s\n", (int)nfsdirent
->inode
, nfsdirent
->name
);
98 nfs_closedir(nfs
, nfsdir
);
100 mount_context
= rpc_init_context();
101 if (mount_getexports_async(mount_context
, client
->server
, mount_export_cb
, client
) != 0) {
102 printf("Failed to start MOUNT/EXPORT\n");
107 void nfs_close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
109 struct client
*client
= private_data
;
112 printf("close failed with \"%s\"\n", (char *)data
);
116 printf("close successful\n");
117 printf("call opendir(%s)\n", NFSDIR
);
118 if (nfs_opendir_async(nfs
, NFSDIR
, nfs_opendir_cb
, client
) != 0) {
119 printf("Failed to start async nfs close\n");
124 void nfs_fstat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
126 struct client
*client
= private_data
;
130 printf("fstat call failed with \"%s\"\n", (char *)data
);
134 printf("Got reply from server for fstat(%s).\n", NFSFILE
);
135 st
= (struct stat
*)data
;
136 printf("Mode %04o\n", st
->st_mode
);
137 printf("Size %d\n", (int)st
->st_size
);
138 printf("Inode %04o\n", (int)st
->st_ino
);
140 printf("Close file\n");
141 if (nfs_close_async(nfs
, client
->nfsfh
, nfs_close_cb
, client
) != 0) {
142 printf("Failed to start async nfs close\n");
147 void nfs_read_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
149 struct client
*client
= private_data
;
154 printf("read failed with \"%s\"\n", (char *)data
);
158 printf("read successful with %d bytes of data\n", status
);
161 printf("%02x ", read_data
[i
]&0xff);
164 printf("Fstat file :%s\n", NFSFILE
);
165 if (nfs_fstat_async(nfs
, client
->nfsfh
, nfs_fstat_cb
, client
) != 0) {
166 printf("Failed to start async nfs fstat\n");
171 void nfs_open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
173 struct client
*client
= private_data
;
177 printf("open call failed with \"%s\"\n", (char *)data
);
182 client
->nfsfh
= nfsfh
;
183 printf("Got reply from server for open(%s). Handle:%p\n", NFSFILE
, data
);
184 printf("Read first 64 bytes\n");
185 if (nfs_pread_async(nfs
, nfsfh
, 0, 64, nfs_read_cb
, client
) != 0) {
186 printf("Failed to start async nfs open\n");
191 void nfs_stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
193 struct client
*client
= private_data
;
197 printf("stat call failed with \"%s\"\n", (char *)data
);
201 printf("Got reply from server for stat(%s).\n", NFSFILE
);
202 st
= (struct stat
*)data
;
203 printf("Mode %04o\n", st
->st_mode
);
204 printf("Size %d\n", (int)st
->st_size
);
205 printf("Inode %04o\n", (int)st
->st_ino
);
207 printf("Open file for reading :%s\n", NFSFILE
);
208 if (nfs_open_async(nfs
, NFSFILE
, O_RDONLY
, nfs_open_cb
, client
) != 0) {
209 printf("Failed to start async nfs open\n");
214 void nfs_mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
216 struct client
*client
= private_data
;
219 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
223 printf("Got reply from server for MOUNT/MNT procedure.\n");
224 printf("Stat file :%s\n", NFSFILE
);
225 if (nfs_stat_async(nfs
, NFSFILE
, nfs_stat_cb
, client
) != 0) {
226 printf("Failed to start async nfs stat\n");
233 int main(int argc _U_
, char *argv
[] _U_
)
235 struct nfs_context
*nfs
;
237 struct client client
;
238 struct pollfd pfds
[2]; /* nfs:0 mount:1 */
240 client
.server
= SERVER
;
241 client
.export
= EXPORT
;
242 client
.is_finished
= 0;
244 nfs
= nfs_init_context();
246 printf("failed to init context\n");
250 ret
= nfs_mount_async(nfs
, client
.server
, client
.export
, nfs_mount_cb
, &client
);
252 printf("Failed to start async nfs mount\n");
259 pfds
[0].fd
= nfs_get_fd(nfs
);
260 pfds
[0].events
= nfs_which_events(nfs
);
263 if (mount_context
!= 0 && rpc_get_fd(mount_context
) != -1) {
264 pfds
[1].fd
= rpc_get_fd(mount_context
);
265 pfds
[1].events
= rpc_which_events(mount_context
);
268 if (poll(&pfds
[0], 2, -1) < 0) {
269 printf("Poll failed");
272 if (mount_context
!= NULL
) {
273 if (rpc_service(mount_context
, pfds
[1].revents
) < 0) {
274 printf("rpc_service failed\n");
278 if (nfs_service(nfs
, pfds
[0].revents
) < 0) {
279 printf("nfs_service failed\n");
282 if (client
.is_finished
) {
287 nfs_destroy_context(nfs
);
288 if (mount_context
!= NULL
) {
289 rpc_destroy_context(mount_context
);
290 mount_context
= NULL
;
292 printf("nfsclient finished\n");