File: nfsclient-sync.c

package info (click to toggle)
libnfs 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,928 kB
  • sloc: sh: 11,409; ansic: 7,313; makefile: 110
file content (310 lines) | stat: -rw-r--r-- 7,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* 
   Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

/* Example program using the highlevel sync interface
 */
#ifdef WIN32
#include "win32_compat.h"
#else
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#endif
 
#define SERVER "10.1.1.27"
#define EXPORT "/VIRTUAL"
#define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated"
#define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
#define NFSFILEW "/BOOKS/Classics/foo"
#define NFSDIR "/BOOKS/Classics/"

#define _GNU_SOURCE

#if defined(WIN32)
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#else
#include <sys/statvfs.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libnfs.h"
#include <rpc/rpc.h>            /* for authunix_create() */
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"

struct client {
       char *server;
       char *export;
       uint32_t mount_port;
       int is_finished;
};


void PrintServerList()
{
  struct nfs_server_list *srvrs;
  struct nfs_server_list *srv;

  srvrs = nfs_find_local_servers();

  for (srv=srvrs; srv; srv = srv->next)
  {
      printf("Found nfs server: %s\n", srv->addr);

  }
  free_nfs_srvr_list(srvrs);
}

char buf[3*1024*1024+337];

int main(int argc _U_, char *argv[] _U_)
{
	struct nfs_context *nfs;
	int i, ret;
	uint64_t offset;
	struct client client;
	struct stat st;
	struct nfsfh  *nfsfh;
	struct nfsdir *nfsdir;
	struct nfsdirent *nfsdirent;
	struct statvfs svfs;
	exports export, tmp;

#if defined(WIN32)
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		printf("Failed to start Winsock2\n");
		exit(10);
	}
#endif

	client.server = SERVER;
	client.export = EXPORT;
	client.is_finished = 0;

  PrintServerList();

	export = mount_getexports(SERVER);
	if (export != NULL) {
		printf("exports on server %s\n", SERVER);
		tmp = export;
		while (tmp != NULL) {
		      printf("Export: %s\n", tmp->ex_dir);
		      tmp = tmp->ex_next;
		}

		mount_free_export_list(export);
	} else {
		printf("no exports on server %s\n", SERVER);
	}	

	nfs = nfs_init_context();
	if (nfs == NULL) {
		printf("failed to init context\n");
		exit(10);
	}

	ret = nfs_mount(nfs, client.server, client.export);
	if (ret != 0) {
 		printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
		exit(10);
	}
	printf("mounted share successfully %s\n", nfs_get_error(nfs));


	ret = nfs_stat(nfs, NFSFILE, &st);
	if (ret != 0) {
		printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("Mode %04o\n", st.st_mode);
	printf("Size %d\n", (int)st.st_size);
	printf("Inode %04o\n", (int)st.st_ino);

	ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
	if (ret != 0) {
		printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}

#if 0
	ret = nfs_read(nfs, nfsfh, 16, buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	for (i=0;i<16;i++) {
		printf("%02x ", buf[i]&0xff);
	}
	printf("\n");
#endif
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	for (i=0;i<16;i++) {
		printf("%02x ", buf[i]&0xff);
	}
	printf("\n");
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);
	ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
	if (ret < 0) {
		printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("read %d bytes\n", ret);

	ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
	if (ret < 0) {
		printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("File position is %d\n", (int)offset);

	printf("seek to end of file\n");
	ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
	if (ret < 0) {
		printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("File position is %d\n", (int)offset);

	ret = nfs_fstat(nfs, nfsfh, &st);
	if (ret != 0) {
		printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	printf("Mode %04o\n", st.st_mode);
	printf("Size %d\n", (int)st.st_size);
	printf("Inode %04o\n", (int)st.st_ino);


	ret = nfs_close(nfs, nfsfh);
	if (ret < 0) {
		printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}

	ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
	if (ret != 0) {
		printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
		exit(10);
	}
	while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
	  char filename[1024];
		printf("Inode:%d Name:%s ", (int)nfsdirent->inode, nfsdirent->name);
		sprintf(filename, "%s/%s", NFSDIR, nfsdirent->name);
		ret = nfs_open(nfs, filename, O_RDONLY, &nfsfh);
		if (ret != 0) {
			printf("Failed to open(%s) %s\n", filename, nfs_get_error(nfs));
			exit(10);
		}
		ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
		if (ret < 0) {
			printf("Error reading file\n");
		}
		printf("Read %d bytes\n", ret);
		ret = nfs_close(nfs, nfsfh);
		if (ret < 0) {
			printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
			exit(10);
		}
	}
	nfs_closedir(nfs, nfsdir);


	ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
	if (ret != 0) {
		printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
		exit(10);
	}
	ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
	if (ret < 0) {
		printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
		exit(10);
	}
	ret = nfs_fsync(nfs, nfsfh);
	if (ret < 0) {
		printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
		exit(10);
	}
	ret = nfs_close(nfs, nfsfh);
	if (ret < 0) {
		printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
		exit(10);
	}


	ret = nfs_statvfs(nfs, NFSDIR, &svfs);
	if (ret < 0) {
		printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
		exit(10);
	}
	printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);


	ret = nfs_access(nfs, NFSFILE, R_OK);
	if (ret != 0) {
		printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
	}

	/* become root */
	nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));

	ret = nfs_link(nfs, NFSFILE, NFSFILER);
	if (ret != 0) {
		printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
	}


	nfs_destroy_context(nfs);
	printf("nfsclient finished\n");
	return 0;
}