openvz: refactor openvzConnectListDomains

Use g_auto where possible, reduce scope of some variables and remove
pointless ret and rc variables.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Ján Tomko 2021-12-13 19:13:24 +01:00
parent 0681365dc3
commit 80134168fe

View File

@ -1334,44 +1334,37 @@ static int openvzConnectListDomains(virConnectPtr conn G_GNUC_UNUSED,
int *ids, int nids) int *ids, int nids)
{ {
int got = 0; int got = 0;
int veid; VIR_AUTOCLOSE outfd = -1;
int outfd = -1; g_autoptr(virCommand) cmd = virCommandNewArgList(VZLIST, "-ovpsid", "-H", NULL);
int rc = -1;
int ret;
char buf[32];
char *endptr;
virCommand *cmd = virCommandNewArgList(VZLIST, "-ovpsid", "-H", NULL);
virCommandSetOutputFD(cmd, &outfd); virCommandSetOutputFD(cmd, &outfd);
if (virCommandRunAsync(cmd, NULL) < 0) if (virCommandRunAsync(cmd, NULL) < 0)
goto cleanup; return -1;
while (got < nids) { while (got < nids) {
ret = openvz_readline(outfd, buf, 32); char *endptr;
if (!ret) char buf[32];
int veid;
if (openvz_readline(outfd, buf, 32) == 0)
break; break;
if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) { if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not parse VPS ID %s"), buf); _("Could not parse VPS ID %s"), buf);
continue; continue;
} }
ids[got] = veid; ids[got++] = veid;
got ++;
} }
if (virCommandWait(cmd, NULL) < 0) if (virCommandWait(cmd, NULL) < 0)
goto cleanup; return -1;
if (VIR_CLOSE(outfd) < 0) { if (VIR_CLOSE(outfd) < 0) {
virReportSystemError(errno, "%s", _("failed to close file")); virReportSystemError(errno, "%s", _("failed to close file"));
goto cleanup; return -1;
} }
rc = got; return got;
cleanup:
VIR_FORCE_CLOSE(outfd);
virCommandFree(cmd);
return rc;
} }
static int openvzConnectNumOfDomains(virConnectPtr conn) static int openvzConnectNumOfDomains(virConnectPtr conn)