/* $Id: VBoxServiceToolBox.cpp 69753 2017-11-19 14:27:58Z vboxsync $ */ /** @file * VBoxServiceToolbox - Internal (BusyBox-like) toolbox. */ /* * Copyright (C) 2012-2017 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. */ /********************************************************************************************************************************* * Header Files * *********************************************************************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef RT_OS_WINDOWS # include /* need umask */ #endif #include #include #include #include "VBoxServiceInternal.h" #include "VBoxServiceToolBox.h" #include "VBoxServiceUtils.h" using namespace guestControl; /********************************************************************************************************************************* * Defined Constants And Macros * *********************************************************************************************************************************/ /** Generic option indices for commands. */ enum { VBOXSERVICETOOLBOXOPT_MACHINE_READABLE = 1000, VBOXSERVICETOOLBOXOPT_VERBOSE }; /** Options indices for "vbox_cat". */ typedef enum VBOXSERVICETOOLBOXCATOPT { VBOXSERVICETOOLBOXCATOPT_NO_CONTENT_INDEXED = 1000 } VBOXSERVICETOOLBOXCATOPT; /** Flags for "vbox_ls". */ typedef enum VBOXSERVICETOOLBOXLSFLAG { VBOXSERVICETOOLBOXLSFLAG_NONE = 0x0, VBOXSERVICETOOLBOXLSFLAG_RECURSIVE = 0x1, VBOXSERVICETOOLBOXLSFLAG_SYMLINKS = 0x2 } VBOXSERVICETOOLBOXLSFLAG; /** Flags for fs object output. */ typedef enum VBOXSERVICETOOLBOXOUTPUTFLAG { VBOXSERVICETOOLBOXOUTPUTFLAG_NONE = 0x0, VBOXSERVICETOOLBOXOUTPUTFLAG_LONG = 0x1, VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE = 0x2 } VBOXSERVICETOOLBOXOUTPUTFLAG; /********************************************************************************************************************************* * Prototypes * *********************************************************************************************************************************/ static RTEXITCODE vgsvcToolboxCat(int argc, char **argv); static RTEXITCODE vgsvcToolboxLs(int argc, char **argv); static RTEXITCODE vgsvcToolboxRm(int argc, char **argv); static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv); static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv); static RTEXITCODE vgsvcToolboxStat(int argc, char **argv); /********************************************************************************************************************************* * Structures and Typedefs * *********************************************************************************************************************************/ /** Pointer to a tool handler function. */ typedef RTEXITCODE (*PFNHANDLER)(int , char **); /** Definition for a specific toolbox tool. */ typedef struct VBOXSERVICETOOLBOXTOOL { /** Friendly name of the tool. */ const char *pszName; /** Main handler to be invoked to use the tool. */ RTEXITCODE (*pfnHandler)(int argc, char **argv); /** Conversion routine to convert the tool's exit code back to an IPRT rc. Optional. * * @todo r=bird: You better revert this, i.e. having pfnHandler return a VBox * status code and have a routine for converting it to RTEXITCODE. * Unless, what you really want to do here is to get a cached status, in * which case you better call it what it is. */ int (*pfnExitCodeConvertToRc)(RTEXITCODE rcExit); } VBOXSERVICETOOLBOXTOOL; /** Pointer to a const tool definition. */ typedef VBOXSERVICETOOLBOXTOOL const *PCVBOXSERVICETOOLBOXTOOL; /** * An file/directory entry. Used to cache * file names/paths for later processing. */ typedef struct VBOXSERVICETOOLBOXPATHENTRY { /** Our node. */ RTLISTNODE Node; /** Name of the entry. */ char *pszName; } VBOXSERVICETOOLBOXPATHENTRY, *PVBOXSERVICETOOLBOXPATHENTRY; typedef struct VBOXSERVICETOOLBOXDIRENTRY { /** Our node. */ RTLISTNODE Node; /** The actual entry. */ RTDIRENTRYEX dirEntry; } VBOXSERVICETOOLBOXDIRENTRY, *PVBOXSERVICETOOLBOXDIRENTRY; /********************************************************************************************************************************* * Global Variables * *********************************************************************************************************************************/ /** Tool definitions. */ static VBOXSERVICETOOLBOXTOOL const g_aTools[] = { { VBOXSERVICE_TOOL_CAT, vgsvcToolboxCat , NULL }, { VBOXSERVICE_TOOL_LS, vgsvcToolboxLs , NULL }, { VBOXSERVICE_TOOL_RM, vgsvcToolboxRm , NULL }, { VBOXSERVICE_TOOL_MKTEMP, vgsvcToolboxMkTemp, NULL }, { VBOXSERVICE_TOOL_MKDIR, vgsvcToolboxMkDir , NULL }, { VBOXSERVICE_TOOL_STAT, vgsvcToolboxStat , NULL } }; /** * Displays a common header for all help text to stdout. */ static void vgsvcToolboxShowUsageHeader(void) { RTPrintf(VBOX_PRODUCT " Guest Toolbox Version " VBOX_VERSION_STRING "\n" "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n" "All rights reserved.\n" "\n"); RTPrintf("Usage:\n\n"); } /** * Displays a help text to stdout. */ static void vgsvcToolboxShowUsage(void) { vgsvcToolboxShowUsageHeader(); RTPrintf(" VBoxService [--use-toolbox] vbox_ [] \n\n" "General options:\n\n" " --machinereadable produce all output in machine-readable form\n" " -V print version number and exit\n" "\n" "Commands:\n\n" " cat [] ...\n" " ls [] [--dereference|-L] [-l] [-R]\n" " [--verbose|-v] [...]\n" " rm [] [-r|-R] ...\n" " mktemp [] [--directory|-d] [--mode|-m ]\n" " [--secure|-s] [--tmpdir|-t ]\n" "