On Mon, Mar 18, 2019 at 12:20:17PM +0000, Daniel P. Berrangé wrote:
The rbd_list method has a quite unpleasant signature returning an array of strings in a single buffer instead of an array. It is being deprecated in favour of rbd_list2. To maintain clarity of code when supporting both APIs in parallel, split the rbd_list code out into a separate method.
Also change the VIR_WARN to report a proper error.
Signed-off-by: Daniel P. Berrangé <[email protected]> --- src/storage/storage_backend_rbd.c | 84 ++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c index 3eae780c44..24ddbc8f69 100644 --- a/src/storage/storage_backend_rbd.c +++ b/src/storage/storage_backend_rbd.c @@ -565,19 +565,68 @@ volStorageBackendRBDRefreshVolInfo(virStorageVolDefPtr vol, return ret; } + +static char ** +virStorageBackendRBDGetVolNames(virStorageBackendRBDStatePtr ptr) +{ + char **names = NULL; + size_t nnames = 0; + int ret;
We usually use 'ret' for the return value of the current function and 'rc' for the values returned by callees.
+ size_t max_size = 1024;
+ VIR_AUTOFREE(char *) namebuf = NULL;
+ const char *name;
+
+ while (true) {
+ if (VIR_ALLOC_N(namebuf, max_size) < 0)
+ goto error;
+
+ ret = rbd_list(ptr->ioctx, namebuf, &max_size);
+ if (ret >= 0)
+ break;
+ if (ret != -ERANGE) {
+ virReportSystemError(-ret, "%s", _("Unable to list RBD images"));
+ goto error;
+ }
+ VIR_FREE(namebuf);
+ }
+
+ for (name = namebuf; name < namebuf + max_size;) {
Strange, this line is winking at me.
+ VIR_AUTOFREE(char *) namedup = NULL; + + if (STREQ(name, "")) + break; + + if (VIR_STRDUP(namedup, name) < 0) + goto error; + + if (VIR_APPEND_ELEMENT(names, nnames, namedup) < 0) + goto error; + + name += strlen(name) + 1; + } +
[...]
- for (name = names; name < names + max_size;) {
+ for (i = 0; names[i] != NULL; i++) {
VIR_AUTOPTR(virStorageVolDef) vol = NULL;
- if (STREQ(name, ""))
- break;
-
if (VIR_ALLOC(vol) < 0)
goto cleanup;
- if (VIR_STRDUP(vol->name, name) < 0)
- goto cleanup;
-
- name += strlen(name) + 1;
+ vol->name = names[i]; + names[i] = NULL;
This can be: VIR_STEAL_PTR(vol->name, names[i]);
r = volStorageBackendRBDRefreshVolInfo(vol, pool, ptr);
Reviewed-by: Ján Tomko <[email protected]> Jano
signature.asc
Description: Digital signature
-- libvir-list mailing list [email protected] https://www.redhat.com/mailman/listinfo/libvir-list
