From: "Richard W.M. Jones" <rjones(a)redhat.com>
Original drv_uri fields:
xmlURIPtr uri; /* URI */
char *socket; /* ?socket parameter from URI. */
const char *format; /* format (NULL == autodetect) */
New drv_uri fields:
char *path; /* disk path */
char *protocol; /* protocol (eg. "nbd") */
char **server; /* server(s) - can be NULL */
char *username; /* username - can be NULL */
const char *format; /* format (NULL == autodetect) */
const char *orig_uri; /* original URI (for error messages etc.) */
This is just code motion.
(cherry picked from commit a3891430bc9fe408505277701962acaa664fc96a)
---
df/main.c | 7 ++-----
fish/options.c | 62 +++++++++++++++++++++++++++++++++++++++++++---------------
fish/options.h | 9 +++++----
3 files changed, 53 insertions(+), 25 deletions(-)
diff --git a/df/main.c b/df/main.c
index e208e9a..5524070 100644
--- a/df/main.c
+++ b/df/main.c
@@ -30,8 +30,6 @@
#include <assert.h>
#include <libintl.h>
-#include <libxml/uri.h>
-
#ifdef HAVE_LIBVIRT
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
@@ -337,10 +335,9 @@ single_drive_display_name (struct drv *drvs)
break;
case drv_uri:
- name = (char *) xmlSaveUri (drvs->uri.uri);
+ name = strdup (drvs->uri.orig_uri);
if (name == NULL) {
- fprintf (stderr, _("%s: xmlSaveUri: could not make printable URI\n"),
- program_name);
+ perror ("strdup");
exit (EXIT_FAILURE);
}
/* Try to shorten the URI to just the final element, if it will
diff --git a/fish/options.c b/fish/options.c
index 387ce79..787402c 100644
--- a/fish/options.c
+++ b/fish/options.c
@@ -35,6 +35,7 @@
static int is_uri (const char *arg);
static void parse_uri (const char *arg, const char *format, struct drv *drv);
static char *query_get (xmlURIPtr uri, const char *search_name);
+static char **make_server (xmlURIPtr uri, const char *socket);
/* Handle the '-a' option when passed on the command line. */
void
@@ -95,8 +96,12 @@ is_uri (const char *arg)
static void
parse_uri (const char *arg, const char *format, struct drv *drv)
{
- xmlURIPtr uri;
- char *socket;
+ CLEANUP_XMLFREEURI xmlURIPtr uri = NULL;
+ CLEANUP_FREE char *socket = NULL;
+ char *path;
+ char *protocol;
+ char **server;
+ char *username;
uri = xmlParseURI (arg);
if (!uri) {
@@ -132,11 +137,37 @@ parse_uri (const char *arg, const char *format, struct drv *drv)
}
*/
+ protocol = strdup (uri->scheme);
+ if (protocol == NULL) {
+ perror ("strdup");
+ exit (EXIT_FAILURE);
+ }
+
+ server = make_server (uri, socket);
+
+ if (uri->user && STRNEQ (uri->user, "")) {
+ username = strdup (uri->user);
+ if (!username) {
+ perror ("username");
+ exit (EXIT_FAILURE);
+ }
+ }
+ else username = NULL;
+
+ path = strdup (uri->path ? uri->path : "");
+ if (!path) {
+ perror ("path");
+ exit (EXIT_FAILURE);
+ }
+
drv->type = drv_uri;
drv->nr_drives = -1;
- drv->uri.uri = uri;
- drv->uri.socket = socket;
+ drv->uri.path = path;
+ drv->uri.protocol = protocol;
+ drv->uri.server = server;
+ drv->uri.username = username;
drv->uri.format = format;
+ drv->uri.orig_uri = arg;
}
/* Code inspired by libvirt src/util/viruri.c, written by danpb,
@@ -285,7 +316,6 @@ add_drives (struct drv *drv, char next_drive)
{
int r;
struct guestfs_add_drive_opts_argv ad_optargs;
- char **server;
if (next_drive > 'z') {
fprintf (stderr,
@@ -336,22 +366,20 @@ add_drives (struct drv *drv, char next_drive)
ad_optargs.format = drv->uri.format;
}
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_PROTOCOL_BITMASK;
- ad_optargs.protocol = drv->uri.uri->scheme;
- ad_optargs.server = server = make_server (drv->uri.uri, drv->uri.socket);
- if (server)
+ ad_optargs.protocol = drv->uri.protocol;
+ if (drv->uri.server) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_SERVER_BITMASK;
- if (drv->uri.uri->user && STRNEQ (drv->uri.uri->user,
"")) {
+ ad_optargs.server = drv->uri.server;
+ }
+ if (drv->uri.username) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_USERNAME_BITMASK;
- ad_optargs.username = drv->uri.uri->user;
+ ad_optargs.username = drv->uri.username;
}
- r = guestfs_add_drive_opts_argv (g, drv->uri.uri->path ? : "",
- &ad_optargs);
+ r = guestfs_add_drive_opts_argv (g, drv->uri.path, &ad_optargs);
if (r == -1)
exit (EXIT_FAILURE);
- guestfs___free_string_list (server);
-
drv->nr_drives = 1;
next_drive++;
break;
@@ -463,8 +491,10 @@ free_drives (struct drv *drv)
/* a.filename and a.format are optargs, don't free them */
break;
case drv_uri:
- xmlFreeURI (drv->uri.uri);
- free (drv->uri.socket);
+ free (drv->uri.path);
+ free (drv->uri.protocol);
+ guestfs___free_string_list (drv->uri.server);
+ free (drv->uri.username);
break;
case drv_d:
/* d.filename is optarg, don't free it */
diff --git a/fish/options.h b/fish/options.h
index bdfabb2..507ec1c 100644
--- a/fish/options.h
+++ b/fish/options.h
@@ -21,8 +21,6 @@
#include <getopt.h>
-#include <libxml/uri.h>
-
#include "guestfs-internal-frontend.h"
/* Provided by guestfish or guestmount. */
@@ -64,9 +62,12 @@ struct drv {
const char *format; /* format (NULL == autodetect) */
} a;
struct {
- xmlURIPtr uri; /* URI */
- char *socket; /* ?socket parameter from URI. */
+ char *path; /* disk path */
+ char *protocol; /* protocol (eg. "nbd") */
+ char **server; /* server(s) - can be NULL */
+ char *username; /* username - can be NULL */
const char *format; /* format (NULL == autodetect) */
+ const char *orig_uri; /* original URI (for error messages etc.) */
} uri;
struct {
char *guest; /* guest name */
--
1.8.3.1