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