提交 7dbbad35 编写于 作者: J Ján Tomko

Switch virCommandRunRegex to use virStringSplit

Instead of running the command asynchronously and reading the output
via fgets, let virCommand collect the output and split it with
virStringSplit.
上级 f2cc4286
...@@ -2779,15 +2779,16 @@ virCommandRunRegex(virCommandPtr cmd, ...@@ -2779,15 +2779,16 @@ virCommandRunRegex(virCommandPtr cmd,
void *data, void *data,
const char *prefix) const char *prefix)
{ {
int fd = -1, err, ret = -1; int err;
FILE *list = NULL;
regex_t *reg; regex_t *reg;
regmatch_t *vars = NULL; regmatch_t *vars = NULL;
char line[1024];
int maxReg = 0; int maxReg = 0;
size_t i, j; size_t i, j, k;
int totgroups = 0, ngroup = 0, maxvars = 0; int totgroups = 0, ngroup = 0, maxvars = 0;
char **groups; char **groups;
char *outbuf = NULL;
char **lines = NULL;
int ret = -1;
/* Compile all regular expressions */ /* Compile all regular expressions */
if (VIR_ALLOC_N(reg, nregex) < 0) if (VIR_ALLOC_N(reg, nregex) < 0)
...@@ -2818,29 +2819,27 @@ virCommandRunRegex(virCommandPtr cmd, ...@@ -2818,29 +2819,27 @@ virCommandRunRegex(virCommandPtr cmd,
if (VIR_ALLOC_N(vars, maxvars+1) < 0) if (VIR_ALLOC_N(vars, maxvars+1) < 0)
goto cleanup; goto cleanup;
virCommandSetOutputFD(cmd, &fd); virCommandSetOutputBuffer(cmd, &outbuf);
if (virCommandRunAsync(cmd, NULL) < 0) { if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
}
if ((list = VIR_FDOPEN(fd, "r")) == NULL) { if (!outbuf) {
virReportError(VIR_ERR_INTERNAL_ERROR, /* no output */
"%s", _("cannot read fd")); ret = 0;
goto cleanup; goto cleanup;
} }
while (fgets(line, sizeof(line), list) != NULL) { if (!(lines = virStringSplit(outbuf, "\n", 0)))
goto cleanup;
for (k = 0; lines[k]; k++) {
char *p = NULL; char *p = NULL;
/* Strip trailing newline */
int len = strlen(line);
if (len && line[len-1] == '\n')
line[len-1] = '\0';
/* ignore any command prefix */ /* ignore any command prefix */
if (prefix) if (prefix)
p = STRSKIP(line, prefix); p = STRSKIP(lines[k], prefix);
if (!p) if (!p)
p = line; p = lines[k];
for (i = 0; i <= maxReg && i < nregex; i++) { for (i = 0; i <= maxReg && i < nregex; i++) {
if (regexec(&reg[i], p, nvars[i]+1, vars, 0) == 0) { if (regexec(&reg[i], p, nvars[i]+1, vars, 0) == 0) {
...@@ -2872,8 +2871,10 @@ virCommandRunRegex(virCommandPtr cmd, ...@@ -2872,8 +2871,10 @@ virCommandRunRegex(virCommandPtr cmd,
} }
} }
ret = virCommandWait(cmd, NULL); ret = 0;
cleanup: cleanup:
virStringFreeList(lines);
VIR_FREE(outbuf);
if (groups) { if (groups) {
for (j = 0; j < totgroups; j++) for (j = 0; j < totgroups; j++)
VIR_FREE(groups[j]); VIR_FREE(groups[j]);
...@@ -2885,10 +2886,6 @@ cleanup: ...@@ -2885,10 +2886,6 @@ cleanup:
regfree(&reg[i]); regfree(&reg[i]);
VIR_FREE(reg); VIR_FREE(reg);
VIR_FORCE_FCLOSE(list);
VIR_FORCE_CLOSE(fd);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册