Instead of having to munge a URI to supply a different export name, we
can exploit the fact that nbd_opt_mode lets us change the preferred
export name after nbd_connect_uri has established its socket.
In fact, by doing this, nbdinfo no longer needs to directly link
against libxml2, so it can now be built regardless of whether the
underlying libnbd.so supports URIs (although use of the program will
fail if libnbd.so was not built against libxml2).
---
info/Makefile.am | 6 ++---
info/nbdinfo.c | 63 +++++++++++++++++++-----------------------------
2 files changed, 27 insertions(+), 42 deletions(-)
diff --git a/info/Makefile.am b/info/Makefile.am
index 05b8137..d049d16 100644
--- a/info/Makefile.am
+++ b/info/Makefile.am
@@ -27,8 +27,6 @@ EXTRA_DIST = \
nbdinfo.pod \
$(NULL)
-if HAVE_LIBXML2
-
TESTS_ENVIRONMENT = LIBNBD_DEBUG=1
LOG_COMPILER = $(top_builddir)/run
TESTS =
@@ -39,11 +37,9 @@ nbdinfo_SOURCES = nbdinfo.c
nbdinfo_CPPFLAGS = -I$(top_srcdir)/include
nbdinfo_CFLAGS = \
$(WARNINGS_CFLAGS) \
- $(LIBXML2_CFLAGS) \
$(NULL)
nbdinfo_LDADD = \
$(top_builddir)/lib/libnbd.la \
- $(LIBXML2_LIBS) \
$(NULL)
if HAVE_POD
@@ -59,6 +55,8 @@ nbdinfo.1: nbdinfo.pod $(top_builddir)/podwrapper.pl
endif HAVE_POD
+if HAVE_LIBXML2
+
TESTS += \
info-list.sh \
info-list-json.sh \
diff --git a/info/nbdinfo.c b/info/nbdinfo.c
index b54dfd4..394f5ac 100644
--- a/info/nbdinfo.c
+++ b/info/nbdinfo.c
@@ -29,7 +29,6 @@
#include <limits.h>
#include <errno.h>
-#include <libxml/uri.h>
#include <libnbd.h>
static bool list_all = false;
@@ -422,7 +421,6 @@ static void
list_all_exports (struct nbd_handle *nbd1, const char *uri)
{
int i;
- xmlURIPtr xmluri = NULL;
int count = nbd_get_nr_list_exports (nbd1);
if (count == -1) {
@@ -434,49 +432,38 @@ list_all_exports (struct nbd_handle *nbd1, const char *uri)
printf ("\t\"exports\": []\n");
for (i = 0; i < count; ++i) {
- char *name, *desc, *new_path, *new_uri;
+ char *name, *desc;
struct nbd_handle *nbd2;
name = nbd_get_list_export_name (nbd1, i);
- if (name) {
- /* We have to modify the original URI to change the export name.
- * In the URI spec, paths always start with '/' (which is ignored).
- */
- xmluri = xmlParseURI (uri);
- if (!xmluri) {
- fprintf (stderr, "unable to parse original URI: %s\n", uri);
- exit (EXIT_FAILURE);
- }
- if (asprintf (&new_path, "/%s", name) == -1) {
- perror ("asprintf");
- exit (EXIT_FAILURE);
- }
- free (xmluri->path);
- xmluri->path = new_path;
- new_uri = (char *) xmlSaveUri (xmluri);
+ if (!name) {
+ fprintf (stderr, "unable to obtain export name: %s\n",
+ nbd_get_error ());
+ exit (EXIT_FAILURE);
+ }
- /* Connect to the new URI. */
- nbd2 = nbd_create ();
- if (nbd2 == NULL) {
- fprintf (stderr, "%s\n", nbd_get_error ());
- exit (EXIT_FAILURE);
- }
- nbd_set_uri_allow_local_file (nbd2, true); /* Allow ?tls-psk-file. */
+ /* Connect to the original URI, but using opt mode to alter the export. */
+ nbd2 = nbd_create ();
+ if (nbd2 == NULL) {
+ fprintf (stderr, "%s\n", nbd_get_error ());
+ exit (EXIT_FAILURE);
+ }
+ nbd_set_uri_allow_local_file (nbd2, true); /* Allow ?tls-psk-file. */
+ nbd_set_opt_mode (nbd2, true);
- if (nbd_connect_uri (nbd2, new_uri) == -1) {
- fprintf (stderr, "%s\n", nbd_get_error ());
- exit (EXIT_FAILURE);
- }
+ if (nbd_connect_uri (nbd2, uri) == -1 ||
+ nbd_set_export_name (nbd2, name) == -1 ||
+ nbd_opt_go (nbd2) == -1) {
+ fprintf (stderr, "%s\n", nbd_get_error ());
+ exit (EXIT_FAILURE);
+ }
- /* List the metadata of this export. */
- desc = nbd_get_list_export_description (nbd1, i);
- list_one_export (nbd2, desc, i == 0, i + 1 == count);
+ /* List the metadata of this export. */
+ desc = nbd_get_list_export_description (nbd1, i);
+ list_one_export (nbd2, desc, i == 0, i + 1 == count);
- nbd_close (nbd2);
- free (new_uri);
- free (desc);
- xmlFreeURI (xmluri); /* this also frees xmluri->path == new_path */
- }
+ nbd_close (nbd2);
+ free (desc);
free (name);
}
}
--
2.28.0