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.
21 #define SERVER "10.1.1.27"
22 #define EXPORT "/VIRTUAL"
23 #define NFSFILE "/BOOKS/Classics/Dracula.djvu"
24 #define NFSDIR "/BOOKS/Classics/"
29 #include <sys/types.h>
35 #include "libnfs-raw.h"
36 #include "libnfs-raw-mount.h"
38 struct rpc_context
*mount_context
;
48 void mount_export_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
50 struct client
*client
= private_data
;
51 exports export
= *(exports
*)data
;
54 printf("MOUNT/EXPORT failed with \"%s\"\n", rpc_get_error(mount_context
));
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
;
66 client
->is_finished
= 1;
69 void nfs_opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
71 struct client
*client
= private_data
;
72 struct nfsdir
*nfsdir
= data
;
73 struct nfsdirent
*nfsdirent
;
76 printf("opendir failed with \"%s\"\n", (char *)data
);
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
);
84 nfs_closedir(nfs
, nfsdir
);
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");
93 void nfs_close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
95 struct client
*client
= private_data
;
98 printf("close failed with \"%s\"\n", (char *)data
);
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");
110 void nfs_fstat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
112 struct client
*client
= private_data
;
116 printf("fstat call failed with \"%s\"\n", (char *)data
);
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
);
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");
133 void nfs_read_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
135 struct client
*client
= private_data
;
140 printf("read failed with \"%s\"\n", (char *)data
);
144 printf("read successful with %d bytes of data\n", status
);
147 printf("%02x ", read_data
[i
]&0xff);
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");
157 void nfs_open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
159 struct client
*client
= private_data
;
163 printf("open call failed with \"%s\"\n", (char *)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");
177 void nfs_stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
179 struct client
*client
= private_data
;
183 printf("stat call failed with \"%s\"\n", (char *)data
);
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
);
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");
200 void nfs_mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
202 struct client
*client
= private_data
;
205 printf("mount/mnt call failed with \"%s\"\n", (char *)data
);
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");
219 int main(int argc _U_
, char *argv
[] _U_
)
221 struct nfs_context
*nfs
;
223 struct client client
;
224 struct pollfd pfds
[2]; /* nfs:0 mount:1 */
226 client
.server
= SERVER
;
227 client
.export
= EXPORT
;
228 client
.is_finished
= 0;
230 nfs
= nfs_init_context();
232 printf("failed to init context\n");
236 ret
= nfs_mount_async(nfs
, client
.server
, client
.export
, nfs_mount_cb
, &client
);
238 printf("Failed to start async nfs mount\n");
245 pfds
[0].fd
= nfs_get_fd(nfs
);
246 pfds
[0].events
= nfs_which_events(nfs
);
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
);
254 if (poll(&pfds
[0], 2, -1) < 0) {
255 printf("Poll failed");
258 if (mount_context
!= NULL
) {
259 if (rpc_service(mount_context
, pfds
[1].revents
) < 0) {
260 printf("rpc_service failed\n");
264 if (nfs_service(nfs
, pfds
[0].revents
) < 0) {
265 printf("nfs_service failed\n");
268 if (client
.is_finished
) {
273 nfs_destroy_context(nfs
);
274 if (mount_context
!= NULL
) {
275 rpc_destroy_context(mount_context
);
276 mount_context
= NULL
;
278 printf("nfsclient finished\n");