VBoxManageGuestCtrl.cpp revision dffef6e9475855654bf500dc63b510a987e8afa1
/* $Id$ */
/** @file
* VBoxManage - Implementation of guestcontrol command.
*/
/*
* Copyright (C) 2010-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* 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 "VBoxManage.h"
#ifndef VBOX_ONLY_DOCS
#include <map>
#include <vector>
#ifdef USE_XPCOM_QUEUE
# include <errno.h>
#endif
#include <signal.h>
#ifdef RT_OS_DARWIN
# include <CoreFoundation/CFRunLoop.h>
#endif
using namespace com;
/**
* IVirtualBoxCallback implementation for handling the GuestControlCallback in
* relation to the "guestcontrol * wait" command.
*/
/** @todo */
/** Set by the signal handler. */
static volatile bool g_fGuestCtrlCanceled = false;
typedef struct COPYCONTEXT
{
{
}
bool fVerbose;
bool fDryRun;
bool fHostToGuest;
} COPYCONTEXT, *PCOPYCONTEXT;
/**
* An entry for a source element, including an optional DOS-like wildcard (*,?).
*/
class SOURCEFILEENTRY
{
public:
SOURCEFILEENTRY(const char *pszSource)
{
}
const char* GetSource() const
{
}
const char* GetFilter() const
{
}
private:
{
if ( !RTFileExists(pszPath)
&& !RTDirExists(pszPath))
{
/* No file and no directory -- maybe a filter? */
if ( pszFilename
{
/* Yep, get the actual filter part. */
/* Remove the filter from actual sourcec directory name. */
}
}
return VINF_SUCCESS; /* @todo */
}
private:
};
/**
*/
typedef struct DESTFILEENTRY
{
/*
* Map for holding destination entires, whereas the key is the destination
* directory and the mapped value is a vector holding all elements for this directoy.
*/
/**
* Special exit codes for returning errors/information of a
* started guest process to the command line VBoxManage was started from.
* Useful for e.g. scripting.
*
* @note These are frozen as of 4.1.0.
*/
enum EXITCODEEXEC
{
/* Process exited normally but with an exit code <> 0. */
EXITCODEEXEC_CODE = 16,
EXITCODEEXEC_FAILED = 17,
EXITCODEEXEC_TERM_SIGNAL = 18,
EXITCODEEXEC_TERM_ABEND = 19,
EXITCODEEXEC_TIMEOUT = 20,
EXITCODEEXEC_DOWN = 21,
};
/**
* RTGetOpt-IDs for the guest execution control command line.
*/
enum GETOPTDEF_EXEC
{
};
enum GETOPTDEF_COPY
{
GETOPTDEF_COPY_DRYRUN = 1000,
};
enum GETOPTDEF_MKDIR
{
GETOPTDEF_MKDIR_PASSWORD = 1000
};
{
GETOPTDEF_SESSIONCLOSE_ALL = 1000
};
enum GETOPTDEF_STAT
{
GETOPTDEF_STAT_PASSWORD = 1000
};
enum OUTPUTTYPE
{
OUTPUTTYPE_UNDEFINED = 0,
OUTPUTTYPE_DOS2UNIX = 10,
OUTPUTTYPE_UNIX2DOS = 20
};
static int ctrlCopyDirExists(PCOPYCONTEXT pContext, bool bGuest, const char *pszDir, bool *fExists);
#endif /* VBOX_ONLY_DOCS */
{
"%s guestcontrol %s <uuid|vmname>\n"
" exec[ute]\n"
" --image <path to program> --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--domain <domain>] [--verbose] [--timeout <msec>]\n"
" [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
" [--wait-exit] [--wait-stdout] [--wait-stderr]\n"
" [--dos2unix] [--unix2dos]\n"
" [-- [<argument1>] ... [<argumentN>]]\n"
/** @todo Add a "--" parameter (has to be last parameter) to directly execute
"\n"
" copyfrom\n"
" <guest source> <host dest> --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--domain <domain>] [--verbose]\n"
" [--dryrun] [--follow] [--recursive]\n"
"\n"
" copyto|cp\n"
" <host source> <guest dest> --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--domain <domain>] [--verbose]\n"
" [--dryrun] [--follow] [--recursive]\n"
"\n"
" createdir[ectory]|mkdir|md\n"
" <guest directory>... --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--domain <domain>] [--verbose]\n"
" [--parents] [--mode <mode>]\n"
"\n"
" createtemp[orary]|mktemp\n"
" <template> --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--directory] [--secure] [--tmpdir <directory>]\n"
" [--domain <domain>] [--mode <mode>] [--verbose]\n"
"\n"
" list <all|sessions|processes> [--verbose]\n"
"\n"
" session close --session-id <ID>\n"
" | --session-name <name or pattern>\n"
" | --all\n"
" [--verbose]\n"
" stat\n"
" <file>... --username <name>\n"
" [--passwordfile <file> | --password <password>]\n"
" [--domain <domain>] [--verbose]\n"
"\n"
" updateadditions\n"
" [--source <guest additions .ISO>] [--verbose]\n"
" [--wait-start]\n"
" [-- [<argument1>] ... [<argumentN>]]\n"
}
#ifndef VBOX_ONLY_DOCS
/**
* Signal handler that sets g_fGuestCtrlCanceled.
*
* This can be executed on any thread in the process, on Windows it may even be
* a thread dedicated to delivering this signal. Do not doing anything
* unnecessary here.
*/
static void guestCtrlSignalHandler(int iSignal)
{
ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
}
/**
* Installs a custom signal handler to get notified
* whenever the user wants to intercept the program.
*/
static void ctrlSignalHandlerInstall()
{
#ifdef SIGBREAK
#endif
}
/**
* Uninstalls a previously installed signal handler.
*/
static void ctrlSignalHandlerUninstall()
{
#ifdef SIGBREAK
#endif
}
/**
* Translates a process status to a human readable
* string.
*/
{
switch (enmStatus)
{
case ProcessStatus_Starting:
return "starting";
case ProcessStatus_Started:
return "started";
case ProcessStatus_Paused:
return "paused";
return "terminating";
return "successfully terminated";
return "terminated by signal";
return "abnormally aborted";
return "timed out";
return "timed out, hanging";
case ProcessStatus_Down:
return "killed";
case ProcessStatus_Error:
return "error";
default:
break;
}
return "unknown";
}
{
int vrc = EXITCODEEXEC_SUCCESS;
switch (enmStatus)
{
case ProcessStatus_Starting:
break;
case ProcessStatus_Started:
break;
case ProcessStatus_Paused:
break;
break;
break;
break;
break;
break;
break;
case ProcessStatus_Down:
* not exactly an error of the started process ... */
break;
case ProcessStatus_Error:
break;
default:
break;
}
return vrc;
}
{
if ( errorInfo.isFullAvailable()
|| errorInfo.isBasicAvailable())
{
/* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
* because it contains more accurate info about what went wrong. */
else
{
RTMsgError("Error details:");
}
return VERR_GENERAL_FAILURE; /** @todo */
}
}
{
return ctrlPrintError(ErrInfo);
}
{
int vrc = VINF_SUCCESS;
do
{
if (!fCanceled)
{
{
}
}
} while(0);
AssertMsgStmt(SUCCEEDED(rc), ("Could not lookup progress information\n"), vrc = VERR_COM_UNEXPECTED);
return vrc;
}
/**
* Un-initializes the VM after guest control usage.
*/
{
}
/**
* Initializes the VM for IGuest operations.
*
* That is, checks whether it's up and running, if it can be locked (shared
* only) and returns a valid IGuest pointer on success.
*
* @return IPRT status code.
* @param pArg Our command line argument structure.
* @param pszNameOrId The VM's name or UUID.
* @param pGuest Where to return the IGuest interface pointer.
*/
{
/* Lookup VM. */
/* Assume it's an UUID. */
machine.asOutParam()));
return VERR_NOT_FOUND;
/* Machine is running? */
if (machineState != MachineState_Running)
{
RTMsgError("Machine \"%s\" is not running (currently %s)!\n",
return VERR_VM_INVALID_VM_STATE;
}
do
{
/* Open a session for the VM. */
/* Get the associated console. */
/* ... and session machine. */
/* Get IGuest interface. */
} while (0);
}
/**
* Prints the desired guest output to a stream.
*
* @return IPRT status code.
* @param pProcess Pointer to appropriate process object.
* @param pStrmOutput Where to write the data.
* @param uHandle Handle where to read the data from.
* @param uTimeoutMS Timeout (in ms) to wait for the operation to complete.
*/
{
int vrc = VINF_SUCCESS;
else
{
if (cbOutputData > 0)
{
/*
* If aOutputData is text data from the guest process' stdout or stderr,
* it has a platform dependent line ending. So standardize on
*/
char *pszBufUTF8;
if (RT_SUCCESS(vrc))
{
for (char *s = pszBufUTF8, *d = s;
s++, d++)
{
if (*s == '\r')
{
/* skip over CR, adjust destination */
d--;
}
else if (s != d)
*d = *s;
}
if (RT_FAILURE(vrc))
}
else
}
}
return vrc;
}
/**
* Returns the remaining time (in ms) based on the start time and a set
* timeout value. Returns RT_INDEFINITE_WAIT if no timeout was specified.
*
* @return RTMSINTERVAL Time left (in ms).
* @param u64StartMs Start time (in ms).
* @param cMsTimeout Timeout value (in ms).
*/
{
return RT_INDEFINITE_WAIT;
if (u64ElapsedMs >= cMsTimeout)
return 0;
return cMsTimeout - u64ElapsedMs;
}
/* <Missing documentation> */
{
/*
* Parse arguments.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
RTMSINTERVAL cMsTimeout = 0;
bool fWaitForExit = false;
bool fVerbose = false;
int vrc = VINF_SUCCESS;
/* Wait for process start in any case. This is useful for scripting VBoxManage
* when relying on its overall exit code. */
&& RT_SUCCESS(vrc))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case GETOPTDEF_EXEC_DOS2UNIX:
if (eOutputType != OUTPUTTYPE_UNDEFINED)
break;
case 'e': /* Environment */
{
char **papszArg;
int cArgs;
if (RT_FAILURE(vrc))
for (int j = 0; j < cArgs; j++)
break;
}
break;
break;
case 'i':
break;
/** @todo Add a hidden flag. */
case 'u': /* User name */
break;
case GETOPTDEF_EXEC_PASSWORD: /* Password */
break;
case 'p': /* Password file */
{
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
break;
case 't': /* Timeout */
break;
case GETOPTDEF_EXEC_UNIX2DOS:
if (eOutputType != OUTPUTTYPE_UNDEFINED)
break;
case 'v': /* Verbose */
fVerbose = true;
break;
fWaitForExit = true;
break;
fWaitForExit = true;
break;
fWaitForExit = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
else
break;
}
default:
}
}
if (strUsername.isEmpty())
/* Any output conversion not supported yet! */
if (eOutputType != OUTPUTTYPE_UNDEFINED)
/*
* Start with the real work.
*/
if (fVerbose)
/** @todo This eventually needs a bit of revamping so that a valid session gets passed
* into this function already so that we don't need to mess around with closing
* the session all over the places below again. Later. */
{
return RTEXITCODE_FAILURE;
}
/* Adjust process creation flags if we don't want to wait for process termination. */
if (!fWaitForExit)
/* Get current time stamp to later calculate rest of timeout left. */
do
{
/*
* Wait for guest session to start.
*/
if (fVerbose)
{
if (cMsTimeout == 0)
RTPrintf("Waiting for guest session to start ...\n");
else
}
rc = pGuestSession->WaitForArray(ComSafeArrayAsInParam(aSessionWaitFlags), cMsTimeout, &sessionWaitResult);
{
break;
}
/* Note: This might happen when Guest Additions < 4.3 are installed which don't
* support dedicated guest sessions. */
if (fVerbose)
RTPrintf("Guest session has been started\n");
if (fVerbose)
{
if (cMsTimeout == 0)
RTPrintf("Waiting for guest process to start ...\n");
else
}
/*
* Execute the process.
*/
pProcess.asOutParam());
{
break;
}
/** @todo does this need signal handling? there's no progress object etc etc */
if (RT_FAILURE(vrc))
if (RT_FAILURE(vrc))
/* Wait for process to exit ... */
bool fReadStdOut, fReadStdErr;
fReadStdOut = fReadStdErr = false;
bool fCompleted = false;
while (!fCompleted && cMsTimeLeft != 0)
{
{
break;
}
switch (waitResult)
{
case ProcessWaitResult_Start:
{
{
break;
}
if (fVerbose)
{
RTPrintf("Process '%s' (PID: %ld) %s\n",
}
else /** @todo Introduce a --quiet option for not printing this. */
{
/* Just print plain PID to make it easier for scripts
* invoking VBoxManage. */
}
/* We're done here if we don't want to wait for termination. */
if (!fWaitForExit)
fCompleted = true;
break;
}
case ProcessWaitResult_StdOut:
fReadStdOut = true;
break;
case ProcessWaitResult_StdErr:
fReadStdErr = true;
break;
/* Process terminated, we're done */
fCompleted = true;
break;
{
* yield to reduce the CPU load due to busy waiting. */
RTThreadYield(); /* Optional, don't check rc. */
/* Try both, stdout + stderr. */
fReadStdOut = fReadStdErr = true;
break;
}
default:
/* Ignore all other results, let the timeout expire */
break;
}
break;
if (fReadStdOut) /* Do we need to fetch stdout data? */
{
fReadStdOut = false;
}
if (fReadStdErr) /* Do we need to fetch stdout data? */
{
fReadStdErr = false;
}
if (RT_FAILURE(vrc))
break;
/* Did we run out of time? */
if ( cMsTimeout
break;
} /* while */
/* Report status back to the user. */
if (fCompleted)
{
{
}
else
{
{
}
else
{
if (fVerbose)
}
}
}
else
{
if (fVerbose)
RTPrintf("Process execution aborted!\n");
}
} while (0);
/*
* Only close the guest session if we waited for the guest
* process to exit. Otherwise we wouldn't have any chance to
*/
if ( fWaitForExit
|| rcExit != EXITCODEEXEC_SUCCESS)
{
if (fVerbose)
RTPrintf("Closing guest session ...\n");
{
if (rcExit == RTEXITCODE_SUCCESS)
}
}
else if (fVerbose)
RTPrintf("Guest session detached\n");
return rcExit;
}
/**
* Creates a copy context structure which then can be used with various
* guest control copy functions. Needs to be free'd with ctrlCopyContextFree().
*
* @return IPRT status code.
* @param pGuest Pointer to IGuest interface to use.
* @param fVerbose Flag indicating if we want to run in verbose mode.
* @param fDryRun Flag indicating if we want to run a dry run only.
* @param fHostToGuest Flag indicating if we want to copy from host to guest
* or vice versa.
* @param strUsername Username of account to use on the guest side.
* @param strPassword Password of account to use.
* @param strDomain Domain of account to use.
* @param strSessionName Session name (only for identification purposes).
* @param ppContext Pointer which receives the allocated copy context.
*/
const Utf8Str &strSessionName,
{
return VINF_SUCCESS;
}
/**
* Frees are previously allocated copy context structure.
*
* @param pContext Pointer to copy context to free.
*/
{
if (pContext)
{
if (pContext->pGuestSession)
delete pContext;
}
}
/**
* Translates a source path to a destination path (can be both sides,
* either host or guest). The source root is needed to determine the start
* of the relative source path which also needs to present in the destination
* path.
*
* @return IPRT status code.
* @param pszSourceRoot Source root path. No trailing directory slash!
* @param pszSource Actual source to transform. Must begin with
* the source root path!
* @param pszDest Destination path.
* @param ppszTranslated Pointer to the allocated, translated destination
* path. Must be free'd with RTStrFree().
*/
const char *pszDest, char **ppszTranslated)
{
#if 0 /** @todo r=bird: It does not make sense to apply host path parsing semantics onto guest paths. I hope this code isn't mixing host/guest paths in the same way anywhere else... @bugref{6344} */
#endif
/* Construct the relative dest destination path by "subtracting" the
* source from the source root, e.g.
*
* source root path = "e:\foo\", source = "e:\foo\bar"
* dest = "d:\baz\"
* translated = "d:\baz\bar\"
*/
char szTranslated[RTPATH_MAX];
int vrc;
if (!RTPathFilename(pszDestPath))
{
}
else
{
if (pszDestFileName)
{
}
else
}
if (RT_SUCCESS(vrc))
{
#if 0
RTPrintf("Root: %s, Source: %s, Dest: %s, Translated: %s\n",
#endif
}
return vrc;
}
#ifdef DEBUG_andy
static int tstTranslatePath()
{
RTAssertSetMayPanic(false /* Do not freak out, please. */);
static struct
{
const char *pszSourceRoot;
const char *pszSource;
const char *pszDest;
const char *pszTranslated;
int iResult;
} aTests[] =
{
/* Invalid stuff. */
#ifdef RT_OS_WINDOWS
/* Windows paths. */
#else /* RT_OS_WINDOWS */
{ "/home/test/foo", "/home/test/foo/baz/bar.txt", "/opt/test", "/opt/test/baz/bar.txt", VINF_SUCCESS },
#endif /* !RT_OS_WINDOWS */
/* Mixed paths*/
/** @todo */
{ NULL }
};
{
RTPrintf("\tSourceRoot=%s, Source=%s, Dest=%s\n",
char *pszTranslated = NULL;
{
RTPrintf("\tReturned %Rrc, expected %Rrc\n",
}
else if ( pszTranslated
{
RTPrintf("\tReturned translated path %s, expected %s\n",
}
if (pszTranslated)
{
}
}
return VINF_SUCCESS; /* @todo */
}
#endif
/**
* Creates a directory on the destination, based on the current copy
* context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Directory to create.
*/
{
bool fDirExists;
if ( RT_SUCCESS(vrc)
&& fDirExists)
{
return VINF_SUCCESS;
}
/* If querying for a directory existence fails there's no point of even trying
* to create such a directory. */
if (RT_FAILURE(vrc))
return vrc;
return VINF_SUCCESS;
{
}
else /* ... or on the host. */
{
if (vrc == VERR_ALREADY_EXISTS)
vrc = VINF_SUCCESS;
}
return vrc;
}
/**
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param bGuest true if directory needs to be checked on the guest
* or false if on the host.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
{
AssertPtrReturn(pContext, false);
AssertPtrReturn(pszDir, false);
AssertPtrReturn(fExists, false);
int vrc = VINF_SUCCESS;
if (bGuest)
{
else
*fExists = fDirExists ? true : false;
}
else
return vrc;
}
/**
* Checks whether a specific directory exists on the destination, based
* on the current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
bool *fExists)
{
}
/**
* Checks whether a specific directory exists on the source, based
* on the current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszDir Actual directory to check.
* @param fExists Pointer which receives the result if the
* given directory exists or not.
*/
bool *fExists)
{
}
/**
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param bGuest true if file needs to be checked on the guest
* or false if on the host.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
{
AssertPtrReturn(pContext, false);
AssertPtrReturn(pszFile, false);
AssertPtrReturn(fExists, false);
int vrc = VINF_SUCCESS;
if (bOnGuest)
{
else
*fExists = fFileExists ? true : false;
}
else
return vrc;
}
/**
* Checks whether a specific file exists on the destination, based on the
* current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
bool *fExists)
{
}
/**
* Checks whether a specific file exists on the source, based on the
* current copy context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFile Actual file to check.
* @param fExists Pointer which receives the result if the
* given file exists or not.
*/
bool *fExists)
{
}
/**
* Copies a source file to the destination.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszFileSource Source file to copy to the destination.
* @param pszFileDest Name of copied file on the destination.
* @param fFlags Copy flags. No supported at the moment and needs
* to be set to 0.
*/
{
RTPrintf("Copying \"%s\" to \"%s\" ...\n",
return VINF_SUCCESS;
int vrc = VINF_SUCCESS;
if (pContext->fHostToGuest)
{
pProgress.asOutParam());
}
else
{
pProgress.asOutParam());
}
{
}
else
{
else
}
return vrc;
}
/**
* Copys a directory (tree) from host to the guest.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory on the host to copy to the guest.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory on the guest.
* @param fFlags Copy flags, such as recursive copying.
* @param pszSubDir Current sub directory to handle. Needs to NULL and only
* is needed for recursion.
*/
const char *pszSubDir /* For recursion. */)
{
/* Filter is optional. */
/* Sub directory is optional. */
/*
* Construct current path.
*/
char szCurDir[RTPATH_MAX];
/* Flag indicating whether the current directory was created on the
* target or not. */
bool fDirCreated = false;
/*
* Open directory without a filter - RTDirOpenFiltered unfortunately
* cannot handle sub directories so we have to do the filtering ourselves.
*/
if (RT_SUCCESS(vrc))
{
if (RT_FAILURE(vrc))
}
if (RT_SUCCESS(vrc))
{
/*
* Enumerate the directory tree.
*/
while (RT_SUCCESS(vrc))
{
if (RT_FAILURE(vrc))
{
if (vrc == VERR_NO_MORE_FILES)
vrc = VINF_SUCCESS;
break;
}
/** @todo r=bird: This ain't gonna work on most UNIX file systems because
* enmType is RTDIRENTRYTYPE_UNKNOWN. This is clearly documented in
* RTDIRENTRY::enmType. For trunk, RTDirQueryUnknownType can be used. */
{
case RTDIRENTRYTYPE_DIRECTORY:
{
/* Skip "." and ".." entries. */
break;
if (fFlags & CopyFileFlag_Recursive)
{
if (pszSubDir)
else
{
}
if (pszNewSub)
{
}
else
}
break;
}
case RTDIRENTRYTYPE_SYMLINK:
if ( (fFlags & CopyFileFlag_Recursive)
&& (fFlags & CopyFileFlag_FollowLinks))
{
/* Fall through to next case is intentional. */
}
else
break;
case RTDIRENTRYTYPE_FILE:
{
if ( pszFilter
{
break; /* Filter does not match. */
}
if (!fDirCreated)
{
char *pszDestDir;
pszDest, &pszDestDir);
if (RT_SUCCESS(vrc))
{
fDirCreated = true;
}
}
if (RT_SUCCESS(vrc))
{
if (pszFileSource)
{
char *pszFileDest;
pszDest, &pszFileDest);
if (RT_SUCCESS(vrc))
{
pszFileDest, 0 /* Flags */);
}
}
}
break;
}
default:
break;
}
if (RT_FAILURE(vrc))
break;
}
}
return vrc;
}
/**
* Copys a directory (tree) from guest to the host.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory on the guest to copy to the host.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory on the host.
* @param fFlags Copy flags, such as recursive copying.
* @param pszSubDir Current sub directory to handle. Needs to NULL and only
* is needed for recursion.
*/
const char *pszSubDir /* For recursion. */)
{
/* Filter is optional. */
/* Sub directory is optional. */
/*
* Construct current path.
*/
char szCurDir[RTPATH_MAX];
if (RT_FAILURE(vrc))
return vrc;
/* Flag indicating whether the current directory was created on the
* target or not. */
bool fDirCreated = false;
pDirectory.asOutParam());
while (true)
{
break;
switch (enmType)
{
case FsObjType_Directory:
{
/* Skip "." and ".." entries. */
break;
{
}
if (fFlags & CopyFileFlag_Recursive)
{
if (pszSubDir)
else
{
}
if (pszNewSub)
{
}
else
}
break;
}
case FsObjType_Symlink:
if ( (fFlags & CopyFileFlag_Recursive)
&& (fFlags & CopyFileFlag_FollowLinks))
{
/* Fall through to next case is intentional. */
}
else
break;
case FsObjType_File:
{
if ( pszFilter
{
break; /* Filter does not match. */
}
if (!fDirCreated)
{
char *pszDestDir;
pszDest, &pszDestDir);
if (RT_SUCCESS(vrc))
{
fDirCreated = true;
}
}
if (RT_SUCCESS(vrc))
{
if (pszFileSource)
{
char *pszFileDest;
pszDest, &pszFileDest);
if (RT_SUCCESS(vrc))
{
pszFileDest, 0 /* Flags */);
}
}
else
}
break;
}
default:
RTPrintf("Warning: Directory entry of type %ld not handled, skipping ...\n",
enmType);
break;
}
if (RT_FAILURE(vrc))
break;
}
{
switch (rc)
{
case E_ABORT: /* No more directory entries left to process. */
break;
case VBOX_E_FILE_ERROR: /* Current entry cannot be accessed to
to missing rights. */
{
RTPrintf("Warning: Cannot access \"%s\", skipping ...\n",
szCurDir);
break;
}
default:
break;
}
}
{
if (RT_SUCCESS(vrc))
}
return vrc;
}
/**
* Copys a directory (tree) to the destination, based on the current copy
* context.
*
* @return IPRT status code.
* @param pContext Pointer to current copy control context.
* @param pszSource Source directory to copy to the destination.
* @param pszFilter DOS-style wildcard filter (?, *). Optional.
* @param pszDest Destination directory where to copy in the source
* source directory.
* @param fFlags Copy flags, such as recursive copying.
*/
{
if (pContext->fHostToGuest)
}
/**
* Creates a source root by stripping file names or filters of the specified source.
*
* @return IPRT status code.
* @param pszSource Source to create source root for.
* @param ppszSourceRoot Pointer that receives the allocated source root. Needs
* to be free'd with ctrlCopyFreeSourceRoot().
*/
{
if ( lenRoot
&& lenRoot > 1
{
if (lenRoot > 1)
}
else
{
/* If there's anything (like a file name or a filter),
* strip it! */
}
return VINF_SUCCESS;
}
/**
* Frees a previously allocated source root.
*
* @return IPRT status code.
* @param pszSourceRoot Source root to free.
*/
static void ctrlCopyFreeSourceRoot(char *pszSourceRoot)
{
}
bool fHostToGuest)
{
/** @todo r=bird: This command isn't very unix friendly in general. mkdir
* is much better (partly because it is much simpler of course). The main
* arguments against this is that (1) all but two options conflicts with
* done windows CMD style (though not in a 100% compatible way), and (3)
* that only one source is allowed - efficiently sabotaging default
* wildcard expansion by a unix shell. The best solution here would be
* two different variant, one windowsy (xcopy) and one unixy (gnu cp). */
/*
* IGuest::CopyToGuest is kept as simple as possible to let the developer choose
* what and how to implement the file enumeration/recursive lookup, like VBoxManage
* does in here.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
bool fVerbose = false;
bool fCopyRecursive = false;
bool fDryRun = false;
int vrc = VINF_SUCCESS;
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case GETOPTDEF_COPY_DRYRUN:
fDryRun = true;
break;
case GETOPTDEF_COPY_FOLLOW:
break;
case 'u': /* User name */
break;
case GETOPTDEF_COPY_PASSWORD: /* Password */
break;
case 'p': /* Password file */
{
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
break;
case 'R': /* Recursive processing */
break;
case GETOPTDEF_COPY_TARGETDIR:
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
/* Last argument and no destination specified with
* --target-directory yet? Then use the current
* (= last) argument as destination. */
{
}
else
{
/* Save the source directory. */
}
break;
}
default:
}
}
if (!vecSources.size())
return errorSyntax(USAGE_GUESTCONTROL,
"No source(s) specified!");
return errorSyntax(USAGE_GUESTCONTROL,
"No destination specified!");
if (strUsername.isEmpty())
return errorSyntax(USAGE_GUESTCONTROL,
"No user name specified!");
/*
* Done parsing arguments, do some more preparations.
*/
if (fVerbose)
{
if (fHostToGuest)
RTPrintf("Copying from host to guest ...\n");
else
RTPrintf("Copying from guest to host ...\n");
if (fDryRun)
RTPrintf("Dry run - no files copied!\n");
}
/* Create the copy context -- it contains all information
* the routines need to know when handling the actual copying. */
"VBoxManage Guest Control Copy", &pContext);
if (RT_FAILURE(vrc))
{
return RTEXITCODE_FAILURE;
}
/* If the destination is a path, (try to) create it. */
/** @todo r=bird: RTPathFilename and RTPathStripFilename won't work
* correctly on non-windows hosts when the guest is from the DOS world (Windows,
* OS/2, DOS). The host doesn't know about DOS slashes, only UNIX slashes and
* will get the wrong idea if some dilligent user does:
*
* copyto myfile.txt 'C:\guestfile.txt'
* or
* copyto myfile.txt 'D:guestfile.txt'
*
* @bugref{6344}
*/
if (!RTPathFilename(pszDest))
{
}
else
{
/* We assume we got a file name as destination -- so strip
* the actual file name and make sure the appropriate
* directories get created. */
}
if (RT_SUCCESS(vrc))
{
/*
* Here starts the actual fun!
* Handle all given sources one by one.
*/
for (unsigned long s = 0; s < vecSources.size(); s++)
{
char *pszSourceRoot;
if (RT_FAILURE(vrc))
{
break;
}
if (fVerbose)
/** @todo Files with filter?? */
bool fSourceIsFile = false;
bool fSourceExists;
if ( cchSource > 1
{
if (pszFilter) /* Directory with filter (so use source root w/o the actual filter). */
else /* Regular directory without filter. */
if (fSourceExists)
{
/* Strip trailing slash from our source element so that other functions
* can use this stuff properly (like RTPathStartsWith). */
}
}
else
{
if ( RT_SUCCESS(vrc)
&& fSourceExists)
{
fSourceIsFile = true;
}
}
if ( RT_SUCCESS(vrc)
&& fSourceExists)
{
if (fSourceIsFile)
{
/* Single file. */
char *pszDestFile;
if (RT_SUCCESS(vrc))
{
pszDestFile, 0 /* Flags */);
}
else
RTMsgError("Unable to translate path for \"%s\", rc=%Rrc\n",
}
else
{
/* Directory (with filter?). */
}
}
if ( RT_SUCCESS(vrc)
&& !fSourceExists)
{
RTMsgError("Warning: Source \"%s\" does not exist, skipping!\n",
continue;
}
else if (RT_FAILURE(vrc))
{
RTMsgError("Error processing \"%s\", rc=%Rrc\n",
break;
}
}
}
}
{
/*
* Parse arguments.
*
* Note! No direct returns here, everyone must go thru the cleanup at the
* end of this function.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
bool fVerbose = false;
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'm': /* Mode */
break;
case 'P': /* Create parents */
break;
case 'u': /* User name */
break;
case GETOPTDEF_MKDIR_PASSWORD: /* Password */
break;
case 'p': /* Password file */
{
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
break;
}
default:
}
}
if (!cDirs)
if (strUsername.isEmpty())
/*
* Create the directories.
*/
{
return RTEXITCODE_FAILURE;
}
{
if (fVerbose)
hrc = pGuestSession->DirectoryCreate(Bstr(it->first).raw(), fDirMode, ComSafeArrayAsInParam(dirCreateFlags));
{
break;
}
it++;
}
if (!pGuestSession.isNull())
pGuestSession->Close();
}
{
/*
* Parse arguments.
*
* Note! No direct returns here, everyone must go thru the cleanup at the
* end of this function.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
bool fDirectory = false;
bool fSecure = false;
bool fVerbose = false;
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'm': /* Mode */
break;
case 'D': /* Create directory */
fDirectory = true;
break;
case 's': /* Secure */
fSecure = true;
break;
case 't': /* Temp directory */
break;
case 'u': /* User name */
break;
case GETOPTDEF_MKDIR_PASSWORD: /* Password */
break;
case 'p': /* Password file */
{
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
break;
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
if (strTemplate.isEmpty())
else
return errorSyntax(USAGE_GUESTCONTROL,
"More than one template specified!\n");
break;
}
default:
}
}
if (strTemplate.isEmpty())
if (strUsername.isEmpty())
if (!fDirectory)
/*
* Create the directories.
*/
if (fVerbose)
{
RTPrintf("Creating temporary directory from template '%s' in directory '%s' ...\n",
else if (fDirectory)
RTPrintf("Creating temporary directory from template '%s' in default temporary directory ...\n",
strTemplate.c_str());
RTPrintf("Creating temporary file from template '%s' in directory '%s' ...\n",
else if (!fDirectory)
RTPrintf("Creating temporary file from template '%s' in default temporary directory ...\n",
strTemplate.c_str());
}
{
return RTEXITCODE_FAILURE;
}
if (fDirectory)
{
directory.asOutParam());
}
// else - temporary file not yet implemented
if (!pGuestSession.isNull())
pGuestSession->Close();
}
{
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
bool fVerbose = false;
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'u': /* User name */
break;
case GETOPTDEF_STAT_PASSWORD: /* Password */
break;
case 'p': /* Password file */
{
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
break;
}
case 'd': /* domain */
break;
case 'L': /* Dereference */
case 'f': /* File-system */
case 'c': /* Format */
case 't': /* Terse */
break; /* Never reached. */
case 'v': /* Verbose */
fVerbose = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
break;
}
default:
}
}
if (!cObjs)
if (strUsername.isEmpty())
/*
* Create the directories.
*/
{
if (fVerbose)
{
/* If there's at least one element which does not exist on the guest,
* drop out with exitcode 1. */
if (fVerbose)
RTPrintf("Cannot stat for element \"%s\": No such element\n",
}
else
{
switch (objType)
{
case FsObjType_File:
break;
case FsObjType_Directory:
break;
case FsObjType_Symlink:
break;
default:
break;
}
/** @todo: Show more information about this element. */
}
it++;
}
if (!pGuestSession.isNull())
pGuestSession->Close();
return rcExit;
}
{
/*
* Check the syntax. We can deduce the correct syntax from the number of
* arguments.
*/
bool fVerbose = false;
bool fWaitStartOnly = false;
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
int vrc = VINF_SUCCESS;
&& RT_SUCCESS(vrc))
{
switch (ch)
{
case 's':
break;
case 'v':
fVerbose = true;
break;
case 'w':
fWaitStartOnly = true;
break;
case VINF_GETOPT_NOT_OPTION:
{
else
break;
}
default:
}
}
if (fVerbose)
RTPrintf("Updating Guest Additions ...\n");
{
break;
}
/* Determine source if not set yet. */
{
RTMsgError("No Guest Additions source found or specified, aborting\n");
}
{
}
if (RT_SUCCESS(vrc))
{
if (fVerbose)
if (fWaitStartOnly)
{
if (fVerbose)
RTPrintf("Preparing and waiting for Guest Additions installer to start ...\n");
}
/* Wait for whole update process to complete. */
pProgress.asOutParam()));
else
{
if (fVerbose)
else
if ( RT_SUCCESS(vrc)
&& fVerbose)
{
RTPrintf("Guest Additions update successful\n");
}
}
}
}
{
/** Use RTGetOpt here when handling command line args gets more complex. */
bool fListAll = false;
bool fListSessions = false;
bool fListProcesses = false;
fListSessions = true;
fListAll = true;
/** @todo Handle "--verbose" using RTGetOpt. */
/** @todo Do we need a machine-readable output here as well? */
if ( fListAll
|| fListSessions)
{
do
{
size_t cTotalProcs = 0;
if (cSessions)
{
RTPrintf("Active guest sessions:\n");
/** @todo Make this output a bit prettier. No time now. */
{
if (!pCurSession.isNull())
{
RTPrintf("\n\tSession #%-3zu ID=%-3RU32 User=%-16ls Name=%ls",
if ( fListAll
|| fListProcesses)
{
{
if (!pCurProcess.isNull())
{
RTPrintf("\n\t\tProcess #%-03zu PID=%-6RU32 Status=[%s] Command=%ls",
}
}
}
}
}
}
else
RTPrintf("No active guest sessions found\n");
} while (0);
}
else
return rcExit;
}
{
/*
* Parse arguments.
*
* Note! No direct returns here, everyone must go thru the cleanup at the
* end of this function.
*/
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
bool fVerbose = false;
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
case 'n': /* Session name pattern */
break;
case 'i': /* Session ID */
break;
/** @todo Add an option for finding all session by a name pattern. */
case 'v': /* Verbose */
fVerbose = true;
break;
strNamePattern = "*";
break;
case VINF_GETOPT_NOT_OPTION:
/** @todo Supply a CSV list of IDs or patterns to close? */
break;
default:
}
}
if ( strNamePattern.isEmpty()
&& ulSessionID == UINT32_MAX)
{
}
else if ( !strNamePattern.isEmpty()
&& ulSessionID != UINT32_MAX)
{
}
do
{
bool fSessionFound = false;
{
pSession = collSessions[i];
{
}
else /* ... or by naming pattern. */
{
fSessionFound = true;
}
if (fSessionFound)
{
if (fVerbose)
RTPrintf("Closing guest session ID=#%RU32 \"%s\" ...\n",
if (fVerbose)
RTPrintf("Guest session successfully closed\n");
}
}
if (!fSessionFound)
{
RTPrintf("No guest session(s) found\n");
}
} while (0);
}
{
/** Use RTGetOpt here when handling command line args gets more complex. */
{
}
}
/**
* Access the guest control store.
*
* @returns program exit code.
* @note see the command line API description for parameters
*/
{
#ifdef DEBUG_andy_disabled
if (RT_FAILURE(tstTranslatePath()))
return RTEXITCODE_FAILURE;
#endif
if (RT_SUCCESS(vrc))
{
int rcExit;
else
return rcExit;
}
return RTEXITCODE_FAILURE;
}
#endif /* !VBOX_ONLY_DOCS */