]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Misc cleanup of "xferlogs" utility.
authorJohn Wolfe <jwolfe@vmware.com>
Mon, 9 Nov 2020 20:29:03 +0000 (12:29 -0800)
committerJohn Wolfe <jwolfe@vmware.com>
Mon, 9 Nov 2020 20:29:03 +0000 (12:29 -0800)
Update the xferlogs usage message to include a 'upd' option, and use
glib to parse and print usage.  Remove the dependency on the vmtools
library.

open-vm-tools/xferlogs/Makefile.am
open-vm-tools/xferlogs/xferlogs.c

index cfe4908510b9e0ffaff0a3aef0a7033501b441a8..80fec0e88b1d57c74516591475f0faf2005816eb 100644 (file)
@@ -1,5 +1,5 @@
 ################################################################################
-### Copyright (C) 2007-2016 VMware, Inc.  All rights reserved.
+### Copyright (c) 2007-2016,2020 VMware, Inc.  All rights reserved.
 ###
 ### This program is free software; you can redistribute it and/or modify
 ### it under the terms of version 2 of the GNU General Public License as
 bin_PROGRAMS = vmware-xferlogs
 
 vmware_xferlogs_LDADD =
-vmware_xferlogs_LDADD += @VMTOOLS_LIBS@
+vmware_xferlogs_LDADD += @GLIB2_LIBS@
+vmware_xferlogs_LDADD += ../lib/rpcOut/libRpcOut.la
+vmware_xferlogs_LDADD += ../lib/rpcVmx/libRpcVmx.la
+vmware_xferlogs_LDADD += ../lib/message/libMessage.la
+vmware_xferlogs_LDADD += ../lib/backdoor/libBackdoor.la
+vmware_xferlogs_LDADD += ../lib/string/libString.la
+vmware_xferlogs_LDADD += ../lib/vmCheck/libVmCheck.la
+vmware_xferlogs_LDADD += ../lib/vmSignal/libVmSignal.la
+vmware_xferlogs_LDADD += ../lib/misc/libMisc.la
+vmware_xferlogs_LDADD += ../lib/stubs/libStubs.la
+
+vmware_xferlogs_CPPFLAGS :=
+vmware_xferlogs_CPPFLAGS += @GLIB2_CPPFLAGS@
 
 vmware_xferlogs_SOURCES =
 vmware_xferlogs_SOURCES += xferlogs.c
index d3c6712c2f1ad1903a969e3b554299efa5490bd1..40d9aad60994f303d0ae1c20cc96d798b8757ebd 100644 (file)
@@ -43,6 +43,7 @@
 #include <string.h>
 #include <errno.h>
 #include <time.h>
+#include <glib.h>
 
 #include "vmware.h"
 #include "vmsupport.h"
@@ -304,13 +305,11 @@ extractFile(char *filename) //IN: vmx log filename e.g. vmware.log
 
 
 static void
-usage(void)
+usage(GOptionContext *optCtx)
 {
-   Warning("xferlogs <options> <filename>\n");
-   Warning("options:\n");
-   Warning("\t-h | --help - prints this usage.\n");
-   Warning("\tenc - encodes and transfers <filename> to the VMX log.\n");
-   Warning("\tdec - extracts encoded data to <filename> from the VMX log.\n");
+   gchar *usage =  g_option_context_get_help(optCtx, TRUE, NULL);
+   g_print("%s", usage);
+   g_free(usage);
 }
 
 
