4bd2c0b942714b26d4c72ee8305842ecdec1b5f2
[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 #define _FILE_OFFSET_BITS 64
19 #define _GNU_SOURCE
20
21 /* Example program using the highlevel sync interface
22 */
23 #ifdef WIN32
24 #include "win32_compat.h"
25 #pragma comment(lib, "ws2_32.lib")
26 WSADATA wsaData;
27 #else
28 #include <string.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #ifndef AROS
33 #include <sys/statvfs.h>
34 #endif
35 #endif
36
37 #ifdef AROS
38 #include "aros_compat.h"
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include "libnfs-zdr.h"
49 #include "libnfs.h"
50 #include "libnfs-raw.h"
51 #include "libnfs-raw-mount.h"
52
53 struct client {
54 char *server;
55 char *export;
56 uint32_t mount_port;
57 int is_finished;
58 };
59
60
61 char buf[3*1024*1024+337];
62
63 void print_usage(void)
64 {
65 fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
66 }
67
68 int main(int argc, char *argv[])
69 {
70 struct nfs_context *nfs = NULL;
71 int i, ret, res;
72 uint64_t offset;
73 struct client client;
74 struct stat st;
75 struct nfsfh *nfsfh;
76 struct nfsdir *nfsdir;
77 struct nfsdirent *nfsdirent;
78 struct statvfs svfs;
79 exports export, tmp;
80 const char *url = NULL;
81 char *server = NULL, *path = NULL, *strp;
82
83 #ifdef WIN32
84 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
85 printf("Failed to start Winsock2\n");
86 exit(10);
87 }
88 #endif
89
90 #ifdef AROS
91 aros_init_socket();
92 #endif
93
94 url = argv[1];
95
96 if (url == NULL) {
97 fprintf(stderr, "No URL specified.\n");
98 print_usage();
99 exit(0);
100 }
101
102 if (strncmp(url, "nfs://", 6)) {
103 fprintf(stderr, "Invalid URL specified.\n");
104 print_usage();
105 exit(0);
106 }
107
108 server = strdup(url + 6);
109 if (server == NULL) {
110 fprintf(stderr, "Failed to strdup server string\n");
111 exit(10);
112 }
113 if (server[0] == '/' || server[0] == '\0') {
114 fprintf(stderr, "Invalid server string.\n");
115 free(server);
116 exit(10);
117 }
118 strp = strchr(server, '/');
119 if (strp == NULL) {
120 fprintf(stderr, "Invalid URL specified.\n");
121 print_usage();
122 free(server);
123 exit(0);
124 }
125 path = strdup(strp);
126 if (path == NULL) {
127 fprintf(stderr, "Failed to strdup server string\n");
128 free(server);
129 exit(10);
130 }
131 if (path[0] != '/') {
132 fprintf(stderr, "Invalid path.\n");
133 free(server);
134 free(path);
135 exit(10);
136 }
137 *strp = 0;
138
139 client.server = server;
140 client.export = path;
141 client.is_finished = 0;
142
143
144 nfs = nfs_init_context();
145 if (nfs == NULL) {
146 printf("failed to init context\n");
147 goto finished;
148 }
149
150 ret = nfs_mount(nfs, client.server, client.export);
151 if (ret != 0) {
152 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
153 goto finished;
154 }
155
156
157 ret = nfs_opendir(nfs, "/", &nfsdir);
158 if (ret != 0) {
159 printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs));
160 exit(10);
161 }
162 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
163 char path[1024];
164
165 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
166 continue;
167 }
168
169 snprintf(path, 1024, "%s/%s", "/", nfsdirent->name);
170 ret = nfs_stat(nfs, path, &st);
171 if (ret != 0) {
172 fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
173 continue;
174 }
175
176 switch (st.st_mode & S_IFMT) {
177 case S_IFLNK:
178 case S_IFREG:
179 printf("-");
180 break;
181 case S_IFDIR:
182 printf("d");
183 break;
184 case S_IFCHR:
185 printf("c");
186 break;
187 case S_IFBLK:
188 printf("b");
189 break;
190 }
191 printf("%c%c%c",
192 "-r"[!!(st.st_mode & S_IRUSR)],
193 "-w"[!!(st.st_mode & S_IWUSR)],
194 "-x"[!!(st.st_mode & S_IXUSR)]
195 );
196 printf("%c%c%c",
197 "-r"[!!(st.st_mode & S_IRGRP)],
198 "-w"[!!(st.st_mode & S_IWGRP)],
199 "-x"[!!(st.st_mode & S_IXGRP)]
200 );
201 printf("%c%c%c",
202 "-r"[!!(st.st_mode & S_IROTH)],
203 "-w"[!!(st.st_mode & S_IWOTH)],
204 "-x"[!!(st.st_mode & S_IXOTH)]
205 );
206 printf(" %2d", st.st_nlink);
207 printf(" %5d", st.st_uid);
208 printf(" %5d", st.st_gid);
209 printf(" %12" PRId64, st.st_size);
210
211 printf(" %s\n", nfsdirent->name);
212 }
213 nfs_closedir(nfs, nfsdir);
214
215
216 finished:
217 free(server);
218 free(path);
219 if (nfs != NULL) {
220 nfs_destroy_context(nfs);
221 }
222 return 0;
223 }
224