docs: add a prebuilt manpage to the distribution
[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;
67a9f57e 35#define PRId64 "ll"
a8a1b858 36#else
67a9f57e 37#include <inttypes.h>
67336940 38#include <string.h>
a8a1b858 39#include <sys/stat.h>
a41dbfe2 40#ifndef AROS
a8a1b858
M
41#include <sys/statvfs.h>
42#endif
eecdc4f3
RS
43#endif
44
00748f36
RS
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48
84004dbf
RS
49#include <stdio.h>
50#include <stdlib.h>
51#include <stdint.h>
52#include <sys/types.h>
53#include <sys/stat.h>
84004dbf 54#include <fcntl.h>
763cd6e3 55#include "libnfs-zdr.h"
84004dbf 56#include "libnfs.h"
df5af25f
RS
57#include "libnfs-raw.h"
58#include "libnfs-raw-mount.h"
84004dbf
RS
59
60struct client {
61 char *server;
62 char *export;
63 uint32_t mount_port;
64 int is_finished;
65};
66
67
67336940 68char buf[3*1024*1024+337];
e078d276 69
67336940
RS
70void print_usage(void)
71{
72 fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
e078d276 73}
74
67336940 75int main(int argc, char *argv[])
84004dbf 76{
67336940
RS
77 struct nfs_context *nfs = NULL;
78 int i, ret, res;
183451cf 79 uint64_t offset;
84004dbf
RS
80 struct client client;
81 struct stat st;
82 struct nfsfh *nfsfh;
83 struct nfsdir *nfsdir;
84 struct nfsdirent *nfsdirent;
84004dbf 85 struct statvfs svfs;
df5af25f 86 exports export, tmp;
67336940
RS
87 const char *url = NULL;
88 char *server = NULL, *path = NULL, *strp;
89
a41dbfe2 90#ifdef WIN32
eecdc4f3
RS
91 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
92 printf("Failed to start Winsock2\n");
93 exit(10);
94 }
95#endif
96
a41dbfe2
RS
97#ifdef AROS
98 aros_init_socket();
99#endif
84004dbf 100
a41dbfe2 101 url = argv[1];
84004dbf 102
67336940
RS
103 if (url == NULL) {
104 fprintf(stderr, "No URL specified.\n");
105 print_usage();
106 exit(0);
84004dbf 107 }
67336940
RS
108
109 if (strncmp(url, "nfs://", 6)) {
110 fprintf(stderr, "Invalid URL specified.\n");
111 print_usage();
112 exit(0);
84004dbf 113 }
67336940
RS
114
115 server = strdup(url + 6);
116 if (server == NULL) {
117 fprintf(stderr, "Failed to strdup server string\n");
57187d21
RS
118 exit(10);
119 }
67336940
RS
120 if (server[0] == '/' || server[0] == '\0') {
121 fprintf(stderr, "Invalid server string.\n");
122 free(server);
57187d21
RS
123 exit(10);
124 }
67336940
RS
125 strp = strchr(server, '/');
126 if (strp == NULL) {
127 fprintf(stderr, "Invalid URL specified.\n");
128 print_usage();
129 free(server);
130 exit(0);
57187d21 131 }
67336940
RS
132 path = strdup(strp);
133 if (path == NULL) {
134 fprintf(stderr, "Failed to strdup server string\n");
135 free(server);
57187d21
RS
136 exit(10);
137 }
67336940
RS
138 if (path[0] != '/') {
139 fprintf(stderr, "Invalid path.\n");
140 free(server);
141 free(path);
57187d21
RS
142 exit(10);
143 }
67336940
RS
144 *strp = 0;
145
146 client.server = server;
147 client.export = path;
148 client.is_finished = 0;
84004dbf 149
84004dbf 150
67336940
RS
151 nfs = nfs_init_context();
152 if (nfs == NULL) {
153 printf("failed to init context\n");
154 goto finished;
84004dbf 155 }
84004dbf 156
67336940 157 ret = nfs_mount(nfs, client.server, client.export);
84004dbf 158 if (ret != 0) {
67336940
RS
159 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
160 goto finished;
84004dbf 161 }
84004dbf
RS
162
163
67336940 164 ret = nfs_opendir(nfs, "/", &nfsdir);
84004dbf 165 if (ret != 0) {
dc3ed8c3 166 printf("Failed to opendir(\"/\") %s\n", nfs_get_error(nfs));
84004dbf
RS
167 exit(10);
168 }
169 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
67336940
RS
170 char path[1024];
171
172 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
173 continue;
57187d21 174 }
67336940 175
67a9f57e 176 sprintf(path, "%s/%s", "/", nfsdirent->name);
67336940
RS
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;
57187d21 181 }
67336940
RS
182
183 switch (st.st_mode & S_IFMT) {
67a9f57e 184#ifndef WIN32
67336940 185 case S_IFLNK:
67a9f57e 186#endif
67336940
RS
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;
57187d21 199 }
67336940
RS
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 );
dc3ed8c3 215 printf(" %2d", (int)st.st_nlink);
67336940
RS
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);
84004dbf
RS
221 }
222 nfs_closedir(nfs, nfsdir);
223
224
67336940
RS
225finished:
226 free(server);
227 free(path);
228 if (nfs != NULL) {
229 nfs_destroy_context(nfs);
84004dbf 230 }
84004dbf
RS
231 return 0;
232}
df5af25f 233