util: storage: Properly parse URIs with missing trailing slash

The URI parser used by libvirt does not populate uri->path if the
trailing slash is missing. The code virStorageSourceParseBackingURI
would then not populate src->path.

As only NBD network disks are allowed to have the 'name' field in the
XML defining the disk source omitted we'd generate an invalid XML which
we'd not parse again.

Fix it by populating src->path with an empty string if the uri is
lacking slash.

As pointed out above NBD is special in this case since we actually allow
it being NULL. The URI path is used as export name. Since an empty
export does not make sense the new approach clears the src->path if the
trailing slash is present but nothing else.

Add test cases now to cover all the various cases for NBD and non-NBD
uris as there was to time only 1 test abusing the quirk witout slash for
NBD and all other URIs contained the slash or in case of NBD also the
export name.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Peter Krempa 2018-10-11 12:22:21 +02:00
parent 4471f7704c
commit 6e7e965dcd
3 changed files with 41 additions and 4 deletions

View File

@ -2579,6 +2579,7 @@ virStorageSourceParseBackingURI(virStorageSourcePtr src,
const char *uristr)
{
virURIPtr uri = NULL;
const char *path = NULL;
char **scheme = NULL;
int ret = -1;
@ -2621,10 +2622,23 @@ virStorageSourceParseBackingURI(virStorageSourcePtr src,
/* XXX We currently don't support auth, so don't bother parsing it */
/* possibly skip the leading slash */
if (uri->path &&
VIR_STRDUP(src->path,
*uri->path == '/' ? uri->path + 1 : uri->path) < 0)
/* uri->path is NULL if the URI does not contain slash after host:
* transport://host:port */
if (uri->path)
path = uri->path;
else
path = "";
/* possibly skip the leading slash */
if (path[0] == '/')
path++;
/* NBD allows empty export name (path) */
if (src->protocol == VIR_STORAGE_NET_PROTOCOL_NBD &&
path[0] == '\0')
path = NULL;
if (VIR_STRDUP(src->path, path) < 0)
goto cleanup;
if (src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {

View File

@ -373,6 +373,9 @@ mymain(void)
/* type VIR_STORAGE_TYPE_BLOCK is not tested since it parses back to 'file' */
/* type VIR_STORAGE_TYPE_DIR it is a 'format' driver in qemu */
TEST_JSON_FORMAT_NET("<source protocol='http' name=''>\n"
" <host name='example.com' port='80'/>\n"
"</source>\n");
TEST_JSON_FORMAT_NET("<source protocol='http' name='file'>\n"
" <host name='example.com' port='80'/>\n"
"</source>\n");

View File

@ -1303,6 +1303,14 @@ mymain(void)
TEST_BACKING_PARSE("path", "<source file='path'/>\n");
TEST_BACKING_PARSE("://", NULL);
TEST_BACKING_PARSE("http://example.com",
"<source protocol='http' name=''>\n"
" <host name='example.com' port='80'/>\n"
"</source>\n");
TEST_BACKING_PARSE("http://example.com/",
"<source protocol='http' name=''>\n"
" <host name='example.com' port='80'/>\n"
"</source>\n");
TEST_BACKING_PARSE("http://example.com/file",
"<source protocol='http' name='file'>\n"
" <host name='example.com' port='80'/>\n"
@ -1315,6 +1323,18 @@ mymain(void)
"<source protocol='nbd' name='blah'>\n"
" <host name='example.org' port='6000'/>\n"
"</source>\n");
TEST_BACKING_PARSE("nbd://example.org:1234",
"<source protocol='nbd'>\n"
" <host name='example.org' port='1234'/>\n"
"</source>\n");
TEST_BACKING_PARSE("nbd://example.org:1234/",
"<source protocol='nbd'>\n"
" <host name='example.org' port='1234'/>\n"
"</source>\n");
TEST_BACKING_PARSE("nbd://example.org:1234/exportname",
"<source protocol='nbd' name='exportname'>\n"
" <host name='example.org' port='1234'/>\n"
"</source>\n");
#ifdef WITH_YAJL
TEST_BACKING_PARSE("json:", NULL);