Commit | Line | Data |
---|---|---|
a6bc1707 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 | ||
18 | #define _FILE_OFFSET_BITS 64 | |
19 | #define _GNU_SOURCE | |
20 | ||
21 | #ifdef HAVE_CONFIG_H | |
22 | #include "config.h" | |
23 | #endif | |
24 | ||
25 | #ifdef AROS | |
26 | #include "aros_compat.h" | |
27 | #endif | |
28 | ||
29 | #ifdef WIN32 | |
30 | #include "win32_compat.h" | |
31 | #pragma comment(lib, "ws2_32.lib") | |
32 | WSADATA wsaData; | |
67a9f57e | 33 | #define PRId64 "ll" |
a6bc1707 | 34 | #else |
67a9f57e | 35 | #include <inttypes.h> |
a6bc1707 RS |
36 | #include <string.h> |
37 | #include <sys/stat.h> | |
1dc9ea0d | 38 | #include <sys/statvfs.h> |
a6bc1707 RS |
39 | #ifndef AROS |
40 | #include <sys/statvfs.h> | |
41 | #endif | |
42 | #endif | |
43 | ||
44 | #ifdef HAVE_UNISTD_H | |
45 | #include <unistd.h> | |
46 | #endif | |
47 | ||
48 | #include <stdio.h> | |
49 | #include <stdlib.h> | |
50 | #include <stdint.h> | |
a6bc1707 RS |
51 | #include <sys/types.h> |
52 | #include <sys/stat.h> | |
53 | #include <fcntl.h> | |
54 | #include "libnfs-zdr.h" | |
55 | #include "libnfs.h" | |
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 | ||
1dc9ea0d PL |
66 | int recursive = 0, summary = 0; |
67 | ||
a6bc1707 RS |
68 | void print_usage(void) |
69 | { | |
1dc9ea0d PL |
70 | fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] <url>\n"); |
71 | } | |
72 | ||
73 | void process_dir(struct nfs_context *nfs, char *dir, int level) { | |
74 | int ret; | |
75 | struct nfsdirent *nfsdirent; | |
76 | struct statvfs svfs; | |
77 | struct nfsdir *nfsdir; | |
78 | struct stat st; | |
79 | ||
80 | if (!level) { | |
81 | printf("Recursion detected!\n"); | |
82 | exit(10); | |
83 | } | |
84 | ||
85 | ret = nfs_opendir(nfs, dir, &nfsdir); | |
86 | if (ret != 0) { | |
87 | printf("Failed to opendir(\"%s\")\n", dir, nfs_get_error(nfs)); | |
88 | exit(10); | |
89 | } | |
90 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
91 | char path[1024]; | |
92 | ||
93 | if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) { | |
94 | continue; | |
95 | } | |
96 | ||
97 | snprintf(path, 1024, "%s/%s", dir, nfsdirent->name); | |
98 | ret = nfs_stat(nfs, path, &st); | |
99 | if (ret != 0) { | |
100 | fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs)); | |
101 | continue; | |
102 | } | |
103 | ||
104 | switch (st.st_mode & S_IFMT) { | |
105 | #ifndef WIN32 | |
106 | case S_IFLNK: | |
107 | printf("l"); | |
108 | break; | |
109 | #endif | |
110 | case S_IFREG: | |
111 | printf("-"); | |
112 | break; | |
113 | case S_IFDIR: | |
114 | printf("d"); | |
115 | break; | |
116 | case S_IFCHR: | |
117 | printf("c"); | |
118 | break; | |
119 | case S_IFBLK: | |
120 | printf("b"); | |
121 | break; | |
122 | } | |
123 | printf("%c%c%c", | |
124 | "-r"[!!(st.st_mode & S_IRUSR)], | |
125 | "-w"[!!(st.st_mode & S_IWUSR)], | |
126 | "-x"[!!(st.st_mode & S_IXUSR)] | |
127 | ); | |
128 | printf("%c%c%c", | |
129 | "-r"[!!(st.st_mode & S_IRGRP)], | |
130 | "-w"[!!(st.st_mode & S_IWGRP)], | |
131 | "-x"[!!(st.st_mode & S_IXGRP)] | |
132 | ); | |
133 | printf("%c%c%c", | |
134 | "-r"[!!(st.st_mode & S_IROTH)], | |
135 | "-w"[!!(st.st_mode & S_IWOTH)], | |
136 | "-x"[!!(st.st_mode & S_IXOTH)] | |
137 | ); | |
138 | printf(" %2d", (int) st.st_nlink); | |
139 | printf(" %5d", st.st_uid); | |
140 | printf(" %5d", st.st_gid); | |
141 | printf(" %12" PRId64, st.st_size); | |
142 | ||
143 | printf(" %s\n", path + 1); | |
144 | ||
145 | if (recursive && (st.st_mode & S_IFMT) == S_IFDIR) { | |
146 | process_dir(nfs, path, level - 1); | |
147 | } | |
148 | } | |
149 | nfs_closedir(nfs, nfsdir); | |
a6bc1707 RS |
150 | } |
151 | ||
152 | int main(int argc, char *argv[]) | |
153 | { | |
154 | struct nfs_context *nfs = NULL; | |
155 | int i, ret, res; | |
156 | uint64_t offset; | |
157 | struct client client; | |
a6bc1707 | 158 | struct nfsfh *nfsfh; |
1dc9ea0d | 159 | struct statvfs stvfs; |
a6bc1707 RS |
160 | exports export, tmp; |
161 | const char *url = NULL; | |
162 | char *server = NULL, *path = NULL, *strp; | |
163 | ||
164 | #ifdef WIN32 | |
165 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { | |
166 | printf("Failed to start Winsock2\n"); | |
167 | exit(10); | |
168 | } | |
169 | #endif | |
170 | ||
171 | #ifdef AROS | |
172 | aros_init_socket(); | |
173 | #endif | |
174 | ||
1dc9ea0d | 175 | url = argv[argc - 1]; |
a6bc1707 RS |
176 | |
177 | if (url == NULL) { | |
178 | fprintf(stderr, "No URL specified.\n"); | |
179 | print_usage(); | |
1dc9ea0d PL |
180 | exit(10); |
181 | } | |
182 | ||
183 | for (i=1; i < argc -1; i++) { | |
184 | if (!strcmp(argv[i], "-R") || !strcmp(argv[i], "--recursive")) { | |
185 | recursive++; | |
186 | } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) { | |
187 | summary++; | |
188 | } else { | |
189 | print_usage(); | |
190 | exit(10); | |
191 | } | |
a6bc1707 RS |
192 | } |
193 | ||
194 | if (strncmp(url, "nfs://", 6)) { | |
195 | fprintf(stderr, "Invalid URL specified.\n"); | |
196 | print_usage(); | |
1dc9ea0d | 197 | exit(10); |
a6bc1707 RS |
198 | } |
199 | ||
200 | server = strdup(url + 6); | |
201 | if (server == NULL) { | |
202 | fprintf(stderr, "Failed to strdup server string\n"); | |
203 | exit(10); | |
204 | } | |
205 | if (server[0] == '/' || server[0] == '\0') { | |
206 | fprintf(stderr, "Invalid server string.\n"); | |
207 | free(server); | |
208 | exit(10); | |
209 | } | |
210 | strp = strchr(server, '/'); | |
211 | if (strp == NULL) { | |
212 | fprintf(stderr, "Invalid URL specified.\n"); | |
213 | print_usage(); | |
214 | free(server); | |
1dc9ea0d | 215 | exit(10); |
a6bc1707 RS |
216 | } |
217 | path = strdup(strp); | |
218 | if (path == NULL) { | |
219 | fprintf(stderr, "Failed to strdup server string\n"); | |
220 | free(server); | |
221 | exit(10); | |
222 | } | |
223 | if (path[0] != '/') { | |
224 | fprintf(stderr, "Invalid path.\n"); | |
225 | free(server); | |
226 | free(path); | |
227 | exit(10); | |
228 | } | |
229 | *strp = 0; | |
1dc9ea0d | 230 | |
a6bc1707 RS |
231 | client.server = server; |
232 | client.export = path; | |
233 | client.is_finished = 0; | |
234 | ||
a6bc1707 RS |
235 | nfs = nfs_init_context(); |
236 | if (nfs == NULL) { | |
237 | printf("failed to init context\n"); | |
238 | goto finished; | |
239 | } | |
240 | ||
241 | ret = nfs_mount(nfs, client.server, client.export); | |
242 | if (ret != 0) { | |
243 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); | |
244 | goto finished; | |
245 | } | |
246 | ||
1dc9ea0d | 247 | process_dir(nfs, "", 16); |
a6bc1707 | 248 | |
1dc9ea0d PL |
249 | if (summary) { |
250 | ret = nfs_statvfs(nfs, "/", &stvfs); | |
a6bc1707 | 251 | if (ret != 0) { |
1dc9ea0d | 252 | goto finished; |
a6bc1707 | 253 | } |
1dc9ea0d PL |
254 | printf("\n%12" PRId64 " of %12" PRId64 " bytes free.\n", |
255 | stvfs.f_frsize * stvfs.f_bfree, stvfs.f_frsize * stvfs.f_blocks); | |
a6bc1707 | 256 | } |
a6bc1707 RS |
257 | finished: |
258 | free(server); | |
259 | free(path); | |
260 | if (nfs != NULL) { | |
261 | nfs_destroy_context(nfs); | |
262 | } | |
263 | return 0; | |
264 | } | |
265 |