[libvirt] [PATCH 11/18] tpm: Refactor virTPMEmulatorInit to use loop

Marc-André Lureau marcandre.lureau at redhat.com
Tue Jul 9 20:23:38 UTC 2019


On Tue, Jul 9, 2019 at 9:24 PM Stefan Berger <stefanb at linux.vnet.ibm.com> wrote:
>
> Refactor virTPMEmulatorInit to use a loop with parameters. This allows
> for easier extension later on.
>
> Signed-off-by: Stefan Berger <stefanb at linux.ibm.com>

for the refactoring,
Reviewed-by: Marc-André Lureau <marcandre.lureau at redhat.com>


> ---
>  src/tpm/virtpm.c | 80 ++++++++++++++++++++++--------------------------
>  1 file changed, 37 insertions(+), 43 deletions(-)
>
> diff --git a/src/tpm/virtpm.c b/src/tpm/virtpm.c
> index 4635d8add0..0179b1e8be 100644
> --- a/src/tpm/virtpm.c
> +++ b/src/tpm/virtpm.c
> @@ -113,51 +113,45 @@ virTPMGetSwtpmIoctl(void)
>  int
>  virTPMEmulatorInit(void)
>  {
> -    if (!swtpm_path) {
> -        swtpm_path = virFindFileInPath("swtpm");
> -        if (!swtpm_path) {
> -            virReportSystemError(ENOENT, "%s",
> -                                 _("Unable to find 'swtpm' binary in $PATH"));
> -            return -1;
> +    static const struct {
> +        const char *name;
> +        char **path;
> +    } prgs[] = {
> +        {
> +            .name = "swtpm",
> +            .path = &swtpm_path,
> +        },
> +        {
> +            .name = "swtpm_setup",
> +            .path = &swtpm_setup,
> +        },
> +        {
> +            .name = "swtpm_ioctl",
> +            .path = &swtpm_ioctl,
>          }
> -        if (!virFileIsExecutable(swtpm_path)) {
> -            virReportError(VIR_ERR_INTERNAL_ERROR,
> -                           _("TPM emulator %s is not an executable"),
> -                           swtpm_path);
> -            VIR_FREE(swtpm_path);
> -            return -1;
> -        }
> -    }
> -
> -    if (!swtpm_setup) {
> -        swtpm_setup = virFindFileInPath("swtpm_setup");
> -        if (!swtpm_setup) {
> -            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> -                           _("Could not find 'swtpm_setup' in PATH"));
> -            return -1;
> -        }
> -        if (!virFileIsExecutable(swtpm_setup)) {
> -            virReportError(VIR_ERR_INTERNAL_ERROR,
> -                           _("'%s' is not an executable"),
> -                           swtpm_setup);
> -            VIR_FREE(swtpm_setup);
> -            return -1;
> -        }
> -    }
> +    };
> +    size_t i;
>
> -    if (!swtpm_ioctl) {
> -        swtpm_ioctl = virFindFileInPath("swtpm_ioctl");
> -        if (!swtpm_ioctl) {
> -            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> -                           _("Could not find swtpm_ioctl in PATH"));
> -            return -1;
> -        }
> -        if (!virFileIsExecutable(swtpm_ioctl)) {
> -            virReportError(VIR_ERR_INTERNAL_ERROR,
> -                           _("swtpm_ioctl program %s is not an executable"),
> -                           swtpm_ioctl);
> -            VIR_FREE(swtpm_ioctl);
> -            return -1;
> +    for (i = 0; i < ARRAY_CARDINALITY(prgs); i++) {
> +        char *path;
> +        bool findit = *prgs[i].path == NULL;
> +
> +        if (findit) {
> +            path = virFindFileInPath(prgs[i].name);
> +            if (!path) {
> +                virReportSystemError(ENOENT,
> +                                _("Unable to find '%s' binary in $PATH"),
> +                                prgs[i].name);
> +                return -1;
> +            }
> +            if (!virFileIsExecutable(path)) {
> +                virReportError(VIR_ERR_INTERNAL_ERROR,
> +                               _("%s is not an executable"),
> +                               path);
> +                VIR_FREE(path);
> +                return -1;
> +            }
> +            *prgs[i].path = path;
>          }
>      }
>
> --
> 2.20.1
>




More information about the libvir-list mailing list