Commit | Line | Data |
---|---|---|
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 | ||
bbed37ca RS |
18 | #define _FILE_OFFSET_BITS 64 |
19 | #define _GNU_SOURCE | |
20 | ||
84004dbf RS |
21 | /* Example program using the highlevel sync interface |
22 | */ | |
a8a1b858 M |
23 | #ifdef WIN32 |
24 | #include "win32_compat.h" | |
25 | #else | |
bbed37ca | 26 | #include <string.h> |
a8a1b858 M |
27 | #include <fcntl.h> |
28 | #include <sys/stat.h> | |
29 | #include <sys/statvfs.h> | |
30 | #endif | |
31 | ||
eecdc4f3 RS |
32 | |
33 | #if defined(WIN32) | |
eecdc4f3 RS |
34 | #pragma comment(lib, "ws2_32.lib") |
35 | WSADATA wsaData; | |
36 | #else | |
37 | #include <sys/statvfs.h> | |
38 | #include <unistd.h> | |
39 | #endif | |
40 | ||
84004dbf RS |
41 | #include <stdio.h> |
42 | #include <stdlib.h> | |
43 | #include <stdint.h> | |
bbed37ca | 44 | #include <inttypes.h> |
84004dbf RS |
45 | #include <sys/types.h> |
46 | #include <sys/stat.h> | |
84004dbf RS |
47 | #include <fcntl.h> |
48 | #include "libnfs.h" | |
49 | #include <rpc/rpc.h> /* for authunix_create() */ | |
bbed37ca | 50 | #include <popt.h> |
df5af25f RS |
51 | #include "libnfs-raw.h" |
52 | #include "libnfs-raw-mount.h" | |
84004dbf RS |
53 | |
54 | struct client { | |
55 | char *server; | |
56 | char *export; | |
57 | uint32_t mount_port; | |
58 | int is_finished; | |
59 | }; | |
60 | ||
61 | ||
bbed37ca | 62 | char buf[3*1024*1024+337]; |
e078d276 | 63 | |
bbed37ca RS |
64 | void print_usage(void) |
65 | { | |
66 | fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n"); | |
e078d276 | 67 | } |
68 | ||
bbed37ca RS |
69 | void print_help(void) |
70 | { | |
71 | fprintf(stderr, "Usage: nfsclient-sync [OPTION...] <url>\n"); | |
72 | fprintf(stderr, "\n"); | |
73 | fprintf(stderr, "Help options:\n"); | |
74 | fprintf(stderr, " -?, --help Show this help message\n"); | |
75 | fprintf(stderr, " --usage Display brief usage message\n"); | |
76 | fprintf(stderr, "\n"); | |
77 | fprintf(stderr, "NFS URL format : nfs://<server>/<export-path>\n"); | |
78 | fprintf(stderr, "\n"); | |
79 | fprintf(stderr, "<host> is either of:\n"); | |
80 | fprintf(stderr, " \"hostname\" nfs.example\n"); | |
81 | fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n"); | |
82 | fprintf(stderr, " \"ipv6-address\" [fce0::1]\n"); | |
83 | } | |
cdb19ec1 | 84 | |
bbed37ca | 85 | int main(int argc, char *argv[]) |
84004dbf | 86 | { |
bbed37ca RS |
87 | struct nfs_context *nfs = NULL; |
88 | int i, ret, res; | |
183451cf | 89 | uint64_t offset; |
84004dbf RS |
90 | struct client client; |
91 | struct stat st; | |
92 | struct nfsfh *nfsfh; | |
93 | struct nfsdir *nfsdir; | |
94 | struct nfsdirent *nfsdirent; | |
84004dbf | 95 | struct statvfs svfs; |
df5af25f | 96 | exports export, tmp; |
bbed37ca RS |
97 | int show_help = 0, show_usage = 0; |
98 | poptContext pc; | |
99 | const char **extra_argv; | |
100 | int extra_argc = 0; | |
101 | const char *url = NULL; | |
102 | char *server = NULL, *path = NULL, *strp; | |
103 | ||
104 | struct poptOption popt_options[] = { | |
105 | { "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL }, | |
106 | { "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL }, | |
107 | POPT_TABLEEND | |
108 | }; | |
df5af25f | 109 | |
eecdc4f3 RS |
110 | #if defined(WIN32) |
111 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { | |
112 | printf("Failed to start Winsock2\n"); | |
113 | exit(10); | |
114 | } | |
115 | #endif | |
116 | ||
bbed37ca RS |
117 | pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_POSIXMEHARDER); |
118 | if ((res = poptGetNextOpt(pc)) < -1) { | |
119 | fprintf(stderr, "Failed to parse option : %s %s\n", | |
120 | poptBadOption(pc, 0), poptStrerror(res)); | |
84004dbf RS |
121 | exit(10); |
122 | } | |
bbed37ca RS |
123 | extra_argv = poptGetArgs(pc); |
124 | if (extra_argv) { | |
125 | url = *extra_argv; | |
126 | extra_argv++; | |
127 | while (extra_argv[extra_argc]) { | |
128 | extra_argc++; | |
129 | } | |
84004dbf | 130 | } |
bbed37ca | 131 | poptFreeContext(pc); |
84004dbf | 132 | |
bbed37ca RS |
133 | if (show_help != 0) { |
134 | print_help(); | |
135 | exit(0); | |
84004dbf | 136 | } |
84004dbf | 137 | |
bbed37ca RS |
138 | if (show_usage != 0) { |
139 | print_usage(); | |
140 | exit(0); | |
84004dbf RS |
141 | } |
142 | ||
bbed37ca RS |
143 | if (url == NULL) { |
144 | fprintf(stderr, "No URL specified.\n"); | |
145 | print_usage(); | |
146 | exit(0); | |
84004dbf | 147 | } |
bbed37ca RS |
148 | |
149 | if (strncmp(url, "nfs://", 6)) { | |
150 | fprintf(stderr, "Invalid URL specified.\n"); | |
151 | print_usage(); | |
152 | exit(0); | |
84004dbf | 153 | } |
bbed37ca RS |
154 | |
155 | server = strdup(url + 6); | |
156 | if (server == NULL) { | |
157 | fprintf(stderr, "Failed to strdup server string\n"); | |
57187d21 RS |
158 | exit(10); |
159 | } | |
bbed37ca RS |
160 | if (server[0] == '/' || server[0] == '\0') { |
161 | fprintf(stderr, "Invalid server string.\n"); | |
162 | free(server); | |
57187d21 RS |
163 | exit(10); |
164 | } | |
bbed37ca RS |
165 | strp = strchr(server, '/'); |
166 | if (strp == NULL) { | |
167 | fprintf(stderr, "Invalid URL specified.\n"); | |
168 | print_usage(); | |
169 | free(server); | |
170 | exit(0); | |
57187d21 | 171 | } |
bbed37ca RS |
172 | path = strdup(strp); |
173 | if (path == NULL) { | |
174 | fprintf(stderr, "Failed to strdup server string\n"); | |
175 | free(server); | |
57187d21 RS |
176 | exit(10); |
177 | } | |
bbed37ca RS |
178 | if (path[0] != '/') { |
179 | fprintf(stderr, "Invalid path.\n"); | |
180 | free(server); | |
181 | free(path); | |
57187d21 RS |
182 | exit(10); |
183 | } | |
bbed37ca RS |
184 | *strp = 0; |
185 | ||
186 | client.server = server; | |
187 | client.export = path; | |
188 | client.is_finished = 0; | |
84004dbf | 189 | |
84004dbf | 190 | |
bbed37ca RS |
191 | nfs = nfs_init_context(); |
192 | if (nfs == NULL) { | |
193 | printf("failed to init context\n"); | |
194 | goto finished; | |
84004dbf | 195 | } |
84004dbf | 196 | |
bbed37ca | 197 | ret = nfs_mount(nfs, client.server, client.export); |
84004dbf | 198 | if (ret != 0) { |
bbed37ca RS |
199 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); |
200 | goto finished; | |
84004dbf | 201 | } |
84004dbf RS |
202 | |
203 | ||
bbed37ca | 204 | ret = nfs_opendir(nfs, "/", &nfsdir); |
84004dbf | 205 | if (ret != 0) { |
bbed37ca | 206 | printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs)); |
84004dbf RS |
207 | exit(10); |
208 | } | |
209 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
bbed37ca RS |
210 | char path[1024]; |
211 | ||
212 | if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) { | |
213 | continue; | |
57187d21 | 214 | } |
bbed37ca RS |
215 | |
216 | snprintf(path, 1024, "%s/%s", "/", nfsdirent->name); | |
217 | ret = nfs_stat(nfs, path, &st); | |
218 | if (ret != 0) { | |
219 | fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs)); | |
220 | continue; | |
57187d21 | 221 | } |
bbed37ca RS |
222 | |
223 | switch (st.st_mode & S_IFMT) { | |
224 | case S_IFLNK: | |
225 | case S_IFREG: | |
226 | printf("-"); | |
227 | break; | |
228 | case S_IFDIR: | |
229 | printf("d"); | |
230 | break; | |
231 | case S_IFCHR: | |
232 | printf("c"); | |
233 | break; | |
234 | case S_IFBLK: | |
235 | printf("b"); | |
236 | break; | |
57187d21 | 237 | } |
bbed37ca RS |
238 | printf("%c%c%c", |
239 | "-r"[!!(st.st_mode & S_IRUSR)], | |
240 | "-w"[!!(st.st_mode & S_IWUSR)], | |
241 | "-x"[!!(st.st_mode & S_IXUSR)] | |
242 | ); | |
243 | printf("%c%c%c", | |
244 | "-r"[!!(st.st_mode & S_IRGRP)], | |
245 | "-w"[!!(st.st_mode & S_IWGRP)], | |
246 | "-x"[!!(st.st_mode & S_IXGRP)] | |
247 | ); | |
248 | printf("%c%c%c", | |
249 | "-r"[!!(st.st_mode & S_IROTH)], | |
250 | "-w"[!!(st.st_mode & S_IWOTH)], | |
251 | "-x"[!!(st.st_mode & S_IXOTH)] | |
252 | ); | |
253 | printf(" %2d", st.st_nlink); | |
254 | printf(" %5d", st.st_uid); | |
255 | printf(" %5d", st.st_gid); | |
256 | printf(" %12" PRId64, st.st_size); | |
257 | ||
258 | printf(" %s\n", nfsdirent->name); | |
84004dbf RS |
259 | } |
260 | nfs_closedir(nfs, nfsdir); | |
261 | ||
262 | ||
bbed37ca RS |
263 | finished: |
264 | free(server); | |
265 | free(path); | |
266 | if (nfs != NULL) { | |
267 | nfs_destroy_context(nfs); | |
84004dbf | 268 | } |
84004dbf RS |
269 | return 0; |
270 | } | |
df5af25f | 271 |