WIN32: More ifdef cleanups
[deb_libnfs.git] / examples / nfsclient-sync.c
CommitLineData
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
67336940
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"
a41dbfe2
RS
25#pragma comment(lib, "ws2_32.lib")
26WSADATA wsaData;
a8a1b858 27#else
67336940 28#include <string.h>
a8a1b858
M
29#include <fcntl.h>
30#include <sys/stat.h>
a41dbfe2
RS
31#include <unistd.h>
32#ifndef AROS
a8a1b858
M
33#include <sys/statvfs.h>
34#endif
eecdc4f3
RS
35#endif
36
a41dbfe2
RS
37#ifdef AROS
38#include "aros_compat.h"
39#endif
40
84004dbf
RS
41#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
67336940 44#include <inttypes.h>
84004dbf
RS
45#include <sys/types.h>
46#include <sys/stat.h>
84004dbf 47#include <fcntl.h>
763cd6e3 48#include "libnfs-zdr.h"
84004dbf 49#include "libnfs.h"
df5af25f
RS
50#include "libnfs-raw.h"
51#include "libnfs-raw-mount.h"
84004dbf
RS
52
53struct client {
54 char *server;
55 char *export;
56 uint32_t mount_port;
57 int is_finished;
58};
59
60
67336940 61char buf[3*1024*1024+337];
e078d276 62
67336940
RS
63void print_usage(void)
64{
65 fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
e078d276 66}
67
67336940 68int main(int argc, char *argv[])
84004dbf 69{
67336940
RS
70 struct nfs_context *nfs = NULL;
71 int i, ret, res;
183451cf 72 uint64_t offset;
84004dbf
RS
73 struct client client;
74 struct stat st;
75 struct nfsfh *nfsfh;
76 struct nfsdir *nfsdir;
77 struct nfsdirent *nfsdirent;
84004dbf 78 struct statvfs svfs;
df5af25f 79 exports export, tmp;
67336940
RS
80 const char *url = NULL;
81 char *server = NULL, *path = NULL, *strp;
82
a41dbfe2 83#ifdef WIN32
eecdc4f3
RS
84 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
85 printf("Failed to start Winsock2\n");
86 exit(10);
87 }
88#endif
89
a41dbfe2
RS
90#ifdef AROS
91 aros_init_socket();
92#endif
84004dbf 93
a41dbfe2 94 url = argv[1];
84004dbf 95
67336940
RS
96 if (url == NULL) {
97 fprintf(stderr, "No URL specified.\n");
98 print_usage();
99 exit(0);
84004dbf 100 }
67336940
RS
101
102 if (strncmp(url, "nfs://", 6)) {
103 fprintf(stderr, "Invalid URL specified.\n");
104 print_usage();
105 exit(0);
84004dbf 106 }
67336940
RS
107
108 server = strdup(url + 6);
109 if (server == NULL) {
110 fprintf(stderr, "Failed to strdup server string\n");
57187d21
RS
111 exit(10);
112 }
67336940
RS
113 if (server[0] == '/' || server[0] == '\0') {
114 fprintf(stderr, "Invalid server string.\n");
115 free(server);
57187d21
RS
116 exit(10);
117 }
67336940
RS
118 strp = strchr(server, '/');
119 if (strp == NULL) {
120 fprintf(stderr, "Invalid URL specified.\n");
121 print_usage();
122 free(server);
123 exit(0);
57187d21 124 }
67336940
RS
125 path = strdup(strp);
126 if (path == NULL) {
127 fprintf(stderr, "Failed to strdup server string\n");
128 free(server);
57187d21
RS
129 exit(10);
130 }
67336940
RS
131 if (path[0] != '/') {
132 fprintf(stderr, "Invalid path.\n");
133 free(server);
134 free(path);
57187d21
RS
135 exit(10);
136 }
67336940
RS
137 *strp = 0;
138
139 client.server = server;
140 client.export = path;
141 client.is_finished = 0;
84004dbf 142
84004dbf 143
67336940
RS
144 nfs = nfs_init_context();
145 if (nfs == NULL) {
146 printf("failed to init context\n");
147 goto finished;
84004dbf 148 }
84004dbf 149
67336940 150 ret = nfs_mount(nfs, client.server, client.export);
84004dbf 151 if (ret != 0) {
67336940
RS
152 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
153 goto finished;
84004dbf 154 }
84004dbf
RS
155
156
67336940 157 ret = nfs_opendir(nfs, "/", &nfsdir);
84004dbf 158 if (ret != 0) {
67336940 159 printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs));
84004dbf
RS
160 exit(10);
161 }
162 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
67336940
RS
163 char path[1024];
164
165 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
166 continue;
57187d21 167 }
67336940
RS
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;
57187d21 174 }
67336940
RS
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;
57187d21 190 }
67336940
RS
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);
84004dbf
RS
212 }
213 nfs_closedir(nfs, nfsdir);
214
215
67336940
RS
216finished:
217 free(server);
218 free(path);
219 if (nfs != NULL) {
220 nfs_destroy_context(nfs);
84004dbf 221 }
84004dbf
RS
222 return 0;
223}
df5af25f 224