int is_finished;
};
- int recursive = 0, summary = 0;
+int recursive = 0, summary = 0, discovery = 0;
void print_usage(void)
{
- fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] <url>\n");
+ fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] [-D|--discovery] <url>\n");
+}
+
+int process_server(const char *server) {
+ struct exportnode *exports;
+ struct exportnode *export;
+
+ exports = mount_getexports(server);
+ if (exports == NULL) {
+ fprintf(stderr, "Failed to get exports for server %s.\n", server);
+ return -1;
+ }
+ for (export=exports; export; export = export->ex_next) {
+ printf("nfs://%s%s\n", server, export->ex_dir);
+ }
+ mount_free_export_list(exports);
+ return 0;
}
void process_dir(struct nfs_context *nfs, char *dir, int level) {
struct nfsdirent *nfsdirent;
struct statvfs svfs;
struct nfsdir *nfsdir;
- struct stat st;
+ struct nfs_stat_64 st;
if (!level) {
printf("Recursion detected!\n");
ret = nfs_opendir(nfs, dir, &nfsdir);
if (ret != 0) {
- printf("Failed to opendir(\"%s\")\n", dir, nfs_get_error(nfs));
+ printf("Failed to opendir(\"%s\") %s\n", dir, nfs_get_error(nfs));
exit(10);
}
while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
}
snprintf(path, 1024, "%s/%s", dir, nfsdirent->name);
- ret = nfs_stat(nfs, path, &st);
+ ret = nfs_stat64(nfs, path, &st);
if (ret != 0) {
fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
continue;
}
- switch (st.st_mode & S_IFMT) {
+ switch (st.nfs_mode & S_IFMT) {
#ifndef WIN32
case S_IFLNK:
printf("l");
break;
}
printf("%c%c%c",
- "-r"[!!(st.st_mode & S_IRUSR)],
- "-w"[!!(st.st_mode & S_IWUSR)],
- "-x"[!!(st.st_mode & S_IXUSR)]
+ "-r"[!!(st.nfs_mode & S_IRUSR)],
+ "-w"[!!(st.nfs_mode & S_IWUSR)],
+ "-x"[!!(st.nfs_mode & S_IXUSR)]
);
printf("%c%c%c",
- "-r"[!!(st.st_mode & S_IRGRP)],
- "-w"[!!(st.st_mode & S_IWGRP)],
- "-x"[!!(st.st_mode & S_IXGRP)]
+ "-r"[!!(st.nfs_mode & S_IRGRP)],
+ "-w"[!!(st.nfs_mode & S_IWGRP)],
+ "-x"[!!(st.nfs_mode & S_IXGRP)]
);
printf("%c%c%c",
- "-r"[!!(st.st_mode & S_IROTH)],
- "-w"[!!(st.st_mode & S_IWOTH)],
- "-x"[!!(st.st_mode & S_IXOTH)]
+ "-r"[!!(st.nfs_mode & S_IROTH)],
+ "-w"[!!(st.nfs_mode & S_IWOTH)],
+ "-x"[!!(st.nfs_mode & S_IXOTH)]
);
- printf(" %2d", (int) st.st_nlink);
- printf(" %5d", st.st_uid);
- printf(" %5d", st.st_gid);
- printf(" %12" PRId64, st.st_size);
+ printf(" %2d", (int)st.nfs_nlink);
+ printf(" %5d", (int)st.nfs_uid);
+ printf(" %5d", (int)st.nfs_gid);
+ printf(" %12" PRId64, st.nfs_size);
printf(" %s\n", path + 1);
- if (recursive && (st.st_mode & S_IFMT) == S_IFDIR) {
+ if (recursive && (st.nfs_mode & S_IFMT) == S_IFDIR) {
process_dir(nfs, path, level - 1);
}
}
int i, ret = 1, res;
uint64_t offset;
struct client client;
- struct nfsfh *nfsfh;
struct statvfs stvfs;
struct nfs_url *url;
exports export, tmp;
- char *server = NULL, *path = NULL, *strp;
#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
recursive++;
} else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) {
summary++;
- } else {
+ } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--discovery")) {
+ discovery++;
+ } else{
goto finished;
}
}
goto finished;
}
+ if (discovery) {
+ url = nfs_parse_url_incomplete(nfs, argv[argc - 1]);
+ if (url == NULL) {
+ fprintf(stderr, "%s\n", nfs_get_error(nfs));
+ goto finished;
+ }
+ if (!url->server) {
+ struct nfs_server_list *srvrs;
+ struct nfs_server_list *srv;
+
+ srvrs = nfs_find_local_servers();
+ if (srvrs == NULL) {
+ fprintf(stderr, "Failed to find local servers.\n");
+ goto finished;
+ }
+ for (srv=srvrs; srv; srv = srv->next) {
+ if (recursive) {
+ process_server(srv->addr);
+ } else {
+ printf("nfs://%s\n", srv->addr);
+ }
+ }
+ free_nfs_srvr_list(srvrs);
+ ret = 0;
+ goto finished;
+ }
+ if (url->server && !url->path) {
+ ret = process_server(url->server);
+ goto finished;
+ }
+ nfs_destroy_url(url);
+ }
+
url = nfs_parse_url_dir(nfs, argv[argc - 1]);
if (url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(nfs));
client.export = url->path;
client.is_finished = 0;
- if (nfs_mount(nfs, client.server, client.export) != 0) {
+ if ((ret = nfs_mount(nfs, client.server, client.export)) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
goto finished;
}
process_dir(nfs, "", 16);
if (summary) {
- if (nfs_statvfs(nfs, "/", &stvfs) != 0) {
+ if (nfs_statvfs(nfs, "", &stvfs) != 0) {
goto finished;
}
printf("\n%12" PRId64 " of %12" PRId64 " bytes free.\n",
}
ret = 0;
-
finished:
- if (ret) {
+ if (ret > 0) {
print_usage();
}
- free(server);
- free(path);
nfs_destroy_url(url);
- if (nfs != NULL) {
+ if (nfs != NULL) {
nfs_destroy_context(nfs);
}
return ret;
}
-