f6b223d2fdf3ceb012679278cc64545d26435c67
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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.
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.
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/>.
18 #define _FILE_OFFSET_BITS 64
26 #include "aros_compat.h"
30 #include "win32_compat.h"
31 #pragma comment(lib, "ws2_32.lib")
38 #include <sys/statvfs.h>
40 #include <sys/statvfs.h>
51 #include <sys/types.h>
54 #include "libnfs-zdr.h"
56 #include "libnfs-raw.h"
57 #include "libnfs-raw-mount.h"
66 int recursive
= 0, summary
= 0, discovery
= 0;
68 void print_usage(void)
70 fprintf(stderr
, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] [-D|--discovery] <url>\n");
73 int process_server(const char *server
) {
74 struct exportnode
*exports
;
75 struct exportnode
*export
;
77 exports
= mount_getexports(server
);
78 if (exports
== NULL
) {
79 fprintf(stderr
, "Failed to get exports for server %s.\n", server
);
82 for (export
=exports
; export
; export
= export
->ex_next
) {
83 printf("nfs://%s%s\n", server
, export
->ex_dir
);
85 mount_free_export_list(exports
);
89 void process_dir(struct nfs_context
*nfs
, char *dir
, int level
) {
91 struct nfsdirent
*nfsdirent
;
93 struct nfsdir
*nfsdir
;
94 struct nfs_stat_64 st
;
97 printf("Recursion detected!\n");
101 ret
= nfs_opendir(nfs
, dir
, &nfsdir
);
103 printf("Failed to opendir(\"%s\") %s\n", dir
, nfs_get_error(nfs
));
106 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
109 if (!strcmp(nfsdirent
->name
, ".") || !strcmp(nfsdirent
->name
, "..")) {
113 snprintf(path
, 1024, "%s/%s", dir
, nfsdirent
->name
);
114 ret
= nfs_stat64(nfs
, path
, &st
);
116 fprintf(stderr
, "Failed to stat(%s) %s\n", path
, nfs_get_error(nfs
));
120 switch (st
.nfs_mode
& S_IFMT
) {
140 "-r"[!!(st
.nfs_mode
& S_IRUSR
)],
141 "-w"[!!(st
.nfs_mode
& S_IWUSR
)],
142 "-x"[!!(st
.nfs_mode
& S_IXUSR
)]
145 "-r"[!!(st
.nfs_mode
& S_IRGRP
)],
146 "-w"[!!(st
.nfs_mode
& S_IWGRP
)],
147 "-x"[!!(st
.nfs_mode
& S_IXGRP
)]
150 "-r"[!!(st
.nfs_mode
& S_IROTH
)],
151 "-w"[!!(st
.nfs_mode
& S_IWOTH
)],
152 "-x"[!!(st
.nfs_mode
& S_IXOTH
)]
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
);
159 printf(" %s\n", path
+ 1);
161 if (recursive
&& (st
.nfs_mode
& S_IFMT
) == S_IFDIR
) {
162 process_dir(nfs
, path
, level
- 1);
165 nfs_closedir(nfs
, nfsdir
);
168 int main(int argc
, char *argv
[])
170 struct nfs_context
*nfs
= NULL
;
173 struct client client
;
174 struct statvfs stvfs
;
179 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0) {
180 printf("Failed to start Winsock2\n");
190 fprintf(stderr
, "No URL specified.\n");
194 for (i
=1; i
< argc
-1; i
++) {
195 if (!strcmp(argv
[i
], "-R") || !strcmp(argv
[i
], "--recursive")) {
197 } else if (!strcmp(argv
[i
], "-s") || !strcmp(argv
[i
], "--summary")) {
199 } else if (!strcmp(argv
[i
], "-D") || !strcmp(argv
[i
], "--discovery")) {
206 nfs
= nfs_init_context();
208 printf("failed to init context\n");
213 url
= nfs_parse_url_incomplete(nfs
, argv
[argc
- 1]);
215 fprintf(stderr
, "%s\n", nfs_get_error(nfs
));
219 struct nfs_server_list
*srvrs
;
220 struct nfs_server_list
*srv
;
222 srvrs
= nfs_find_local_servers();
224 fprintf(stderr
, "Failed to find local servers.\n");
227 for (srv
=srvrs
; srv
; srv
= srv
->next
) {
229 process_server(srv
->addr
);
231 printf("nfs://%s\n", srv
->addr
);
234 free_nfs_srvr_list(srvrs
);
238 if (url
->server
&& !url
->path
) {
239 ret
= process_server(url
->server
);
242 nfs_destroy_url(url
);
245 url
= nfs_parse_url_dir(nfs
, argv
[argc
- 1]);
247 fprintf(stderr
, "%s\n", nfs_get_error(nfs
));
251 client
.server
= url
->server
;
252 client
.export
= url
->path
;
253 client
.is_finished
= 0;
255 if ((ret
= nfs_mount(nfs
, client
.server
, client
.export
)) != 0) {
256 fprintf(stderr
, "Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
260 process_dir(nfs
, "", 16);
263 if (nfs_statvfs(nfs
, "/", &stvfs
) != 0) {
266 printf("\n%12" PRId64
" of %12" PRId64
" bytes free.\n",
267 stvfs
.f_frsize
* stvfs
.f_bfree
, stvfs
.f_frsize
* stvfs
.f_blocks
);
275 nfs_destroy_url(url
);
277 nfs_destroy_context(nfs
);