d2b92e3297e6221f0d485b4a8e7ea60a6c371f2b
[deb_libnfs.git] / examples / nfsclient-async.c
1 /*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
3
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.
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 General Public License for more details.
13
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/>.
16 */
17
18 /* Example program using the highlevel async interface.
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef WIN32
25 #include "win32_compat.h"
26 #else
27 #include <sys/stat.h>
28 #endif
29
30 #ifdef HAVE_POLL_H
31 #include <poll.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #define SERVER "10.1.1.27"
39 #define EXPORT "/VIRTUAL"
40 #define NFSFILE "/BOOKS/Classics/Dracula.djvu"
41 #define NFSDIR "/BOOKS/Classics/"
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdint.h>
46 #include <sys/types.h>
47 #include <fcntl.h>
48 #include "libnfs-zdr.h"
49 #include "libnfs.h"
50 #include "libnfs-raw.h"
51 #include "libnfs-raw-mount.h"
52
53 struct rpc_context *mount_context;
54
55 struct client {
56 char *server;
57 char *export;
58 uint32_t mount_port;
59 struct nfsfh *nfsfh;
60 int is_finished;
61 };
62
63 void mount_export_cb(struct rpc_context *mount_context, int status, void *data, void *private_data)
64 {
65 struct client *client = private_data;
66 exports export = *(exports *)data;
67
68 if (status < 0) {
69 printf("MOUNT/EXPORT failed with \"%s\"\n", rpc_get_error(mount_context));
70 exit(10);
71 }
72
73 printf("Got exports list from server %s\n", client->server);
74 while (export != NULL) {
75 printf("Export: %s\n", export->ex_dir);
76 export = export->ex_next;
77 }
78
79 mount_context = NULL;
80
81 client->is_finished = 1;
82 }
83
84 void nfs_opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
85 {
86 struct client *client = private_data;
87 struct nfsdir *nfsdir = data;
88 struct nfsdirent *nfsdirent;
89
90 if (status < 0) {
91 printf("opendir failed with \"%s\"\n", (char *)data);
92 exit(10);
93 }
94
95 printf("opendir successful\n");
96 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
97 printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
98 }
99 nfs_closedir(nfs, nfsdir);
100
101 mount_context = rpc_init_context();
102 if (mount_getexports_async(mount_context, client->server, mount_export_cb, client) != 0) {
103 printf("Failed to start MOUNT/EXPORT\n");
104 exit(10);
105 }
106 }
107
108 void nfs_close_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
109 {
110 struct client *client = private_data;
111
112 if (status < 0) {
113 printf("close failed with \"%s\"\n", (char *)data);
114 exit(10);
115 }
116
117 printf("close successful\n");
118 printf("call opendir(%s)\n", NFSDIR);
119 if (nfs_opendir_async(nfs, NFSDIR, nfs_opendir_cb, client) != 0) {
120 printf("Failed to start async nfs close\n");
121 exit(10);
122 }
123 }
124
125 void nfs_fstat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
126 {
127 struct client *client = private_data;
128 struct stat *st;
129
130 if (status < 0) {
131 printf("fstat call failed with \"%s\"\n", (char *)data);
132 exit(10);
133 }
134
135 printf("Got reply from server for fstat(%s).\n", NFSFILE);
136 st = (struct stat *)data;
137 printf("Mode %04o\n", st->st_mode);
138 printf("Size %d\n", (int)st->st_size);
139 printf("Inode %04o\n", (int)st->st_ino);
140
141 printf("Close file\n");
142 if (nfs_close_async(nfs, client->nfsfh, nfs_close_cb, client) != 0) {
143 printf("Failed to start async nfs close\n");
144 exit(10);
145 }
146 }
147
148 void nfs_read_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
149 {
150 struct client *client = private_data;
151 char *read_data;
152 int i;
153
154 if (status < 0) {
155 printf("read failed with \"%s\"\n", (char *)data);
156 exit(10);
157 }
158
159 printf("read successful with %d bytes of data\n", status);
160 read_data = data;
161 for (i=0;i<16;i++) {
162 printf("%02x ", read_data[i]&0xff);
163 }
164 printf("\n");
165 printf("Fstat file :%s\n", NFSFILE);
166 if (nfs_fstat_async(nfs, client->nfsfh, nfs_fstat_cb, client) != 0) {
167 printf("Failed to start async nfs fstat\n");
168 exit(10);
169 }
170 }
171
172 void nfs_open_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
173 {
174 struct client *client = private_data;
175 struct nfsfh *nfsfh;
176
177 if (status < 0) {
178 printf("open call failed with \"%s\"\n", (char *)data);
179 exit(10);
180 }
181
182 nfsfh = data;
183 client->nfsfh = nfsfh;
184 printf("Got reply from server for open(%s). Handle:%p\n", NFSFILE, data);
185 printf("Read first 64 bytes\n");
186 if (nfs_pread_async(nfs, nfsfh, 0, 64, nfs_read_cb, client) != 0) {
187 printf("Failed to start async nfs open\n");
188 exit(10);
189 }
190 }
191
192 void nfs_stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
193 {
194 struct client *client = private_data;
195 struct stat *st;
196
197 if (status < 0) {
198 printf("stat call failed with \"%s\"\n", (char *)data);
199 exit(10);
200 }
201
202 printf("Got reply from server for stat(%s).\n", NFSFILE);
203 st = (struct stat *)data;
204 printf("Mode %04o\n", st->st_mode);
205 printf("Size %d\n", (int)st->st_size);
206 printf("Inode %04o\n", (int)st->st_ino);
207
208 printf("Open file for reading :%s\n", NFSFILE);
209 if (nfs_open_async(nfs, NFSFILE, O_RDONLY, nfs_open_cb, client) != 0) {
210 printf("Failed to start async nfs open\n");
211 exit(10);
212 }
213 }
214
215 void nfs_mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
216 {
217 struct client *client = private_data;
218
219 if (status < 0) {
220 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
221 exit(10);
222 }
223
224 printf("Got reply from server for MOUNT/MNT procedure.\n");
225 printf("Stat file :%s\n", NFSFILE);
226 if (nfs_stat_async(nfs, NFSFILE, nfs_stat_cb, client) != 0) {
227 printf("Failed to start async nfs stat\n");
228 exit(10);
229 }
230 }
231
232
233
234 int main(int argc _U_, char *argv[] _U_)
235 {
236 struct nfs_context *nfs;
237 int ret;
238 struct client client;
239 struct pollfd pfds[2]; /* nfs:0 mount:1 */
240
241 client.server = SERVER;
242 client.export = EXPORT;
243 client.is_finished = 0;
244
245 nfs = nfs_init_context();
246 if (nfs == NULL) {
247 printf("failed to init context\n");
248 exit(10);
249 }
250
251 ret = nfs_mount_async(nfs, client.server, client.export, nfs_mount_cb, &client);
252 if (ret != 0) {
253 printf("Failed to start async nfs mount\n");
254 exit(10);
255 }
256
257 for (;;) {
258 int num_fds;
259
260 pfds[0].fd = nfs_get_fd(nfs);
261 pfds[0].events = nfs_which_events(nfs);
262 num_fds = 1;
263
264 if (mount_context != 0 && rpc_get_fd(mount_context) != -1) {
265 pfds[1].fd = rpc_get_fd(mount_context);
266 pfds[1].events = rpc_which_events(mount_context);
267 num_fds = 2;
268 }
269 if (poll(&pfds[0], 2, -1) < 0) {
270 printf("Poll failed");
271 exit(10);
272 }
273 if (mount_context != NULL) {
274 if (rpc_service(mount_context, pfds[1].revents) < 0) {
275 printf("rpc_service failed\n");
276 break;
277 }
278 }
279 if (nfs_service(nfs, pfds[0].revents) < 0) {
280 printf("nfs_service failed\n");
281 break;
282 }
283 if (client.is_finished) {
284 break;
285 }
286 }
287
288 nfs_destroy_context(nfs);
289 if (mount_context != NULL) {
290 rpc_destroy_context(mount_context);
291 mount_context = NULL;
292 }
293 printf("nfsclient finished\n");
294 return 0;
295 }