Commit | Line | Data |
---|---|---|
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") |
34 | WSADATA 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 AM |
54 | #include <fcntl.h> |
55 | #include "libnfs.h" | |
dabf4152 AM |
56 | #include "libnfs-raw.h" |
57 | #include "libnfs-raw-mount.h" | |
58 | ||
59 | struct client { | |
60 | char *server; | |
61 | char *export; | |
62 | uint32_t mount_port; | |
63 | int is_finished; | |
64 | }; | |
65 | ||
66 | ||
ee872606 | 67 | char buf[3*1024*1024+337]; |
5670ec6e | 68 | |
ee872606 RRS |
69 | void print_usage(void) |
70 | { | |
71 | fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n"); | |
5670ec6e AM |
72 | } |
73 | ||
ee872606 | 74 | int main(int argc, char *argv[]) |
dabf4152 | 75 | { |
ee872606 RRS |
76 | struct nfs_context *nfs = NULL; |
77 | int i, ret, res; | |
5670ec6e | 78 | uint64_t offset; |
dabf4152 AM |
79 | struct client client; |
80 | struct stat st; | |
81 | struct nfsfh *nfsfh; | |
82 | struct nfsdir *nfsdir; | |
83 | struct nfsdirent *nfsdirent; | |
5670ec6e AM |
84 | struct statvfs svfs; |
85 | exports export, tmp; | |
ee872606 RRS |
86 | const char *url = NULL; |
87 | char *server = NULL, *path = NULL, *strp; | |
5670ec6e | 88 | |
ee872606 | 89 | #ifdef WIN32 |
5670ec6e AM |
90 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { |
91 | printf("Failed to start Winsock2\n"); | |
92 | exit(10); | |
93 | } | |
94 | #endif | |
95 | ||
ee872606 RRS |
96 | #ifdef AROS |
97 | aros_init_socket(); | |
98 | #endif | |
dabf4152 | 99 | |
ee872606 | 100 | url = argv[1]; |
dabf4152 | 101 | |
ee872606 RRS |
102 | if (url == NULL) { |
103 | fprintf(stderr, "No URL specified.\n"); | |
104 | print_usage(); | |
105 | exit(0); | |
dabf4152 | 106 | } |
dabf4152 | 107 | |
ee872606 RRS |
108 | if (strncmp(url, "nfs://", 6)) { |
109 | fprintf(stderr, "Invalid URL specified.\n"); | |
110 | print_usage(); | |
111 | exit(0); | |
dabf4152 AM |
112 | } |
113 | ||
ee872606 RRS |
114 | server = strdup(url + 6); |
115 | if (server == NULL) { | |
116 | fprintf(stderr, "Failed to strdup server string\n"); | |
dabf4152 AM |
117 | exit(10); |
118 | } | |
ee872606 RRS |
119 | if (server[0] == '/' || server[0] == '\0') { |
120 | fprintf(stderr, "Invalid server string.\n"); | |
121 | free(server); | |
dabf4152 AM |
122 | exit(10); |
123 | } | |
ee872606 RRS |
124 | strp = strchr(server, '/'); |
125 | if (strp == NULL) { | |
126 | fprintf(stderr, "Invalid URL specified.\n"); | |
127 | print_usage(); | |
128 | free(server); | |
129 | exit(0); | |
dabf4152 | 130 | } |
ee872606 RRS |
131 | path = strdup(strp); |
132 | if (path == NULL) { | |
133 | fprintf(stderr, "Failed to strdup server string\n"); | |
134 | free(server); | |
dabf4152 AM |
135 | exit(10); |
136 | } | |
ee872606 RRS |
137 | if (path[0] != '/') { |
138 | fprintf(stderr, "Invalid path.\n"); | |
139 | free(server); | |
140 | free(path); | |
dabf4152 AM |
141 | exit(10); |
142 | } | |
ee872606 RRS |
143 | *strp = 0; |
144 | ||
145 | client.server = server; | |
146 | client.export = path; | |
147 | client.is_finished = 0; | |
dabf4152 | 148 | |
dabf4152 | 149 | |
ee872606 RRS |
150 | nfs = nfs_init_context(); |
151 | if (nfs == NULL) { | |
152 | printf("failed to init context\n"); | |
153 | goto finished; | |
dabf4152 | 154 | } |
dabf4152 | 155 | |
ee872606 | 156 | ret = nfs_mount(nfs, client.server, client.export); |
dabf4152 | 157 | if (ret != 0) { |
ee872606 RRS |
158 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); |
159 | goto finished; | |
dabf4152 | 160 | } |
dabf4152 | 161 | |
dabf4152 | 162 | |
ee872606 | 163 | ret = nfs_opendir(nfs, "/", &nfsdir); |
dabf4152 | 164 | if (ret != 0) { |
ee872606 | 165 | printf("Failed to opendir(\"/\") %s\n", nfs_get_error(nfs)); |
dabf4152 AM |
166 | exit(10); |
167 | } | |
168 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
ee872606 RRS |
169 | char path[1024]; |
170 | ||
171 | if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) { | |
172 | continue; | |
dabf4152 | 173 | } |
ee872606 RRS |
174 | |
175 | sprintf(path, "%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; | |
dabf4152 | 180 | } |
ee872606 RRS |
181 | |
182 | switch (st.st_mode & S_IFMT) { | |
183 | #ifndef WIN32 | |
184 | case S_IFLNK: | |
185 | #endif | |
186 | case S_IFREG: | |
187 | printf("-"); | |
188 | break; | |
189 | case S_IFDIR: | |
190 | printf("d"); | |
191 | break; | |
192 | case S_IFCHR: | |
193 | printf("c"); | |
194 | break; | |
195 | case S_IFBLK: | |
196 | printf("b"); | |
197 | break; | |
dabf4152 | 198 | } |
ee872606 RRS |
199 | printf("%c%c%c", |
200 | "-r"[!!(st.st_mode & S_IRUSR)], | |
201 | "-w"[!!(st.st_mode & S_IWUSR)], | |
202 | "-x"[!!(st.st_mode & S_IXUSR)] | |
203 | ); | |
204 | printf("%c%c%c", | |
205 | "-r"[!!(st.st_mode & S_IRGRP)], | |
206 | "-w"[!!(st.st_mode & S_IWGRP)], | |
207 | "-x"[!!(st.st_mode & S_IXGRP)] | |
208 | ); | |
209 | printf("%c%c%c", | |
210 | "-r"[!!(st.st_mode & S_IROTH)], | |
211 | "-w"[!!(st.st_mode & S_IWOTH)], | |
212 | "-x"[!!(st.st_mode & S_IXOTH)] | |
213 | ); | |
214 | printf(" %2d", (int)st.st_nlink); | |
215 | printf(" %5d", st.st_uid); | |
216 | printf(" %5d", st.st_gid); | |
217 | printf(" %12" PRId64, st.st_size); | |
218 | ||
219 | printf(" %s\n", nfsdirent->name); | |
dabf4152 AM |
220 | } |
221 | nfs_closedir(nfs, nfsdir); | |
222 | ||
223 | ||
ee872606 RRS |
224 | finished: |
225 | free(server); | |
226 | free(path); | |
227 | if (nfs != NULL) { | |
228 | nfs_destroy_context(nfs); | |
dabf4152 | 229 | } |
dabf4152 AM |
230 | return 0; |
231 | } | |
232 |