@@ -318,47 +317,116 @@ int
 main(int argc,
      char *argv[])
 {
+   gchar *gAppName;
+   gchar *encBuffer = NULL; // storage for filename for 'enc' option
+   gchar *decBuffer = NULL; // storage for filename for 'dec' option
+   gchar *vmsupportStatus = NULL; // storage for vmsupport status for 'upd' opt
+   int success = -1;
    int status;
+   /*
+    * This flag will be true if option passed starts with '-'.
+    * This means we can use glib parser to see if it is a
+    * valid option. Otherwise, try original parsing.
+    */
+   gboolean useGlibParser = (argc > 1) && (argv[1][0] == '-');
+
+   GOptionEntry options[] = {
+      {"put", 'p', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_FILENAME, &encBuffer,
+       "encodes and transfers <filename> to the VMX log.", "<filename>"},
+      {"get", 'g', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_FILENAME, &decBuffer,
+       "extracts encoded data to <filename> from the VMX log.", "<filename>"},
+      {"update", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
+       &vmsupportStatus, "updates status of vmsupport to <status>.",
+       "<status>"},
+      {NULL},
+   };
+   GOptionContext *optCtx;
 
 #ifdef _WIN32
    WinUtil_EnableSafePathSearching(TRUE);
 #endif
 
+   gAppName = g_path_get_basename(argv[0]);
+   g_set_prgname(gAppName);
+   optCtx = g_option_context_new(NULL);
+   g_option_context_add_main_entries(optCtx, options, NULL);
+
    /*
     * Check if environment is a VM
     */
    if (!VmCheck_IsVirtualWorld()) {
-      fprintf(stderr, "Error: %s must be run inside a virtual machine"
-                      " on a VMware hypervisor product.\n", argv[0]);
-      return -1;
+      g_printerr("Error: %s must be run inside a virtual machine"
+                 " on a VMware hypervisor product.\n", gAppName);
+      goto out;
    }
 
-   if (argc == 2 &&
-       (!strncmp(argv[1], "-h", 2) ||
-        !strncmp(argv[1], "--help", 6))) {
-      usage();
-      return 0;
-   }
+   if (useGlibParser) {
+     /*
+      * numOptions will count the number of options passed
+      * and fail if more than one option is passed.
+      */
+      int numOptions;
+      GError *gErr = NULL;
+
+      if (!g_option_context_parse(optCtx, &argc, &argv, &gErr)) {
+         g_printerr("%s: %s\n", gAppName, gErr->message);
+         g_error_free(gErr);
+         goto out;
+      }
 
-   if (argc != 3) {
-      usage();
-      return -1;
+      numOptions = (encBuffer != NULL ? 1 : 0) + (decBuffer != NULL ? 1 : 0) +
+                   (vmsupportStatus != NULL ? 1 : 0);
+
+      if (numOptions > 1) {
+         g_printerr("%s: Use one option per command.\n", gAppName);
+         usage(optCtx);
+         goto out;
+      }
+   } else {
+      /*
+       * If not using glib parser then check for old style options
+       */
+      if (argc != 3) {
+         g_printerr("%s: Incorrect number of arguments.\n", gAppName);
+         usage(optCtx);
+         goto out;
+      }
+
+      if ((strncmp(argv[1], "enc", 3) == 0)) {
+         encBuffer = argv[2];
+      } else if ((strncmp(argv[1], "dec", 3) == 0)) {
+         decBuffer = argv[2];
+      } else if ((strncmp(argv[1], "upd", 3) == 0)) {
+         vmsupportStatus = argv[2];
+      }
    }
 
-   if (!strncmp(argv[1], "enc", 3)) {
-      xmitFile(argv[2]);
-   } else if (!strncmp(argv[1], "dec", 3)) {
-      extractFile(argv[2]);
-   } else if (!strncmp(argv[1], "upd", 3)) {
-      if (StrUtil_StrToInt(&status, argv[2])) {
-         RpcOut_sendOne(NULL, NULL, RPC_VMSUPPORT_STATUS " %d", status);
-      } else {
-         return -1;
+   if (encBuffer != NULL) {
+      xmitFile(encBuffer);
+   } else if (decBuffer != NULL) {
+      extractFile(decBuffer);
+   } else if (vmsupportStatus != NULL) {
+      if (!(StrUtil_StrToInt(&status, vmsupportStatus))) {
+         g_printerr("%s: Bad value specified.\n", gAppName);
+         goto out;
       }
+      RpcOut_sendOne(NULL, NULL, RPC_VMSUPPORT_STATUS " %d", status);
    } else {
-      usage();
-      return -1;
+      g_printerr("%s: Incorrect usage.\n", gAppName);
+      usage(optCtx);
+      goto out;
    }
-   return 0;
-}
 
+   success = 0;
+
+out:
+   if (useGlibParser) {
+      g_free(encBuffer);
+      g_free(decBuffer);
+      g_free(vmsupportStatus);
+   }
+
+   g_option_context_free(optCtx);
+   g_free(gAppName);
+   return success;
+}