socket: fix broken connect for non broadcast traffic
[deb_libnfs.git] / utils / nfs-ls.c
CommitLineData
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")
32WSADATA 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
59struct client {
60 char *server;
61 char *export;
62 uint32_t mount_port;
63 int is_finished;
64};
65
17d3cbb1 66int recursive = 0, summary = 0, discovery = 0;
1dc9ea0d 67
a6bc1707
RS
68void print_usage(void)
69{
17d3cbb1
PL
70 fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] [-D|--discovery] <url>\n");
71}
72
73int process_server(const char *server) {
74 struct exportnode *exports;
75 struct exportnode *export;
76
77 exports = mount_getexports(server);
78 if (exports == NULL) {
79 fprintf(stderr, "Failed to get exports for server %s.\n", server);
80 return -1;
81 }
82 for (export=exports; export; export = export->ex_next) {
83 printf("nfs://%s%s\n", server, export->ex_dir);
84 }
85 mount_free_export_list(exports);
86 return 0;
1dc9ea0d
PL
87}
88
89void process_dir(struct nfs_context *nfs, char *dir, int level) {
90 int ret;
91 struct nfsdirent *nfsdirent;
92 struct statvfs svfs;
93 struct nfsdir *nfsdir;
479302f7 94 struct nfs_stat_64 st;
1dc9ea0d
PL
95
96 if (!level) {
97 printf("Recursion detected!\n");
98 exit(10);
99 }
100
101 ret = nfs_opendir(nfs, dir, &nfsdir);
102 if (ret != 0) {
dc3ed8c3 103 printf("Failed to opendir(\"%s\") %s\n", dir, nfs_get_error(nfs));
1dc9ea0d
PL
104 exit(10);
105 }
106 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
107 char path[1024];
108
109 if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
110 continue;
111 }
112
113 snprintf(path, 1024, "%s/%s", dir, nfsdirent->name);
479302f7 114 ret = nfs_stat64(nfs, path, &st);
1dc9ea0d
PL
115 if (ret != 0) {
116 fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
117 continue;
118 }
119
479302f7 120 switch (st.nfs_mode & S_IFMT) {
1dc9ea0d
PL
121#ifndef WIN32
122 case S_IFLNK:
123 printf("l");
124 break;
125#endif
126 case S_IFREG:
127 printf("-");
128 break;
129 case S_IFDIR:
130 printf("d");
131 break;
132 case S_IFCHR:
133 printf("c");
134 break;
135 case S_IFBLK:
136 printf("b");
137 break;
138 }
139 printf("%c%c%c",
479302f7
RS
140 "-r"[!!(st.nfs_mode & S_IRUSR)],
141 "-w"[!!(st.nfs_mode & S_IWUSR)],
142 "-x"[!!(st.nfs_mode & S_IXUSR)]
1dc9ea0d
PL
143 );
144 printf("%c%c%c",
479302f7
RS
145 "-r"[!!(st.nfs_mode & S_IRGRP)],
146 "-w"[!!(st.nfs_mode & S_IWGRP)],
147 "-x"[!!(st.nfs_mode & S_IXGRP)]
1dc9ea0d
PL
148 );
149 printf("%c%c%c",
479302f7
RS
150 "-r"[!!(st.nfs_mode & S_IROTH)],
151 "-w"[!!(st.nfs_mode & S_IWOTH)],
152 "-x"[!!(st.nfs_mode & S_IXOTH)]
1dc9ea0d 153 );
479302f7
RS
154 printf(" %2d", (int)st.nfs_nlink);
155 printf(" %5d", (int)st.nfs_uid);
156 printf(" %5d", (int)st.nfs_gid);
157 printf(" %12" PRId64, st.nfs_size);
1dc9ea0d
PL
158
159 printf(" %s\n", path + 1);
160
479302f7 161 if (recursive && (st.nfs_mode & S_IFMT) == S_IFDIR) {
1dc9ea0d
PL
162 process_dir(nfs, path, level - 1);
163 }
164 }
165 nfs_closedir(nfs, nfsdir);
a6bc1707
RS
166}
167
168int main(int argc, char *argv[])
169{
170 struct nfs_context *nfs = NULL;
d2ec73c7 171 int i, ret = 1, res;
a6bc1707
RS
172 uint64_t offset;
173 struct client client;
1dc9ea0d 174 struct statvfs stvfs;
76ead54b 175 struct nfs_url *url = NULL;
a6bc1707 176 exports export, tmp;
a6bc1707
RS
177
178#ifdef WIN32
179 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
180 printf("Failed to start Winsock2\n");
181 exit(10);
182 }
183#endif
184
185#ifdef AROS
186 aros_init_socket();
187#endif
188
d2ec73c7 189 if (argc < 2) {
a6bc1707 190 fprintf(stderr, "No URL specified.\n");
d2ec73c7 191 goto finished;
1dc9ea0d
PL
192 }
193
194 for (i=1; i < argc -1; i++) {
195 if (!strcmp(argv[i], "-R") || !strcmp(argv[i], "--recursive")) {
196 recursive++;
197 } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) {
198 summary++;
17d3cbb1
PL
199 } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--discovery")) {
200 discovery++;
201 } else{
d2ec73c7 202 goto finished;
1dc9ea0d 203 }
a6bc1707
RS
204 }
205
a6bc1707
RS
206 nfs = nfs_init_context();
207 if (nfs == NULL) {
208 printf("failed to init context\n");
209 goto finished;
210 }
211
17d3cbb1
PL
212 if (discovery) {
213 url = nfs_parse_url_incomplete(nfs, argv[argc - 1]);
214 if (url == NULL) {
215 fprintf(stderr, "%s\n", nfs_get_error(nfs));
216 goto finished;
217 }
218 if (!url->server) {
219 struct nfs_server_list *srvrs;
220 struct nfs_server_list *srv;
221
222 srvrs = nfs_find_local_servers();
223 if (srvrs == NULL) {
224 fprintf(stderr, "Failed to find local servers.\n");
225 goto finished;
226 }
227 for (srv=srvrs; srv; srv = srv->next) {
228 if (recursive) {
229 process_server(srv->addr);
230 } else {
231 printf("nfs://%s\n", srv->addr);
232 }
233 }
234 free_nfs_srvr_list(srvrs);
235 ret = 0;
236 goto finished;
237 }
238 if (url->server && !url->path) {
239 ret = process_server(url->server);
240 goto finished;
241 }
242 nfs_destroy_url(url);
243 }
244
d2ec73c7
PL
245 url = nfs_parse_url_dir(nfs, argv[argc - 1]);
246 if (url == NULL) {
247 fprintf(stderr, "%s\n", nfs_get_error(nfs));
248 goto finished;
249 }
250
251 client.server = url->server;
252 client.export = url->path;
253 client.is_finished = 0;
254
17d3cbb1 255 if ((ret = nfs_mount(nfs, client.server, client.export)) != 0) {
d2ec73c7 256 fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
a6bc1707
RS
257 goto finished;
258 }
259
1dc9ea0d 260 process_dir(nfs, "", 16);
a6bc1707 261
1dc9ea0d 262 if (summary) {
9ecd7868 263 if (nfs_statvfs(nfs, "", &stvfs) != 0) {
1dc9ea0d 264 goto finished;
a6bc1707 265 }
1dc9ea0d
PL
266 printf("\n%12" PRId64 " of %12" PRId64 " bytes free.\n",
267 stvfs.f_frsize * stvfs.f_bfree, stvfs.f_frsize * stvfs.f_blocks);
a6bc1707 268 }
d2ec73c7
PL
269
270 ret = 0;
a6bc1707 271finished:
17d3cbb1 272 if (ret > 0) {
d2ec73c7
PL
273 print_usage();
274 }
d2ec73c7 275 nfs_destroy_url(url);
17d3cbb1 276 if (nfs != NULL) {
a6bc1707
RS
277 nfs_destroy_context(nfs);
278 }
d2ec73c7 279 return ret;
a6bc1707 280}