X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Fnfs-cp.c;h=bbd136a91c1dbddfbf4f7123df0ded70ac02b74a;hb=2db42ce2fc89bc88050564037688888bbb8876e9;hp=eeea40423790d2b5b2ace872e76545f583db45b7;hpb=3d574b1eab581b59afce94a85853ec47e7ef404e;p=deb_libnfs.git diff --git a/examples/nfs-cp.c b/examples/nfs-cp.c index eeea404..bbd136a 100644 --- a/examples/nfs-cp.c +++ b/examples/nfs-cp.c @@ -29,6 +29,8 @@ #ifdef WIN32 #include "win32_compat.h" +#pragma comment(lib, "ws2_32.lib") +WSADATA wsaData; #else #include #include @@ -47,7 +49,6 @@ #include #include #include -#include "libnfs-zdr.h" #include "libnfs.h" #include "libnfs-raw.h" #include "libnfs-raw-mount.h" @@ -57,6 +58,7 @@ struct file_context { int fd; struct nfs_context *nfs; struct nfsfh *nfsfh; + struct nfs_url *url; }; void usage(void) @@ -79,6 +81,7 @@ free_file_context(struct file_context *file_context) if (file_context->nfs != NULL) { nfs_destroy_context(file_context->nfs); } + nfs_destroy_url(file_context->url); free(file_context); } @@ -118,7 +121,6 @@ static struct file_context * open_file(const char *url, int flags) { struct file_context *file_context; - char *server = NULL, *path = NULL, *file = NULL, *strp; file_context = malloc(sizeof(struct file_context)); if (file_context == NULL) { @@ -129,8 +131,8 @@ open_file(const char *url, int flags) file_context->fd = -1; file_context->nfs = NULL; file_context->nfsfh = NULL; + file_context->url = NULL; - if (strncmp(url, "nfs://", 6)) { file_context->is_nfs = 0; file_context->fd = open(url, flags, 0660); @@ -151,77 +153,40 @@ open_file(const char *url, int flags) return NULL; } - server = strdup(url + 6); - if (server == NULL) { - fprintf(stderr, "Failed to strdup server string\n"); - free_file_context(file_context); - return NULL; - } - if (server[0] == '/' || server[0] == '\0') { - fprintf(stderr, "Invalid server string.\n"); - free(server); - free_file_context(file_context); - return NULL; - } - strp = strchr(server, '/'); - path = strdup(strp); - *strp = 0; - if (path == NULL) { - fprintf(stderr, "Invalid URL specified.\n"); - free(server); + file_context->url = nfs_parse_url_full(file_context->nfs, url); + if (file_context->url == NULL) { + fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs)); free_file_context(file_context); return NULL; } - strp = strrchr(path, '/'); - if (strp == NULL) { - fprintf(stderr, "Invalid URL specified.\n"); - free(path); - free(server); - free_file_context(file_context); - return NULL; - } - file = strdup(strp); - *strp = 0; - - if (nfs_mount(file_context->nfs, server, path) != 0) { - fprintf(stderr, "Failed to mount nfs share : %s\n", + if (nfs_mount(file_context->nfs, file_context->url->server, + file_context->url->path) != 0) { + fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(file_context->nfs)); - free(file); - free(path); - free(server); free_file_context(file_context); return NULL; } if (flags == O_RDONLY) { - if (nfs_open(file_context->nfs, file, flags, + if (nfs_open(file_context->nfs, file_context->url->file, flags, &file_context->nfsfh) != 0) { - fprintf(stderr, "Failed to open file : %s\n", + fprintf(stderr, "Failed to open file %s: %s\n", + file_context->url->file, nfs_get_error(file_context->nfs)); - free(file); - free(path); - free(server); free_file_context(file_context); return NULL; } } else { - if (nfs_creat(file_context->nfs, file, 0660, + if (nfs_creat(file_context->nfs, file_context->url->file, 0660, &file_context->nfsfh) != 0) { - fprintf(stderr, "Failed to creat file %s %s\n", file, + fprintf(stderr, "Failed to creat file %s: %s\n", + file_context->url->file, nfs_get_error(file_context->nfs)); - free(file); - free(path); - free(server); free_file_context(file_context); return NULL; } } - - free(file); - free(path); - free(server); - return file_context; } @@ -295,7 +260,7 @@ int main(int argc, char *argv[]) off += count; } - printf("copied %d bytes\n", (int)count); + printf("copied %d bytes\n", (int)off); free_file_context(src); free_file_context(dst);