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