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