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>
34 #include <ccan/nfs/nfs.h>
44 void nfs_opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
46 struct client *client = private_data;
47 struct nfsdir *nfsdir = data;
48 struct nfsdirent *nfsdirent;
51 printf("opendir failed with \"%s\"\n", (char *)data);
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);
59 nfs_closedir(nfs, nfsdir);
61 client->is_finished = 1;
64 void nfs_close_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
66 struct client *client = private_data;
69 printf("close failed with \"%s\"\n", (char *)data);
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");
81 void nfs_fstat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
83 struct client *client = private_data;
87 printf("fstat call failed with \"%s\"\n", (char *)data);
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);
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");
104 void nfs_read_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
106 struct client *client = private_data;
111 printf("read failed with \"%s\"\n", (char *)data);
115 printf("read successful with %d bytes of data\n", status);
118 printf("%02x ", read_data[i]&0xff);
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");
128 void nfs_open_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
130 struct client *client = private_data;
134 printf("open call failed with \"%s\"\n", (char *)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");
148 void nfs_stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
150 struct client *client = private_data;
154 printf("stat call failed with \"%s\"\n", (char *)data);
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);
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");
171 void nfs_mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
173 struct client *client = private_data;
176 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
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");
190 int main(int argc, char *argv[])
192 struct nfs_context *nfs;
195 struct client client;
197 client.server = SERVER;
198 client.export = EXPORT;
199 client.is_finished = 0;
201 nfs = nfs_init_context();
203 printf("failed to init context\n");
207 ret = nfs_mount_async(nfs, client.server, client.export, nfs_mount_cb, &client);
209 printf("Failed to start async nfs mount\n");
214 pfd.fd = nfs_get_fd(nfs);
215 pfd.events = nfs_which_events(nfs);
217 if (poll(&pfd, 1, -1) < 0) {
218 printf("Poll failed");
221 if (nfs_service(nfs, pfd.revents) < 0) {
222 printf("nfs_service failed\n");
225 if (client.is_finished) {
230 nfs_destroy_context(nfs);
231 printf("nfsclient finished\n");