From e715648e8ba193fc377fcf52d641f3c9175b44d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Wed, 24 Nov 2021 13:13:15 +0000 Subject: [PATCH] util: refactor TPM helper methods to reduce duplicationm MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The TPM helper methods for querying the binary path and capabilities have the same patterns across all swtpm binaries. This code duplication can be reduced by introducing helper methods. Reviewed-by: Ján Tomko Signed-off-by: Daniel P. Berrangé --- src/util/virtpm.c | 53 ++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/src/util/virtpm.c b/src/util/virtpm.c index 1a34f8ce56..cc9173cecd 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -109,49 +109,37 @@ static struct stat swtpm_ioctl_stat; typedef int (*virTPMBinaryCapsParse)(const char *); -char * -virTPMGetSwtpm(void) +static char * +virTPMBinaryGetPath(char **path_var) { char *s; - if (!swtpm_path && virTPMEmulatorInit() < 0) + if (!*path_var && virTPMEmulatorInit() < 0) return NULL; virMutexLock(&swtpm_tools_lock); - s = g_strdup(swtpm_path); + s = g_strdup(*path_var); virMutexUnlock(&swtpm_tools_lock); return s; } char * -virTPMGetSwtpmSetup(void) +virTPMGetSwtpm(void) { - char *s; - - if (!swtpm_setup_path && virTPMEmulatorInit() < 0) - return NULL; - - virMutexLock(&swtpm_tools_lock); - s = g_strdup(swtpm_setup_path); - virMutexUnlock(&swtpm_tools_lock); + return virTPMBinaryGetPath(&swtpm_path); +} - return s; +char * +virTPMGetSwtpmSetup(void) +{ + return virTPMBinaryGetPath(&swtpm_setup_path); } char * virTPMGetSwtpmIoctl(void) { - char *s; - - if (!swtpm_ioctl_path && virTPMEmulatorInit() < 0) - return NULL; - - virMutexLock(&swtpm_tools_lock); - s = g_strdup(swtpm_ioctl_path); - virMutexUnlock(&swtpm_tools_lock); - - return s; + return virTPMBinaryGetPath(&swtpm_ioctl_path); } /* virTPMExecGetCaps @@ -345,18 +333,23 @@ virTPMEmulatorInit(void) return ret; } -bool -virTPMSwtpmCapsGet(unsigned int cap) +static bool +virTPMBinaryGetCaps(virBitmap **caps_var, + unsigned int cap) { if (virTPMEmulatorInit() < 0) return false; - return virBitmapIsBitSet(swtpm_caps, cap); + return virBitmapIsBitSet(*caps_var, cap); +} + +bool +virTPMSwtpmCapsGet(unsigned int cap) +{ + return virTPMBinaryGetCaps(&swtpm_caps, cap); } bool virTPMSwtpmSetupCapsGet(unsigned int cap) { - if (virTPMEmulatorInit() < 0) - return false; - return virBitmapIsBitSet(swtpm_setup_caps, cap); + return virTPMBinaryGetCaps(&swtpm_setup_caps, cap); } -- 2.47.2