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