VirtualBox

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

Last change on this file since 76454 was 75926, checked in by vboxsync, 6 years ago

Main,Frontends: s/FileName/Filename/g (API); Took a shot a completing the code for populating IGuestFsObjInfo. bugref:9320

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