From: John Wolfe Date: Mon, 8 Nov 2021 21:33:58 +0000 (-0800) Subject: AppInfo Enhancement: Remove duplicate applications. X-Git-Tag: stable-12.0.0~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0186a8af01f1dd09090cfcd1ddd5487d60c1026e;p=thirdparty%2Fopen-vm-tools.git AppInfo Enhancement: Remove duplicate applications. Update appinfo to remove duplicate applications from guestVar. Add a "remove-duplicates" tools.conf key, which is enabled by default. Setting "remove-duplicates=false" disables the functionality. --- diff --git a/open-vm-tools/lib/include/conf.h b/open-vm-tools/lib/include/conf.h index 766b54221..62abec5f7 100644 --- a/open-vm-tools/lib/include/conf.h +++ b/open-vm-tools/lib/include/conf.h @@ -84,6 +84,14 @@ */ #define CONFNAME_APPINFO_DISABLED "disabled" +/** + * Defines the configuration to remove duplicate applications. + * + * @param boolean Set to TRUE to remove duplicate apps. + * Set to FALSE to keep duplicate apps. + */ +#define CONFNAME_APPINFO_REMOVE_DUPLICATES "remove-duplicates" + /** * Defines the configuration to use the WMI for getting the application * version information. diff --git a/open-vm-tools/services/plugins/appInfo/appInfo.c b/open-vm-tools/services/plugins/appInfo/appInfo.c index 0f70f4c44..d6be49def 100644 --- a/open-vm-tools/services/plugins/appInfo/appInfo.c +++ b/open-vm-tools/services/plugins/appInfo/appInfo.c @@ -70,6 +70,14 @@ VM_EMBED_VERSION(VMTOOLSD_VERSION_STRING); */ #define APP_INFO_CONF_DEFAULT_DISABLED_VALUE FALSE +/** + * Default value for CONFNAME_APPINFO_REMOVE_DUPLICATES setting in + * tools configuration file. + * + * TRUE will remove duplicate applications. + */ +#define APP_INFO_CONF_DEFAULT_REMOVE_DUPLICATES TRUE + /** * Default value for CONFNAME_APPINFO_USE_WMI setting in * tools configuration file. @@ -243,6 +251,8 @@ AppInfoGatherTask(ToolsAppCtx *ctx, // IN GSList *appNode; static Atomic_uint64 updateCounter = {0}; uint64 counter = (uint64) Atomic_ReadInc64(&updateCounter) + 1; + GHashTable *appsAdded = NULL; + gchar *key = NULL; static char headerFmt[] = "{\n" "\"" APP_INFO_KEY_VERSION "\":\"%d\", \n" @@ -257,6 +267,11 @@ AppInfoGatherTask(ToolsAppCtx *ctx, // IN "\"" APP_INFO_KEY_APP_VERSION "\":\"%s\"" "}"; static char jsonSuffix[] = "]}"; + gboolean removeDup = + VMTools_ConfigGetBoolean(ctx->config, + CONFGROUPNAME_APPINFO, + CONFNAME_APPINFO_REMOVE_DUPLICATES, + APP_INFO_CONF_DEFAULT_REMOVE_DUPLICATES); DynBuf_Init(&dynBuffer); @@ -275,6 +290,10 @@ AppInfoGatherTask(ToolsAppCtx *ctx, // IN DynBuf_Append(&dynBuffer, tmpBuf, len); appList = AppInfo_SortAppList(AppInfo_GetAppList(ctx->config)); + if (removeDup) { + appsAdded = g_hash_table_new_full(g_str_hash, g_str_equal, + g_free, NULL); + } for (appNode = appList; appNode != NULL; appNode = appNode->next) { size_t currentBufferSize = DynBuf_GetSize(&dynBuffer); @@ -285,6 +304,16 @@ AppInfoGatherTask(ToolsAppCtx *ctx, // IN goto next_entry; } + if (removeDup) { + key = g_strdup_printf("%s|%s", appInfo->appName, appInfo->version); + /* + * If the key already exists, then this app is a duplicate. Free + * the key and move to the next application. + */ + if (g_hash_table_contains(appsAdded, key)) { + goto next_entry; + } + } escapedCmd = CodeSet_JsonEscape(appInfo->appName); if (NULL == escapedCmd) { @@ -325,8 +354,14 @@ AppInfoGatherTask(ToolsAppCtx *ctx, // IN } DynBuf_Append(&dynBuffer, tmpBuf, len); + if (removeDup) { + g_hash_table_add(appsAdded, key); + key = NULL; + } next_entry: + g_free(key); + key = NULL; free(escapedCmd); escapedCmd = NULL; free(escapedVersion); @@ -340,6 +375,10 @@ quit: free(escapedCmd); free(escapedVersion); AppInfo_DestroyAppList(appList); + if (appsAdded != NULL) { + g_hash_table_destroy(appsAdded); + } + g_free(key); g_free(tstamp); DynBuf_Destroy(&dynBuffer); } diff --git a/open-vm-tools/tools.conf b/open-vm-tools/tools.conf index 1682c3c44..79f634f53 100644 --- a/open-vm-tools/tools.conf +++ b/open-vm-tools/tools.conf @@ -259,6 +259,10 @@ # version info, otherwise native Win32 API is used. #useWMI=false +# Whether to remove the duplicate applications information in the +# guestinfo variable. +#remove-duplicates=true + [servicediscovery] # This plugin provides admins with additional info for better VM management.