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