1 | /* $Id: VBoxManageGuestCtrl.cpp 36987 2011-05-06 15:33:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - Implementation of guestcontrol command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "VBoxManage.h"
|
---|
23 |
|
---|
24 | #ifndef VBOX_ONLY_DOCS
|
---|
25 |
|
---|
26 | #include <VBox/com/com.h>
|
---|
27 | #include <VBox/com/string.h>
|
---|
28 | #include <VBox/com/array.h>
|
---|
29 | #include <VBox/com/ErrorInfo.h>
|
---|
30 | #include <VBox/com/errorprint.h>
|
---|
31 | #include <VBox/com/VirtualBox.h>
|
---|
32 | #include <VBox/com/EventQueue.h>
|
---|
33 |
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 |
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/dir.h>
|
---|
39 | #include <iprt/file.h>
|
---|
40 | #include <iprt/isofs.h>
|
---|
41 | #include <iprt/getopt.h>
|
---|
42 | #include <iprt/list.h>
|
---|
43 | #include <iprt/path.h>
|
---|
44 | #include <iprt/thread.h>
|
---|
45 |
|
---|
46 | #include <map>
|
---|
47 | #include <vector>
|
---|
48 |
|
---|
49 | #ifdef USE_XPCOM_QUEUE
|
---|
50 | # include <sys/select.h>
|
---|
51 | # include <errno.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #include <signal.h>
|
---|
55 |
|
---|
56 | #ifdef RT_OS_DARWIN
|
---|
57 | # include <CoreFoundation/CFRunLoop.h>
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | using namespace com;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * IVirtualBoxCallback implementation for handling the GuestControlCallback in
|
---|
64 | * relation to the "guestcontrol * wait" command.
|
---|
65 | */
|
---|
66 | /** @todo */
|
---|
67 |
|
---|
68 | /** Set by the signal handler. */
|
---|
69 | static volatile bool g_fGuestCtrlCanceled = false;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * An entry for a source element, including an optional filter.
|
---|
73 | */
|
---|
74 | typedef struct SOURCEFILEENTRY
|
---|
75 | {
|
---|
76 | SOURCEFILEENTRY(const char *pszSource, const char *pszFilter)
|
---|
77 | : mSource(pszSource),
|
---|
78 | mFilter(pszFilter) {}
|
---|
79 | SOURCEFILEENTRY(const char *pszSource)
|
---|
80 | : mSource(pszSource)
|
---|
81 | {
|
---|
82 | if ( !RTFileExists(pszSource)
|
---|
83 | && !RTDirExists(pszSource))
|
---|
84 | {
|
---|
85 | /* No file and no directory -- maybe a filter? */
|
---|
86 | if (NULL != strpbrk(RTPathFilename(pszSource), "*?"))
|
---|
87 | {
|
---|
88 | /* Yep, get the actual filter part. */
|
---|
89 | mFilter = RTPathFilename(pszSource);
|
---|
90 | /* Remove the filter from actual sourcec directory name. */
|
---|
91 | RTPathStripFilename(mSource.mutableRaw());
|
---|
92 | mSource.jolt();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | Utf8Str mSource;
|
---|
97 | Utf8Str mFilter;
|
---|
98 | } SOURCEFILEENTRY, *PSOURCEFILEENTRY;
|
---|
99 | typedef std::vector<SOURCEFILEENTRY> SOURCEVEC, *PSOURCEVEC;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * An entry for an element which needs to be copied to the guest.
|
---|
103 | */
|
---|
104 | typedef struct DESTFILEENTRY
|
---|
105 | {
|
---|
106 | DESTFILEENTRY(Utf8Str strFileName) : mFileName(strFileName) {}
|
---|
107 | Utf8Str mFileName;
|
---|
108 | } DESTFILEENTRY, *PDESTFILEENTRY;
|
---|
109 | /*
|
---|
110 | * Map for holding destination entires, whereas the key is the destination
|
---|
111 | * directory and the mapped value is a vector holding all elements for this directoy.
|
---|
112 | */
|
---|
113 | typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> > DESTDIRMAP, *PDESTDIRMAP;
|
---|
114 | typedef std::map< Utf8Str, std::vector<DESTFILEENTRY> >::iterator DESTDIRMAPITER, *PDESTDIRMAPITER;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Special exit codes for returning errors/information of a
|
---|
118 | * started guest process to the command line VBoxManage was started from.
|
---|
119 | * Useful for e.g. scripting.
|
---|
120 | *
|
---|
121 | * @note These are frozen as of 4.1.0.
|
---|
122 | */
|
---|
123 | enum EXITCODEEXEC
|
---|
124 | {
|
---|
125 | EXITCODEEXEC_SUCCESS = RTEXITCODE_SUCCESS,
|
---|
126 | /* Process exited normally but with an exit code <> 0. */
|
---|
127 | EXITCODEEXEC_CODE = 16,
|
---|
128 | EXITCODEEXEC_FAILED = 17,
|
---|
129 | EXITCODEEXEC_TERM_SIGNAL = 18,
|
---|
130 | EXITCODEEXEC_TERM_ABEND = 19,
|
---|
131 | EXITCODEEXEC_TIMEOUT = 20,
|
---|
132 | EXITCODEEXEC_DOWN = 21,
|
---|
133 | EXITCODEEXEC_CANCELED = 22
|
---|
134 | };
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * RTGetOpt-IDs for the guest execution control command line.
|
---|
138 | */
|
---|
139 | enum GETOPTDEF_EXEC
|
---|
140 | {
|
---|
141 | GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES = 1000,
|
---|
142 | GETOPTDEF_EXEC_OUTPUTFORMAT,
|
---|
143 | GETOPTDEF_EXEC_DOS2UNIX,
|
---|
144 | GETOPTDEF_EXEC_UNIX2DOS,
|
---|
145 | GETOPTDEF_EXEC_WAITFOREXIT,
|
---|
146 | GETOPTDEF_EXEC_WAITFORSTDOUT,
|
---|
147 | GETOPTDEF_EXEC_WAITFORSTDERR
|
---|
148 | };
|
---|
149 |
|
---|
150 | enum GETOPTDEF_COPYTO
|
---|
151 | {
|
---|
152 | GETOPTDEF_COPYTO_DRYRUN = 1000,
|
---|
153 | GETOPTDEF_COPYTO_FOLLOW,
|
---|
154 | GETOPTDEF_COPYTO_PASSWORD,
|
---|
155 | GETOPTDEF_COPYTO_TARGETDIR,
|
---|
156 | GETOPTDEF_COPYTO_USERNAME
|
---|
157 | };
|
---|
158 |
|
---|
159 | enum OUTPUTTYPE
|
---|
160 | {
|
---|
161 | OUTPUTTYPE_UNDEFINED = 0,
|
---|
162 | OUTPUTTYPE_DOS2UNIX = 10,
|
---|
163 | OUTPUTTYPE_UNIX2DOS = 20
|
---|
164 | };
|
---|
165 |
|
---|
166 | #endif /* VBOX_ONLY_DOCS */
|
---|
167 |
|
---|
168 | void usageGuestControl(PRTSTREAM pStrm)
|
---|
169 | {
|
---|
170 | RTStrmPrintf(pStrm,
|
---|
171 | "VBoxManage guestcontrol <vmname>|<uuid>\n"
|
---|
172 | " exec[ute]\n"
|
---|
173 | " --image <path to program>\n"
|
---|
174 | " --username <name> --password <password>\n"
|
---|
175 | " [--dos2unix]\n"
|
---|
176 | " [--environment \"<NAME>=<VALUE> [<NAME>=<VALUE>]\"]\n"
|
---|
177 | " [--timeout <msec>] [--unix2dos] [--verbose]\n"
|
---|
178 | " [--wait-exit] [--wait-stdout] [--wait-stdout]\n"
|
---|
179 | //" [--output-format=<dos>|<unix>]\n"
|
---|
180 | " [--output-type=<binary>|<text>]\n"
|
---|
181 | " [-- [<argument1>] ... [<argumentN>]\n"
|
---|
182 | /** @todo Add a "--" parameter (has to be last parameter) to directly execute
|
---|
183 | * stuff, e.g. "VBoxManage guestcontrol execute <VMName> --username <> ... -- /bin/rm -Rf /foo". */
|
---|
184 | "\n"
|
---|
185 | " copyto|cp\n"
|
---|
186 | " <source on host> <destination on guest>\n"
|
---|
187 | " --username <name> --password <password>\n"
|
---|
188 | " [--dryrun] [--follow] [--recursive] [--verbose]\n"
|
---|
189 | "\n"
|
---|
190 | " createdir[ectory]|mkdir|md\n"
|
---|
191 | " <directory to create on guest>\n"
|
---|
192 | " --username <name> --password <password>\n"
|
---|
193 | " [--parents] [--mode <mode>] [--verbose]\n"
|
---|
194 | "\n"
|
---|
195 | " updateadditions\n"
|
---|
196 | " [--source <guest additions .ISO>] [--verbose]\n"
|
---|
197 | "\n");
|
---|
198 | }
|
---|
199 |
|
---|
200 | #ifndef VBOX_ONLY_DOCS
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Signal handler that sets g_fGuestCtrlCanceled.
|
---|
204 | *
|
---|
205 | * This can be executed on any thread in the process, on Windows it may even be
|
---|
206 | * a thread dedicated to delivering this signal. Do not doing anything
|
---|
207 | * unnecessary here.
|
---|
208 | */
|
---|
209 | static void guestCtrlSignalHandler(int iSignal)
|
---|
210 | {
|
---|
211 | NOREF(iSignal);
|
---|
212 | ASMAtomicWriteBool(&g_fGuestCtrlCanceled, true);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Installs a custom signal handler to get notified
|
---|
217 | * whenever the user wants to intercept the program.
|
---|
218 | */
|
---|
219 | static void ctrlSignalHandlerInstall()
|
---|
220 | {
|
---|
221 | signal(SIGINT, guestCtrlSignalHandler);
|
---|
222 | #ifdef SIGBREAK
|
---|
223 | signal(SIGBREAK, guestCtrlSignalHandler);
|
---|
224 | #endif
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Uninstalls a previously installed signal handler.
|
---|
229 | */
|
---|
230 | static void ctrlSignalHandlerUninstall()
|
---|
231 | {
|
---|
232 | signal(SIGINT, SIG_DFL);
|
---|
233 | #ifdef SIGBREAK
|
---|
234 | signal(SIGBREAK, SIG_DFL);
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Translates a process status to a human readable
|
---|
240 | * string.
|
---|
241 | */
|
---|
242 | static const char *ctrlExecProcessStatusToText(ExecuteProcessStatus_T enmStatus)
|
---|
243 | {
|
---|
244 | switch (enmStatus)
|
---|
245 | {
|
---|
246 | case ExecuteProcessStatus_Started:
|
---|
247 | return "started";
|
---|
248 | case ExecuteProcessStatus_TerminatedNormally:
|
---|
249 | return "successfully terminated";
|
---|
250 | case ExecuteProcessStatus_TerminatedSignal:
|
---|
251 | return "terminated by signal";
|
---|
252 | case ExecuteProcessStatus_TerminatedAbnormally:
|
---|
253 | return "abnormally aborted";
|
---|
254 | case ExecuteProcessStatus_TimedOutKilled:
|
---|
255 | return "timed out";
|
---|
256 | case ExecuteProcessStatus_TimedOutAbnormally:
|
---|
257 | return "timed out, hanging";
|
---|
258 | case ExecuteProcessStatus_Down:
|
---|
259 | return "killed";
|
---|
260 | case ExecuteProcessStatus_Error:
|
---|
261 | return "error";
|
---|
262 | default:
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | return "unknown";
|
---|
266 | }
|
---|
267 |
|
---|
268 | static int ctrlExecProcessStatusToExitCode(ExecuteProcessStatus_T enmStatus, ULONG uExitCode)
|
---|
269 | {
|
---|
270 | int rc = EXITCODEEXEC_SUCCESS;
|
---|
271 | switch (enmStatus)
|
---|
272 | {
|
---|
273 | case ExecuteProcessStatus_Started:
|
---|
274 | rc = EXITCODEEXEC_SUCCESS;
|
---|
275 | break;
|
---|
276 | case ExecuteProcessStatus_TerminatedNormally:
|
---|
277 | rc = !uExitCode ? EXITCODEEXEC_SUCCESS : EXITCODEEXEC_CODE;
|
---|
278 | break;
|
---|
279 | case ExecuteProcessStatus_TerminatedSignal:
|
---|
280 | rc = EXITCODEEXEC_TERM_SIGNAL;
|
---|
281 | break;
|
---|
282 | case ExecuteProcessStatus_TerminatedAbnormally:
|
---|
283 | rc = EXITCODEEXEC_TERM_ABEND;
|
---|
284 | break;
|
---|
285 | case ExecuteProcessStatus_TimedOutKilled:
|
---|
286 | rc = EXITCODEEXEC_TIMEOUT;
|
---|
287 | break;
|
---|
288 | case ExecuteProcessStatus_TimedOutAbnormally:
|
---|
289 | rc = EXITCODEEXEC_TIMEOUT;
|
---|
290 | break;
|
---|
291 | case ExecuteProcessStatus_Down:
|
---|
292 | /* Service/OS is stopping, process was killed, so
|
---|
293 | * not exactly an error of the started process ... */
|
---|
294 | rc = EXITCODEEXEC_DOWN;
|
---|
295 | break;
|
---|
296 | case ExecuteProcessStatus_Error:
|
---|
297 | rc = EXITCODEEXEC_FAILED;
|
---|
298 | break;
|
---|
299 | default:
|
---|
300 | AssertMsgFailed(("Unknown exit code (%u) from guest process returned!\n", enmStatus));
|
---|
301 | break;
|
---|
302 | }
|
---|
303 | return rc;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static int ctrlPrintError(com::ErrorInfo &errorInfo)
|
---|
307 | {
|
---|
308 | if ( errorInfo.isFullAvailable()
|
---|
309 | || errorInfo.isBasicAvailable())
|
---|
310 | {
|
---|
311 | /* If we got a VBOX_E_IPRT error we handle the error in a more gentle way
|
---|
312 | * because it contains more accurate info about what went wrong. */
|
---|
313 | if (errorInfo.getResultCode() == VBOX_E_IPRT_ERROR)
|
---|
314 | RTMsgError("%ls.", errorInfo.getText().raw());
|
---|
315 | else
|
---|
316 | {
|
---|
317 | RTMsgError("Error details:");
|
---|
318 | GluePrintErrorInfo(errorInfo);
|
---|
319 | }
|
---|
320 | return VERR_GENERAL_FAILURE; /** @todo */
|
---|
321 | }
|
---|
322 | AssertMsgFailedReturn(("Object has indicated no error!?\n"),
|
---|
323 | VERR_INVALID_PARAMETER);
|
---|
324 | }
|
---|
325 |
|
---|
326 | static int ctrlPrintError(IUnknown *pObj, const GUID &aIID)
|
---|
327 | {
|
---|
328 | com::ErrorInfo ErrInfo(pObj, aIID);
|
---|
329 | return ctrlPrintError(ErrInfo);
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | static int ctrlPrintProgressError(ComPtr<IProgress> progress)
|
---|
334 | {
|
---|
335 | int rc;
|
---|
336 | BOOL fCanceled;
|
---|
337 | if ( SUCCEEDED(progress->COMGETTER(Canceled(&fCanceled)))
|
---|
338 | && fCanceled)
|
---|
339 | {
|
---|
340 | rc = VERR_CANCELLED;
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | com::ProgressErrorInfo ErrInfo(progress);
|
---|
345 | rc = ctrlPrintError(ErrInfo);
|
---|
346 | }
|
---|
347 | return rc;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Un-initializes the VM after guest control usage.
|
---|
352 | */
|
---|
353 | static void ctrlUninitVM(HandlerArg *pArg)
|
---|
354 | {
|
---|
355 | AssertPtrReturnVoid(pArg);
|
---|
356 | if (pArg->session)
|
---|
357 | pArg->session->UnlockMachine();
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Initializes the VM for IGuest operations.
|
---|
362 | *
|
---|
363 | * That is, checks whether it's up and running, if it can be locked (shared
|
---|
364 | * only) and returns a valid IGuest pointer on success.
|
---|
365 | *
|
---|
366 | * @return IPRT status code.
|
---|
367 | * @param pArg Our command line argument structure.
|
---|
368 | * @param pszNameOrId The VM's name or UUID.
|
---|
369 | * @param pGuest Where to return the IGuest interface pointer.
|
---|
370 | */
|
---|
371 | static int ctrlInitVM(HandlerArg *pArg, const char *pszNameOrId, ComPtr<IGuest> *pGuest)
|
---|
372 | {
|
---|
373 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
374 | AssertPtrReturn(pszNameOrId, VERR_INVALID_PARAMETER);
|
---|
375 |
|
---|
376 | /* Lookup VM. */
|
---|
377 | ComPtr<IMachine> machine;
|
---|
378 | /* Assume it's an UUID. */
|
---|
379 | HRESULT rc;
|
---|
380 | CHECK_ERROR(pArg->virtualBox, FindMachine(Bstr(pszNameOrId).raw(),
|
---|
381 | machine.asOutParam()));
|
---|
382 | if (FAILED(rc))
|
---|
383 | return VERR_NOT_FOUND;
|
---|
384 |
|
---|
385 | /* Machine is running? */
|
---|
386 | MachineState_T machineState;
|
---|
387 | CHECK_ERROR_RET(machine, COMGETTER(State)(&machineState), 1);
|
---|
388 | if (machineState != MachineState_Running)
|
---|
389 | {
|
---|
390 | RTMsgError("Machine \"%s\" is not running (currently %s)!\n",
|
---|
391 | pszNameOrId, machineStateToName(machineState, false));
|
---|
392 | return VERR_VM_INVALID_VM_STATE;
|
---|
393 | }
|
---|
394 |
|
---|
395 | do
|
---|
396 | {
|
---|
397 | /* Open a session for the VM. */
|
---|
398 | CHECK_ERROR_BREAK(machine, LockMachine(pArg->session, LockType_Shared));
|
---|
399 | /* Get the associated console. */
|
---|
400 | ComPtr<IConsole> console;
|
---|
401 | CHECK_ERROR_BREAK(pArg->session, COMGETTER(Console)(console.asOutParam()));
|
---|
402 | /* ... and session machine. */
|
---|
403 | ComPtr<IMachine> sessionMachine;
|
---|
404 | CHECK_ERROR_BREAK(pArg->session, COMGETTER(Machine)(sessionMachine.asOutParam()));
|
---|
405 | /* Get IGuest interface. */
|
---|
406 | CHECK_ERROR_BREAK(console, COMGETTER(Guest)(pGuest->asOutParam()));
|
---|
407 | } while (0);
|
---|
408 |
|
---|
409 | if (FAILED(rc))
|
---|
410 | ctrlUninitVM(pArg);
|
---|
411 | return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /* <Missing docuemntation> */
|
---|
415 | static int handleCtrlExecProgram(ComPtr<IGuest> guest, HandlerArg *pArg)
|
---|
416 | {
|
---|
417 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
418 |
|
---|
419 | /*
|
---|
420 | * Parse arguments.
|
---|
421 | */
|
---|
422 | if (pArg->argc < 2) /* At least the command we want to execute in the guest should be present :-). */
|
---|
423 | return errorSyntax(USAGE_GUESTCONTROL, "Incorrect parameters");
|
---|
424 |
|
---|
425 | static const RTGETOPTDEF s_aOptions[] =
|
---|
426 | {
|
---|
427 | { "--dos2unix", GETOPTDEF_EXEC_DOS2UNIX, RTGETOPT_REQ_NOTHING },
|
---|
428 | { "--environment", 'e', RTGETOPT_REQ_STRING },
|
---|
429 | { "--flags", 'f', RTGETOPT_REQ_STRING },
|
---|
430 | { "--ignore-operhaned-processes", GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES, RTGETOPT_REQ_NOTHING },
|
---|
431 | { "--image", 'i', RTGETOPT_REQ_STRING },
|
---|
432 | { "--password", 'p', RTGETOPT_REQ_STRING },
|
---|
433 | { "--timeout", 't', RTGETOPT_REQ_UINT32 },
|
---|
434 | { "--unix2dos", GETOPTDEF_EXEC_UNIX2DOS, RTGETOPT_REQ_NOTHING },
|
---|
435 | { "--username", 'u', RTGETOPT_REQ_STRING },
|
---|
436 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
437 | { "--wait-exit", GETOPTDEF_EXEC_WAITFOREXIT, RTGETOPT_REQ_NOTHING },
|
---|
438 | { "--wait-stdout", GETOPTDEF_EXEC_WAITFORSTDOUT, RTGETOPT_REQ_NOTHING },
|
---|
439 | { "--wait-stderr", GETOPTDEF_EXEC_WAITFORSTDERR, RTGETOPT_REQ_NOTHING }
|
---|
440 | };
|
---|
441 |
|
---|
442 | int ch;
|
---|
443 | RTGETOPTUNION ValueUnion;
|
---|
444 | RTGETOPTSTATE GetState;
|
---|
445 | RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
|
---|
446 |
|
---|
447 | Utf8Str Utf8Cmd;
|
---|
448 | uint32_t fExecFlags = ExecuteProcessFlag_None;
|
---|
449 | uint32_t fOutputFlags = ProcessOutputFlag_None;
|
---|
450 | com::SafeArray<IN_BSTR> args;
|
---|
451 | com::SafeArray<IN_BSTR> env;
|
---|
452 | Utf8Str Utf8UserName;
|
---|
453 | Utf8Str Utf8Password;
|
---|
454 | uint32_t cMsTimeout = 0;
|
---|
455 | OUTPUTTYPE eOutputType = OUTPUTTYPE_UNDEFINED;
|
---|
456 | bool fOutputBinary = false;
|
---|
457 | bool fWaitForExit = false;
|
---|
458 | bool fWaitForStdOut = false;
|
---|
459 | bool fVerbose = false;
|
---|
460 |
|
---|
461 | int vrc = VINF_SUCCESS;
|
---|
462 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
463 | && RT_SUCCESS(vrc))
|
---|
464 | {
|
---|
465 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
466 | switch (ch)
|
---|
467 | {
|
---|
468 | case GETOPTDEF_EXEC_DOS2UNIX:
|
---|
469 | if (eOutputType != OUTPUTTYPE_UNDEFINED)
|
---|
470 | return errorSyntax(USAGE_GUESTCONTROL, "More than one output type (dos2unix/unix2dos) specified!");
|
---|
471 | eOutputType = OUTPUTTYPE_DOS2UNIX;
|
---|
472 | break;
|
---|
473 |
|
---|
474 | case 'e': /* Environment */
|
---|
475 | {
|
---|
476 | char **papszArg;
|
---|
477 | int cArgs;
|
---|
478 |
|
---|
479 | vrc = RTGetOptArgvFromString(&papszArg, &cArgs, ValueUnion.psz, NULL);
|
---|
480 | if (RT_FAILURE(vrc))
|
---|
481 | return errorSyntax(USAGE_GUESTCONTROL, "Failed to parse environment value, rc=%Rrc", vrc);
|
---|
482 | for (int j = 0; j < cArgs; j++)
|
---|
483 | env.push_back(Bstr(papszArg[j]).raw());
|
---|
484 |
|
---|
485 | RTGetOptArgvFree(papszArg);
|
---|
486 | break;
|
---|
487 | }
|
---|
488 |
|
---|
489 | case GETOPTDEF_EXEC_IGNOREORPHANEDPROCESSES:
|
---|
490 | fExecFlags |= ExecuteProcessFlag_IgnoreOrphanedProcesses;
|
---|
491 | break;
|
---|
492 |
|
---|
493 | case 'i':
|
---|
494 | Utf8Cmd = ValueUnion.psz;
|
---|
495 | break;
|
---|
496 |
|
---|
497 | /** @todo Add a hidden flag. */
|
---|
498 |
|
---|
499 | case 'p': /* Password */
|
---|
500 | Utf8Password = ValueUnion.psz;
|
---|
501 | break;
|
---|
502 |
|
---|
503 | case 't': /* Timeout */
|
---|
504 | cMsTimeout = ValueUnion.u32;
|
---|
505 | break;
|
---|
506 |
|
---|
507 | case GETOPTDEF_EXEC_UNIX2DOS:
|
---|
508 | if (eOutputType != OUTPUTTYPE_UNDEFINED)
|
---|
509 | return errorSyntax(USAGE_GUESTCONTROL, "More than one output type (dos2unix/unix2dos) specified!");
|
---|
510 | eOutputType = OUTPUTTYPE_UNIX2DOS;
|
---|
511 | break;
|
---|
512 |
|
---|
513 | case 'u': /* User name */
|
---|
514 | Utf8UserName = ValueUnion.psz;
|
---|
515 | break;
|
---|
516 |
|
---|
517 | case 'v': /* Verbose */
|
---|
518 | fVerbose = true;
|
---|
519 | break;
|
---|
520 |
|
---|
521 | case GETOPTDEF_EXEC_WAITFOREXIT:
|
---|
522 | fWaitForExit = true;
|
---|
523 | break;
|
---|
524 |
|
---|
525 | case GETOPTDEF_EXEC_WAITFORSTDOUT:
|
---|
526 | fWaitForExit = true;
|
---|
527 | fWaitForStdOut = true;
|
---|
528 | break;
|
---|
529 |
|
---|
530 | case GETOPTDEF_EXEC_WAITFORSTDERR:
|
---|
531 | fWaitForExit = (fOutputFlags |= ProcessOutputFlag_StdErr) ? true : false;
|
---|
532 | break;
|
---|
533 |
|
---|
534 | case VINF_GETOPT_NOT_OPTION:
|
---|
535 | {
|
---|
536 | if (args.size() == 0 && Utf8Cmd.isEmpty())
|
---|
537 | Utf8Cmd = ValueUnion.psz;
|
---|
538 | else
|
---|
539 | args.push_back(Bstr(ValueUnion.psz).raw());
|
---|
540 | break;
|
---|
541 | }
|
---|
542 |
|
---|
543 | default:
|
---|
544 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | if (Utf8Cmd.isEmpty())
|
---|
549 | return errorSyntax(USAGE_GUESTCONTROL, "No command to execute specified!");
|
---|
550 |
|
---|
551 | if (Utf8UserName.isEmpty())
|
---|
552 | return errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
|
---|
553 |
|
---|
554 | /*
|
---|
555 | * <missing comment indicating that we're done parsing args and started doing something else>
|
---|
556 | */
|
---|
557 | HRESULT rc = S_OK;
|
---|
558 | if (fVerbose)
|
---|
559 | {
|
---|
560 | if (cMsTimeout == 0)
|
---|
561 | RTPrintf("Waiting for guest to start process ...\n");
|
---|
562 | else
|
---|
563 | RTPrintf("Waiting for guest to start process (within %ums)\n", cMsTimeout);
|
---|
564 | }
|
---|
565 |
|
---|
566 | /* Get current time stamp to later calculate rest of timeout left. */
|
---|
567 | uint64_t u64StartMS = RTTimeMilliTS();
|
---|
568 |
|
---|
569 | /* Execute the process. */
|
---|
570 | int rcProc = RTEXITCODE_FAILURE;
|
---|
571 | ComPtr<IProgress> progress;
|
---|
572 | ULONG uPID = 0;
|
---|
573 | rc = guest->ExecuteProcess(Bstr(Utf8Cmd).raw(),
|
---|
574 | fExecFlags,
|
---|
575 | ComSafeArrayAsInParam(args),
|
---|
576 | ComSafeArrayAsInParam(env),
|
---|
577 | Bstr(Utf8UserName).raw(),
|
---|
578 | Bstr(Utf8Password).raw(),
|
---|
579 | cMsTimeout,
|
---|
580 | &uPID,
|
---|
581 | progress.asOutParam());
|
---|
582 | if (FAILED(rc))
|
---|
583 | return ctrlPrintError(guest, COM_IIDOF(IGuest));
|
---|
584 |
|
---|
585 | if (fVerbose)
|
---|
586 | RTPrintf("Process '%s' (PID: %u) started\n", Utf8Cmd.c_str(), uPID);
|
---|
587 | if (fWaitForExit)
|
---|
588 | {
|
---|
589 | if (fVerbose)
|
---|
590 | {
|
---|
591 | if (cMsTimeout) /* Wait with a certain timeout. */
|
---|
592 | {
|
---|
593 | /* Calculate timeout value left after process has been started. */
|
---|
594 | uint64_t u64Elapsed = RTTimeMilliTS() - u64StartMS;
|
---|
595 | /* Is timeout still bigger than current difference? */
|
---|
596 | if (cMsTimeout > u64Elapsed)
|
---|
597 | RTPrintf("Waiting for process to exit (%ums left) ...\n", cMsTimeout - u64Elapsed);
|
---|
598 | else
|
---|
599 | RTPrintf("No time left to wait for process!\n"); /** @todo a bit misleading ... */
|
---|
600 | }
|
---|
601 | else /* Wait forever. */
|
---|
602 | RTPrintf("Waiting for process to exit ...\n");
|
---|
603 | }
|
---|
604 |
|
---|
605 | /* Setup signal handling if cancelable. */
|
---|
606 | ASSERT(progress);
|
---|
607 | bool fCanceledAlready = false;
|
---|
608 | BOOL fCancelable;
|
---|
609 | HRESULT hrc = progress->COMGETTER(Cancelable)(&fCancelable);
|
---|
610 | if (FAILED(hrc))
|
---|
611 | fCancelable = FALSE;
|
---|
612 | if (fCancelable)
|
---|
613 | ctrlSignalHandlerInstall();
|
---|
614 |
|
---|
615 | /* Wait for process to exit ... */
|
---|
616 | BOOL fCompleted = FALSE;
|
---|
617 | BOOL fCanceled = FALSE;
|
---|
618 | while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
|
---|
619 | {
|
---|
620 | SafeArray<BYTE> aOutputData;
|
---|
621 | ULONG cbOutputData = 0;
|
---|
622 |
|
---|
623 | /*
|
---|
624 | * Some data left to output?
|
---|
625 | */
|
---|
626 | if (fOutputFlags || fWaitForStdOut)
|
---|
627 | {
|
---|
628 | /** @todo r=bird: The timeout argument is bogus in several
|
---|
629 | * ways:
|
---|
630 | * 1. RT_MAX will evaluate the arguments twice, which may
|
---|
631 | * result in different values because RTTimeMilliTS()
|
---|
632 | * returns a higher value the 2nd time. Worst case:
|
---|
633 | * Imagine when RT_MAX calculates the remaining time
|
---|
634 | * out (first expansion) there is say 60 ms left. Then
|
---|
635 | * we're preempted and rescheduled after, say, 120 ms.
|
---|
636 | * We call RTTimeMilliTS() again and ends up with a
|
---|
637 | * value -60 ms, which translate to a UINT32_MAX - 59
|
---|
638 | * ms timeout.
|
---|
639 | *
|
---|
640 | * 2. When the period expires, we will wait forever since
|
---|
641 | * both 0 and -1 mean indefinite timeout with this API,
|
---|
642 | * at least that's one way of reading the main code.
|
---|
643 | *
|
---|
644 | * 3. There is a signed/unsigned ambiguity in the
|
---|
645 | * RT_MAX expression. The left hand side is signed
|
---|
646 | * integer (0), the right side is unsigned 64-bit. From
|
---|
647 | * what I can tell, the compiler will treat this as
|
---|
648 | * unsigned 64-bit and never return 0.
|
---|
649 | */
|
---|
650 | rc = guest->GetProcessOutput(uPID, fOutputFlags,
|
---|
651 | RT_MAX(0, cMsTimeout - (RTTimeMilliTS() - u64StartMS)) /* Timeout in ms */,
|
---|
652 | _64K, ComSafeArrayAsOutParam(aOutputData));
|
---|
653 | if (FAILED(rc))
|
---|
654 | {
|
---|
655 | vrc = ctrlPrintError(guest, COM_IIDOF(IGuest));
|
---|
656 | cbOutputData = 0;
|
---|
657 | }
|
---|
658 | else
|
---|
659 | {
|
---|
660 | cbOutputData = aOutputData.size();
|
---|
661 | if (cbOutputData > 0)
|
---|
662 | {
|
---|
663 | /** @todo r=bird: Use a VFS I/O stream filter for doing this, it's a
|
---|
664 | * generic problem and the new VFS APIs will handle it more
|
---|
665 | * transparently. (requires writing dos2unix/unix2dos filters ofc) */
|
---|
666 | if (eOutputType != OUTPUTTYPE_UNDEFINED)
|
---|
667 | {
|
---|
668 | /*
|
---|
669 | * If aOutputData is text data from the guest process' stdout or stderr,
|
---|
670 | * it has a platform dependent line ending. So standardize on
|
---|
671 | * Unix style, as RTStrmWrite does the LF -> CR/LF replacement on
|
---|
672 | * Windows. Otherwise we end up with CR/CR/LF on Windows.
|
---|
673 | */
|
---|
674 | ULONG cbOutputDataPrint = cbOutputData;
|
---|
675 | for (BYTE *s = aOutputData.raw(), *d = s;
|
---|
676 | s - aOutputData.raw() < (ssize_t)cbOutputData;
|
---|
677 | s++, d++)
|
---|
678 | {
|
---|
679 | if (*s == '\r')
|
---|
680 | {
|
---|
681 | /* skip over CR, adjust destination */
|
---|
682 | d--;
|
---|
683 | cbOutputDataPrint--;
|
---|
684 | }
|
---|
685 | else if (s != d)
|
---|
686 | *d = *s;
|
---|
687 | }
|
---|
688 | RTStrmWrite(g_pStdOut, aOutputData.raw(), cbOutputDataPrint);
|
---|
689 | }
|
---|
690 | else /* Just dump all data as we got it ... */
|
---|
691 | RTStrmWrite(g_pStdOut, aOutputData.raw(), cbOutputData);
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | /* No more output data left? */
|
---|
697 | if (cbOutputData <= 0)
|
---|
698 | {
|
---|
699 | /* Only break out from process handling loop if we processed (displayed)
|
---|
700 | * all output data or if there simply never was output data and the process
|
---|
701 | * has been marked as complete. */
|
---|
702 | if (fCompleted)
|
---|
703 | break;
|
---|
704 | }
|
---|
705 |
|
---|
706 | /* Process async cancelation */
|
---|
707 | if (g_fGuestCtrlCanceled && !fCanceledAlready)
|
---|
708 | {
|
---|
709 | hrc = progress->Cancel();
|
---|
710 | if (SUCCEEDED(hrc))
|
---|
711 | fCanceledAlready = TRUE;
|
---|
712 | else
|
---|
713 | g_fGuestCtrlCanceled = false;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /* Progress canceled by Main API? */
|
---|
717 | if ( SUCCEEDED(progress->COMGETTER(Canceled(&fCanceled)))
|
---|
718 | && fCanceled)
|
---|
719 | break;
|
---|
720 |
|
---|
721 | /* Did we run out of time? */
|
---|
722 | if ( cMsTimeout
|
---|
723 | && RTTimeMilliTS() - u64StartMS > cMsTimeout)
|
---|
724 | {
|
---|
725 | progress->Cancel();
|
---|
726 | break;
|
---|
727 | }
|
---|
728 | } /* while */
|
---|
729 |
|
---|
730 | /* Undo signal handling */
|
---|
731 | if (fCancelable)
|
---|
732 | ctrlSignalHandlerUninstall();
|
---|
733 |
|
---|
734 | /* Report status back to the user. */
|
---|
735 | if (fCanceled)
|
---|
736 | {
|
---|
737 | if (fVerbose)
|
---|
738 | RTPrintf("Process execution canceled!\n");
|
---|
739 | rcProc = EXITCODEEXEC_CANCELED;
|
---|
740 | }
|
---|
741 | else if ( fCompleted
|
---|
742 | && SUCCEEDED(rc)) /* The GetProcessOutput rc. */
|
---|
743 | {
|
---|
744 | LONG iRc;
|
---|
745 | CHECK_ERROR_RET(progress, COMGETTER(ResultCode)(&iRc), rc);
|
---|
746 | if (FAILED(iRc))
|
---|
747 | vrc = ctrlPrintProgressError(progress);
|
---|
748 | else
|
---|
749 | {
|
---|
750 | ExecuteProcessStatus_T retStatus;
|
---|
751 | ULONG uRetExitCode, uRetFlags;
|
---|
752 | rc = guest->GetProcessStatus(uPID, &uRetExitCode, &uRetFlags, &retStatus);
|
---|
753 | if (SUCCEEDED(rc) && fVerbose)
|
---|
754 | RTPrintf("Exit code=%u (Status=%u [%s], Flags=%u)\n", uRetExitCode, retStatus, ctrlExecProcessStatusToText(retStatus), uRetFlags);
|
---|
755 | rcProc = ctrlExecProcessStatusToExitCode(retStatus, uRetExitCode);
|
---|
756 | }
|
---|
757 | }
|
---|
758 | else
|
---|
759 | {
|
---|
760 | if (fVerbose)
|
---|
761 | RTPrintf("Process execution aborted!\n");
|
---|
762 | rcProc = EXITCODEEXEC_TERM_ABEND;
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | if (RT_FAILURE(vrc) || FAILED(rc))
|
---|
767 | return RTEXITCODE_FAILURE;
|
---|
768 | return rcProc;
|
---|
769 | }
|
---|
770 |
|
---|
771 | /**
|
---|
772 | * Reads a specified directory (recursively) based on the copy flags
|
---|
773 | * and appends all matching entries to the supplied list.
|
---|
774 | *
|
---|
775 | * @return IPRT status code.
|
---|
776 | * @param pszRootDir Directory to start with. Must end with
|
---|
777 | * a trailing slash and must be absolute.
|
---|
778 | * @param pszSubDir Sub directory part relative to the root
|
---|
779 | * directory; needed for recursion.
|
---|
780 | * @param pszFilter Search filter (e.g. *.pdf).
|
---|
781 | * @param pszDest Destination directory.
|
---|
782 | * @param fFlags Copy flags.
|
---|
783 | * @param pcObjects Where to store the overall objects to
|
---|
784 | * copy found.
|
---|
785 | * @param dirMap Reference to destination directory map to store found
|
---|
786 | * directories (primary key) + files (secondary key, vector).
|
---|
787 | */
|
---|
788 | static int ctrlCopyDirectoryRead(const char *pszRootDir, const char *pszSubDir,
|
---|
789 | const char *pszFilter, const char *pszDest,
|
---|
790 | uint32_t fFlags, uint32_t *pcObjects, DESTDIRMAP &dirMap)
|
---|
791 | {
|
---|
792 | AssertPtrReturn(pszRootDir, VERR_INVALID_POINTER);
|
---|
793 | /* Sub directory is optional. */
|
---|
794 | /* Filter directory is optional. */
|
---|
795 | AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
|
---|
796 | AssertPtrReturn(pcObjects, VERR_INVALID_POINTER);
|
---|
797 |
|
---|
798 | /*
|
---|
799 | * Construct current path.
|
---|
800 | */
|
---|
801 | char szCurDir[RTPATH_MAX];
|
---|
802 | int rc = RTStrCopy(szCurDir, sizeof(szCurDir), pszRootDir);
|
---|
803 | if (RT_SUCCESS(rc) && pszSubDir != NULL)
|
---|
804 | rc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir);
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * Open directory without a filter - RTDirOpenFiltered unfortunately
|
---|
808 | * cannot handle sub directories so we have to do the filtering ourselves.
|
---|
809 | */
|
---|
810 | PRTDIR pDir = NULL;
|
---|
811 | if (RT_SUCCESS(rc))
|
---|
812 | {
|
---|
813 | rc = RTDirOpen(&pDir, szCurDir);
|
---|
814 | if (RT_FAILURE(rc))
|
---|
815 | pDir = NULL;
|
---|
816 | }
|
---|
817 | if (RT_SUCCESS(rc))
|
---|
818 | {
|
---|
819 | /*
|
---|
820 | * Enumerate the directory tree.
|
---|
821 | */
|
---|
822 | while (RT_SUCCESS(rc))
|
---|
823 | {
|
---|
824 | RTDIRENTRY DirEntry;
|
---|
825 | rc = RTDirRead(pDir, &DirEntry, NULL);
|
---|
826 | if (RT_FAILURE(rc))
|
---|
827 | {
|
---|
828 | if (rc == VERR_NO_MORE_FILES)
|
---|
829 | rc = VINF_SUCCESS;
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | switch (DirEntry.enmType)
|
---|
833 | {
|
---|
834 | case RTDIRENTRYTYPE_DIRECTORY:
|
---|
835 | /* Skip "." and ".." entries. */
|
---|
836 | if ( !strcmp(DirEntry.szName, ".")
|
---|
837 | || !strcmp(DirEntry.szName, ".."))
|
---|
838 | break;
|
---|
839 |
|
---|
840 | if (fFlags & CopyFileFlag_Recursive)
|
---|
841 | {
|
---|
842 | char *pszNewSub = NULL;
|
---|
843 | if (pszSubDir)
|
---|
844 | RTStrAPrintf(&pszNewSub, "%s/%s", pszSubDir, DirEntry.szName);
|
---|
845 | else
|
---|
846 | RTStrAPrintf(&pszNewSub, "%s", DirEntry.szName);
|
---|
847 |
|
---|
848 | if (pszNewSub)
|
---|
849 | {
|
---|
850 | dirMap[pszNewSub];
|
---|
851 |
|
---|
852 | rc = ctrlCopyDirectoryRead(pszRootDir, pszNewSub,
|
---|
853 | pszFilter, pszDest,
|
---|
854 | fFlags, pcObjects, dirMap);
|
---|
855 | RTStrFree(pszNewSub);
|
---|
856 | }
|
---|
857 | else
|
---|
858 | rc = VERR_NO_MEMORY;
|
---|
859 | }
|
---|
860 | break;
|
---|
861 |
|
---|
862 | case RTDIRENTRYTYPE_SYMLINK:
|
---|
863 | if ( (fFlags & CopyFileFlag_Recursive)
|
---|
864 | && (fFlags & CopyFileFlag_FollowLinks))
|
---|
865 | {
|
---|
866 | /* Fall through to next case is intentional. */
|
---|
867 | }
|
---|
868 | else
|
---|
869 | break;
|
---|
870 |
|
---|
871 | case RTDIRENTRYTYPE_FILE:
|
---|
872 | {
|
---|
873 | if ( !pszFilter
|
---|
874 | || RTStrSimplePatternMatch(pszFilter, DirEntry.szName))
|
---|
875 | {
|
---|
876 | dirMap[pszSubDir].push_back(DESTFILEENTRY(Utf8Str(DirEntry.szName)));
|
---|
877 | *pcObjects += 1;
|
---|
878 | }
|
---|
879 | }
|
---|
880 | break;
|
---|
881 |
|
---|
882 | default:
|
---|
883 | break;
|
---|
884 | }
|
---|
885 | if (RT_FAILURE(rc))
|
---|
886 | break;
|
---|
887 | }
|
---|
888 |
|
---|
889 | RTDirClose(pDir);
|
---|
890 | }
|
---|
891 | return rc;
|
---|
892 | }
|
---|
893 |
|
---|
894 | /**
|
---|
895 | * Constructs a destinations map from a source entry and a destination root.
|
---|
896 | *
|
---|
897 | * @return IPRT status code.
|
---|
898 | * @param sourceEntry Reference to a specified source entry to use.
|
---|
899 | * @param fFlags Copy file flags. Needed for recursive directory parsing.
|
---|
900 | * @param pszDestRoot Pointer to destination root. This can be used to add one or
|
---|
901 | * more directories to the actual destination path.
|
---|
902 | * @param mapDest Reference to the destination map for storing the actual result.
|
---|
903 | * @param pcObjects Pointer to a total object (file) count to copy.
|
---|
904 | */
|
---|
905 | static int ctrlCopyConstructDestinations(SOURCEFILEENTRY &sourceEntry, uint32_t fFlags, const char *pszDestRoot,
|
---|
906 | DESTDIRMAP &mapDest, uint32_t *pcObjects)
|
---|
907 | {
|
---|
908 | int rc = VINF_SUCCESS;
|
---|
909 | const char *pszSource = sourceEntry.mSource.c_str();
|
---|
910 | if ( RTPathFilename(pszSource)
|
---|
911 | && RTFileExists(pszSource))
|
---|
912 | {
|
---|
913 | /* Source is a single file. */
|
---|
914 | char *pszFileName = RTPathFilename(pszSource);
|
---|
915 | mapDest[Utf8Str(pszDestRoot)].push_back(DESTFILEENTRY(pszFileName));
|
---|
916 |
|
---|
917 | *pcObjects++;
|
---|
918 | }
|
---|
919 | else
|
---|
920 | {
|
---|
921 | /* Source is either a directory or a filter (e.g. *.dll). */
|
---|
922 | rc = ctrlCopyDirectoryRead(pszSource,
|
---|
923 | NULL /* pszSubDir */,
|
---|
924 | sourceEntry.mFilter.isEmpty() ? NULL : sourceEntry.mFilter.c_str(),
|
---|
925 | pszDestRoot, fFlags, pcObjects, mapDest);
|
---|
926 | }
|
---|
927 | return rc;
|
---|
928 | }
|
---|
929 |
|
---|
930 | /**
|
---|
931 | * Determines the destination root for a specified source entry.
|
---|
932 | *
|
---|
933 | * @return IPRT status code.
|
---|
934 | * @param ppszDestRoot Receives pointer of allocated destination root.
|
---|
935 | * @param sourceEntry Source entry to determine the destination root for.
|
---|
936 | * @param pszDest Original destination string to use.
|
---|
937 | */
|
---|
938 | static int ctrlCopyGetDestinationRoot(char **ppszDestRoot, SOURCEFILEENTRY &sourceEntry, const char *pszDest)
|
---|
939 | {
|
---|
940 | AssertPtrReturn(ppszDestRoot, VERR_INVALID_POINTER);
|
---|
941 | AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
|
---|
942 |
|
---|
943 | int rc = VINF_SUCCESS;
|
---|
944 |
|
---|
945 | /*
|
---|
946 | * If a source filter is set (e.g. *.dll) use the original
|
---|
947 | * destination as our final root, because we want to copy all filtered
|
---|
948 | * files directly into the original root (and its sub directories if apply).
|
---|
949 | */
|
---|
950 | char *pszDestRoot;
|
---|
951 | if (!sourceEntry.mFilter.isEmpty())
|
---|
952 | pszDestRoot = RTStrDup(pszDest);
|
---|
953 | else
|
---|
954 | {
|
---|
955 | /*
|
---|
956 | * However, if no source filter is set we want to also copy the original
|
---|
957 | * source directory to our destination so that source "c:\foo", dest "c:\temp"
|
---|
958 | * becomes "c:\temp\foo".
|
---|
959 | */
|
---|
960 | int iLen = RTStrAPrintf(&pszDestRoot, "%s/%s",
|
---|
961 | pszDest, RTPathFilename(sourceEntry.mSource.c_str()));
|
---|
962 | if (!iLen)
|
---|
963 | rc = VERR_NO_MEMORY;
|
---|
964 | }
|
---|
965 |
|
---|
966 | if (RT_SUCCESS(rc))
|
---|
967 | *ppszDestRoot = pszDestRoot;
|
---|
968 | return rc;
|
---|
969 | }
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Prepares the destination directory hirarchy on the guest side by creating the directories
|
---|
973 | * and sets the appropriate access rights.
|
---|
974 | *
|
---|
975 | * @return IPRT status code.
|
---|
976 | * @param pGuest IGuest interface pointer.
|
---|
977 | * @param itDest Destination map iterator to process.
|
---|
978 | * @param pszDestRoot Destination root to use.
|
---|
979 | * @param pszUsername Username to use.
|
---|
980 | * @param pszPassword Password to use.
|
---|
981 | */
|
---|
982 | static int ctrlCopyPrepareDestDirectory(IGuest *pGuest, DESTDIRMAPITER itDest, const char *pszDestRoot,
|
---|
983 | const char *pszUsername, const char *pszPassword)
|
---|
984 | {
|
---|
985 | AssertPtrReturn(pGuest, VERR_INVALID_POINTER);
|
---|
986 | AssertPtrReturn(pszDestRoot, VERR_INVALID_POINTER);
|
---|
987 | AssertPtrReturn(pszUsername, VERR_INVALID_POINTER);
|
---|
988 | AssertPtrReturn(pszPassword, VERR_INVALID_POINTER);
|
---|
989 |
|
---|
990 | ComPtr<IProgress> progress;
|
---|
991 | char *pszDestFinal = NULL;
|
---|
992 | int rc = VINF_SUCCESS;
|
---|
993 |
|
---|
994 | /* Create root directory (= empty name) and skip the rest for
|
---|
995 | * this round. */
|
---|
996 | if (itDest->first.isEmpty())
|
---|
997 | {
|
---|
998 | pszDestFinal = RTStrDup(pszDestRoot);
|
---|
999 | if (!pszDestFinal)
|
---|
1000 | rc = VERR_NO_MEMORY;
|
---|
1001 | }
|
---|
1002 | else /* Create sub-directories, also empty ones. */
|
---|
1003 | {
|
---|
1004 | if (!RTStrAPrintf(&pszDestFinal, "%s/%s", pszDestRoot, itDest->first.c_str()))
|
---|
1005 | rc = VERR_NO_MEMORY;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | if (RT_SUCCESS(rc) && pszDestFinal)
|
---|
1009 | {
|
---|
1010 | HRESULT hrc = pGuest->CreateDirectory(Bstr(pszDestFinal).raw(),
|
---|
1011 | Bstr(pszUsername).raw(), Bstr(pszPassword).raw(),
|
---|
1012 | 700, CreateDirectoryFlag_Parents, progress.asOutParam());
|
---|
1013 | if (FAILED(hrc))
|
---|
1014 | rc = ctrlPrintError(pGuest, COM_IIDOF(IGuest));
|
---|
1015 | RTStrFree(pszDestFinal);
|
---|
1016 | }
|
---|
1017 | return rc;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /**
|
---|
1021 | * Copys a file from host to the guest.
|
---|
1022 | *
|
---|
1023 | * @return IPRT status code.
|
---|
1024 | * @param pGuest IGuest interface pointer.
|
---|
1025 | * @param pszSource Source path of existing host file to copy.
|
---|
1026 | * @param pszDest Destination path on guest to copy the file to.
|
---|
1027 | * @param pszUserName User name on guest to use for the copy operation.
|
---|
1028 | * @param pszPassword Password of user account.
|
---|
1029 | * @param fFlags Copy flags.
|
---|
1030 | */
|
---|
1031 | static int ctrlCopyFileToGuest(IGuest *pGuest, const char *pszSource, const char *pszDest,
|
---|
1032 | const char *pszUserName, const char *pszPassword,
|
---|
1033 | uint32_t fFlags)
|
---|
1034 | {
|
---|
1035 | AssertPtrReturn(pGuest, VERR_INVALID_POINTER);
|
---|
1036 | AssertPtrReturn(pszSource, VERR_INVALID_POINTER);
|
---|
1037 | AssertPtrReturn(pszDest, VERR_INVALID_POINTER);
|
---|
1038 | AssertPtrReturn(pszUserName, VERR_INVALID_POINTER);
|
---|
1039 | AssertPtrReturn(pszPassword, VERR_INVALID_POINTER);
|
---|
1040 |
|
---|
1041 | int vrc = VINF_SUCCESS;
|
---|
1042 | ComPtr<IProgress> progress;
|
---|
1043 | HRESULT rc = pGuest->CopyToGuest(Bstr(pszSource).raw(), Bstr(pszDest).raw(),
|
---|
1044 | Bstr(pszUserName).raw(), Bstr(pszPassword).raw(),
|
---|
1045 | fFlags, progress.asOutParam());
|
---|
1046 | if (FAILED(rc))
|
---|
1047 | vrc = ctrlPrintError(pGuest, COM_IIDOF(IGuest));
|
---|
1048 | else
|
---|
1049 | {
|
---|
1050 | rc = showProgress(progress);
|
---|
1051 | if (FAILED(rc))
|
---|
1052 | vrc = ctrlPrintProgressError(progress);
|
---|
1053 | }
|
---|
1054 | return vrc;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 |
|
---|
1058 | static int ctrlCopyToDestDirectory(IGuest *pGuest, bool fVerbose, DESTDIRMAPITER itDest, const char *pszDestRoot,
|
---|
1059 | SOURCEFILEENTRY &sourceEntry, uint32_t uFlags, const char *pszUsername, const char *pszPassword)
|
---|
1060 | {
|
---|
1061 | AssertPtrReturn(pGuest, VERR_INVALID_POINTER);
|
---|
1062 | AssertPtrReturn(pszDestRoot, VERR_INVALID_POINTER);
|
---|
1063 | AssertPtrReturn(pszUsername, VERR_INVALID_POINTER);
|
---|
1064 | AssertPtrReturn(pszPassword, VERR_INVALID_POINTER);
|
---|
1065 |
|
---|
1066 | int rc = VINF_SUCCESS;
|
---|
1067 | for (unsigned long l = 0; l < itDest->second.size(); l++)
|
---|
1068 | {
|
---|
1069 | int iLen;
|
---|
1070 | char *pszSource;
|
---|
1071 | if (itDest->first.isEmpty())
|
---|
1072 | iLen = RTStrAPrintf(&pszSource, "%s/%s", sourceEntry.mSource.c_str(),
|
---|
1073 | itDest->second[l].mFileName.c_str());
|
---|
1074 | else
|
---|
1075 | iLen = RTStrAPrintf(&pszSource, "%s/%s/%s", sourceEntry.mSource.c_str(),
|
---|
1076 | itDest->first.c_str(), itDest->second[l].mFileName.c_str());
|
---|
1077 | if (!iLen)
|
---|
1078 | {
|
---|
1079 | rc = VERR_NO_MEMORY;
|
---|
1080 | break;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | char *pszDest;
|
---|
1084 | if (itDest->first.isEmpty())
|
---|
1085 | iLen = RTStrAPrintf(&pszDest, "%s/%s", pszDestRoot,
|
---|
1086 | itDest->second[l].mFileName.c_str());
|
---|
1087 | else
|
---|
1088 | iLen = RTStrAPrintf(&pszDest, "%s/%s/%s", pszDestRoot, itDest->first.c_str(),
|
---|
1089 | itDest->second[l].mFileName.c_str());
|
---|
1090 | if (!iLen)
|
---|
1091 | {
|
---|
1092 | rc = VERR_NO_MEMORY;
|
---|
1093 | RTStrFree(pszSource);
|
---|
1094 | break;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | if (fVerbose)
|
---|
1098 | RTPrintf("\"%s\" -> \"%s\"\n", pszSource, pszDest);
|
---|
1099 |
|
---|
1100 | /* Finally copy the desired file (if no dry run selected). */
|
---|
1101 | rc = ctrlCopyFileToGuest(pGuest, pszSource, pszDest,
|
---|
1102 | pszUsername, pszPassword, uFlags);
|
---|
1103 | RTStrFree(pszSource);
|
---|
1104 | RTStrFree(pszDest);
|
---|
1105 | }
|
---|
1106 | return rc;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | static int handleCtrlCopyTo(ComPtr<IGuest> guest, HandlerArg *pArg)
|
---|
1110 | {
|
---|
1111 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
1112 |
|
---|
1113 | /** @todo r=bird: This command isn't very unix friendly in general. mkdir
|
---|
1114 | * is much better (partly because it is much simpler of course). The main
|
---|
1115 | * arguments against this is that (1) all but two options conflicts with
|
---|
1116 | * what 'man cp' tells me on a GNU/Linux system, (2) wildchar matching is
|
---|
1117 | * done windows CMD style (though not in a 100% compatible way), and (3)
|
---|
1118 | * that only one source is allowed - efficiently sabotaging default
|
---|
1119 | * wildcard expansion by a unix shell. The best solution here would be
|
---|
1120 | * two different variant, one windowsy (xcopy) and one unixy (gnu cp). */
|
---|
1121 |
|
---|
1122 | /*
|
---|
1123 | * IGuest::CopyToGuest is kept as simple as possible to let the developer choose
|
---|
1124 | * what and how to implement the file enumeration/recursive lookup, like VBoxManage
|
---|
1125 | * does in here.
|
---|
1126 | */
|
---|
1127 |
|
---|
1128 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1129 | {
|
---|
1130 | { "--dryrun", GETOPTDEF_COPYTO_DRYRUN, RTGETOPT_REQ_NOTHING },
|
---|
1131 | { "--follow", GETOPTDEF_COPYTO_FOLLOW, RTGETOPT_REQ_NOTHING },
|
---|
1132 | { "--password", GETOPTDEF_COPYTO_PASSWORD, RTGETOPT_REQ_STRING },
|
---|
1133 | { "--recursive", 'R', RTGETOPT_REQ_NOTHING },
|
---|
1134 | { "--target-directory", GETOPTDEF_COPYTO_TARGETDIR, RTGETOPT_REQ_STRING },
|
---|
1135 | { "--username", GETOPTDEF_COPYTO_USERNAME, RTGETOPT_REQ_STRING },
|
---|
1136 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1137 | };
|
---|
1138 |
|
---|
1139 | int ch;
|
---|
1140 | RTGETOPTUNION ValueUnion;
|
---|
1141 | RTGETOPTSTATE GetState;
|
---|
1142 | RTGetOptInit(&GetState, pArg->argc, pArg->argv,
|
---|
1143 | s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1144 |
|
---|
1145 | Utf8Str Utf8Source;
|
---|
1146 | Utf8Str Utf8Dest;
|
---|
1147 | Utf8Str Utf8UserName;
|
---|
1148 | Utf8Str Utf8Password;
|
---|
1149 | uint32_t fFlags = CopyFileFlag_None;
|
---|
1150 | bool fVerbose = false;
|
---|
1151 | bool fCopyRecursive = false;
|
---|
1152 | bool fDryRun = false;
|
---|
1153 |
|
---|
1154 | SOURCEVEC vecSources;
|
---|
1155 |
|
---|
1156 | int vrc = VINF_SUCCESS;
|
---|
1157 | while ((ch = RTGetOpt(&GetState, &ValueUnion)))
|
---|
1158 | {
|
---|
1159 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1160 | switch (ch)
|
---|
1161 | {
|
---|
1162 | case GETOPTDEF_COPYTO_DRYRUN:
|
---|
1163 | fDryRun = true;
|
---|
1164 | break;
|
---|
1165 |
|
---|
1166 | case GETOPTDEF_COPYTO_FOLLOW:
|
---|
1167 | fFlags |= CopyFileFlag_FollowLinks;
|
---|
1168 | break;
|
---|
1169 |
|
---|
1170 | case GETOPTDEF_COPYTO_PASSWORD:
|
---|
1171 | Utf8Password = ValueUnion.psz;
|
---|
1172 | break;
|
---|
1173 |
|
---|
1174 | case 'R': /* Recursive processing */
|
---|
1175 | fFlags |= CopyFileFlag_Recursive;
|
---|
1176 | break;
|
---|
1177 |
|
---|
1178 | case GETOPTDEF_COPYTO_TARGETDIR:
|
---|
1179 | Utf8Dest = ValueUnion.psz;
|
---|
1180 | break;
|
---|
1181 |
|
---|
1182 | case GETOPTDEF_COPYTO_USERNAME:
|
---|
1183 | Utf8UserName = ValueUnion.psz;
|
---|
1184 | break;
|
---|
1185 |
|
---|
1186 | case 'v': /* Verbose */
|
---|
1187 | fVerbose = true;
|
---|
1188 | break;
|
---|
1189 |
|
---|
1190 | case VINF_GETOPT_NOT_OPTION:
|
---|
1191 | {
|
---|
1192 | /* Last argument and no destination specified with
|
---|
1193 | * --target-directory yet? Then use the current argument
|
---|
1194 | * as destination. */
|
---|
1195 | if ( pArg->argc == GetState.iNext
|
---|
1196 | && Utf8Dest.isEmpty())
|
---|
1197 | {
|
---|
1198 | Utf8Dest = ValueUnion.psz;
|
---|
1199 | }
|
---|
1200 | else
|
---|
1201 | {
|
---|
1202 | /* Save the source directory. */
|
---|
1203 | vecSources.push_back(SOURCEFILEENTRY(ValueUnion.psz));
|
---|
1204 | }
|
---|
1205 | break;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | default:
|
---|
1209 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | if (!vecSources.size())
|
---|
1214 | return errorSyntax(USAGE_GUESTCONTROL,
|
---|
1215 | "No source(s) specified!");
|
---|
1216 |
|
---|
1217 | if (Utf8Dest.isEmpty())
|
---|
1218 | return errorSyntax(USAGE_GUESTCONTROL,
|
---|
1219 | "No destination specified!");
|
---|
1220 |
|
---|
1221 | if (Utf8UserName.isEmpty())
|
---|
1222 | return errorSyntax(USAGE_GUESTCONTROL,
|
---|
1223 | "No user name specified!");
|
---|
1224 |
|
---|
1225 | /*
|
---|
1226 | * Done parsing arguments, do some more preparations.
|
---|
1227 | */
|
---|
1228 | if (fVerbose)
|
---|
1229 | {
|
---|
1230 | if (fDryRun)
|
---|
1231 | RTPrintf("Dry run - no files copied!\n");
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* Strip traling slash from destination path. */
|
---|
1235 | RTPathStripTrailingSlash(Utf8Dest.mutableRaw());
|
---|
1236 | Utf8Dest.jolt();
|
---|
1237 |
|
---|
1238 | /*
|
---|
1239 | * Here starts the actual fun!
|
---|
1240 | */
|
---|
1241 | for (unsigned long s = 0; s < vecSources.size(); s++)
|
---|
1242 | {
|
---|
1243 | uint32_t cObjects = 0;
|
---|
1244 | DESTDIRMAP mapDest;
|
---|
1245 |
|
---|
1246 | char *pszDestRoot;
|
---|
1247 | vrc = ctrlCopyGetDestinationRoot(&pszDestRoot, vecSources[s], Utf8Dest.c_str());
|
---|
1248 | if (RT_SUCCESS(vrc))
|
---|
1249 | {
|
---|
1250 | vrc = ctrlCopyConstructDestinations(vecSources[s], fFlags, pszDestRoot,
|
---|
1251 | mapDest, &cObjects);
|
---|
1252 | if (RT_FAILURE(vrc))
|
---|
1253 | {
|
---|
1254 | if ( fVerbose
|
---|
1255 | && vrc == VERR_FILE_NOT_FOUND)
|
---|
1256 | {
|
---|
1257 | RTPrintf("Warning: Source \"%s\" does not exist, skipping!\n",
|
---|
1258 | vecSources[s].mSource.c_str());
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 | else
|
---|
1262 | {
|
---|
1263 | /*
|
---|
1264 | * Prepare directory structure of each destination directory.
|
---|
1265 | */
|
---|
1266 | DESTDIRMAPITER itDest;
|
---|
1267 | ComPtr<IProgress> progress;
|
---|
1268 | for (itDest = mapDest.begin(); itDest != mapDest.end(); itDest++)
|
---|
1269 | {
|
---|
1270 | if (!fDryRun)
|
---|
1271 | vrc = ctrlCopyPrepareDestDirectory(guest, itDest, pszDestRoot,
|
---|
1272 | Utf8UserName.c_str(), Utf8Password.c_str());
|
---|
1273 | if (RT_FAILURE(vrc))
|
---|
1274 | break;
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | if (fVerbose)
|
---|
1278 | {
|
---|
1279 | if (!cObjects)
|
---|
1280 | RTPrintf("Warning: Source \"%s\" has no (matching) files to copy, skipping!\n",
|
---|
1281 | vecSources[s].mSource.c_str());
|
---|
1282 | else
|
---|
1283 | RTPrintf("Copying \"%s\" (%u files) ...\n",
|
---|
1284 | vecSources[s].mSource.c_str(), cObjects);
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /*
|
---|
1288 | * Copy files of each destination root directory to the guest.
|
---|
1289 | */
|
---|
1290 | for (itDest = mapDest.begin(); itDest != mapDest.end(); itDest++)
|
---|
1291 | {
|
---|
1292 | if (fVerbose && itDest->second.size())
|
---|
1293 | {
|
---|
1294 | if (itDest->first.isEmpty())
|
---|
1295 | RTPrintf("Copying %u files ...\n", itDest->second.size());
|
---|
1296 | else
|
---|
1297 | RTPrintf("Copying directory \"%s\" (%u files) ...\n",
|
---|
1298 | itDest->first.c_str(), itDest->second.size());
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | if (!fDryRun)
|
---|
1302 | vrc = ctrlCopyToDestDirectory(guest, fVerbose, itDest, pszDestRoot,
|
---|
1303 | vecSources[s], fFlags, Utf8UserName.c_str(), Utf8Password.c_str());
|
---|
1304 | if (RT_FAILURE(vrc))
|
---|
1305 | break;
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 | RTStrFree(pszDestRoot);
|
---|
1309 | }
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | static int handleCtrlCreateDirectory(ComPtr<IGuest> guest, HandlerArg *pArg)
|
---|
1316 | {
|
---|
1317 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
1318 |
|
---|
1319 | /*
|
---|
1320 | * Parse arguments.
|
---|
1321 | *
|
---|
1322 | * Note! No direct returns here, everyone must go thru the cleanup at the
|
---|
1323 | * end of this function.
|
---|
1324 | */
|
---|
1325 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1326 | {
|
---|
1327 | { "--mode", 'm', RTGETOPT_REQ_UINT32 },
|
---|
1328 | { "--parents", 'P', RTGETOPT_REQ_NOTHING },
|
---|
1329 | { "--password", 'p', RTGETOPT_REQ_STRING },
|
---|
1330 | { "--username", 'u', RTGETOPT_REQ_STRING },
|
---|
1331 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1332 | };
|
---|
1333 |
|
---|
1334 | int ch;
|
---|
1335 | RTGETOPTUNION ValueUnion;
|
---|
1336 | RTGETOPTSTATE GetState;
|
---|
1337 | RTGetOptInit(&GetState, pArg->argc, pArg->argv,
|
---|
1338 | s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1339 |
|
---|
1340 | Utf8Str Utf8UserName;
|
---|
1341 | Utf8Str Utf8Password;
|
---|
1342 | uint32_t fFlags = CreateDirectoryFlag_None;
|
---|
1343 | uint32_t fDirMode = 0; /* Default mode. */
|
---|
1344 | bool fVerbose = false;
|
---|
1345 |
|
---|
1346 | DESTDIRMAP mapDirs;
|
---|
1347 |
|
---|
1348 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
1349 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1350 | && rcExit == RTEXITCODE_SUCCESS)
|
---|
1351 | {
|
---|
1352 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
1353 | switch (ch)
|
---|
1354 | {
|
---|
1355 | case 'm': /* Mode */
|
---|
1356 | fDirMode = ValueUnion.u32;
|
---|
1357 | break;
|
---|
1358 |
|
---|
1359 | case 'P': /* Create parents */
|
---|
1360 | fFlags |= CreateDirectoryFlag_Parents;
|
---|
1361 | break;
|
---|
1362 |
|
---|
1363 | case 'p': /* Password */
|
---|
1364 | Utf8Password = ValueUnion.psz;
|
---|
1365 | break;
|
---|
1366 |
|
---|
1367 | case 'u': /* User name */
|
---|
1368 | Utf8UserName = ValueUnion.psz;
|
---|
1369 | break;
|
---|
1370 |
|
---|
1371 | case 'v': /* Verbose */
|
---|
1372 | fVerbose = true;
|
---|
1373 | break;
|
---|
1374 |
|
---|
1375 | case VINF_GETOPT_NOT_OPTION:
|
---|
1376 | {
|
---|
1377 | mapDirs[ValueUnion.psz]; /* Add destination directory to map. */
|
---|
1378 | break;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | default:
|
---|
1382 | rcExit = RTGetOptPrintError(ch, &ValueUnion);
|
---|
1383 | break;
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | uint32_t cDirs = mapDirs.size();
|
---|
1388 | if (rcExit == RTEXITCODE_SUCCESS && !cDirs)
|
---|
1389 | rcExit = errorSyntax(USAGE_GUESTCONTROL, "No directory to create specified!");
|
---|
1390 |
|
---|
1391 | if (rcExit == RTEXITCODE_SUCCESS && Utf8UserName.isEmpty())
|
---|
1392 | rcExit = errorSyntax(USAGE_GUESTCONTROL, "No user name specified!");
|
---|
1393 |
|
---|
1394 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
1395 | {
|
---|
1396 | /*
|
---|
1397 | * Create the directories.
|
---|
1398 | */
|
---|
1399 | HRESULT hrc = S_OK;
|
---|
1400 | if (fVerbose && cDirs)
|
---|
1401 | RTPrintf("Creating %u directories ...\n", cDirs);
|
---|
1402 |
|
---|
1403 | DESTDIRMAPITER it = mapDirs.begin();
|
---|
1404 | while (it != mapDirs.end())
|
---|
1405 | {
|
---|
1406 | if (fVerbose)
|
---|
1407 | RTPrintf("Creating directory \"%s\" ...\n", it->first.c_str());
|
---|
1408 |
|
---|
1409 | ComPtr<IProgress> progress;
|
---|
1410 | hrc = guest->CreateDirectory(Bstr(it->first).raw(),
|
---|
1411 | Bstr(Utf8UserName).raw(), Bstr(Utf8Password).raw(),
|
---|
1412 | fDirMode, fFlags, progress.asOutParam());
|
---|
1413 | if (FAILED(hrc))
|
---|
1414 | {
|
---|
1415 | ctrlPrintError(guest, COM_IIDOF(IGuest)); /* Return code ignored, save original rc. */
|
---|
1416 | break;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | it++;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | if (FAILED(hrc))
|
---|
1423 | rcExit = RTEXITCODE_FAILURE;
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | return rcExit;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | static int handleCtrlUpdateAdditions(ComPtr<IGuest> guest, HandlerArg *pArg)
|
---|
1430 | {
|
---|
1431 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
1432 |
|
---|
1433 | /*
|
---|
1434 | * Check the syntax. We can deduce the correct syntax from the number of
|
---|
1435 | * arguments.
|
---|
1436 | */
|
---|
1437 | Utf8Str Utf8Source;
|
---|
1438 | bool fVerbose = false;
|
---|
1439 |
|
---|
1440 | static const RTGETOPTDEF s_aOptions[] =
|
---|
1441 | {
|
---|
1442 | { "--source", 's', RTGETOPT_REQ_STRING },
|
---|
1443 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
1444 | };
|
---|
1445 |
|
---|
1446 | int ch;
|
---|
1447 | RTGETOPTUNION ValueUnion;
|
---|
1448 | RTGETOPTSTATE GetState;
|
---|
1449 | RTGetOptInit(&GetState, pArg->argc, pArg->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
|
---|
1450 |
|
---|
1451 | int vrc = VINF_SUCCESS;
|
---|
1452 | while ( (ch = RTGetOpt(&GetState, &ValueUnion))
|
---|
1453 | && RT_SUCCESS(vrc))
|
---|
1454 | {
|
---|
1455 | switch (ch)
|
---|
1456 | {
|
---|
1457 | case 's':
|
---|
1458 | Utf8Source = ValueUnion.psz;
|
---|
1459 | break;
|
---|
1460 |
|
---|
1461 | case 'v':
|
---|
1462 | fVerbose = true;
|
---|
1463 | break;
|
---|
1464 |
|
---|
1465 | default:
|
---|
1466 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
1467 | }
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | if (fVerbose)
|
---|
1471 | RTPrintf("Updating Guest Additions ...\n");
|
---|
1472 |
|
---|
1473 | #ifdef DEBUG_andy
|
---|
1474 | if (Utf8Source.isEmpty())
|
---|
1475 | Utf8Source = "c:\\Downloads\\VBoxGuestAdditions-r67158.iso";
|
---|
1476 | #endif
|
---|
1477 |
|
---|
1478 | /* Determine source if not set yet. */
|
---|
1479 | if (Utf8Source.isEmpty())
|
---|
1480 | {
|
---|
1481 | char strTemp[RTPATH_MAX];
|
---|
1482 | vrc = RTPathAppPrivateNoArch(strTemp, sizeof(strTemp));
|
---|
1483 | AssertRC(vrc);
|
---|
1484 | Utf8Str Utf8Src1 = Utf8Str(strTemp).append("/VBoxGuestAdditions.iso");
|
---|
1485 |
|
---|
1486 | vrc = RTPathExecDir(strTemp, sizeof(strTemp));
|
---|
1487 | AssertRC(vrc);
|
---|
1488 | Utf8Str Utf8Src2 = Utf8Str(strTemp).append("/additions/VBoxGuestAdditions.iso");
|
---|
1489 |
|
---|
1490 | /* Check the standard image locations */
|
---|
1491 | if (RTFileExists(Utf8Src1.c_str()))
|
---|
1492 | Utf8Source = Utf8Src1;
|
---|
1493 | else if (RTFileExists(Utf8Src2.c_str()))
|
---|
1494 | Utf8Source = Utf8Src2;
|
---|
1495 | else
|
---|
1496 | {
|
---|
1497 | RTMsgError("Source could not be determined! Please use --source to specify a valid source.\n");
|
---|
1498 | vrc = VERR_FILE_NOT_FOUND;
|
---|
1499 | }
|
---|
1500 | }
|
---|
1501 | else if (!RTFileExists(Utf8Source.c_str()))
|
---|
1502 | {
|
---|
1503 | RTMsgError("Source \"%s\" does not exist!\n", Utf8Source.c_str());
|
---|
1504 | vrc = VERR_FILE_NOT_FOUND;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | if (RT_SUCCESS(vrc))
|
---|
1508 | {
|
---|
1509 | if (fVerbose)
|
---|
1510 | RTPrintf("Using source: %s\n", Utf8Source.c_str());
|
---|
1511 |
|
---|
1512 | HRESULT rc = S_OK;
|
---|
1513 | ComPtr<IProgress> progress;
|
---|
1514 | CHECK_ERROR(guest, UpdateGuestAdditions(Bstr(Utf8Source).raw(),
|
---|
1515 | /* Wait for whole update process to complete. */
|
---|
1516 | AdditionsUpdateFlag_None,
|
---|
1517 | progress.asOutParam()));
|
---|
1518 | if (FAILED(rc))
|
---|
1519 | vrc = ctrlPrintError(guest, COM_IIDOF(IGuest));
|
---|
1520 | else
|
---|
1521 | {
|
---|
1522 | rc = showProgress(progress);
|
---|
1523 | if (FAILED(rc))
|
---|
1524 | vrc = ctrlPrintProgressError(progress);
|
---|
1525 | else if (fVerbose)
|
---|
1526 | RTPrintf("Guest Additions update successful.\n");
|
---|
1527 | }
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | return RT_SUCCESS(vrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | /**
|
---|
1534 | * Access the guest control store.
|
---|
1535 | *
|
---|
1536 | * @returns program exit code.
|
---|
1537 | * @note see the command line API description for parameters
|
---|
1538 | */
|
---|
1539 | int handleGuestControl(HandlerArg *pArg)
|
---|
1540 | {
|
---|
1541 | AssertPtrReturn(pArg, VERR_INVALID_PARAMETER);
|
---|
1542 |
|
---|
1543 | HandlerArg arg = *pArg;
|
---|
1544 | arg.argc = pArg->argc - 2; /* Skip VM name and sub command. */
|
---|
1545 | arg.argv = pArg->argv + 2; /* Same here. */
|
---|
1546 |
|
---|
1547 | ComPtr<IGuest> guest;
|
---|
1548 | int vrc = ctrlInitVM(pArg, pArg->argv[0] /* VM Name */, &guest);
|
---|
1549 | if (RT_SUCCESS(vrc))
|
---|
1550 | {
|
---|
1551 | int rcExit;
|
---|
1552 | if ( !strcmp(pArg->argv[1], "exec")
|
---|
1553 | || !strcmp(pArg->argv[1], "execute"))
|
---|
1554 | {
|
---|
1555 | rcExit = handleCtrlExecProgram(guest, &arg);
|
---|
1556 | }
|
---|
1557 | else if ( !strcmp(pArg->argv[1], "copyto")
|
---|
1558 | || !strcmp(pArg->argv[1], "cp"))
|
---|
1559 | {
|
---|
1560 | rcExit = handleCtrlCopyTo(guest, &arg);
|
---|
1561 | }
|
---|
1562 | else if ( !strcmp(pArg->argv[1], "createdirectory")
|
---|
1563 | || !strcmp(pArg->argv[1], "createdir")
|
---|
1564 | || !strcmp(pArg->argv[1], "mkdir")
|
---|
1565 | || !strcmp(pArg->argv[1], "md"))
|
---|
1566 | {
|
---|
1567 | rcExit = handleCtrlCreateDirectory(guest, &arg);
|
---|
1568 | }
|
---|
1569 | else if ( !strcmp(pArg->argv[1], "updateadditions")
|
---|
1570 | || !strcmp(pArg->argv[1], "updateadds"))
|
---|
1571 | {
|
---|
1572 | rcExit = handleCtrlUpdateAdditions(guest, &arg);
|
---|
1573 | }
|
---|
1574 | else
|
---|
1575 | rcExit = errorSyntax(USAGE_GUESTCONTROL, "No sub command specified!");
|
---|
1576 |
|
---|
1577 | ctrlUninitVM(pArg);
|
---|
1578 | return rcExit;
|
---|
1579 | }
|
---|
1580 | return RTEXITCODE_FAILURE;
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | #endif /* !VBOX_ONLY_DOCS */
|
---|
1584 |
|
---|