storage: zfs: Don't split string if we need only first/last component

Use str(r)chr to find the correct bit rather than fully splitting the
string.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-03-23 16:41:50 +01:00
parent 7f5c2ad88f
commit f443574193

View File

@ -102,7 +102,7 @@ virStorageBackendZFSParseVol(virStoragePoolObjPtr pool,
virStorageVolDefPtr volume = NULL; virStorageVolDefPtr volume = NULL;
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool); virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
g_auto(GStrv) tokens = NULL; g_auto(GStrv) tokens = NULL;
g_auto(GStrv) name_tokens = NULL; char *tmp;
if (!(tokens = virStringSplitCount(volume_string, "\t", 0, &count))) if (!(tokens = virStringSplitCount(volume_string, "\t", 0, &count)))
return -1; return -1;
@ -110,10 +110,9 @@ virStorageBackendZFSParseVol(virStoragePoolObjPtr pool,
if (count != 3) if (count != 3)
goto cleanup; goto cleanup;
if (!(name_tokens = virStringSplitCount(tokens[0], "/", 0, &count))) vol_name = tokens[0];
goto cleanup; if ((tmp = strrchr(vol_name, '/')))
vol_name = tmp + 1;
vol_name = name_tokens[count-1];
if (vol == NULL) if (vol == NULL)
volume = virStorageVolDefFindByName(pool, vol_name); volume = virStorageVolDefFindByName(pool, vol_name);
@ -218,7 +217,8 @@ virStorageBackendZFSRefreshPool(virStoragePoolObjPtr pool G_GNUC_UNUSED)
g_autoptr(virCommand) cmd = NULL; g_autoptr(virCommand) cmd = NULL;
g_auto(GStrv) lines = NULL; g_auto(GStrv) lines = NULL;
g_auto(GStrv) tokens = NULL; g_auto(GStrv) tokens = NULL;
g_auto(GStrv) name_tokens = NULL; g_autofree char *name = g_strdup(def->source.name);
char *tmp;
/** /**
* $ zpool get -Hp health,size,free,allocated test * $ zpool get -Hp health,size,free,allocated test
@ -230,13 +230,13 @@ virStorageBackendZFSRefreshPool(virStoragePoolObjPtr pool G_GNUC_UNUSED)
* *
* Here we just provide a list of properties we want to see * Here we just provide a list of properties we want to see
*/ */
if (!(name_tokens = g_strsplit(def->source.name, "/", 0))) if ((tmp = strchr(name, '/')))
goto cleanup; *tmp = '\0';
cmd = virCommandNewArgList(ZPOOL, cmd = virCommandNewArgList(ZPOOL,
"get", "-Hp", "get", "-Hp",
"health,size,free,allocated", "health,size,free,allocated",
name_tokens[0], name,
NULL); NULL);
virCommandSetOutputBuffer(cmd, &zpool_props); virCommandSetOutputBuffer(cmd, &zpool_props);
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)