Commit | Line | Data |
---|---|---|
84004dbf RS |
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 | ||
36 | struct client { | |
37 | char *server; | |
38 | char *export; | |
39 | uint32_t mount_port; | |
40 | struct nfsfh *nfsfh; | |
41 | int is_finished; | |
42 | }; | |
43 | ||
44 | void nfs_opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
45 | { | |
46 | struct client *client = private_data; | |
47 | struct nfsdir *nfsdir = data; | |
48 | struct nfsdirent *nfsdirent; | |
49 | ||
50 | if (status < 0) { | |
51 | printf("opendir failed with \"%s\"\n", (char *)data); | |
52 | exit(10); | |
53 | } | |
54 | ||
55 | printf("opendir successful\n"); | |
56 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
57 | printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name); | |
58 | } | |
59 | nfs_closedir(nfs, nfsdir); | |
60 | ||
61 | client->is_finished = 1; | |
62 | } | |
63 | ||
64 | void nfs_close_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
65 | { | |
66 | struct client *client = private_data; | |
67 | ||
68 | if (status < 0) { | |
69 | printf("close failed with \"%s\"\n", (char *)data); | |
70 | exit(10); | |
71 | } | |
72 | ||
73 | printf("close successful\n"); | |
74 | printf("call opendir(%s)\n", NFSDIR); | |
75 | if (nfs_opendir_async(nfs, NFSDIR, nfs_opendir_cb, client) != 0) { | |
76 | printf("Failed to start async nfs close\n"); | |
77 | exit(10); | |
78 | } | |
79 | } | |
80 | ||
81 | void nfs_fstat_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
82 | { | |
83 | struct client *client = private_data; | |
84 | struct stat *st; | |
85 | ||
86 | if (status < 0) { | |
87 | printf("fstat call failed with \"%s\"\n", (char *)data); | |
88 | exit(10); | |
89 | } | |
90 | ||
91 | printf("Got reply from server for fstat(%s).\n", NFSFILE); | |
92 | st = (struct stat *)data; | |
93 | printf("Mode %04o\n", st->st_mode); | |
94 | printf("Size %d\n", (int)st->st_size); | |
95 | printf("Inode %04o\n", (int)st->st_ino); | |
96 | ||
97 | printf("Close file\n"); | |
98 | if (nfs_close_async(nfs, client->nfsfh, nfs_close_cb, client) != 0) { | |
99 | printf("Failed to start async nfs close\n"); | |
100 | exit(10); | |
101 | } | |
102 | } | |
103 | ||
104 | void nfs_read_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
105 | { | |
106 | struct client *client = private_data; | |
107 | char *read_data; | |
108 | int i; | |
109 | ||
110 | if (status < 0) { | |
111 | printf("read failed with \"%s\"\n", (char *)data); | |
112 | exit(10); | |
113 | } | |
114 | ||
115 | printf("read successful with %d bytes of data\n", status); | |
116 | read_data = data; | |
117 | for (i=0;i<16;i++) { | |
118 | printf("%02x ", read_data[i]&0xff); | |
119 | } | |
120 | printf("\n"); | |
121 | printf("Fstat file :%s\n", NFSFILE); | |
122 | if (nfs_fstat_async(nfs, client->nfsfh, nfs_fstat_cb, client) != 0) { | |
123 | printf("Failed to start async nfs fstat\n"); | |
124 | exit(10); | |
125 | } | |
126 | } | |
127 | ||
128 | void nfs_open_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
129 | { | |
130 | struct client *client = private_data; | |
131 | struct nfsfh *nfsfh; | |
132 | ||
133 | if (status < 0) { | |
134 | printf("open call failed with \"%s\"\n", (char *)data); | |
135 | exit(10); | |
136 | } | |
137 | ||
138 | nfsfh = data; | |
139 | client->nfsfh = nfsfh; | |
140 | printf("Got reply from server for open(%s). Handle:%p\n", NFSFILE, data); | |
141 | printf("Read first 16 bytes\n"); | |
142 | if (nfs_pread_async(nfs, nfsfh, 0, 16, nfs_read_cb, client) != 0) { | |
143 | printf("Failed to start async nfs open\n"); | |
144 | exit(10); | |
145 | } | |
146 | } | |
147 | ||
148 | void nfs_stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
149 | { | |
150 | struct client *client = private_data; | |
151 | struct stat *st; | |
152 | ||
153 | if (status < 0) { | |
154 | printf("stat call failed with \"%s\"\n", (char *)data); | |
155 | exit(10); | |
156 | } | |
157 | ||
158 | printf("Got reply from server for stat(%s).\n", NFSFILE); | |
159 | st = (struct stat *)data; | |
160 | printf("Mode %04o\n", st->st_mode); | |
161 | printf("Size %d\n", (int)st->st_size); | |
162 | printf("Inode %04o\n", (int)st->st_ino); | |
163 | ||
164 | printf("Open file for reading :%s\n", NFSFILE); | |
165 | if (nfs_open_async(nfs, NFSFILE, O_RDONLY, nfs_open_cb, client) != 0) { | |
166 | printf("Failed to start async nfs open\n"); | |
167 | exit(10); | |
168 | } | |
169 | } | |
170 | ||
171 | void nfs_mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data) | |
172 | { | |
173 | struct client *client = private_data; | |
174 | ||
175 | if (status < 0) { | |
176 | printf("mount/mnt call failed with \"%s\"\n", (char *)data); | |
177 | exit(10); | |
178 | } | |
179 | ||
180 | printf("Got reply from server for MOUNT/MNT procedure.\n"); | |
181 | printf("Stat file :%s\n", NFSFILE); | |
182 | if (nfs_stat_async(nfs, NFSFILE, nfs_stat_cb, client) != 0) { | |
183 | printf("Failed to start async nfs stat\n"); | |
184 | exit(10); | |
185 | } | |
186 | } | |
187 | ||
188 | ||
189 | ||
190 | int main(int argc, char *argv[]) | |
191 | { | |
192 | struct nfs_context *nfs; | |
193 | struct pollfd pfd; | |
194 | int ret; | |
195 | struct client client; | |
196 | ||
197 | client.server = SERVER; | |
198 | client.export = EXPORT; | |
199 | client.is_finished = 0; | |
200 | ||
201 | nfs = nfs_init_context(); | |
202 | if (nfs == NULL) { | |
203 | printf("failed to init context\n"); | |
204 | exit(10); | |
205 | } | |
206 | ||
207 | ret = nfs_mount_async(nfs, client.server, client.export, nfs_mount_cb, &client); | |
208 | if (ret != 0) { | |
209 | printf("Failed to start async nfs mount\n"); | |
210 | exit(10); | |
211 | } | |
212 | ||
213 | for (;;) { | |
214 | pfd.fd = nfs_get_fd(nfs); | |
215 | pfd.events = nfs_which_events(nfs); | |
216 | ||
217 | if (poll(&pfd, 1, -1) < 0) { | |
218 | printf("Poll failed"); | |
219 | exit(10); | |
220 | } | |
221 | if (nfs_service(nfs, pfd.revents) < 0) { | |
222 | printf("nfs_service failed\n"); | |
223 | break; | |
224 | } | |
225 | if (client.is_finished) { | |
226 | break; | |
227 | } | |
228 | } | |
229 | ||
230 | nfs_destroy_context(nfs); | |
231 | printf("nfsclient finished\n"); | |
232 | return 0; | |
233 | } |