[libvirt] [PATCH 07/16] storage: Covert regex helpers to virCommand
Eric Blake
eblake at redhat.com
Wed May 11 21:34:05 UTC 2011
On 05/10/2011 02:07 PM, Cole Robinson wrote:
> @@ -1372,6 +1371,9 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
> int maxReg = 0, i, j;
> int totgroups = 0, ngroup = 0, maxvars = 0;
> char **groups;
> + virCommandPtr cmd = NULL;
> + const char * const *tmp;
No need for tmp...
> + pid_t pid = -1;
>
> /* Compile all regular expressions */
> if (VIR_ALLOC_N(reg, nregex) < 0) {
> @@ -1408,10 +1410,14 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
> goto cleanup;
> }
>
> + cmd = virCommandNew(prog[0]);
> + tmp = prog;
> + while (*(++tmp)) {
> + virCommandAddArg(cmd, *tmp);
> + }
...if you replace this with virCommandNewArgs(prog).
> +cleanup:
> + if (pid > 0 && virCommandWait(cmd, outexit) < 0)
> + ret = -1;
>
> - cleanup:
> if (groups) {
> for (j = 0 ; j < totgroups ; j++)
> VIR_FREE(groups[j]);
> @@ -1478,29 +1486,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
> VIR_FORCE_FCLOSE(list);
> VIR_FORCE_CLOSE(fd);
>
> - while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR);
> -
> - /* Don't bother checking exit status if we already failed */
> - if (failed)
> - return -1;
> -
> - if (err == -1) {
> - virReportSystemError(errno,
> - _("failed to wait for command '%s'"),
> - prog[0]);
> - return -1;
> - } else {
> - if (WIFEXITED(exitstatus)) {
> - if (outexit != NULL)
> - *outexit = WEXITSTATUS(exitstatus);
> - } else {
> - virStorageReportError(VIR_ERR_INTERNAL_ERROR,
> - "%s", _("command did not exit cleanly"));
> - return -1;
> - }
> - }
> -
> - return 0;
> + return ret;
This leaks the memory for cmd; you still need a call to
virCommandFree(cmd) somewhere in the cleanup.
> @@ -1540,9 +1527,14 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
> for (i = 0; i < n_columns; i++)
> v[i] = NULL;
>
> - /* Run the program and capture its output */
> - if (virExec(prog, NULL, NULL,
> - &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
> + cmd = virCommandNew(prog[0]);
> + tmp = prog;
> + while (*(++tmp)) {
> + virCommandAddArg(cmd, *tmp);
> + }
Again, virCommandNewArgs is more compact.
--
Eric Blake eblake at redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20110511/e78b3b83/attachment-0001.sig>
More information about the libvir-list
mailing list