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