From 24064e96d22c8836fefb3689bd1064fdd9eabfc7 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Tue, 2 May 2017 17:36:27 -0700 Subject: [PATCH] Add example util for nfs_link() Signed-off-by: Ronnie Sahlberg --- examples/Makefile.am | 3 +- examples/nfs-ln.c | 125 +++++++++++++++++++++++++++++++++++++++++++ lib/libnfs-sync.c | 3 +- 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 examples/nfs-ln.c diff --git a/examples/Makefile.am b/examples/Makefile.am index 4e55cb9..a0f95e3 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = nfsclient-async nfsclient-raw nfsclient-sync nfsclient-bcast nfsclient-listservers nfs-io nfs4-cat portmap-client portmap-server +noinst_PROGRAMS = nfsclient-async nfsclient-raw nfsclient-sync nfsclient-bcast nfsclient-listservers nfs-io nfs-ln nfs4-cat portmap-client portmap-server if HAVE_TALLOC_TEVENT noinst_PROGRAMS += nfs4-cat-talloc @@ -22,6 +22,7 @@ nfsclient_sync_LDADD = $(COMMON_LIBS) nfsclient_bcast_LDADD = $(COMMON_LIBS) nfsclient_listservers_LDADD = $(COMMON_LIBS) nfs_io_LDADD = $(COMMON_LIBS) +nfs_ln_LDADD = $(COMMON_LIBS) nfs4_cat_LDADD = $(COMMON_LIBS) -levent nfs4_cat_talloc_LDADD = $(COMMON_LIBS) -ltevent -ltalloc portmap_client_LDADD = $(COMMON_LIBS) diff --git a/examples/nfs-ln.c b/examples/nfs-ln.c new file mode 100644 index 0000000..fb33809 --- /dev/null +++ b/examples/nfs-ln.c @@ -0,0 +1,125 @@ +/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */ +/* + Copyright (C) by Ronnie Sahlberg 2017 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef AROS +#include "aros_compat.h" +#endif + +#ifdef WIN32 +#include "win32_compat.h" +#pragma comment(lib, "ws2_32.lib") +WSADATA wsaData; +#define PRId64 "ll" +#else +#include +#include +#include +#include +#ifndef AROS +#include +#endif +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include "libnfs.h" +#include "libnfs-raw.h" +#include "libnfs-raw-mount.h" + +void print_usage(void) +{ + fprintf(stderr, "Usage: nfs-ln \n"); +} + +int main(int argc, char *argv[]) +{ + int ret = 1; + struct nfs_context *nfs = NULL; + struct nfsfh *nfsfh = NULL; + struct nfs_url *url = NULL; + +#ifdef WIN32 + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { + printf("Failed to start Winsock2\n"); + exit(10); + } +#endif + +#ifdef AROS + aros_init_socket(); +#endif + + if (argc != 4) { + print_usage(); + goto finished; + } + + nfs = nfs_init_context(); + if (nfs == NULL) { + printf("failed to init context\n"); + goto finished; + } + + url = nfs_parse_url_full(nfs, argv[1]); + if (url == NULL) { + fprintf(stderr, "%s\n", nfs_get_error(nfs)); + goto finished; + } + + if (nfs_mount(nfs, url->server, url->path) != 0) { + fprintf(stderr, "Failed to mount nfs share : %s\n", + nfs_get_error(nfs)); + goto finished; + } + + ret = nfs_link(nfs, argv[2], argv[3]); + if (ret) { + fprintf(stderr, "nfs_link() failed with %s\n", + nfs_get_error(nfs)); + goto finished; + } + +finished: + if (ret > 0) { + print_usage(); + } + nfs_destroy_url(url); + if (nfs != NULL) { + if (nfsfh) { + nfs_close(nfs, nfsfh); + } + nfs_destroy_context(nfs); + } + return !!ret; +} + diff --git a/lib/libnfs-sync.c b/lib/libnfs-sync.c index 7569968..ca924d1 100644 --- a/lib/libnfs-sync.c +++ b/lib/libnfs-sync.c @@ -1420,7 +1420,8 @@ int nfs_link(struct nfs_context *nfs, const char *oldpath, const char *newpath) cb_data.is_finished = 0; if (nfs_link_async(nfs, oldpath, newpath, link_cb, &cb_data) != 0) { - nfs_set_error(nfs, "nfs_link_async failed"); + nfs_set_error(nfs, "nfs_link_async failed: %s", + nfs_get_error(nfs)); return -1; }