2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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.
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.
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/>.
18 #define _FILE_OFFSET_BITS 64
21 /* Example program using the highlevel sync interface
24 #include "win32_compat.h"
29 #include <sys/statvfs.h>
34 #pragma comment(lib, "ws2_32.lib")
37 #include <sys/statvfs.h>
45 #include <sys/types.h>
48 #include "libnfs-zdr.h"
50 #include <rpc/rpc.h> /* for authunix_create() */
52 #include "libnfs-raw.h"
53 #include "libnfs-raw-mount.h"
63 char buf
[3*1024*1024+337];
65 void print_usage(void)
67 fprintf(stderr
, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
72 fprintf(stderr
, "Usage: nfsclient-sync [OPTION...] <url>\n");
73 fprintf(stderr
, "\n");
74 fprintf(stderr
, "Help options:\n");
75 fprintf(stderr
, " -?, --help Show this help message\n");
76 fprintf(stderr
, " --usage Display brief usage message\n");
77 fprintf(stderr
, "\n");
78 fprintf(stderr
, "NFS URL format : nfs://<server>/<export-path>\n");
79 fprintf(stderr
, "\n");
80 fprintf(stderr
, "<host> is either of:\n");
81 fprintf(stderr
, " \"hostname\" nfs.example\n");
82 fprintf(stderr
, " \"ipv4-address\" 10.1.1.27\n");
83 fprintf(stderr
, " \"ipv6-address\" [fce0::1]\n");
86 int main(int argc
, char *argv
[])
88 struct nfs_context
*nfs
= NULL
;
94 struct nfsdir
*nfsdir
;
95 struct nfsdirent
*nfsdirent
;
98 int show_help
= 0, show_usage
= 0;
100 const char **extra_argv
;
102 const char *url
= NULL
;
103 char *server
= NULL
, *path
= NULL
, *strp
;
105 struct poptOption popt_options
[] = {
106 { "help", '?', POPT_ARG_NONE
, &show_help
, 0, "Show this help message", NULL
},
107 { "usage", 0, POPT_ARG_NONE
, &show_usage
, 0, "Display brief usage message", NULL
},
112 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0) {
113 printf("Failed to start Winsock2\n");
118 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_POSIXMEHARDER
);
119 if ((res
= poptGetNextOpt(pc
)) < -1) {
120 fprintf(stderr
, "Failed to parse option : %s %s\n",
121 poptBadOption(pc
, 0), poptStrerror(res
));
124 extra_argv
= poptGetArgs(pc
);
128 while (extra_argv
[extra_argc
]) {
134 if (show_help
!= 0) {
139 if (show_usage
!= 0) {
145 fprintf(stderr
, "No URL specified.\n");
150 if (strncmp(url
, "nfs://", 6)) {
151 fprintf(stderr
, "Invalid URL specified.\n");
156 server
= strdup(url
+ 6);
157 if (server
== NULL
) {
158 fprintf(stderr
, "Failed to strdup server string\n");
161 if (server
[0] == '/' || server
[0] == '\0') {
162 fprintf(stderr
, "Invalid server string.\n");
166 strp
= strchr(server
, '/');
168 fprintf(stderr
, "Invalid URL specified.\n");
175 fprintf(stderr
, "Failed to strdup server string\n");
179 if (path
[0] != '/') {
180 fprintf(stderr
, "Invalid path.\n");
187 client
.server
= server
;
188 client
.export
= path
;
189 client
.is_finished
= 0;
192 nfs
= nfs_init_context();
194 printf("failed to init context\n");
198 ret
= nfs_mount(nfs
, client
.server
, client
.export
);
200 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
205 ret
= nfs_opendir(nfs
, "/", &nfsdir
);
207 printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs
));
210 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
213 if (!strcmp(nfsdirent
->name
, ".") || !strcmp(nfsdirent
->name
, "..")) {
217 snprintf(path
, 1024, "%s/%s", "/", nfsdirent
->name
);
218 ret
= nfs_stat(nfs
, path
, &st
);
220 fprintf(stderr
, "Failed to stat(%s) %s\n", path
, nfs_get_error(nfs
));
224 switch (st
.st_mode
& S_IFMT
) {
240 "-r"[!!(st
.st_mode
& S_IRUSR
)],
241 "-w"[!!(st
.st_mode
& S_IWUSR
)],
242 "-x"[!!(st
.st_mode
& S_IXUSR
)]
245 "-r"[!!(st
.st_mode
& S_IRGRP
)],
246 "-w"[!!(st
.st_mode
& S_IWGRP
)],
247 "-x"[!!(st
.st_mode
& S_IXGRP
)]
250 "-r"[!!(st
.st_mode
& S_IROTH
)],
251 "-w"[!!(st
.st_mode
& S_IWOTH
)],
252 "-x"[!!(st
.st_mode
& S_IXOTH
)]
254 printf(" %2d", st
.st_nlink
);
255 printf(" %5d", st
.st_uid
);
256 printf(" %5d", st
.st_gid
);
257 printf(" %12" PRId64
, st
.st_size
);
259 printf(" %s\n", nfsdirent
->name
);
261 nfs_closedir(nfs
, nfsdir
);
268 nfs_destroy_context(nfs
);