7aed4dfe6cc6260ce1fd1b88635d6b7d601cf615
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;
68 void print_usage(void)
70 fprintf(stderr
, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] <url>\n");
73 void process_dir(struct nfs_context
*nfs
, char *dir
, int level
) {
75 struct nfsdirent
*nfsdirent
;
77 struct nfsdir
*nfsdir
;
81 printf("Recursion detected!\n");
85 ret
= nfs_opendir(nfs
, dir
, &nfsdir
);
87 printf("Failed to opendir(\"%s\")\n", dir
, nfs_get_error(nfs
));
90 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
93 if (!strcmp(nfsdirent
->name
, ".") || !strcmp(nfsdirent
->name
, "..")) {
97 snprintf(path
, 1024, "%s/%s", dir
, nfsdirent
->name
);
98 ret
= nfs_stat(nfs
, path
, &st
);
100 fprintf(stderr
, "Failed to stat(%s) %s\n", path
, nfs_get_error(nfs
));
104 switch (st
.st_mode
& S_IFMT
) {
124 "-r"[!!(st
.st_mode
& S_IRUSR
)],
125 "-w"[!!(st
.st_mode
& S_IWUSR
)],
126 "-x"[!!(st
.st_mode
& S_IXUSR
)]
129 "-r"[!!(st
.st_mode
& S_IRGRP
)],
130 "-w"[!!(st
.st_mode
& S_IWGRP
)],
131 "-x"[!!(st
.st_mode
& S_IXGRP
)]
134 "-r"[!!(st
.st_mode
& S_IROTH
)],
135 "-w"[!!(st
.st_mode
& S_IWOTH
)],
136 "-x"[!!(st
.st_mode
& S_IXOTH
)]
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
);
143 printf(" %s\n", path
+ 1);
145 if (recursive
&& (st
.st_mode
& S_IFMT
) == S_IFDIR
) {
146 process_dir(nfs
, path
, level
- 1);
149 nfs_closedir(nfs
, nfsdir
);
152 int main(int argc
, char *argv
[])
154 struct nfs_context
*nfs
= NULL
;
157 struct client client
;
159 struct statvfs stvfs
;
161 const char *url
= NULL
;
162 char *server
= NULL
, *path
= NULL
, *strp
;
165 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0) {
166 printf("Failed to start Winsock2\n");
175 url
= argv
[argc
- 1];
178 fprintf(stderr
, "No URL specified.\n");
183 for (i
=1; i
< argc
-1; i
++) {
184 if (!strcmp(argv
[i
], "-R") || !strcmp(argv
[i
], "--recursive")) {
186 } else if (!strcmp(argv
[i
], "-s") || !strcmp(argv
[i
], "--summary")) {
194 if (strncmp(url
, "nfs://", 6)) {
195 fprintf(stderr
, "Invalid URL specified.\n");
200 server
= strdup(url
+ 6);
201 if (server
== NULL
) {
202 fprintf(stderr
, "Failed to strdup server string\n");
205 if (server
[0] == '/' || server
[0] == '\0') {
206 fprintf(stderr
, "Invalid server string.\n");
210 strp
= strchr(server
, '/');
212 fprintf(stderr
, "Invalid URL specified.\n");
219 fprintf(stderr
, "Failed to strdup server string\n");
223 if (path
[0] != '/') {
224 fprintf(stderr
, "Invalid path.\n");
231 client
.server
= server
;
232 client
.export
= path
;
233 client
.is_finished
= 0;
235 nfs
= nfs_init_context();
237 printf("failed to init context\n");
241 ret
= nfs_mount(nfs
, client
.server
, client
.export
);
243 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
247 process_dir(nfs
, "", 16);
250 ret
= nfs_statvfs(nfs
, "/", &stvfs
);
254 printf("\n%12" PRId64
" of %12" PRId64
" bytes free.\n",
255 stvfs
.f_frsize
* stvfs
.f_bfree
, stvfs
.f_frsize
* stvfs
.f_blocks
);
261 nfs_destroy_context(nfs
);