Imported Upstream version 1.2.0
[deb_libnfs.git] / examples / nfsclient-sync.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 sync interface
19 */
20 #ifdef WIN32
21 #include "win32_compat.h"
22 #else
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/statvfs.h>
26 #endif
27
28 #define SERVER "10.1.1.27"
29 #define EXPORT "/VIRTUAL"
30 #define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated"
31 #define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
32 #define NFSFILEW "/BOOKS/Classics/foo"
33 #define NFSDIR "/BOOKS/Classics/"
34
35 #define _GNU_SOURCE
36
37 #if defined(WIN32)
38 #pragma comment(lib, "ws2_32.lib")
39 WSADATA wsaData;
40 #else
41 #include <sys/statvfs.h>
42 #include <unistd.h>
43 #endif
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdint.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include "libnfs.h"
52 #include <rpc/rpc.h> /* for authunix_create() */
53 #include "libnfs-raw.h"
54 #include "libnfs-raw-mount.h"
55
56 struct client {
57 char *server;
58 char *export;
59 uint32_t mount_port;
60 int is_finished;
61 };
62
63
64 void PrintServerList()
65 {
66 struct nfs_server_list *srvrs;
67 struct nfs_server_list *srv;
68
69 srvrs = nfs_find_local_servers();
70
71 for (srv=srvrs; srv; srv = srv->next)
72 {
73 printf("Found nfs server: %s\n", srv->addr);
74
75 }
76 free_nfs_srvr_list(srvrs);
77 }
78
79 char buf[3*1024*1024+337];
80
81 int main(int argc _U_, char *argv[] _U_)
82 {
83 struct nfs_context *nfs;
84 int i, ret;
85 uint64_t offset;
86 struct client client;
87 struct stat st;
88 struct nfsfh *nfsfh;
89 struct nfsdir *nfsdir;
90 struct nfsdirent *nfsdirent;
91 struct statvfs svfs;
92 exports export, tmp;
93
94 #if defined(WIN32)
95 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
96 printf("Failed to start Winsock2\n");
97 exit(10);
98 }
99 #endif
100
101 client.server = SERVER;
102 client.export = EXPORT;
103 client.is_finished = 0;
104
105 PrintServerList();
106
107 export = mount_getexports(SERVER);
108 if (export != NULL) {
109 printf("exports on server %s\n", SERVER);
110 tmp = export;
111 while (tmp != NULL) {
112 printf("Export: %s\n", tmp->ex_dir);
113 tmp = tmp->ex_next;
114 }
115
116 mount_free_export_list(export);
117 } else {
118 printf("no exports on server %s\n", SERVER);
119 }
120
121 nfs = nfs_init_context();
122 if (nfs == NULL) {
123 printf("failed to init context\n");
124 exit(10);
125 }
126
127 ret = nfs_mount(nfs, client.server, client.export);
128 if (ret != 0) {
129 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
130 exit(10);
131 }
132 printf("mounted share successfully %s\n", nfs_get_error(nfs));
133
134
135 ret = nfs_stat(nfs, NFSFILE, &st);
136 if (ret != 0) {
137 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
138 exit(10);
139 }
140 printf("Mode %04o\n", st.st_mode);
141 printf("Size %d\n", (int)st.st_size);
142 printf("Inode %04o\n", (int)st.st_ino);
143
144 ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
145 if (ret != 0) {
146 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
147 exit(10);
148 }
149
150 #if 0
151 ret = nfs_read(nfs, nfsfh, 16, buf);
152 if (ret < 0) {
153 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
154 exit(10);
155 }
156 printf("read %d bytes\n", ret);
157 for (i=0;i<16;i++) {
158 printf("%02x ", buf[i]&0xff);
159 }
160 printf("\n");
161 #endif
162 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
163 if (ret < 0) {
164 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
165 exit(10);
166 }
167 printf("read %d bytes\n", ret);
168 for (i=0;i<16;i++) {
169 printf("%02x ", buf[i]&0xff);
170 }
171 printf("\n");
172 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
173 if (ret < 0) {
174 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
175 exit(10);
176 }
177 printf("read %d bytes\n", ret);
178 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
179 if (ret < 0) {
180 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
181 exit(10);
182 }
183 printf("read %d bytes\n", ret);
184 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
185 if (ret < 0) {
186 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
187 exit(10);
188 }
189 printf("read %d bytes\n", ret);
190 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
191 if (ret < 0) {
192 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
193 exit(10);
194 }
195 printf("read %d bytes\n", ret);
196 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
197 if (ret < 0) {
198 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
199 exit(10);
200 }
201 printf("read %d bytes\n", ret);
202
203 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
204 if (ret < 0) {
205 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
206 exit(10);
207 }
208 printf("File position is %d\n", (int)offset);
209
210 printf("seek to end of file\n");
211 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
212 if (ret < 0) {
213 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
214 exit(10);
215 }
216 printf("File position is %d\n", (int)offset);
217
218 ret = nfs_fstat(nfs, nfsfh, &st);
219 if (ret != 0) {
220 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
221 exit(10);
222 }
223 printf("Mode %04o\n", st.st_mode);
224 printf("Size %d\n", (int)st.st_size);
225 printf("Inode %04o\n", (int)st.st_ino);
226
227
228 ret = nfs_close(nfs, nfsfh);
229 if (ret < 0) {
230 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
231 exit(10);
232 }
233
234 ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
235 if (ret != 0) {
236 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
237 exit(10);
238 }
239 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
240 char filename[1024];
241 printf("Inode:%d Name:%s ", (int)nfsdirent->inode, nfsdirent->name);
242 sprintf(filename, "%s/%s", NFSDIR, nfsdirent->name);
243 ret = nfs_open(nfs, filename, O_RDONLY, &nfsfh);
244 if (ret != 0) {
245 printf("Failed to open(%s) %s\n", filename, nfs_get_error(nfs));
246 exit(10);
247 }
248 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
249 if (ret < 0) {
250 printf("Error reading file\n");
251 }
252 printf("Read %d bytes\n", ret);
253 ret = nfs_close(nfs, nfsfh);
254 if (ret < 0) {
255 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
256 exit(10);
257 }
258 }
259 nfs_closedir(nfs, nfsdir);
260
261
262 ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
263 if (ret != 0) {
264 printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
265 exit(10);
266 }
267 ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
268 if (ret < 0) {
269 printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
270 exit(10);
271 }
272 ret = nfs_fsync(nfs, nfsfh);
273 if (ret < 0) {
274 printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
275 exit(10);
276 }
277 ret = nfs_close(nfs, nfsfh);
278 if (ret < 0) {
279 printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
280 exit(10);
281 }
282
283
284 ret = nfs_statvfs(nfs, NFSDIR, &svfs);
285 if (ret < 0) {
286 printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
287 exit(10);
288 }
289 printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
290
291
292 ret = nfs_access(nfs, NFSFILE, R_OK);
293 if (ret != 0) {
294 printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
295 }
296
297 /* become root */
298 nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
299
300 ret = nfs_link(nfs, NFSFILE, NFSFILER);
301 if (ret != 0) {
302 printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
303 }
304
305
306 nfs_destroy_context(nfs);
307 printf("nfsclient finished\n");
308 return 0;
309 }
310