From: John Wolfe Date: Mon, 9 Nov 2020 20:29:03 +0000 (-0800) Subject: Misc cleanup of "xferlogs" utility. X-Git-Tag: stable-11.3.0~255 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e51d9ca883dc892c126ba231ece82d2cc87898e7;p=thirdparty%2Fopen-vm-tools.git Misc cleanup of "xferlogs" utility. 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. --- diff --git a/open-vm-tools/xferlogs/Makefile.am b/open-vm-tools/xferlogs/Makefile.am index cfe490851..80fec0e88 100644 --- a/open-vm-tools/xferlogs/Makefile.am +++ b/open-vm-tools/xferlogs/Makefile.am @@ -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 @@ -18,7 +18,19 @@ 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 diff --git a/open-vm-tools/xferlogs/xferlogs.c b/open-vm-tools/xferlogs/xferlogs.c index d3c6712c2..40d9aad60 100644 --- a/open-vm-tools/xferlogs/xferlogs.c +++ b/open-vm-tools/xferlogs/xferlogs.c @@ -43,6 +43,7 @@ #include #include #include +#include #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 \n"); - Warning("options:\n"); - Warning("\t-h | --help - prints this usage.\n"); - Warning("\tenc - encodes and transfers to the VMX log.\n"); - Warning("\tdec - extracts encoded data to 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 to the VMX log.", ""}, + {"get", 'g', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_FILENAME, &decBuffer, + "extracts encoded data to from the VMX log.", ""}, + {"update", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, + &vmsupportStatus, "updates status of vmsupport to .", + ""}, + {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; +}