VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestSessionImplTasks.cpp@ 79294

Last change on this file since 79294 was 79189, checked in by vboxsync, 5 years ago

Main/Guest*: Must always initalize rcGuest because experience inddicates that the code handling it cannot always be depended upon to set it. bugref:9320

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 94.1 KB
Line 
1/* $Id: GuestSessionImplTasks.cpp 79189 2019-06-17 16:04:55Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest session tasks.
4 */
5
6/*
7 * Copyright (C) 2012-2019 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#define LOG_GROUP LOG_GROUP_MAIN_GUESTSESSION
23#include "LoggingNew.h"
24
25#include "GuestImpl.h"
26#ifndef VBOX_WITH_GUEST_CONTROL
27# error "VBOX_WITH_GUEST_CONTROL must defined in this file"
28#endif
29#include "GuestSessionImpl.h"
30#include "GuestSessionImplTasks.h"
31#include "GuestCtrlImplPrivate.h"
32
33#include "Global.h"
34#include "AutoCaller.h"
35#include "ConsoleImpl.h"
36#include "ProgressImpl.h"
37
38#include <memory> /* For auto_ptr. */
39
40#include <iprt/env.h>
41#include <iprt/file.h> /* For CopyTo/From. */
42#include <iprt/dir.h>
43#include <iprt/path.h>
44#include <iprt/fsvfs.h>
45
46
47/*********************************************************************************************************************************
48* Defines *
49*********************************************************************************************************************************/
50
51/**
52 * (Guest Additions) ISO file flags.
53 * Needed for handling Guest Additions updates.
54 */
55#define ISOFILE_FLAG_NONE 0
56/** Copy over the file from host to the
57 * guest. */
58#define ISOFILE_FLAG_COPY_FROM_ISO RT_BIT(0)
59/** Execute file on the guest after it has
60 * been successfully transfered. */
61#define ISOFILE_FLAG_EXECUTE RT_BIT(7)
62/** File is optional, does not have to be
63 * existent on the .ISO. */
64#define ISOFILE_FLAG_OPTIONAL RT_BIT(8)
65
66
67// session task classes
68/////////////////////////////////////////////////////////////////////////////
69
70GuestSessionTask::GuestSessionTask(GuestSession *pSession)
71 : ThreadTask("GenericGuestSessionTask")
72{
73 mSession = pSession;
74
75 switch (mSession->i_getPathStyle())
76 {
77 case PathStyle_DOS:
78 mfPathStyle = RTPATH_STR_F_STYLE_DOS;
79 mPathStyle = "\\";
80 break;
81
82 default:
83 mfPathStyle = RTPATH_STR_F_STYLE_UNIX;
84 mPathStyle = "/";
85 break;
86 }
87}
88
89GuestSessionTask::~GuestSessionTask(void)
90{
91}
92
93int GuestSessionTask::createAndSetProgressObject(ULONG cOperations /* = 1 */)
94{
95 LogFlowThisFunc(("cOperations=%ld\n", cOperations));
96
97 /* Create the progress object. */
98 ComObjPtr<Progress> pProgress;
99 HRESULT hr = pProgress.createObject();
100 if (FAILED(hr))
101 return VERR_COM_UNEXPECTED;
102
103 hr = pProgress->init(static_cast<IGuestSession*>(mSession),
104 Bstr(mDesc).raw(),
105 TRUE /* aCancelable */, cOperations, Bstr(mDesc).raw());
106 if (FAILED(hr))
107 return VERR_COM_UNEXPECTED;
108
109 mProgress = pProgress;
110
111 LogFlowFuncLeave();
112 return VINF_SUCCESS;
113}
114
115#if 0 /* unsed */
116/** @note The task object is owned by the thread after this returns, regardless of the result. */
117int GuestSessionTask::RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress)
118{
119 LogFlowThisFunc(("strDesc=%s\n", strDesc.c_str()));
120
121 mDesc = strDesc;
122 mProgress = pProgress;
123 HRESULT hrc = createThreadWithType(RTTHREADTYPE_MAIN_HEAVY_WORKER);
124
125 LogFlowThisFunc(("Returning hrc=%Rhrc\n", hrc));
126 return Global::vboxStatusCodeToCOM(hrc);
127}
128#endif
129
130int GuestSessionTask::getGuestProperty(const ComObjPtr<Guest> &pGuest,
131 const Utf8Str &strPath, Utf8Str &strValue)
132{
133 ComObjPtr<Console> pConsole = pGuest->i_getConsole();
134 const ComPtr<IMachine> pMachine = pConsole->i_machine();
135
136 Assert(!pMachine.isNull());
137 Bstr strTemp, strFlags;
138 LONG64 i64Timestamp;
139 HRESULT hr = pMachine->GetGuestProperty(Bstr(strPath).raw(),
140 strTemp.asOutParam(),
141 &i64Timestamp, strFlags.asOutParam());
142 if (SUCCEEDED(hr))
143 {
144 strValue = strTemp;
145 return VINF_SUCCESS;
146 }
147 return VERR_NOT_FOUND;
148}
149
150int GuestSessionTask::setProgress(ULONG uPercent)
151{
152 if (mProgress.isNull()) /* Progress is optional. */
153 return VINF_SUCCESS;
154
155 BOOL fCanceled;
156 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
157 && fCanceled)
158 return VERR_CANCELLED;
159 BOOL fCompleted;
160 if ( SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
161 && fCompleted)
162 {
163 AssertMsgFailed(("Setting value of an already completed progress\n"));
164 return VINF_SUCCESS;
165 }
166 HRESULT hr = mProgress->SetCurrentOperationProgress(uPercent);
167 if (FAILED(hr))
168 return VERR_COM_UNEXPECTED;
169
170 return VINF_SUCCESS;
171}
172
173int GuestSessionTask::setProgressSuccess(void)
174{
175 if (mProgress.isNull()) /* Progress is optional. */
176 return VINF_SUCCESS;
177
178 BOOL fCompleted;
179 if ( SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
180 && !fCompleted)
181 {
182#ifdef VBOX_STRICT
183 ULONG uCurOp; mProgress->COMGETTER(Operation(&uCurOp));
184 ULONG cOps; mProgress->COMGETTER(OperationCount(&cOps));
185 AssertMsg(uCurOp + 1 /* Zero-based */ == cOps, ("Not all operations done yet (%u/%u)\n", uCurOp + 1, cOps));
186#endif
187 HRESULT hr = mProgress->i_notifyComplete(S_OK);
188 if (FAILED(hr))
189 return VERR_COM_UNEXPECTED; /** @todo Find a better rc. */
190 }
191
192 return VINF_SUCCESS;
193}
194
195HRESULT GuestSessionTask::setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg)
196{
197 LogFlowFunc(("hr=%Rhrc, strMsg=%s\n",
198 hr, strMsg.c_str()));
199
200 if (mProgress.isNull()) /* Progress is optional. */
201 return hr; /* Return original rc. */
202
203 BOOL fCanceled;
204 BOOL fCompleted;
205 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
206 && !fCanceled
207 && SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
208 && !fCompleted)
209 {
210#if 0 /* Had to quick fix this myself now that the test case triggers it. Please address properly as suggested below. */
211 HRESULT hr2 = mProgress->i_notifyComplete(hr,
212 COM_IIDOF(IGuestSession),
213 GuestSession::getStaticComponentName(),
214 /** @todo r=bird: i_notifyComplete takes a format string, so this is
215 * potentially risky business if a user input mentioned by the message
216 * text contains '%s'! With code below for how to do this less
217 * painfully and with fewer string copies. */
218 strMsg.c_str());
219#else
220 HRESULT hr2 = mProgress->i_notifyComplete(hr,
221 COM_IIDOF(IGuestSession),
222 GuestSession::getStaticComponentName(),
223 "%s", strMsg.c_str());
224#endif
225 if (FAILED(hr2))
226 return hr2;
227 }
228 return hr; /* Return original rc. */
229}
230
231HRESULT GuestSessionTask::setProgressErrorMsg(HRESULT hrc, int vrc, const char *pszFormat, ...)
232{
233 LogFlowFunc(("hrc=%Rhrc, vrc=%Rrc, pszFormat=%s\n", hrc, vrc, pszFormat));
234
235 /* The progress object is optional. */
236 if (!mProgress.isNull())
237 {
238 BOOL fCanceled;
239 BOOL fCompleted;
240 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
241 && !fCanceled
242 && SUCCEEDED(mProgress->COMGETTER(Completed(&fCompleted)))
243 && !fCompleted)
244 {
245 va_list va;
246 va_start(va, pszFormat);
247 HRESULT hrc2 = mProgress->i_notifyCompleteBothV(hrc, vrc, COM_IIDOF(IGuestSession),
248 GuestSession::getStaticComponentName(), pszFormat, va);
249 va_end(va);
250 if (FAILED(hrc2))
251 hrc = hrc2;
252 }
253 }
254 return hrc;
255}
256
257/**
258 * Creates a directory on the guest.
259 *
260 * @return VBox status code. VERR_ALREADY_EXISTS if directory on the guest already exists.
261 * @param strPath Absolute path to directory on the guest (guest style path) to create.
262 * @param enmDirectoryCreateFlags Directory creation flags.
263 * @param fMode Directory mode to use for creation.
264 * @param fFollowSymlinks Whether to follow symlinks on the guest or not.
265 * @param fCanExist Whether the directory to create is allowed to exist already.
266 */
267int GuestSessionTask::directoryCreateOnGuest(const com::Utf8Str &strPath,
268 DirectoryCreateFlag_T enmDirectoryCreateFlags, uint32_t fMode,
269 bool fFollowSymlinks, bool fCanExist)
270{
271 LogFlowFunc(("strPath=%s, enmDirectoryCreateFlags=0x%x, fMode=%RU32, fFollowSymlinks=%RTbool, fCanExist=%RTbool\n",
272 strPath.c_str(), enmDirectoryCreateFlags, fMode, fFollowSymlinks, fCanExist));
273
274 GuestFsObjData objData;
275 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
276 int rc = mSession->i_directoryQueryInfo(strPath, fFollowSymlinks, objData, &rcGuest);
277 if (RT_SUCCESS(rc))
278 {
279 if (!fCanExist)
280 {
281 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
282 Utf8StrFmt(GuestSession::tr("Guest directory \"%s\" already exists"), strPath.c_str()));
283 return VERR_ALREADY_EXISTS;
284 }
285 }
286 else
287 {
288 switch (rc)
289 {
290 case VERR_GSTCTL_GUEST_ERROR:
291 {
292 switch (rcGuest)
293 {
294 case VERR_FILE_NOT_FOUND:
295 case VERR_PATH_NOT_FOUND:
296 rc = mSession->i_directoryCreate(strPath.c_str(), fMode, enmDirectoryCreateFlags, &rcGuest);
297 break;
298 default:
299 break;
300 }
301 break;
302 }
303
304 default:
305 break;
306 }
307 }
308
309 if (RT_FAILURE(rc))
310 {
311 if (rc == VERR_GSTCTL_GUEST_ERROR)
312 {
313 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestProcess::i_guestErrorToString(rcGuest));
314 }
315 else
316 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
317 Utf8StrFmt(GuestSession::tr("Error creating directory on the guest: %Rrc"), strPath.c_str(), rc));
318 }
319
320 LogFlowFuncLeaveRC(rc);
321 return rc;
322}
323
324/**
325 * Creates a directory on the host.
326 *
327 * @return VBox status code. VERR_ALREADY_EXISTS if directory on the guest already exists.
328 * @param strPath Absolute path to directory on the host (host style path) to create.
329 * @param fCreate Directory creation flags.
330 * @param fMode Directory mode to use for creation.
331 * @param fCanExist Whether the directory to create is allowed to exist already.
332 */
333int GuestSessionTask::directoryCreateOnHost(const com::Utf8Str &strPath, uint32_t fCreate, uint32_t fMode, bool fCanExist)
334{
335 LogFlowFunc(("strPath=%s, fCreate=0x%x, fMode=%RU32, fCanExist=%RTbool\n", strPath.c_str(), fCreate, fMode, fCanExist));
336
337 int rc = RTDirCreate(strPath.c_str(), fMode, fCreate);
338 if (RT_FAILURE(rc))
339 {
340 if (rc == VERR_ALREADY_EXISTS)
341 {
342 if (!fCanExist)
343 {
344 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
345 Utf8StrFmt(GuestSession::tr("Host directory \"%s\" already exists"), strPath.c_str()));
346 }
347 else
348 rc = VINF_SUCCESS;
349 }
350 else
351 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
352 Utf8StrFmt(GuestSession::tr("Could not create host directory \"%s\": %Rrc"),
353 strPath.c_str(), rc));
354 }
355
356 LogFlowFuncLeaveRC(rc);
357 return rc;
358}
359
360/**
361 * Main function for copying a file from guest to the host.
362 *
363 * @return VBox status code.
364 * @param srcFile Guest file (source) to copy to the host. Must be in opened and ready state already.
365 * @param phDstFile Pointer to host file handle (destination) to copy to. Must be in opened and ready state already.
366 * @param fFileCopyFlags File copy flags.
367 * @param offCopy Offset (in bytes) where to start copying the source file.
368 * @param cbSize Size (in bytes) to copy from the source file.
369 */
370int GuestSessionTask::fileCopyFromGuestInner(ComObjPtr<GuestFile> &srcFile, PRTFILE phDstFile, FileCopyFlag_T fFileCopyFlags,
371 uint64_t offCopy, uint64_t cbSize)
372{
373 RT_NOREF(fFileCopyFlags);
374
375 BOOL fCanceled = FALSE;
376 uint64_t cbWrittenTotal = 0;
377 uint64_t cbToRead = cbSize;
378
379 uint32_t uTimeoutMs = 30 * 1000; /* 30s timeout. */
380
381 int rc = VINF_SUCCESS;
382
383 if (offCopy)
384 {
385 uint64_t offActual;
386 rc = srcFile->i_seekAt(offCopy, GUEST_FILE_SEEKTYPE_BEGIN, uTimeoutMs, &offActual);
387 if (RT_FAILURE(rc))
388 {
389 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
390 Utf8StrFmt(GuestSession::tr("Seeking to offset %RU64 failed: %Rrc"), offCopy, rc));
391 return rc;
392 }
393 }
394
395 BYTE byBuf[_64K];
396 while (cbToRead)
397 {
398 uint32_t cbRead;
399 const uint32_t cbChunk = RT_MIN(cbToRead, sizeof(byBuf));
400 rc = srcFile->i_readData(cbChunk, uTimeoutMs, byBuf, sizeof(byBuf), &cbRead);
401 if (RT_FAILURE(rc))
402 {
403 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
404 Utf8StrFmt(GuestSession::tr("Reading %RU32 bytes @ %RU64 from guest failed: %Rrc"), cbChunk, cbWrittenTotal, rc));
405 break;
406 }
407
408 rc = RTFileWrite(*phDstFile, byBuf, cbRead, NULL /* No partial writes */);
409 if (RT_FAILURE(rc))
410 {
411 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
412 Utf8StrFmt(GuestSession::tr("Writing %RU32 bytes to file on host failed: %Rrc"), cbRead, rc));
413 break;
414 }
415
416 Assert(cbToRead >= cbRead);
417 cbToRead -= cbRead;
418
419 /* Update total bytes written to the guest. */
420 cbWrittenTotal += cbRead;
421 Assert(cbWrittenTotal <= cbSize);
422
423 /* Did the user cancel the operation above? */
424 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
425 && fCanceled)
426 break;
427
428 rc = setProgress((ULONG)(cbWrittenTotal / ((uint64_t)cbSize / 100.0)));
429 if (RT_FAILURE(rc))
430 break;
431 }
432
433 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
434 && fCanceled)
435 return VINF_SUCCESS;
436
437 if (RT_FAILURE(rc))
438 return rc;
439
440 /*
441 * Even if we succeeded until here make sure to check whether we really transfered
442 * everything.
443 */
444 if ( cbSize > 0
445 && cbWrittenTotal == 0)
446 {
447 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
448 * to the destination -> access denied. */
449 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
450 Utf8StrFmt(GuestSession::tr("Writing guest file to host failed: Access denied")));
451 rc = VERR_ACCESS_DENIED;
452 }
453 else if (cbWrittenTotal < cbSize)
454 {
455 /* If we did not copy all let the user know. */
456 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
457 Utf8StrFmt(GuestSession::tr("Copying guest file to host to failed (%RU64/%RU64 bytes transfered)"),
458 cbWrittenTotal, cbSize));
459 rc = VERR_INTERRUPTED;
460 }
461
462 LogFlowFuncLeaveRC(rc);
463 return rc;
464}
465
466/**
467 * Copies a file from the guest to the host.
468 *
469 * @return VBox status code. VINF_NO_CHANGE if file was skipped.
470 * @param strSource Full path of source file on the guest to copy.
471 * @param strDest Full destination path and file name (host style) to copy file to.
472 * @param fFileCopyFlags File copy flags.
473 */
474int GuestSessionTask::fileCopyFromGuest(const Utf8Str &strSource, const Utf8Str &strDest, FileCopyFlag_T fFileCopyFlags)
475{
476 LogFlowThisFunc(("strSource=%s, strDest=%s, enmFileCopyFlags=0x%x\n", strSource.c_str(), strDest.c_str(), fFileCopyFlags));
477
478 GuestFileOpenInfo srcOpenInfo;
479 RT_ZERO(srcOpenInfo);
480 srcOpenInfo.mFilename = strSource;
481 srcOpenInfo.mOpenAction = FileOpenAction_OpenExisting;
482 srcOpenInfo.mAccessMode = FileAccessMode_ReadOnly;
483 srcOpenInfo.mSharingMode = FileSharingMode_All; /** @todo Use _Read when implemented. */
484
485 ComObjPtr<GuestFile> srcFile;
486
487 GuestFsObjData srcObjData;
488 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
489 int rc = mSession->i_fsQueryInfo(strSource, TRUE /* fFollowSymlinks */, srcObjData, &rcGuest);
490 if (RT_FAILURE(rc))
491 {
492 switch (rc)
493 {
494 case VERR_GSTCTL_GUEST_ERROR:
495 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestFile::i_guestErrorToString(rcGuest));
496 break;
497
498 default:
499 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
500 Utf8StrFmt(GuestSession::tr("Source file lookup for \"%s\" failed: %Rrc"),
501 strSource.c_str(), rc));
502 break;
503 }
504 }
505 else
506 {
507 switch (srcObjData.mType)
508 {
509 case FsObjType_File:
510 break;
511
512 case FsObjType_Symlink:
513 if (!(fFileCopyFlags & FileCopyFlag_FollowLinks))
514 {
515 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
516 Utf8StrFmt(GuestSession::tr("Source file \"%s\" is a symbolic link"),
517 strSource.c_str(), rc));
518 rc = VERR_IS_A_SYMLINK;
519 }
520 break;
521
522 default:
523 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
524 Utf8StrFmt(GuestSession::tr("Source element \"%s\" is not a file"), strSource.c_str()));
525 rc = VERR_NOT_A_FILE;
526 break;
527 }
528 }
529
530 if (RT_FAILURE(rc))
531 return rc;
532
533 rc = mSession->i_fileOpen(srcOpenInfo, srcFile, &rcGuest);
534 if (RT_FAILURE(rc))
535 {
536 switch (rc)
537 {
538 case VERR_GSTCTL_GUEST_ERROR:
539 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestFile::i_guestErrorToString(rcGuest));
540 break;
541
542 default:
543 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
544 Utf8StrFmt(GuestSession::tr("Source file \"%s\" could not be opened: %Rrc"),
545 strSource.c_str(), rc));
546 break;
547 }
548 }
549
550 if (RT_FAILURE(rc))
551 return rc;
552
553 char *pszDstFile = NULL;
554 RTFSOBJINFO dstObjInfo;
555 RT_ZERO(dstObjInfo);
556
557 bool fSkip = false; /* Whether to skip handling the file. */
558
559 if (RT_SUCCESS(rc))
560 {
561 rc = RTPathQueryInfo(strDest.c_str(), &dstObjInfo, RTFSOBJATTRADD_NOTHING);
562 if (RT_SUCCESS(rc))
563 {
564 if (fFileCopyFlags & FileCopyFlag_NoReplace)
565 {
566 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
567 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" already exists"), strDest.c_str()));
568 rc = VERR_ALREADY_EXISTS;
569 }
570
571 if (fFileCopyFlags & FileCopyFlag_Update)
572 {
573 RTTIMESPEC srcModificationTimeTS;
574 RTTimeSpecSetSeconds(&srcModificationTimeTS, srcObjData.mModificationTime);
575 if (RTTimeSpecCompare(&srcModificationTimeTS, &dstObjInfo.ModificationTime) <= 0)
576 {
577 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
578 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" has same or newer modification date"),
579 strDest.c_str()));
580 fSkip = true;
581 }
582 }
583 }
584 else
585 {
586 if (rc != VERR_FILE_NOT_FOUND) /* Destination file does not exist (yet)? */
587 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
588 Utf8StrFmt(GuestSession::tr("Destination file lookup for \"%s\" failed: %Rrc"),
589 strDest.c_str(), rc));
590 }
591 }
592
593 if (fSkip)
594 {
595 int rc2 = srcFile->i_closeFile(&rcGuest);
596 AssertRC(rc2);
597 return VINF_SUCCESS;
598 }
599
600 if (RT_SUCCESS(rc))
601 {
602 if (RTFS_IS_FILE(dstObjInfo.Attr.fMode))
603 {
604 if (fFileCopyFlags & FileCopyFlag_NoReplace)
605 {
606 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
607 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" already exists"),
608 strDest.c_str(), rc));
609 rc = VERR_ALREADY_EXISTS;
610 }
611 else
612 pszDstFile = RTStrDup(strDest.c_str());
613 }
614 else if (RTFS_IS_DIRECTORY(dstObjInfo.Attr.fMode))
615 {
616 /* Build the final file name with destination path (on the host). */
617 char szDstPath[RTPATH_MAX];
618 RTStrPrintf2(szDstPath, sizeof(szDstPath), "%s", strDest.c_str());
619
620 if ( !strDest.endsWith("\\")
621 && !strDest.endsWith("/"))
622 RTPathAppend(szDstPath, sizeof(szDstPath), "/"); /* IPRT can handle / on all hosts. */
623
624 RTPathAppend(szDstPath, sizeof(szDstPath), RTPathFilenameEx(strSource.c_str(), mfPathStyle));
625
626 pszDstFile = RTStrDup(szDstPath);
627 }
628 else if (RTFS_IS_SYMLINK(dstObjInfo.Attr.fMode))
629 {
630 if (!(fFileCopyFlags & FileCopyFlag_FollowLinks))
631 {
632 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
633 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" is a symbolic link"),
634 strDest.c_str(), rc));
635 rc = VERR_IS_A_SYMLINK;
636 }
637 else
638 pszDstFile = RTStrDup(strDest.c_str());
639 }
640 else
641 {
642 LogFlowThisFunc(("Object type %RU32 not implemented yet\n", dstObjInfo.Attr.fMode));
643 rc = VERR_NOT_IMPLEMENTED;
644 }
645 }
646 else if (rc == VERR_FILE_NOT_FOUND)
647 pszDstFile = RTStrDup(strDest.c_str());
648
649 if ( RT_SUCCESS(rc)
650 || rc == VERR_FILE_NOT_FOUND)
651 {
652 if (!pszDstFile)
653 {
654 setProgressErrorMsg(VBOX_E_IPRT_ERROR, Utf8StrFmt(GuestSession::tr("No memory to allocate destination file path")));
655 rc = VERR_NO_MEMORY;
656 }
657 else
658 {
659 RTFILE hDstFile;
660 rc = RTFileOpen(&hDstFile, pszDstFile,
661 RTFILE_O_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE); /** @todo Use the correct open modes! */
662 if (RT_SUCCESS(rc))
663 {
664 LogFlowThisFunc(("Copying '%s' to '%s' (%RI64 bytes) ...\n", strSource.c_str(), pszDstFile, srcObjData.mObjectSize));
665
666 rc = fileCopyFromGuestInner(srcFile, &hDstFile, fFileCopyFlags, 0 /* Offset, unused */, (uint64_t)srcObjData.mObjectSize);
667
668 int rc2 = RTFileClose(hDstFile);
669 AssertRC(rc2);
670 }
671 else
672 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
673 Utf8StrFmt(GuestSession::tr("Opening/creating destination file \"%s\" failed: %Rrc"),
674 pszDstFile, rc));
675 }
676 }
677
678 RTStrFree(pszDstFile);
679
680 int rc2 = srcFile->i_closeFile(&rcGuest);
681 AssertRC(rc2);
682
683 LogFlowFuncLeaveRC(rc);
684 return rc;
685}
686
687/**
688 * Main function for copying a file from host to the guest.
689 *
690 * @return VBox status code.
691 * @param hVfsFile The VFS file handle to read from.
692 * @param dstFile Guest file (destination) to copy to the guest. Must be in opened and ready state already.
693 * @param fFileCopyFlags File copy flags.
694 * @param offCopy Offset (in bytes) where to start copying the source file.
695 * @param cbSize Size (in bytes) to copy from the source file.
696 */
697int GuestSessionTask::fileCopyToGuestInner(RTVFSFILE hVfsFile, ComObjPtr<GuestFile> &dstFile, FileCopyFlag_T fFileCopyFlags,
698 uint64_t offCopy, uint64_t cbSize)
699{
700 RT_NOREF(fFileCopyFlags);
701
702 BOOL fCanceled = FALSE;
703 uint64_t cbWrittenTotal = 0;
704 uint64_t cbToRead = cbSize;
705
706 uint32_t uTimeoutMs = 30 * 1000; /* 30s timeout. */
707
708 int rc = VINF_SUCCESS;
709
710 if (offCopy)
711 {
712 uint64_t offActual;
713 rc = RTVfsFileSeek(hVfsFile, offCopy, RTFILE_SEEK_END, &offActual);
714 if (RT_FAILURE(rc))
715 {
716 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
717 Utf8StrFmt(GuestSession::tr("Seeking to offset %RU64 failed: %Rrc"), offCopy, rc));
718 return rc;
719 }
720 }
721
722 BYTE byBuf[_64K];
723 while (cbToRead)
724 {
725 size_t cbRead;
726 const uint32_t cbChunk = RT_MIN(cbToRead, sizeof(byBuf));
727 rc = RTVfsFileRead(hVfsFile, byBuf, cbChunk, &cbRead);
728 if (RT_FAILURE(rc))
729 {
730 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
731 Utf8StrFmt(GuestSession::tr("Reading %RU32 bytes @ %RU64 from host failed: %Rrc"), cbChunk, cbWrittenTotal, rc));
732 break;
733 }
734
735 rc = dstFile->i_writeData(uTimeoutMs, byBuf, (uint32_t)cbRead, NULL /* No partial writes */);
736 if (RT_FAILURE(rc))
737 {
738 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
739 Utf8StrFmt(GuestSession::tr("Writing %zu bytes to file on guest failed: %Rrc"), cbRead, rc));
740 break;
741 }
742
743 Assert(cbToRead >= cbRead);
744 cbToRead -= cbRead;
745
746 /* Update total bytes written to the guest. */
747 cbWrittenTotal += cbRead;
748 Assert(cbWrittenTotal <= cbSize);
749
750 /* Did the user cancel the operation above? */
751 if ( SUCCEEDED(mProgress->COMGETTER(Canceled(&fCanceled)))
752 && fCanceled)
753 break;
754
755 rc = setProgress((ULONG)(cbWrittenTotal / ((uint64_t)cbSize / 100.0)));
756 if (RT_FAILURE(rc))
757 break;
758 }
759
760 if (RT_FAILURE(rc))
761 return rc;
762
763 /*
764 * Even if we succeeded until here make sure to check whether we really transfered
765 * everything.
766 */
767 if ( cbSize > 0
768 && cbWrittenTotal == 0)
769 {
770 /* If nothing was transfered but the file size was > 0 then "vbox_cat" wasn't able to write
771 * to the destination -> access denied. */
772 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
773 Utf8StrFmt(GuestSession::tr("Writing to destination file failed: Access denied")));
774 rc = VERR_ACCESS_DENIED;
775 }
776 else if (cbWrittenTotal < cbSize)
777 {
778 /* If we did not copy all let the user know. */
779 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
780 Utf8StrFmt(GuestSession::tr("Copying to destination failed (%RU64/%RU64 bytes transfered)"),
781 cbWrittenTotal, cbSize));
782 rc = VERR_INTERRUPTED;
783 }
784
785 LogFlowFuncLeaveRC(rc);
786 return rc;
787}
788
789/**
790 * Copies a file from the guest to the host.
791 *
792 * @return VBox status code. VINF_NO_CHANGE if file was skipped.
793 * @param strSource Full path of source file on the host to copy.
794 * @param strDest Full destination path and file name (guest style) to copy file to.
795 * @param fFileCopyFlags File copy flags.
796 */
797int GuestSessionTask::fileCopyToGuest(const Utf8Str &strSource, const Utf8Str &strDest, FileCopyFlag_T fFileCopyFlags)
798{
799 LogFlowThisFunc(("strSource=%s, strDest=%s, fFileCopyFlags=0x%x\n", strSource.c_str(), strDest.c_str(), fFileCopyFlags));
800
801 Utf8Str strDestFinal = strDest;
802
803 GuestFsObjData dstObjData;
804 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
805 int rc = mSession->i_fsQueryInfo(strDest, TRUE /* fFollowSymlinks */, dstObjData, &rcGuest);
806 if (rc == VERR_GSTCTL_GUEST_ERROR && rcGuest == VERR_FILE_NOT_FOUND) /* File might not exist on the guest yet. */
807 rc = VINF_SUCCESS;
808 else if (RT_FAILURE(rc))
809 {
810 setProgressErrorMsg(VBOX_E_IPRT_ERROR, rc == VERR_GSTCTL_GUEST_ERROR ? rcGuest : rc,
811 GuestSession::tr("Destination file lookup for \"%s\" failed: %Rrc"),
812 strDest.c_str(), rc == VERR_GSTCTL_GUEST_ERROR ? rcGuest : rc);
813 return rc;
814 }
815 else
816 {
817 switch (dstObjData.mType)
818 {
819 case FsObjType_Directory:
820 {
821 /* Build the final file name with destination path (on the host). */
822 char szDstPath[RTPATH_MAX];
823 /** @todo r=bird: WTF IS THIS SUPPOSED TO BE?!? Use RTStrCopy, memcpy, or similar!
824 * Ever thought about modifying strDestFinal (it's 'Dst' not 'Dest' btw.)
825 * directly? There are any number of append and insert methods in the
826 * RTCString/Utf8Str class for doing that! */
827 RTStrPrintf2(szDstPath, sizeof(szDstPath), "%s", strDest.c_str());
828
829 /** @todo r=bird: You're totally ignoring the guest slash-style here! Callers
830 * should have this info. */
831 if ( !strDest.endsWith("\\")
832 && !strDest.endsWith("/"))
833 RTStrCat(szDstPath, sizeof(szDstPath), "/");
834
835 RTStrCat(szDstPath, sizeof(szDstPath), RTPathFilename(strSource.c_str()));
836
837 strDestFinal = szDstPath;
838 break;
839 }
840
841 case FsObjType_File:
842 if (fFileCopyFlags & FileCopyFlag_NoReplace)
843 {
844 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
845 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" already exists"),
846 strDest.c_str(), rc));
847 rc = VERR_ALREADY_EXISTS;
848 }
849 break;
850
851 case FsObjType_Symlink:
852 if (!(fFileCopyFlags & FileCopyFlag_FollowLinks))
853 {
854 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
855 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" is a symbolic link"),
856 strDest.c_str(), rc));
857 rc = VERR_IS_A_SYMLINK;
858 }
859 break;
860
861 default:
862 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
863 Utf8StrFmt(GuestSession::tr("Destination element \"%s\" not supported"), strDest.c_str()));
864 rc = VERR_NOT_SUPPORTED;
865 break;
866 }
867 }
868
869 if (RT_FAILURE(rc))
870 return rc;
871
872 GuestFileOpenInfo dstOpenInfo;
873 RT_ZERO(dstOpenInfo);
874 dstOpenInfo.mFilename = strDestFinal;
875 if (fFileCopyFlags & FileCopyFlag_NoReplace)
876 dstOpenInfo.mOpenAction = FileOpenAction_CreateNew;
877 else
878 dstOpenInfo.mOpenAction = FileOpenAction_CreateOrReplace;
879 dstOpenInfo.mAccessMode = FileAccessMode_WriteOnly;
880 dstOpenInfo.mSharingMode = FileSharingMode_All; /** @todo Use _Read when implemented. */
881
882 ComObjPtr<GuestFile> dstFile;
883 rc = mSession->i_fileOpen(dstOpenInfo, dstFile, &rcGuest);
884 if (RT_FAILURE(rc))
885 {
886 setProgressErrorMsg(VBOX_E_IPRT_ERROR, rc == VERR_GSTCTL_GUEST_ERROR ? rcGuest : rc,
887 GuestSession::tr("Destination file \"%s\" could not be opened: %Rrc"),
888 strDestFinal.c_str(), rc == VERR_GSTCTL_GUEST_ERROR ? rcGuest : rc);
889 return rc;
890 }
891
892 char szSrcReal[RTPATH_MAX];
893
894 RTFSOBJINFO srcObjInfo;
895 RT_ZERO(srcObjInfo);
896
897 bool fSkip = false; /* Whether to skip handling the file. */
898
899 if (RT_SUCCESS(rc))
900 {
901 rc = RTPathReal(strSource.c_str(), szSrcReal, sizeof(szSrcReal));
902 if (RT_FAILURE(rc))
903 {
904 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
905 Utf8StrFmt(GuestSession::tr("Source path lookup for \"%s\" failed: %Rrc"),
906 strSource.c_str(), rc));
907 }
908 else
909 {
910 rc = RTPathQueryInfo(szSrcReal, &srcObjInfo, RTFSOBJATTRADD_NOTHING);
911 if (RT_SUCCESS(rc))
912 {
913 if (fFileCopyFlags & FileCopyFlag_Update)
914 {
915 RTTIMESPEC dstModificationTimeTS;
916 RTTimeSpecSetSeconds(&dstModificationTimeTS, dstObjData.mModificationTime);
917 if (RTTimeSpecCompare(&dstModificationTimeTS, &srcObjInfo.ModificationTime) <= 0)
918 {
919 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
920 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" has same or newer modification date"),
921 strDestFinal.c_str()));
922 fSkip = true;
923 }
924 }
925 }
926 else
927 {
928 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
929 Utf8StrFmt(GuestSession::tr("Source file lookup for \"%s\" failed: %Rrc"),
930 szSrcReal, rc));
931 }
932 }
933 }
934
935 if (fSkip)
936 {
937 int rc2 = dstFile->i_closeFile(&rcGuest);
938 AssertRC(rc2);
939 return VINF_SUCCESS;
940 }
941
942 if (RT_SUCCESS(rc))
943 {
944 RTVFSFILE hSrcFile;
945 rc = RTVfsFileOpenNormal(szSrcReal, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE, &hSrcFile); /** @todo Use the correct open modes! */
946 if (RT_SUCCESS(rc))
947 {
948 LogFlowThisFunc(("Copying '%s' to '%s' (%RI64 bytes) ...\n",
949 szSrcReal, strDestFinal.c_str(), srcObjInfo.cbObject));
950
951 rc = fileCopyToGuestInner(hSrcFile, dstFile, fFileCopyFlags, 0 /* Offset, unused */, srcObjInfo.cbObject);
952
953 int rc2 = RTVfsFileRelease(hSrcFile);
954 AssertRC(rc2);
955 }
956 else
957 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
958 Utf8StrFmt(GuestSession::tr("Opening source file \"%s\" failed: %Rrc"),
959 szSrcReal, rc));
960 }
961
962 int rc2 = dstFile->i_closeFile(&rcGuest);
963 AssertRC(rc2);
964
965 LogFlowFuncLeaveRC(rc);
966 return rc;
967}
968
969/**
970 * Adds a guest file system entry to a given list.
971 *
972 * @return VBox status code.
973 * @param strFile Path to file system entry to add.
974 * @param fsObjData Guest file system information of entry to add.
975 */
976int FsList::AddEntryFromGuest(const Utf8Str &strFile, const GuestFsObjData &fsObjData)
977{
978 LogFlowFunc(("Adding '%s'\n", strFile.c_str()));
979
980 FsEntry *pEntry = NULL;
981 try
982 {
983 pEntry = new FsEntry();
984 pEntry->fMode = fsObjData.GetFileMode();
985 pEntry->strPath = strFile;
986
987 mVecEntries.push_back(pEntry);
988 }
989 catch (...)
990 {
991 if (pEntry)
992 delete pEntry;
993 return VERR_NO_MEMORY;
994 }
995
996 return VINF_SUCCESS;
997}
998
999/**
1000 * Adds a host file system entry to a given list.
1001 *
1002 * @return VBox status code.
1003 * @param strFile Path to file system entry to add.
1004 * @param pcObjInfo File system information of entry to add.
1005 */
1006int FsList::AddEntryFromHost(const Utf8Str &strFile, PCRTFSOBJINFO pcObjInfo)
1007{
1008 LogFlowFunc(("Adding '%s'\n", strFile.c_str()));
1009
1010 FsEntry *pEntry = NULL;
1011 try
1012 {
1013 pEntry = new FsEntry();
1014 pEntry->fMode = pcObjInfo->Attr.fMode & RTFS_TYPE_MASK;
1015 pEntry->strPath = strFile;
1016
1017 mVecEntries.push_back(pEntry);
1018 }
1019 catch (...)
1020 {
1021 if (pEntry)
1022 delete pEntry;
1023 return VERR_NO_MEMORY;
1024 }
1025
1026 return VINF_SUCCESS;
1027}
1028
1029FsList::FsList(const GuestSessionTask &Task)
1030 : mTask(Task)
1031{
1032}
1033
1034FsList::~FsList()
1035{
1036 Destroy();
1037}
1038
1039/**
1040 * Initializes a file list.
1041 *
1042 * @return VBox status code.
1043 * @param strSrcRootAbs Source root path (absolute) for this file list.
1044 * @param strDstRootAbs Destination root path (absolute) for this file list.
1045 * @param SourceSpec Source specification to use.
1046 */
1047int FsList::Init(const Utf8Str &strSrcRootAbs, const Utf8Str &strDstRootAbs,
1048 const GuestSessionFsSourceSpec &SourceSpec)
1049{
1050 mSrcRootAbs = strSrcRootAbs;
1051 mDstRootAbs = strDstRootAbs;
1052 mSourceSpec = SourceSpec;
1053
1054 /* If the source is a directory, make sure the path is properly terminated already. */
1055 if (mSourceSpec.enmType == FsObjType_Directory)
1056 {
1057 if ( !mSrcRootAbs.endsWith("/")
1058 && !mSrcRootAbs.endsWith("\\"))
1059 mSrcRootAbs += "/";
1060
1061 if ( !mDstRootAbs.endsWith("/")
1062 && !mDstRootAbs.endsWith("\\"))
1063 mDstRootAbs += "/";
1064 }
1065
1066 return VINF_SUCCESS;
1067}
1068
1069/**
1070 * Destroys a file list.
1071 */
1072void FsList::Destroy(void)
1073{
1074 LogFlowFuncEnter();
1075
1076 FsEntries::iterator itEntry = mVecEntries.begin();
1077 while (itEntry != mVecEntries.end())
1078 {
1079 FsEntry *pEntry = *itEntry;
1080 delete pEntry;
1081 mVecEntries.erase(itEntry);
1082 itEntry = mVecEntries.begin();
1083 }
1084
1085 Assert(mVecEntries.empty());
1086
1087 LogFlowFuncLeave();
1088}
1089
1090/**
1091 * Builds a guest file list from a given path (and optional filter).
1092 *
1093 * @return VBox status code.
1094 * @param strPath Directory on the guest to build list from.
1095 * @param strSubDir Current sub directory path; needed for recursion.
1096 * Set to an empty path.
1097 */
1098int FsList::AddDirFromGuest(const Utf8Str &strPath, const Utf8Str &strSubDir /* = "" */)
1099{
1100 Utf8Str strPathAbs = strPath;
1101 if ( !strPathAbs.endsWith("/")
1102 && !strPathAbs.endsWith("\\"))
1103 strPathAbs += "/";
1104
1105 Utf8Str strPathSub = strSubDir;
1106 if ( strPathSub.isNotEmpty()
1107 && !strPathSub.endsWith("/")
1108 && !strPathSub.endsWith("\\"))
1109 strPathSub += "/";
1110
1111 strPathAbs += strPathSub;
1112
1113 LogFlowFunc(("Entering '%s' (sub '%s')\n", strPathAbs.c_str(), strPathSub.c_str()));
1114
1115 GuestDirectoryOpenInfo dirOpenInfo;
1116 dirOpenInfo.mFilter = "";
1117 dirOpenInfo.mPath = strPathAbs;
1118 dirOpenInfo.mFlags = 0; /** @todo Handle flags? */
1119
1120 const ComObjPtr<GuestSession> &pSession = mTask.GetSession();
1121
1122 ComObjPtr <GuestDirectory> pDir;
1123 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1124 int rc = pSession->i_directoryOpen(dirOpenInfo, pDir, &rcGuest);
1125 if (RT_FAILURE(rc))
1126 {
1127 switch (rc)
1128 {
1129 case VERR_INVALID_PARAMETER:
1130 break;
1131
1132 case VERR_GSTCTL_GUEST_ERROR:
1133 break;
1134
1135 default:
1136 break;
1137 }
1138
1139 return rc;
1140 }
1141
1142 if (strPathSub.isNotEmpty())
1143 {
1144 GuestFsObjData fsObjData;
1145 fsObjData.mType = FsObjType_Directory;
1146
1147 rc = AddEntryFromGuest(strPathSub, fsObjData);
1148 }
1149
1150 if (RT_SUCCESS(rc))
1151 {
1152 ComObjPtr<GuestFsObjInfo> fsObjInfo;
1153 while (RT_SUCCESS(rc = pDir->i_readInternal(fsObjInfo, &rcGuest)))
1154 {
1155 FsObjType_T enmObjType = FsObjType_Unknown; /* Shut up MSC. */
1156 HRESULT hr2 = fsObjInfo->COMGETTER(Type)(&enmObjType);
1157 AssertComRC(hr2);
1158
1159 com::Bstr bstrName;
1160 hr2 = fsObjInfo->COMGETTER(Name)(bstrName.asOutParam());
1161 AssertComRC(hr2);
1162
1163 Utf8Str strEntry = strPathSub + Utf8Str(bstrName);
1164
1165 LogFlowFunc(("Entry '%s'\n", strEntry.c_str()));
1166
1167 switch (enmObjType)
1168 {
1169 case FsObjType_Directory:
1170 {
1171 if ( bstrName.equals(".")
1172 || bstrName.equals(".."))
1173 {
1174 break;
1175 }
1176
1177 if (!(mSourceSpec.Type.Dir.fRecursive))
1178 break;
1179
1180 rc = AddDirFromGuest(strPath, strEntry);
1181 break;
1182 }
1183
1184 case FsObjType_Symlink:
1185 {
1186 if (mSourceSpec.Type.Dir.fFollowSymlinks)
1187 {
1188 /** @todo Symlink handling from guest is not imlemented yet.
1189 * See IGuestSession::symlinkRead(). */
1190 LogRel2(("Guest Control: Warning: Symlink support on guest side not available, skipping \"%s\"",
1191 strEntry.c_str()));
1192 }
1193 break;
1194 }
1195
1196 case FsObjType_File:
1197 {
1198 rc = AddEntryFromGuest(strEntry, fsObjInfo->i_getData());
1199 break;
1200 }
1201
1202 default:
1203 break;
1204 }
1205 }
1206
1207 if (rc == VERR_NO_MORE_FILES) /* End of listing reached? */
1208 rc = VINF_SUCCESS;
1209 }
1210
1211 int rc2 = pDir->i_closeInternal(&rcGuest);
1212 if (RT_SUCCESS(rc))
1213 rc = rc2;
1214
1215 return rc;
1216}
1217
1218/**
1219 * Builds a host file list from a given path (and optional filter).
1220 *
1221 * @return VBox status code.
1222 * @param strPath Directory on the host to build list from.
1223 * @param strSubDir Current sub directory path; needed for recursion.
1224 * Set to an empty path.
1225 */
1226int FsList::AddDirFromHost(const Utf8Str &strPath, const Utf8Str &strSubDir)
1227{
1228 Utf8Str strPathAbs = strPath;
1229 if ( !strPathAbs.endsWith("/")
1230 && !strPathAbs.endsWith("\\"))
1231 strPathAbs += "/";
1232
1233 Utf8Str strPathSub = strSubDir;
1234 if ( strPathSub.isNotEmpty()
1235 && !strPathSub.endsWith("/")
1236 && !strPathSub.endsWith("\\"))
1237 strPathSub += "/";
1238
1239 strPathAbs += strPathSub;
1240
1241 LogFlowFunc(("Entering '%s' (sub '%s')\n", strPathAbs.c_str(), strPathSub.c_str()));
1242
1243 RTFSOBJINFO objInfo;
1244 int rc = RTPathQueryInfo(strPathAbs.c_str(), &objInfo, RTFSOBJATTRADD_NOTHING);
1245 if (RT_SUCCESS(rc))
1246 {
1247 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
1248 {
1249 if (strPathSub.isNotEmpty())
1250 rc = AddEntryFromHost(strPathSub, &objInfo);
1251
1252 if (RT_SUCCESS(rc))
1253 {
1254 RTDIR hDir;
1255 rc = RTDirOpen(&hDir, strPathAbs.c_str());
1256 if (RT_SUCCESS(rc))
1257 {
1258 do
1259 {
1260 /* Retrieve the next directory entry. */
1261 RTDIRENTRYEX Entry;
1262 rc = RTDirReadEx(hDir, &Entry, NULL, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1263 if (RT_FAILURE(rc))
1264 {
1265 if (rc == VERR_NO_MORE_FILES)
1266 rc = VINF_SUCCESS;
1267 break;
1268 }
1269
1270 Utf8Str strEntry = strPathSub + Utf8Str(Entry.szName);
1271
1272 LogFlowFunc(("Entry '%s'\n", strEntry.c_str()));
1273
1274 switch (Entry.Info.Attr.fMode & RTFS_TYPE_MASK)
1275 {
1276 case RTFS_TYPE_DIRECTORY:
1277 {
1278 /* Skip "." and ".." entries. */
1279 if (RTDirEntryExIsStdDotLink(&Entry))
1280 break;
1281
1282 if (!(mSourceSpec.Type.Dir.fRecursive))
1283 break;
1284
1285 rc = AddDirFromHost(strPath, strEntry);
1286 break;
1287 }
1288
1289 case RTFS_TYPE_FILE:
1290 {
1291 rc = AddEntryFromHost(strEntry, &Entry.Info);
1292 break;
1293 }
1294
1295 case RTFS_TYPE_SYMLINK:
1296 {
1297 if (mSourceSpec.Type.Dir.fFollowSymlinks)
1298 {
1299 Utf8Str strEntryAbs = strPathAbs + Utf8Str(Entry.szName);
1300
1301 char szPathReal[RTPATH_MAX];
1302 rc = RTPathReal(strEntryAbs.c_str(), szPathReal, sizeof(szPathReal));
1303 if (RT_SUCCESS(rc))
1304 {
1305 rc = RTPathQueryInfo(szPathReal, &objInfo, RTFSOBJATTRADD_NOTHING);
1306 if (RT_SUCCESS(rc))
1307 {
1308 LogFlowFunc(("Symlink '%s' -> '%s'\n", strEntryAbs.c_str(), szPathReal));
1309
1310 if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
1311 {
1312 LogFlowFunc(("Symlink to directory\n"));
1313 rc = AddDirFromHost(strPath, strEntry);
1314 }
1315 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
1316 {
1317 LogFlowFunc(("Symlink to file\n"));
1318 rc = AddEntryFromHost(strEntry, &objInfo);
1319 }
1320 else
1321 rc = VERR_NOT_SUPPORTED;
1322 }
1323 else
1324 LogFlowFunc(("Unable to query symlink info for '%s', rc=%Rrc\n", szPathReal, rc));
1325 }
1326 else
1327 {
1328 LogFlowFunc(("Unable to resolve symlink for '%s', rc=%Rrc\n", strPathAbs.c_str(), rc));
1329 if (rc == VERR_FILE_NOT_FOUND) /* Broken symlink, skip. */
1330 rc = VINF_SUCCESS;
1331 }
1332 }
1333 break;
1334 }
1335
1336 default:
1337 break;
1338 }
1339
1340 } while (RT_SUCCESS(rc));
1341
1342 RTDirClose(hDir);
1343 }
1344 }
1345 }
1346 else if (RTFS_IS_FILE(objInfo.Attr.fMode))
1347 {
1348 rc = VERR_IS_A_FILE;
1349 }
1350 else if (RTFS_IS_SYMLINK(objInfo.Attr.fMode))
1351 {
1352 rc = VERR_IS_A_SYMLINK;
1353 }
1354 else
1355 rc = VERR_NOT_SUPPORTED;
1356 }
1357 else
1358 LogFlowFunc(("Unable to query '%s', rc=%Rrc\n", strPathAbs.c_str(), rc));
1359
1360 LogFlowFuncLeaveRC(rc);
1361 return rc;
1362}
1363
1364GuestSessionTaskOpen::GuestSessionTaskOpen(GuestSession *pSession, uint32_t uFlags, uint32_t uTimeoutMS)
1365 : GuestSessionTask(pSession)
1366 , mFlags(uFlags)
1367 , mTimeoutMS(uTimeoutMS)
1368{
1369 m_strTaskName = "gctlSesOpen";
1370}
1371
1372GuestSessionTaskOpen::~GuestSessionTaskOpen(void)
1373{
1374
1375}
1376
1377int GuestSessionTaskOpen::Run(void)
1378{
1379 LogFlowThisFuncEnter();
1380
1381 AutoCaller autoCaller(mSession);
1382 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1383
1384 int vrc = mSession->i_startSession(NULL /*pvrcGuest*/);
1385 /* Nothing to do here anymore. */
1386
1387 LogFlowFuncLeaveRC(vrc);
1388 return vrc;
1389}
1390
1391GuestSessionCopyTask::GuestSessionCopyTask(GuestSession *pSession)
1392 : GuestSessionTask(pSession)
1393{
1394}
1395
1396GuestSessionCopyTask::~GuestSessionCopyTask()
1397{
1398 FsLists::iterator itList = mVecLists.begin();
1399 while (itList != mVecLists.end())
1400 {
1401 FsList *pFsList = (*itList);
1402 pFsList->Destroy();
1403 delete pFsList;
1404 mVecLists.erase(itList);
1405 itList = mVecLists.begin();
1406 }
1407
1408 Assert(mVecLists.empty());
1409}
1410
1411GuestSessionTaskCopyFrom::GuestSessionTaskCopyFrom(GuestSession *pSession, GuestSessionFsSourceSet const &vecSrc,
1412 const Utf8Str &strDest)
1413 : GuestSessionCopyTask(pSession)
1414{
1415 m_strTaskName = "gctlCpyFrm";
1416
1417 mSources = vecSrc;
1418 mDest = strDest;
1419}
1420
1421GuestSessionTaskCopyFrom::~GuestSessionTaskCopyFrom(void)
1422{
1423}
1424
1425HRESULT GuestSessionTaskCopyFrom::Init(const Utf8Str &strTaskDesc)
1426{
1427 setTaskDesc(strTaskDesc);
1428
1429 /* Create the progress object. */
1430 ComObjPtr<Progress> pProgress;
1431 HRESULT hr = pProgress.createObject();
1432 if (FAILED(hr))
1433 return hr;
1434
1435 mProgress = pProgress;
1436
1437 int rc = VINF_SUCCESS;
1438
1439 ULONG cOperations = 0;
1440 Utf8Str strErrorInfo;
1441
1442 /**
1443 * Note: We need to build up the file/directory here instead of GuestSessionTaskCopyFrom::Run
1444 * because the caller expects a ready-for-operation progress object on return.
1445 * The progress object will have a variable operation count, based on the elements to
1446 * be processed.
1447 */
1448
1449 GuestSessionFsSourceSet::iterator itSrc = mSources.begin();
1450 while (itSrc != mSources.end())
1451 {
1452 Utf8Str strSrc = itSrc->strSource;
1453 Utf8Str strDst = mDest;
1454
1455 bool fFollowSymlinks;
1456
1457 if (itSrc->enmType == FsObjType_Directory)
1458 {
1459 /* If the source does not end with a slash, copy over the entire directory
1460 * (and not just its contents). */
1461 if ( !strSrc.endsWith("/")
1462 && !strSrc.endsWith("\\"))
1463 {
1464 if ( !strDst.endsWith("/")
1465 && !strDst.endsWith("\\"))
1466 strDst += "/";
1467
1468 strDst += Utf8StrFmt("%s", RTPathFilenameEx(strSrc.c_str(), mfPathStyle));
1469 }
1470
1471 fFollowSymlinks = itSrc->Type.Dir.fFollowSymlinks;
1472 }
1473 else
1474 {
1475 fFollowSymlinks = RT_BOOL(itSrc->Type.File.fCopyFlags & FileCopyFlag_FollowLinks);
1476 }
1477
1478 LogFlowFunc(("strSrc=%s, strDst=%s, fFollowSymlinks=%RTbool\n", strSrc.c_str(), strDst.c_str(), fFollowSymlinks));
1479
1480 GuestFsObjData srcObjData;
1481 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1482 rc = mSession->i_fsQueryInfo(strSrc, fFollowSymlinks, srcObjData, &rcGuest);
1483 if (RT_FAILURE(rc))
1484 {
1485 strErrorInfo = Utf8StrFmt(GuestSession::tr("No such source file/directory: %s"), strSrc.c_str());
1486 break;
1487 }
1488
1489 if (srcObjData.mType == FsObjType_Directory)
1490 {
1491 if (itSrc->enmType != FsObjType_Directory)
1492 {
1493 strErrorInfo = Utf8StrFmt(GuestSession::tr("Source is not a file: %s"), strSrc.c_str());
1494 rc = VERR_NOT_A_FILE;
1495 break;
1496 }
1497 }
1498 else
1499 {
1500 if (itSrc->enmType != FsObjType_File)
1501 {
1502 strErrorInfo = Utf8StrFmt(GuestSession::tr("Source is not a directory: %s"), strSrc.c_str());
1503 rc = VERR_NOT_A_DIRECTORY;
1504 break;
1505 }
1506 }
1507
1508 FsList *pFsList = NULL;
1509 try
1510 {
1511 pFsList = new FsList(*this);
1512 rc = pFsList->Init(strSrc, strDst, *itSrc);
1513 if (RT_SUCCESS(rc))
1514 {
1515 if (itSrc->enmType == FsObjType_Directory)
1516 {
1517 rc = pFsList->AddDirFromGuest(strSrc);
1518 }
1519 else
1520 rc = pFsList->AddEntryFromGuest(RTPathFilename(strSrc.c_str()), srcObjData);
1521 }
1522
1523 if (RT_FAILURE(rc))
1524 {
1525 delete pFsList;
1526 strErrorInfo = Utf8StrFmt(GuestSession::tr("Error adding source '%s' to list: %Rrc"), strSrc.c_str(), rc);
1527 break;
1528 }
1529
1530 mVecLists.push_back(pFsList);
1531 }
1532 catch (...)
1533 {
1534 rc = VERR_NO_MEMORY;
1535 break;
1536 }
1537
1538 AssertPtr(pFsList);
1539 cOperations += (ULONG)pFsList->mVecEntries.size();
1540
1541 itSrc++;
1542 }
1543
1544 if (cOperations) /* Use the first element as description (if available). */
1545 {
1546 Assert(mVecLists.size());
1547 Assert(mVecLists[0]->mVecEntries.size());
1548
1549 Utf8Str strFirstOp = mDest + mVecLists[0]->mVecEntries[0]->strPath;
1550 hr = pProgress->init(static_cast<IGuestSession*>(mSession), Bstr(mDesc).raw(),
1551 TRUE /* aCancelable */, cOperations + 1 /* Number of operations */, Bstr(strFirstOp).raw());
1552 }
1553 else /* If no operations have been defined, go with an "empty" progress object when will be used for error handling. */
1554 hr = pProgress->init(static_cast<IGuestSession*>(mSession), Bstr(mDesc).raw(),
1555 TRUE /* aCancelable */, 1 /* cOperations */, Bstr(mDesc).raw());
1556
1557 if (RT_FAILURE(rc))
1558 {
1559 Assert(strErrorInfo.isNotEmpty());
1560 setProgressErrorMsg(VBOX_E_IPRT_ERROR, strErrorInfo);
1561 }
1562
1563 LogFlowFunc(("Returning %Rhrc (%Rrc)\n", hr, rc));
1564 return rc;
1565}
1566
1567int GuestSessionTaskCopyFrom::Run(void)
1568{
1569 LogFlowThisFuncEnter();
1570
1571 AutoCaller autoCaller(mSession);
1572 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1573
1574 int rc = VINF_SUCCESS;
1575
1576 FsLists::const_iterator itList = mVecLists.begin();
1577 while (itList != mVecLists.end())
1578 {
1579 FsList *pList = *itList;
1580 AssertPtr(pList);
1581
1582 const bool fCopyIntoExisting = pList->mSourceSpec.Type.Dir.fCopyFlags & DirectoryCopyFlag_CopyIntoExisting;
1583 const bool fFollowSymlinks = true; /** @todo */
1584 const uint32_t fDirMode = 0700; /** @todo Play safe by default; implement ACLs. */
1585 uint32_t fDirCreate = 0;
1586
1587 if (!fFollowSymlinks)
1588 fDirCreate |= RTDIRCREATE_FLAGS_NO_SYMLINKS;
1589
1590 LogFlowFunc(("List: srcRootAbs=%s, dstRootAbs=%s\n", pList->mSrcRootAbs.c_str(), pList->mDstRootAbs.c_str()));
1591
1592 /* Create the root directory. */
1593 if ( pList->mSourceSpec.enmType == FsObjType_Directory
1594 && pList->mSourceSpec.fDryRun == false)
1595 {
1596 rc = directoryCreateOnHost(pList->mDstRootAbs, fDirCreate, fDirMode, fCopyIntoExisting);
1597 if (RT_FAILURE(rc))
1598 break;
1599 }
1600
1601 FsEntries::const_iterator itEntry = pList->mVecEntries.begin();
1602 while (itEntry != pList->mVecEntries.end())
1603 {
1604 FsEntry *pEntry = *itEntry;
1605 AssertPtr(pEntry);
1606
1607 Utf8Str strSrcAbs = pList->mSrcRootAbs;
1608 Utf8Str strDstAbs = pList->mDstRootAbs;
1609 if (pList->mSourceSpec.enmType == FsObjType_Directory)
1610 {
1611 strSrcAbs += pEntry->strPath;
1612 strDstAbs += pEntry->strPath;
1613
1614 if (pList->mSourceSpec.enmPathStyle == PathStyle_DOS)
1615 strDstAbs.findReplace('\\', '/');
1616 }
1617
1618 mProgress->SetNextOperation(Bstr(strSrcAbs).raw(), 1);
1619
1620 switch (pEntry->fMode & RTFS_TYPE_MASK)
1621 {
1622 case RTFS_TYPE_DIRECTORY:
1623 LogFlowFunc(("Directory '%s': %s -> %s\n", pEntry->strPath.c_str(), strSrcAbs.c_str(), strDstAbs.c_str()));
1624 if (!pList->mSourceSpec.fDryRun)
1625 rc = directoryCreateOnHost(strDstAbs, fDirCreate, fDirMode, fCopyIntoExisting);
1626 break;
1627
1628 case RTFS_TYPE_FILE:
1629 LogFlowFunc(("File '%s': %s -> %s\n", pEntry->strPath.c_str(), strSrcAbs.c_str(), strDstAbs.c_str()));
1630 if (!pList->mSourceSpec.fDryRun)
1631 rc = fileCopyFromGuest(strSrcAbs, strDstAbs, FileCopyFlag_None);
1632 break;
1633
1634 default:
1635 LogFlowFunc(("Warning: Type %d for '%s' is not supported\n",
1636 pEntry->fMode & RTFS_TYPE_MASK, strSrcAbs.c_str()));
1637 break;
1638 }
1639
1640 if (RT_FAILURE(rc))
1641 break;
1642
1643 ++itEntry;
1644 }
1645
1646 if (RT_FAILURE(rc))
1647 break;
1648
1649 ++itList;
1650 }
1651
1652 if (RT_SUCCESS(rc))
1653 rc = setProgressSuccess();
1654
1655 LogFlowFuncLeaveRC(rc);
1656 return rc;
1657}
1658
1659GuestSessionTaskCopyTo::GuestSessionTaskCopyTo(GuestSession *pSession, GuestSessionFsSourceSet const &vecSrc,
1660 const Utf8Str &strDest)
1661 : GuestSessionCopyTask(pSession)
1662{
1663 m_strTaskName = "gctlCpyTo";
1664
1665 mSources = vecSrc;
1666 mDest = strDest;
1667}
1668
1669GuestSessionTaskCopyTo::~GuestSessionTaskCopyTo(void)
1670{
1671}
1672
1673HRESULT GuestSessionTaskCopyTo::Init(const Utf8Str &strTaskDesc)
1674{
1675 LogFlowFuncEnter();
1676
1677 setTaskDesc(strTaskDesc);
1678
1679 /* Create the progress object. */
1680 ComObjPtr<Progress> pProgress;
1681 HRESULT hr = pProgress.createObject();
1682 if (FAILED(hr))
1683 return hr;
1684
1685 mProgress = pProgress;
1686
1687 int rc = VINF_SUCCESS;
1688
1689 ULONG cOperations = 0;
1690 Utf8Str strErrorInfo;
1691
1692 /**
1693 * Note: We need to build up the file/directory here instead of GuestSessionTaskCopyTo::Run
1694 * because the caller expects a ready-for-operation progress object on return.
1695 * The progress object will have a variable operation count, based on the elements to
1696 * be processed.
1697 */
1698
1699 GuestSessionFsSourceSet::iterator itSrc = mSources.begin();
1700 while (itSrc != mSources.end())
1701 {
1702 Utf8Str strSrc = itSrc->strSource;
1703 Utf8Str strDst = mDest;
1704
1705 if (itSrc->enmType == FsObjType_Directory)
1706 {
1707 /* If the source does not end with a slash, copy over the entire directory
1708 * (and not just its contents). */
1709 if ( !strSrc.endsWith("/")
1710 && !strSrc.endsWith("\\"))
1711 {
1712 if ( !strDst.endsWith("/")
1713 && !strDst.endsWith("\\"))
1714 strDst += "/";
1715
1716 strDst += Utf8StrFmt("%s", RTPathFilenameEx(strSrc.c_str(), mfPathStyle));
1717 }
1718 }
1719
1720 LogFlowFunc(("strSrc=%s, strDst=%s\n", strSrc.c_str(), strDst.c_str()));
1721
1722 RTFSOBJINFO srcFsObjInfo;
1723 rc = RTPathQueryInfo(strSrc.c_str(), &srcFsObjInfo, RTFSOBJATTRADD_NOTHING);
1724 if (RT_FAILURE(rc))
1725 {
1726 strErrorInfo = Utf8StrFmt(GuestSession::tr("No such source file/directory: %s"), strSrc.c_str());
1727 break;
1728 }
1729
1730 if (RTFS_IS_DIRECTORY(srcFsObjInfo.Attr.fMode))
1731 {
1732 if (itSrc->enmType != FsObjType_Directory)
1733 {
1734 strErrorInfo = Utf8StrFmt(GuestSession::tr("Source is not a file: %s"), strSrc.c_str());
1735 rc = VERR_NOT_A_FILE;
1736 break;
1737 }
1738 }
1739 else
1740 {
1741 if (itSrc->enmType == FsObjType_Directory)
1742 {
1743 strErrorInfo = Utf8StrFmt(GuestSession::tr("Source is not a directory: %s"), strSrc.c_str());
1744 rc = VERR_NOT_A_DIRECTORY;
1745 break;
1746 }
1747 }
1748
1749 FsList *pFsList = NULL;
1750 try
1751 {
1752 pFsList = new FsList(*this);
1753 rc = pFsList->Init(strSrc, strDst, *itSrc);
1754 if (RT_SUCCESS(rc))
1755 {
1756 if (itSrc->enmType == FsObjType_Directory)
1757 {
1758 rc = pFsList->AddDirFromHost(strSrc);
1759 }
1760 else
1761 rc = pFsList->AddEntryFromHost(RTPathFilename(strSrc.c_str()), &srcFsObjInfo);
1762 }
1763
1764 if (RT_FAILURE(rc))
1765 {
1766 delete pFsList;
1767 strErrorInfo = Utf8StrFmt(GuestSession::tr("Error adding source '%s' to list: %Rrc"), strSrc.c_str(), rc);
1768 break;
1769 }
1770
1771 mVecLists.push_back(pFsList);
1772 }
1773 catch (...)
1774 {
1775 rc = VERR_NO_MEMORY;
1776 break;
1777 }
1778
1779 AssertPtr(pFsList);
1780 cOperations += (ULONG)pFsList->mVecEntries.size();
1781
1782 itSrc++;
1783 }
1784
1785 if (cOperations) /* Use the first element as description (if available). */
1786 {
1787 Assert(mVecLists.size());
1788 Assert(mVecLists[0]->mVecEntries.size());
1789
1790 hr = pProgress->init(static_cast<IGuestSession*>(mSession), Bstr(mDesc).raw(),
1791 TRUE /* aCancelable */, cOperations + 1 /* Number of operations */,
1792 Bstr(mDesc).raw());
1793 }
1794 else /* If no operations have been defined, go with an "empty" progress object when will be used for error handling. */
1795 hr = pProgress->init(static_cast<IGuestSession*>(mSession), Bstr(mDesc).raw(),
1796 TRUE /* aCancelable */, 1 /* cOperations */, Bstr(mDesc).raw());
1797
1798 if (RT_FAILURE(rc))
1799 {
1800 Assert(strErrorInfo.isNotEmpty());
1801 setProgressErrorMsg(VBOX_E_IPRT_ERROR, strErrorInfo);
1802 }
1803
1804 LogFlowFunc(("Returning %Rhrc (%Rrc)\n", hr, rc));
1805 return hr;
1806}
1807
1808int GuestSessionTaskCopyTo::Run(void)
1809{
1810 LogFlowThisFuncEnter();
1811
1812 AutoCaller autoCaller(mSession);
1813 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1814
1815 int rc = VINF_SUCCESS;
1816
1817 FsLists::const_iterator itList = mVecLists.begin();
1818 while (itList != mVecLists.end())
1819 {
1820 FsList *pList = *itList;
1821 AssertPtr(pList);
1822
1823 bool fCopyIntoExisting = false;
1824 bool fFollowSymlinks = false;
1825 uint32_t fDirMode = 0700; /** @todo Play safe by default; implement ACLs. */
1826
1827 LogFlowFunc(("List: srcRootAbs=%s, dstRootAbs=%s\n", pList->mSrcRootAbs.c_str(), pList->mDstRootAbs.c_str()));
1828
1829 /* Create the root directory. */
1830 if (pList->mSourceSpec.enmType == FsObjType_Directory)
1831 {
1832 fCopyIntoExisting = RT_BOOL(pList->mSourceSpec.Type.Dir.fCopyFlags & DirectoryCopyFlag_CopyIntoExisting);
1833 fFollowSymlinks = pList->mSourceSpec.Type.Dir.fFollowSymlinks;
1834
1835 if (pList->mSourceSpec.fDryRun == false)
1836 {
1837 rc = directoryCreateOnGuest(pList->mDstRootAbs, DirectoryCreateFlag_None, fDirMode,
1838 fFollowSymlinks, fCopyIntoExisting);
1839 if (RT_FAILURE(rc))
1840 break;
1841 }
1842 }
1843 else if (pList->mSourceSpec.enmType == FsObjType_File)
1844 {
1845 fCopyIntoExisting = !(pList->mSourceSpec.Type.File.fCopyFlags & FileCopyFlag_NoReplace);
1846 fFollowSymlinks = RT_BOOL(pList->mSourceSpec.Type.File.fCopyFlags & FileCopyFlag_FollowLinks);
1847 }
1848 else
1849 AssertFailed();
1850
1851 FsEntries::const_iterator itEntry = pList->mVecEntries.begin();
1852 while (itEntry != pList->mVecEntries.end())
1853 {
1854 FsEntry *pEntry = *itEntry;
1855 AssertPtr(pEntry);
1856
1857 Utf8Str strSrcAbs = pList->mSrcRootAbs;
1858 Utf8Str strDstAbs = pList->mDstRootAbs;
1859 if (pList->mSourceSpec.enmType == FsObjType_Directory)
1860 {
1861 strSrcAbs += pEntry->strPath;
1862 strDstAbs += pEntry->strPath;
1863 }
1864
1865 mProgress->SetNextOperation(Bstr(strSrcAbs).raw(), 1);
1866
1867 switch (pEntry->fMode & RTFS_TYPE_MASK)
1868 {
1869 case RTFS_TYPE_DIRECTORY:
1870 LogFlowFunc(("Directory '%s': %s -> %s\n", pEntry->strPath.c_str(), strSrcAbs.c_str(), strDstAbs.c_str()));
1871 if (!pList->mSourceSpec.fDryRun)
1872 {
1873 rc = directoryCreateOnGuest(strDstAbs, DirectoryCreateFlag_None, fDirMode,
1874 fFollowSymlinks, fCopyIntoExisting);
1875 if (RT_FAILURE(rc))
1876 break;
1877 }
1878 break;
1879
1880 case RTFS_TYPE_FILE:
1881 LogFlowFunc(("File '%s': %s -> %s\n", pEntry->strPath.c_str(), strSrcAbs.c_str(), strDstAbs.c_str()));
1882 if (!pList->mSourceSpec.fDryRun)
1883 rc = fileCopyToGuest(strSrcAbs, strDstAbs, pList->mSourceSpec.Type.File.fCopyFlags);
1884 break;
1885
1886 default:
1887 LogFlowFunc(("Warning: Type %d for '%s' is not supported\n",
1888 pEntry->fMode & RTFS_TYPE_MASK, strSrcAbs.c_str()));
1889 break;
1890 }
1891
1892 if (RT_FAILURE(rc))
1893 break;
1894
1895 ++itEntry;
1896 }
1897
1898 if (RT_FAILURE(rc))
1899 break;
1900
1901 ++itList;
1902 }
1903
1904 if (RT_SUCCESS(rc))
1905 rc = setProgressSuccess();
1906
1907 LogFlowFuncLeaveRC(rc);
1908 return rc;
1909}
1910
1911GuestSessionTaskUpdateAdditions::GuestSessionTaskUpdateAdditions(GuestSession *pSession,
1912 const Utf8Str &strSource,
1913 const ProcessArguments &aArguments,
1914 uint32_t fFlags)
1915 : GuestSessionTask(pSession)
1916{
1917 m_strTaskName = "gctlUpGA";
1918
1919 mSource = strSource;
1920 mArguments = aArguments;
1921 mFlags = fFlags;
1922}
1923
1924GuestSessionTaskUpdateAdditions::~GuestSessionTaskUpdateAdditions(void)
1925{
1926
1927}
1928
1929int GuestSessionTaskUpdateAdditions::addProcessArguments(ProcessArguments &aArgumentsDest, const ProcessArguments &aArgumentsSource)
1930{
1931 int rc = VINF_SUCCESS;
1932
1933 try
1934 {
1935 /* Filter out arguments which already are in the destination to
1936 * not end up having them specified twice. Not the fastest method on the
1937 * planet but does the job. */
1938 ProcessArguments::const_iterator itSource = aArgumentsSource.begin();
1939 while (itSource != aArgumentsSource.end())
1940 {
1941 bool fFound = false;
1942 ProcessArguments::iterator itDest = aArgumentsDest.begin();
1943 while (itDest != aArgumentsDest.end())
1944 {
1945 if ((*itDest).equalsIgnoreCase((*itSource)))
1946 {
1947 fFound = true;
1948 break;
1949 }
1950 ++itDest;
1951 }
1952
1953 if (!fFound)
1954 aArgumentsDest.push_back((*itSource));
1955
1956 ++itSource;
1957 }
1958 }
1959 catch(std::bad_alloc &)
1960 {
1961 return VERR_NO_MEMORY;
1962 }
1963
1964 return rc;
1965}
1966
1967int GuestSessionTaskUpdateAdditions::copyFileToGuest(GuestSession *pSession, RTVFS hVfsIso,
1968 Utf8Str const &strFileSource, const Utf8Str &strFileDest,
1969 bool fOptional)
1970{
1971 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1972 AssertReturn(hVfsIso != NIL_RTVFS, VERR_INVALID_POINTER);
1973
1974 RTVFSFILE hVfsFile = NIL_RTVFSFILE;
1975 int rc = RTVfsFileOpen(hVfsIso, strFileSource.c_str(), RTFILE_O_OPEN | RTFILE_O_READ, &hVfsFile);
1976 if (RT_SUCCESS(rc))
1977 {
1978 uint64_t cbSrcSize = 0;
1979 rc = RTVfsFileGetSize(hVfsFile, &cbSrcSize);
1980 if (RT_SUCCESS(rc))
1981 {
1982 LogRel(("Copying Guest Additions installer file \"%s\" to \"%s\" on guest ...\n",
1983 strFileSource.c_str(), strFileDest.c_str()));
1984
1985 GuestFileOpenInfo dstOpenInfo;
1986 RT_ZERO(dstOpenInfo);
1987 dstOpenInfo.mFilename = strFileDest;
1988 dstOpenInfo.mOpenAction = FileOpenAction_CreateOrReplace;
1989 dstOpenInfo.mAccessMode = FileAccessMode_WriteOnly;
1990 dstOpenInfo.mSharingMode = FileSharingMode_All; /** @todo Use _Read when implemented. */
1991
1992 ComObjPtr<GuestFile> dstFile;
1993 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
1994 rc = mSession->i_fileOpen(dstOpenInfo, dstFile, &rcGuest);
1995 if (RT_FAILURE(rc))
1996 {
1997 switch (rc)
1998 {
1999 case VERR_GSTCTL_GUEST_ERROR:
2000 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestFile::i_guestErrorToString(rcGuest));
2001 break;
2002
2003 default:
2004 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2005 Utf8StrFmt(GuestSession::tr("Destination file \"%s\" could not be opened: %Rrc"),
2006 strFileDest.c_str(), rc));
2007 break;
2008 }
2009 }
2010 else
2011 {
2012 rc = fileCopyToGuestInner(hVfsFile, dstFile, FileCopyFlag_None, 0 /*cbOffset*/, cbSrcSize);
2013
2014 int rc2 = dstFile->i_closeFile(&rcGuest);
2015 AssertRC(rc2);
2016 }
2017 }
2018
2019 RTVfsFileRelease(hVfsFile);
2020 if (RT_FAILURE(rc))
2021 return rc;
2022 }
2023 else
2024 {
2025 if (fOptional)
2026 return VINF_SUCCESS;
2027
2028 return rc;
2029 }
2030
2031 return rc;
2032}
2033
2034int GuestSessionTaskUpdateAdditions::runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo)
2035{
2036 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
2037
2038 LogRel(("Running %s ...\n", procInfo.mName.c_str()));
2039
2040 GuestProcessTool procTool;
2041 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
2042 int vrc = procTool.init(pSession, procInfo, false /* Async */, &rcGuest);
2043 if (RT_SUCCESS(vrc))
2044 {
2045 if (RT_SUCCESS(rcGuest))
2046 vrc = procTool.wait(GUESTPROCESSTOOL_WAIT_FLAG_NONE, &rcGuest);
2047 if (RT_SUCCESS(vrc))
2048 vrc = procTool.getTerminationStatus();
2049 }
2050
2051 if (RT_FAILURE(vrc))
2052 {
2053 switch (vrc)
2054 {
2055 case VERR_GSTCTL_PROCESS_EXIT_CODE:
2056 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2057 Utf8StrFmt(GuestSession::tr("Running update file \"%s\" on guest failed: %Rrc"),
2058 procInfo.mExecutable.c_str(), procTool.getRc()));
2059 break;
2060
2061 case VERR_GSTCTL_GUEST_ERROR:
2062 setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestProcess::i_guestErrorToString(rcGuest));
2063 break;
2064
2065 case VERR_INVALID_STATE: /** @todo Special guest control rc needed! */
2066 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2067 Utf8StrFmt(GuestSession::tr("Update file \"%s\" reported invalid running state"),
2068 procInfo.mExecutable.c_str()));
2069 break;
2070
2071 default:
2072 setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2073 Utf8StrFmt(GuestSession::tr("Error while running update file \"%s\" on guest: %Rrc"),
2074 procInfo.mExecutable.c_str(), vrc));
2075 break;
2076 }
2077 }
2078
2079 return vrc;
2080}
2081
2082int GuestSessionTaskUpdateAdditions::Run(void)
2083{
2084 LogFlowThisFuncEnter();
2085
2086 ComObjPtr<GuestSession> pSession = mSession;
2087 Assert(!pSession.isNull());
2088
2089 AutoCaller autoCaller(pSession);
2090 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2091
2092 int rc = setProgress(10);
2093 if (RT_FAILURE(rc))
2094 return rc;
2095
2096 HRESULT hr = S_OK;
2097
2098 LogRel(("Automatic update of Guest Additions started, using \"%s\"\n", mSource.c_str()));
2099
2100 ComObjPtr<Guest> pGuest(mSession->i_getParent());
2101#if 0
2102 /*
2103 * Wait for the guest being ready within 30 seconds.
2104 */
2105 AdditionsRunLevelType_T addsRunLevel;
2106 uint64_t tsStart = RTTimeSystemMilliTS();
2107 while ( SUCCEEDED(hr = pGuest->COMGETTER(AdditionsRunLevel)(&addsRunLevel))
2108 && ( addsRunLevel != AdditionsRunLevelType_Userland
2109 && addsRunLevel != AdditionsRunLevelType_Desktop))
2110 {
2111 if ((RTTimeSystemMilliTS() - tsStart) > 30 * 1000)
2112 {
2113 rc = VERR_TIMEOUT;
2114 break;
2115 }
2116
2117 RTThreadSleep(100); /* Wait a bit. */
2118 }
2119
2120 if (FAILED(hr)) rc = VERR_TIMEOUT;
2121 if (rc == VERR_TIMEOUT)
2122 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2123 Utf8StrFmt(GuestSession::tr("Guest Additions were not ready within time, giving up")));
2124#else
2125 /*
2126 * For use with the GUI we don't want to wait, just return so that the manual .ISO mounting
2127 * can continue.
2128 */
2129 AdditionsRunLevelType_T addsRunLevel;
2130 if ( FAILED(hr = pGuest->COMGETTER(AdditionsRunLevel)(&addsRunLevel))
2131 || ( addsRunLevel != AdditionsRunLevelType_Userland
2132 && addsRunLevel != AdditionsRunLevelType_Desktop))
2133 {
2134 if (addsRunLevel == AdditionsRunLevelType_System)
2135 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2136 Utf8StrFmt(GuestSession::tr("Guest Additions are installed but not fully loaded yet, aborting automatic update")));
2137 else
2138 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2139 Utf8StrFmt(GuestSession::tr("Guest Additions not installed or ready, aborting automatic update")));
2140 rc = VERR_NOT_SUPPORTED;
2141 }
2142#endif
2143
2144 if (RT_SUCCESS(rc))
2145 {
2146 /*
2147 * Determine if we are able to update automatically. This only works
2148 * if there are recent Guest Additions installed already.
2149 */
2150 Utf8Str strAddsVer;
2151 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/Version", strAddsVer);
2152 if ( RT_SUCCESS(rc)
2153 && RTStrVersionCompare(strAddsVer.c_str(), "4.1") < 0)
2154 {
2155 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2156 Utf8StrFmt(GuestSession::tr("Guest has too old Guest Additions (%s) installed for automatic updating, please update manually"),
2157 strAddsVer.c_str()));
2158 rc = VERR_NOT_SUPPORTED;
2159 }
2160 }
2161
2162 Utf8Str strOSVer;
2163 eOSType osType = eOSType_Unknown;
2164 if (RT_SUCCESS(rc))
2165 {
2166 /*
2167 * Determine guest OS type and the required installer image.
2168 */
2169 Utf8Str strOSType;
2170 rc = getGuestProperty(pGuest, "/VirtualBox/GuestInfo/OS/Product", strOSType);
2171 if (RT_SUCCESS(rc))
2172 {
2173 if ( strOSType.contains("Microsoft", Utf8Str::CaseInsensitive)
2174 || strOSType.contains("Windows", Utf8Str::CaseInsensitive))
2175 {
2176 osType = eOSType_Windows;
2177
2178 /*
2179 * Determine guest OS version.
2180 */
2181 rc = getGuestProperty(pGuest, "/VirtualBox/GuestInfo/OS/Release", strOSVer);
2182 if (RT_FAILURE(rc))
2183 {
2184 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2185 Utf8StrFmt(GuestSession::tr("Unable to detected guest OS version, please update manually")));
2186 rc = VERR_NOT_SUPPORTED;
2187 }
2188
2189 /* Because Windows 2000 + XP and is bitching with WHQL popups even if we have signed drivers we
2190 * can't do automated updates here. */
2191 /* Windows XP 64-bit (5.2) is a Windows 2003 Server actually, so skip this here. */
2192 if ( RT_SUCCESS(rc)
2193 && RTStrVersionCompare(strOSVer.c_str(), "5.0") >= 0)
2194 {
2195 if ( strOSVer.startsWith("5.0") /* Exclude the build number. */
2196 || strOSVer.startsWith("5.1") /* Exclude the build number. */)
2197 {
2198 /* If we don't have AdditionsUpdateFlag_WaitForUpdateStartOnly set we can't continue
2199 * because the Windows Guest Additions installer will fail because of WHQL popups. If the
2200 * flag is set this update routine ends successfully as soon as the installer was started
2201 * (and the user has to deal with it in the guest). */
2202 if (!(mFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly))
2203 {
2204 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2205 Utf8StrFmt(GuestSession::tr("Windows 2000 and XP are not supported for automatic updating due to WHQL interaction, please update manually")));
2206 rc = VERR_NOT_SUPPORTED;
2207 }
2208 }
2209 }
2210 else
2211 {
2212 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2213 Utf8StrFmt(GuestSession::tr("%s (%s) not supported for automatic updating, please update manually"),
2214 strOSType.c_str(), strOSVer.c_str()));
2215 rc = VERR_NOT_SUPPORTED;
2216 }
2217 }
2218 else if (strOSType.contains("Solaris", Utf8Str::CaseInsensitive))
2219 {
2220 osType = eOSType_Solaris;
2221 }
2222 else /* Everything else hopefully means Linux :-). */
2223 osType = eOSType_Linux;
2224
2225#if 1 /* Only Windows is supported (and tested) at the moment. */
2226 if ( RT_SUCCESS(rc)
2227 && osType != eOSType_Windows)
2228 {
2229 hr = setProgressErrorMsg(VBOX_E_NOT_SUPPORTED,
2230 Utf8StrFmt(GuestSession::tr("Detected guest OS (%s) does not support automatic Guest Additions updating, please update manually"),
2231 strOSType.c_str()));
2232 rc = VERR_NOT_SUPPORTED;
2233 }
2234#endif
2235 }
2236 }
2237
2238 if (RT_SUCCESS(rc))
2239 {
2240 /*
2241 * Try to open the .ISO file to extract all needed files.
2242 */
2243 RTVFSFILE hVfsFileIso;
2244 rc = RTVfsFileOpenNormal(mSource.c_str(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, &hVfsFileIso);
2245 if (RT_SUCCESS(rc))
2246 {
2247 RTVFS hVfsIso;
2248 rc = RTFsIso9660VolOpen(hVfsFileIso, 0 /*fFlags*/, &hVfsIso, NULL);
2249 if (RT_FAILURE(rc))
2250 {
2251 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2252 Utf8StrFmt(GuestSession::tr("Unable to open Guest Additions .ISO file \"%s\": %Rrc"),
2253 mSource.c_str(), rc));
2254 }
2255 else
2256 {
2257 /* Set default installation directories. */
2258 Utf8Str strUpdateDir = "/tmp/";
2259 if (osType == eOSType_Windows)
2260 strUpdateDir = "C:\\Temp\\";
2261
2262 rc = setProgress(5);
2263
2264 /* Try looking up the Guest Additions installation directory. */
2265 if (RT_SUCCESS(rc))
2266 {
2267 /* Try getting the installed Guest Additions version to know whether we
2268 * can install our temporary Guest Addition data into the original installation
2269 * directory.
2270 *
2271 * Because versions prior to 4.2 had bugs wrt spaces in paths we have to choose
2272 * a different location then.
2273 */
2274 bool fUseInstallDir = false;
2275
2276 Utf8Str strAddsVer;
2277 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/Version", strAddsVer);
2278 if ( RT_SUCCESS(rc)
2279 && RTStrVersionCompare(strAddsVer.c_str(), "4.2r80329") > 0)
2280 {
2281 fUseInstallDir = true;
2282 }
2283
2284 if (fUseInstallDir)
2285 {
2286 if (RT_SUCCESS(rc))
2287 rc = getGuestProperty(pGuest, "/VirtualBox/GuestAdd/InstallDir", strUpdateDir);
2288 if (RT_SUCCESS(rc))
2289 {
2290 if (osType == eOSType_Windows)
2291 {
2292 strUpdateDir.findReplace('/', '\\');
2293 strUpdateDir.append("\\Update\\");
2294 }
2295 else
2296 strUpdateDir.append("/update/");
2297 }
2298 }
2299 }
2300
2301 if (RT_SUCCESS(rc))
2302 LogRel(("Guest Additions update directory is: %s\n",
2303 strUpdateDir.c_str()));
2304
2305 /* Create the installation directory. */
2306 int rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
2307 rc = pSession->i_directoryCreate(strUpdateDir, 755 /* Mode */, DirectoryCreateFlag_Parents, &rcGuest);
2308 if (RT_FAILURE(rc))
2309 {
2310 switch (rc)
2311 {
2312 case VERR_GSTCTL_GUEST_ERROR:
2313 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR, GuestProcess::i_guestErrorToString(rcGuest));
2314 break;
2315
2316 default:
2317 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2318 Utf8StrFmt(GuestSession::tr("Error creating installation directory \"%s\" on the guest: %Rrc"),
2319 strUpdateDir.c_str(), rc));
2320 break;
2321 }
2322 }
2323 if (RT_SUCCESS(rc))
2324 rc = setProgress(10);
2325
2326 if (RT_SUCCESS(rc))
2327 {
2328 /* Prepare the file(s) we want to copy over to the guest and
2329 * (maybe) want to run. */
2330 switch (osType)
2331 {
2332 case eOSType_Windows:
2333 {
2334 /* Do we need to install our certificates? We do this for W2K and up. */
2335 bool fInstallCert = false;
2336
2337 /* Only Windows 2000 and up need certificates to be installed. */
2338 if (RTStrVersionCompare(strOSVer.c_str(), "5.0") >= 0)
2339 {
2340 fInstallCert = true;
2341 LogRel(("Certificates for auto updating WHQL drivers will be installed\n"));
2342 }
2343 else
2344 LogRel(("Skipping installation of certificates for WHQL drivers\n"));
2345
2346 if (fInstallCert)
2347 {
2348 static struct { const char *pszDst, *pszIso; } const s_aCertFiles[] =
2349 {
2350 { "vbox.cer", "CERT/VBOX.CER" },
2351 { "vbox-sha1.cer", "CERT/VBOX_SHA1.CER" },
2352 { "vbox-sha256.cer", "CERT/VBOX_SHA256.CER" },
2353 { "vbox-sha256-r3.cer", "CERT/VBOX_SHA256_R3.CER" },
2354 { "oracle-vbox.cer", "CERT/ORACLE_VBOX.CER" },
2355 };
2356 uint32_t fCopyCertUtil = ISOFILE_FLAG_COPY_FROM_ISO;
2357 for (uint32_t i = 0; i < RT_ELEMENTS(s_aCertFiles); i++)
2358 {
2359 /* Skip if not present on the ISO. */
2360 RTFSOBJINFO ObjInfo;
2361 rc = RTVfsQueryPathInfo(hVfsIso, s_aCertFiles[i].pszIso, &ObjInfo, RTFSOBJATTRADD_NOTHING,
2362 RTPATH_F_ON_LINK);
2363 if (RT_FAILURE(rc))
2364 continue;
2365
2366 /* Copy the certificate certificate. */
2367 Utf8Str const strDstCert(strUpdateDir + s_aCertFiles[i].pszDst);
2368 mFiles.push_back(ISOFile(s_aCertFiles[i].pszIso,
2369 strDstCert,
2370 ISOFILE_FLAG_COPY_FROM_ISO | ISOFILE_FLAG_OPTIONAL));
2371
2372 /* Out certificate installation utility. */
2373 /* First pass: Copy over the file (first time only) + execute it to remove any
2374 * existing VBox certificates. */
2375 GuestProcessStartupInfo siCertUtilRem;
2376 siCertUtilRem.mName = "VirtualBox Certificate Utility, removing old VirtualBox certificates";
2377 siCertUtilRem.mArguments.push_back(Utf8Str("remove-trusted-publisher"));
2378 siCertUtilRem.mArguments.push_back(Utf8Str("--root")); /* Add root certificate as well. */
2379 siCertUtilRem.mArguments.push_back(strDstCert);
2380 siCertUtilRem.mArguments.push_back(strDstCert);
2381 mFiles.push_back(ISOFile("CERT/VBOXCERTUTIL.EXE",
2382 strUpdateDir + "VBoxCertUtil.exe",
2383 fCopyCertUtil | ISOFILE_FLAG_EXECUTE | ISOFILE_FLAG_OPTIONAL,
2384 siCertUtilRem));
2385 fCopyCertUtil = 0;
2386 /* Second pass: Only execute (but don't copy) again, this time installng the
2387 * recent certificates just copied over. */
2388 GuestProcessStartupInfo siCertUtilAdd;
2389 siCertUtilAdd.mName = "VirtualBox Certificate Utility, installing VirtualBox certificates";
2390 siCertUtilAdd.mArguments.push_back(Utf8Str("add-trusted-publisher"));
2391 siCertUtilAdd.mArguments.push_back(Utf8Str("--root")); /* Add root certificate as well. */
2392 siCertUtilAdd.mArguments.push_back(strDstCert);
2393 siCertUtilAdd.mArguments.push_back(strDstCert);
2394 mFiles.push_back(ISOFile("CERT/VBOXCERTUTIL.EXE",
2395 strUpdateDir + "VBoxCertUtil.exe",
2396 ISOFILE_FLAG_EXECUTE | ISOFILE_FLAG_OPTIONAL,
2397 siCertUtilAdd));
2398 }
2399 }
2400 /* The installers in different flavors, as we don't know (and can't assume)
2401 * the guest's bitness. */
2402 mFiles.push_back(ISOFile("VBOXWINDOWSADDITIONS_X86.EXE",
2403 strUpdateDir + "VBoxWindowsAdditions-x86.exe",
2404 ISOFILE_FLAG_COPY_FROM_ISO));
2405 mFiles.push_back(ISOFile("VBOXWINDOWSADDITIONS_AMD64.EXE",
2406 strUpdateDir + "VBoxWindowsAdditions-amd64.exe",
2407 ISOFILE_FLAG_COPY_FROM_ISO));
2408 /* The stub loader which decides which flavor to run. */
2409 GuestProcessStartupInfo siInstaller;
2410 siInstaller.mName = "VirtualBox Windows Guest Additions Installer";
2411 /* Set a running timeout of 5 minutes -- the Windows Guest Additions
2412 * setup can take quite a while, so be on the safe side. */
2413 siInstaller.mTimeoutMS = 5 * 60 * 1000;
2414 siInstaller.mArguments.push_back(Utf8Str("/S")); /* We want to install in silent mode. */
2415 siInstaller.mArguments.push_back(Utf8Str("/l")); /* ... and logging enabled. */
2416 /* Don't quit VBoxService during upgrade because it still is used for this
2417 * piece of code we're in right now (that is, here!) ... */
2418 siInstaller.mArguments.push_back(Utf8Str("/no_vboxservice_exit"));
2419 /* Tell the installer to report its current installation status
2420 * using a running VBoxTray instance via balloon messages in the
2421 * Windows taskbar. */
2422 siInstaller.mArguments.push_back(Utf8Str("/post_installstatus"));
2423 /* Add optional installer command line arguments from the API to the
2424 * installer's startup info. */
2425 rc = addProcessArguments(siInstaller.mArguments, mArguments);
2426 AssertRC(rc);
2427 /* If the caller does not want to wait for out guest update process to end,
2428 * complete the progress object now so that the caller can do other work. */
2429 if (mFlags & AdditionsUpdateFlag_WaitForUpdateStartOnly)
2430 siInstaller.mFlags |= ProcessCreateFlag_WaitForProcessStartOnly;
2431 mFiles.push_back(ISOFile("VBOXWINDOWSADDITIONS.EXE",
2432 strUpdateDir + "VBoxWindowsAdditions.exe",
2433 ISOFILE_FLAG_COPY_FROM_ISO | ISOFILE_FLAG_EXECUTE, siInstaller));
2434 break;
2435 }
2436 case eOSType_Linux:
2437 /** @todo Add Linux support. */
2438 break;
2439 case eOSType_Solaris:
2440 /** @todo Add Solaris support. */
2441 break;
2442 default:
2443 AssertReleaseMsgFailed(("Unsupported guest type: %d\n", osType));
2444 break;
2445 }
2446 }
2447
2448 if (RT_SUCCESS(rc))
2449 {
2450 /* We want to spend 40% total for all copying operations. So roughly
2451 * calculate the specific percentage step of each copied file. */
2452 uint8_t uOffset = 20; /* Start at 20%. */
2453 uint8_t uStep = 40 / (uint8_t)mFiles.size(); Assert(mFiles.size() <= 10);
2454
2455 LogRel(("Copying over Guest Additions update files to the guest ...\n"));
2456
2457 std::vector<ISOFile>::const_iterator itFiles = mFiles.begin();
2458 while (itFiles != mFiles.end())
2459 {
2460 if (itFiles->fFlags & ISOFILE_FLAG_COPY_FROM_ISO)
2461 {
2462 bool fOptional = false;
2463 if (itFiles->fFlags & ISOFILE_FLAG_OPTIONAL)
2464 fOptional = true;
2465 rc = copyFileToGuest(pSession, hVfsIso, itFiles->strSource, itFiles->strDest, fOptional);
2466 if (RT_FAILURE(rc))
2467 {
2468 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2469 Utf8StrFmt(GuestSession::tr("Error while copying file \"%s\" to \"%s\" on the guest: %Rrc"),
2470 itFiles->strSource.c_str(), itFiles->strDest.c_str(), rc));
2471 break;
2472 }
2473 }
2474
2475 rc = setProgress(uOffset);
2476 if (RT_FAILURE(rc))
2477 break;
2478 uOffset += uStep;
2479
2480 ++itFiles;
2481 }
2482 }
2483
2484 /* Done copying, close .ISO file. */
2485 RTVfsRelease(hVfsIso);
2486
2487 if (RT_SUCCESS(rc))
2488 {
2489 /* We want to spend 35% total for all copying operations. So roughly
2490 * calculate the specific percentage step of each copied file. */
2491 uint8_t uOffset = 60; /* Start at 60%. */
2492 uint8_t uStep = 35 / (uint8_t)mFiles.size(); Assert(mFiles.size() <= 10);
2493
2494 LogRel(("Executing Guest Additions update files ...\n"));
2495
2496 std::vector<ISOFile>::iterator itFiles = mFiles.begin();
2497 while (itFiles != mFiles.end())
2498 {
2499 if (itFiles->fFlags & ISOFILE_FLAG_EXECUTE)
2500 {
2501 rc = runFileOnGuest(pSession, itFiles->mProcInfo);
2502 if (RT_FAILURE(rc))
2503 break;
2504 }
2505
2506 rc = setProgress(uOffset);
2507 if (RT_FAILURE(rc))
2508 break;
2509 uOffset += uStep;
2510
2511 ++itFiles;
2512 }
2513 }
2514
2515 if (RT_SUCCESS(rc))
2516 {
2517 LogRel(("Automatic update of Guest Additions succeeded\n"));
2518 rc = setProgressSuccess();
2519 }
2520 }
2521
2522 RTVfsFileRelease(hVfsFileIso);
2523 }
2524 }
2525
2526 if (RT_FAILURE(rc))
2527 {
2528 if (rc == VERR_CANCELLED)
2529 {
2530 LogRel(("Automatic update of Guest Additions was canceled\n"));
2531
2532 hr = setProgressErrorMsg(VBOX_E_IPRT_ERROR,
2533 Utf8StrFmt(GuestSession::tr("Installation was canceled")));
2534 }
2535 else
2536 {
2537 Utf8Str strError = Utf8StrFmt("No further error information available (%Rrc)", rc);
2538 if (!mProgress.isNull()) /* Progress object is optional. */
2539 {
2540 com::ProgressErrorInfo errorInfo(mProgress);
2541 if ( errorInfo.isFullAvailable()
2542 || errorInfo.isBasicAvailable())
2543 {
2544 strError = errorInfo.getText();
2545 }
2546 }
2547
2548 LogRel(("Automatic update of Guest Additions failed: %s (%Rhrc)\n",
2549 strError.c_str(), hr));
2550 }
2551
2552 LogRel(("Please install Guest Additions manually\n"));
2553 }
2554
2555 /** @todo Clean up copied / left over installation files. */
2556
2557 LogFlowFuncLeaveRC(rc);
2558 return rc;
2559}
2560
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette