VirtualBox

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

Last change on this file since 93920 was 93309, checked in by vboxsync, 3 years ago

Main: VC++ 19.2 update 11 build adjustments (lossy floating point conversions). bugref:8489

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