X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Fnfs-ls.c;h=c680617effc5e1ee640b96cea114f1c56f70e2dd;hb=4edd78302d2a019fa4ca496531e4da0ebc9ca8a4;hp=7aed4dfe6cc6260ce1fd1b88635d6b7d601cf615;hpb=1dc9ea0d52ba69fa5b1265bc3c24214d429a5baf;p=deb_libnfs.git diff --git a/examples/nfs-ls.c b/examples/nfs-ls.c index 7aed4df..c680617 100644 --- a/examples/nfs-ls.c +++ b/examples/nfs-ls.c @@ -63,11 +63,27 @@ struct client { 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] \n"); + fprintf(stderr, "Usage: nfs-ls [-?|--help|--usage] [-R|--recursive] [-s|--summary] [-D|--discovery] \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) { @@ -75,7 +91,7 @@ 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"); @@ -84,7 +100,7 @@ void process_dir(struct nfs_context *nfs, char *dir, int level) { 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) { @@ -95,13 +111,13 @@ void process_dir(struct nfs_context *nfs, char *dir, int level) { } 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"); @@ -121,28 +137,28 @@ void process_dir(struct nfs_context *nfs, char *dir, int level) { 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); } } @@ -152,14 +168,12 @@ void process_dir(struct nfs_context *nfs, char *dir, int level) { int main(int argc, char *argv[]) { struct nfs_context *nfs = NULL; - int i, ret, res; + int i, ret = 1, res; uint64_t offset; struct client client; - struct nfsfh *nfsfh; struct statvfs stvfs; + struct nfs_url *url; exports export, tmp; - const char *url = NULL; - char *server = NULL, *path = NULL, *strp; #ifdef WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { @@ -172,12 +186,9 @@ int main(int argc, char *argv[]) aros_init_socket(); #endif - url = argv[argc - 1]; - - if (url == NULL) { + if (argc < 2) { fprintf(stderr, "No URL specified.\n"); - print_usage(); - exit(10); + goto finished; } for (i=1; i < argc -1; i++) { @@ -185,81 +196,85 @@ int main(int argc, char *argv[]) recursive++; } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--summary")) { summary++; - } else { - print_usage(); - exit(10); + } else if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--discovery")) { + discovery++; + } else{ + goto finished; } } - if (strncmp(url, "nfs://", 6)) { - fprintf(stderr, "Invalid URL specified.\n"); - print_usage(); - exit(10); + nfs = nfs_init_context(); + if (nfs == NULL) { + printf("failed to init context\n"); + goto finished; } - server = strdup(url + 6); - if (server == NULL) { - fprintf(stderr, "Failed to strdup server string\n"); - exit(10); - } - if (server[0] == '/' || server[0] == '\0') { - fprintf(stderr, "Invalid server string.\n"); - free(server); - exit(10); - } - strp = strchr(server, '/'); - if (strp == NULL) { - fprintf(stderr, "Invalid URL specified.\n"); - print_usage(); - free(server); - exit(10); - } - path = strdup(strp); - if (path == NULL) { - fprintf(stderr, "Failed to strdup server string\n"); - free(server); - exit(10); - } - if (path[0] != '/') { - fprintf(stderr, "Invalid path.\n"); - free(server); - free(path); - exit(10); - } - *strp = 0; + 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; - client.server = server; - client.export = path; - client.is_finished = 0; + 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); + } - nfs = nfs_init_context(); - if (nfs == NULL) { - printf("failed to init context\n"); + url = nfs_parse_url_dir(nfs, argv[argc - 1]); + if (url == NULL) { + fprintf(stderr, "%s\n", nfs_get_error(nfs)); goto finished; } - ret = nfs_mount(nfs, client.server, client.export); - if (ret != 0) { - printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); + client.server = url->server; + client.export = url->path; + client.is_finished = 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) { - ret = nfs_statvfs(nfs, "/", &stvfs); - if (ret != 0) { + if (nfs_statvfs(nfs, "", &stvfs) != 0) { goto finished; } printf("\n%12" PRId64 " of %12" PRId64 " bytes free.\n", stvfs.f_frsize * stvfs.f_bfree, stvfs.f_frsize * stvfs.f_blocks); } + + ret = 0; finished: - free(server); - free(path); - if (nfs != NULL) { + if (ret > 0) { + print_usage(); + } + nfs_destroy_url(url); + if (nfs != NULL) { nfs_destroy_context(nfs); } - return 0; + return ret; } -