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>
55 #include "libnfs-raw.h"
56 #include "libnfs-raw-mount.h"
65 int recursive
= 0, summary
= 0, discovery
= 0;
67 void print_usage(void)
69 fprintf(stderr
, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] [-D|--discovery] <url>\n");
72 int process_server(const char *server
) {
73 struct exportnode
*exports
;
74 struct exportnode
*export
;
76 exports
= mount_getexports(server
);
77 if (exports
== NULL
) {
78 fprintf(stderr
, "Failed to get exports for server %s.\n", server
);
81 for (export
=exports
; export
; export
= export
->ex_next
) {
82 printf("nfs://%s%s\n", server
, export
->ex_dir
);
84 mount_free_export_list(exports
);
88 void process_dir(struct nfs_context
*nfs
, char *dir
, int level
) {
90 struct nfsdirent
*nfsdirent
;
92 struct nfsdir
*nfsdir
;
95 printf("Recursion detected!\n");
99 ret
= nfs_opendir(nfs
, dir
, &nfsdir
);
101 printf("Failed to opendir(\"%s\") %s\n", dir
, nfs_get_error(nfs
));
104 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
107 if (!strcmp(nfsdirent
->name
, ".") || !strcmp(nfsdirent
->name
, "..")) {
110 snprintf(path
, 1024, "%s/%s", dir
, nfsdirent
->name
);
112 switch (nfsdirent
->mode
& S_IFMT
) {
132 "-r"[!!(nfsdirent
->mode
& S_IRUSR
)],
133 "-w"[!!(nfsdirent
->mode
& S_IWUSR
)],
134 "-x"[!!(nfsdirent
->mode
& S_IXUSR
)]
137 "-r"[!!(nfsdirent
->mode
& S_IRGRP
)],
138 "-w"[!!(nfsdirent
->mode
& S_IWGRP
)],
139 "-x"[!!(nfsdirent
->mode
& S_IXGRP
)]
142 "-r"[!!(nfsdirent
->mode
& S_IROTH
)],
143 "-w"[!!(nfsdirent
->mode
& S_IWOTH
)],
144 "-x"[!!(nfsdirent
->mode
& S_IXOTH
)]
146 printf(" %2d", (int)nfsdirent
->nlink
);
147 printf(" %5d", (int)nfsdirent
->uid
);
148 printf(" %5d", (int)nfsdirent
->gid
);
149 printf(" %12" PRId64
, nfsdirent
->size
);
151 printf(" %s\n", path
+ 1);
153 if (recursive
&& (nfsdirent
->mode
& S_IFMT
) == S_IFDIR
) {
154 process_dir(nfs
, path
, level
- 1);
157 nfs_closedir(nfs
, nfsdir
);
160 int main(int argc
, char *argv
[])
162 struct nfs_context
*nfs
= NULL
;
165 struct client client
;
166 struct statvfs stvfs
;
167 struct nfs_url
*url
= NULL
;
171 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0) {
172 printf("Failed to start Winsock2\n");
182 fprintf(stderr
, "No URL specified.\n");
186 for (i
=1; i
< argc
-1; i
++) {
187 if (!strcmp(argv
[i
], "-R") || !strcmp(argv
[i
], "--recursive")) {
189 } else if (!strcmp(argv
[i
], "-s") || !strcmp(argv
[i
], "--summary")) {
191 } else if (!strcmp(argv
[i
], "-D") || !strcmp(argv
[i
], "--discovery")) {
198 nfs
= nfs_init_context();
200 printf("failed to init context\n");
205 url
= nfs_parse_url_incomplete(nfs
, argv
[argc
- 1]);
207 fprintf(stderr
, "%s\n", nfs_get_error(nfs
));
211 struct nfs_server_list
*srvrs
;
212 struct nfs_server_list
*srv
;
214 srvrs
= nfs_find_local_servers();
216 fprintf(stderr
, "Failed to find local servers.\n");
219 for (srv
=srvrs
; srv
; srv
= srv
->next
) {
221 process_server(srv
->addr
);
223 printf("nfs://%s\n", srv
->addr
);
226 free_nfs_srvr_list(srvrs
);
230 if (url
->server
&& !url
->path
) {
231 ret
= process_server(url
->server
);
234 nfs_destroy_url(url
);
237 url
= nfs_parse_url_dir(nfs
, argv
[argc
- 1]);
239 fprintf(stderr
, "%s\n", nfs_get_error(nfs
));
243 client
.server
= url
->server
;
244 client
.export
= url
->path
;
245 client
.is_finished
= 0;
247 if ((ret
= nfs_mount(nfs
, client
.server
, client
.export
)) != 0) {
248 fprintf(stderr
, "Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
252 process_dir(nfs
, "", 16);
255 if (nfs_statvfs(nfs
, "", &stvfs
) != 0) {
258 printf("\n%12" PRId64
" of %12" PRId64
" bytes free.\n",
259 stvfs
.f_frsize
* stvfs
.f_bfree
, stvfs
.f_frsize
* stvfs
.f_blocks
);
267 nfs_destroy_url(url
);
269 nfs_destroy_context(nfs
);