]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
AppInfo Enhancement: Remove duplicate applications.
authorJohn Wolfe <jwolfe@vmware.com>
Mon, 8 Nov 2021 21:33:58 +0000 (13:33 -0800)
committerJohn Wolfe <jwolfe@vmware.com>
Mon, 8 Nov 2021 21:33:58 +0000 (13:33 -0800)
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.

open-vm-tools/lib/include/conf.h
open-vm-tools/services/plugins/appInfo/appInfo.c
open-vm-tools/tools.conf

index 766b542214a92d2db8c6df84d13de65a23a4f124..62abec5f72b1ee2cfc698eb336bbfba9122ad22c 100644 (file)
  */
 #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.
index 0f70f4c44241ea7eb3a7d4a491907c684d2a7fae..d6be49def55f8d8d5886532a20e280093af195b4 100644 (file)
@@ -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);
 }
index 1682c3c44b5520b69f948ed2dbdd022b1e563c24..79f634f53a7bf0ea291e5c5219ae363515b00bb7 100644 (file)
 # 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.