VirtualBox

source: vbox/trunk/src/VBox/Main/ApplianceImpl.cpp@ 29436

Last change on this file since 29436 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.5 KB
Line 
1/* $Id: ApplianceImpl.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 *
4 * IAppliance and IVirtualSystem COM class implementations.
5 */
6
7/*
8 * Copyright (C) 2008-2010 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <iprt/path.h>
20
21#include <VBox/com/array.h>
22
23#include "ApplianceImpl.h"
24#include "VFSExplorerImpl.h"
25#include "VirtualBoxImpl.h"
26#include "GuestOSTypeImpl.h"
27#include "ProgressImpl.h"
28#include "MachineImpl.h"
29
30#include "AutoCaller.h"
31#include "Logging.h"
32
33#include "ApplianceImplPrivate.h"
34
35using namespace std;
36
37////////////////////////////////////////////////////////////////////////////////
38//
39// Internal helpers
40//
41////////////////////////////////////////////////////////////////////////////////
42
43static const struct
44{
45 ovf::CIMOSType_T cim;
46 const char *pcszVbox;
47}
48g_osTypes[] =
49{
50 { ovf::CIMOSType_CIMOS_Unknown, SchemaDefs_OSTypeId_Other },
51 { ovf::CIMOSType_CIMOS_OS2, SchemaDefs_OSTypeId_OS2 },
52 { ovf::CIMOSType_CIMOS_MSDOS, SchemaDefs_OSTypeId_DOS },
53 { ovf::CIMOSType_CIMOS_WIN3x, SchemaDefs_OSTypeId_Windows31 },
54 { ovf::CIMOSType_CIMOS_WIN95, SchemaDefs_OSTypeId_Windows95 },
55 { ovf::CIMOSType_CIMOS_WIN98, SchemaDefs_OSTypeId_Windows98 },
56 { ovf::CIMOSType_CIMOS_WINNT, SchemaDefs_OSTypeId_WindowsNT4 },
57 { ovf::CIMOSType_CIMOS_NetWare, SchemaDefs_OSTypeId_Netware },
58 { ovf::CIMOSType_CIMOS_NovellOES, SchemaDefs_OSTypeId_Netware },
59 { ovf::CIMOSType_CIMOS_Solaris, SchemaDefs_OSTypeId_OpenSolaris },
60 { ovf::CIMOSType_CIMOS_SunOS, SchemaDefs_OSTypeId_OpenSolaris },
61 { ovf::CIMOSType_CIMOS_FreeBSD, SchemaDefs_OSTypeId_FreeBSD },
62 { ovf::CIMOSType_CIMOS_NetBSD, SchemaDefs_OSTypeId_NetBSD },
63 { ovf::CIMOSType_CIMOS_QNX, SchemaDefs_OSTypeId_QNX },
64 { ovf::CIMOSType_CIMOS_Windows2000, SchemaDefs_OSTypeId_Windows2000 },
65 { ovf::CIMOSType_CIMOS_WindowsMe, SchemaDefs_OSTypeId_WindowsMe },
66 { ovf::CIMOSType_CIMOS_OpenBSD, SchemaDefs_OSTypeId_OpenBSD },
67 { ovf::CIMOSType_CIMOS_WindowsXP, SchemaDefs_OSTypeId_WindowsXP },
68 { ovf::CIMOSType_CIMOS_WindowsXPEmbedded, SchemaDefs_OSTypeId_WindowsXP },
69 { ovf::CIMOSType_CIMOS_WindowsEmbeddedforPointofService, SchemaDefs_OSTypeId_WindowsXP },
70 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2003, SchemaDefs_OSTypeId_Windows2003 },
71 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2003_64, SchemaDefs_OSTypeId_Windows2003_64 },
72 { ovf::CIMOSType_CIMOS_WindowsXP_64, SchemaDefs_OSTypeId_WindowsXP_64 },
73 { ovf::CIMOSType_CIMOS_WindowsVista, SchemaDefs_OSTypeId_WindowsVista },
74 { ovf::CIMOSType_CIMOS_WindowsVista_64, SchemaDefs_OSTypeId_WindowsVista_64 },
75 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2008, SchemaDefs_OSTypeId_Windows2008 },
76 { ovf::CIMOSType_CIMOS_MicrosoftWindowsServer2008_64, SchemaDefs_OSTypeId_Windows2008_64 },
77 { ovf::CIMOSType_CIMOS_FreeBSD_64, SchemaDefs_OSTypeId_FreeBSD_64 },
78 { ovf::CIMOSType_CIMOS_RedHatEnterpriseLinux, SchemaDefs_OSTypeId_RedHat },
79 { ovf::CIMOSType_CIMOS_RedHatEnterpriseLinux_64, SchemaDefs_OSTypeId_RedHat_64 },
80 { ovf::CIMOSType_CIMOS_Solaris_64, SchemaDefs_OSTypeId_OpenSolaris_64 },
81 { ovf::CIMOSType_CIMOS_SUSE, SchemaDefs_OSTypeId_OpenSUSE },
82 { ovf::CIMOSType_CIMOS_SLES, SchemaDefs_OSTypeId_OpenSUSE },
83 { ovf::CIMOSType_CIMOS_NovellLinuxDesktop, SchemaDefs_OSTypeId_OpenSUSE },
84 { ovf::CIMOSType_CIMOS_SUSE_64, SchemaDefs_OSTypeId_OpenSUSE_64 },
85 { ovf::CIMOSType_CIMOS_SLES_64, SchemaDefs_OSTypeId_OpenSUSE_64 },
86 { ovf::CIMOSType_CIMOS_LINUX, SchemaDefs_OSTypeId_Linux },
87 { ovf::CIMOSType_CIMOS_SunJavaDesktopSystem, SchemaDefs_OSTypeId_Linux },
88 { ovf::CIMOSType_CIMOS_TurboLinux, SchemaDefs_OSTypeId_Linux},
89
90 // { ovf::CIMOSType_CIMOS_TurboLinux_64, },
91
92 { ovf::CIMOSType_CIMOS_Mandriva, SchemaDefs_OSTypeId_Mandriva },
93 { ovf::CIMOSType_CIMOS_Mandriva_64, SchemaDefs_OSTypeId_Mandriva_64 },
94 { ovf::CIMOSType_CIMOS_Ubuntu, SchemaDefs_OSTypeId_Ubuntu },
95 { ovf::CIMOSType_CIMOS_Ubuntu_64, SchemaDefs_OSTypeId_Ubuntu_64 },
96 { ovf::CIMOSType_CIMOS_Debian, SchemaDefs_OSTypeId_Debian },
97 { ovf::CIMOSType_CIMOS_Debian_64, SchemaDefs_OSTypeId_Debian_64 },
98 { ovf::CIMOSType_CIMOS_Linux_2_4_x, SchemaDefs_OSTypeId_Linux24 },
99 { ovf::CIMOSType_CIMOS_Linux_2_4_x_64, SchemaDefs_OSTypeId_Linux24_64 },
100 { ovf::CIMOSType_CIMOS_Linux_2_6_x, SchemaDefs_OSTypeId_Linux26 },
101 { ovf::CIMOSType_CIMOS_Linux_2_6_x_64, SchemaDefs_OSTypeId_Linux26_64 },
102 { ovf::CIMOSType_CIMOS_Linux_64, SchemaDefs_OSTypeId_Linux26_64 }
103};
104
105/* Pattern structure for matching the OS type description field */
106struct osTypePattern
107{
108 const char *pcszPattern;
109 const char *pcszVbox;
110};
111
112/* These are the 32-Bit ones. They are sorted by priority. */
113static const osTypePattern g_osTypesPattern[] =
114{
115 {"Windows NT", SchemaDefs_OSTypeId_WindowsNT4},
116 {"Windows XP", SchemaDefs_OSTypeId_WindowsXP},
117 {"Windows 2000", SchemaDefs_OSTypeId_Windows2000},
118 {"Windows 2003", SchemaDefs_OSTypeId_Windows2003},
119 {"Windows Vista", SchemaDefs_OSTypeId_WindowsVista},
120 {"Windows 2008", SchemaDefs_OSTypeId_Windows2008},
121 {"SUSE", SchemaDefs_OSTypeId_OpenSUSE},
122 {"Novell", SchemaDefs_OSTypeId_OpenSUSE},
123 {"Red Hat", SchemaDefs_OSTypeId_RedHat},
124 {"Mandriva", SchemaDefs_OSTypeId_Mandriva},
125 {"Ubuntu", SchemaDefs_OSTypeId_Ubuntu},
126 {"Debian", SchemaDefs_OSTypeId_Debian},
127 {"QNX", SchemaDefs_OSTypeId_QNX},
128 {"Linux 2.4", SchemaDefs_OSTypeId_Linux24},
129 {"Linux 2.6", SchemaDefs_OSTypeId_Linux26},
130 {"Linux", SchemaDefs_OSTypeId_Linux},
131 {"OpenSolaris", SchemaDefs_OSTypeId_OpenSolaris},
132 {"Solaris", SchemaDefs_OSTypeId_OpenSolaris},
133 {"FreeBSD", SchemaDefs_OSTypeId_FreeBSD},
134 {"NetBSD", SchemaDefs_OSTypeId_NetBSD},
135 {"Windows 95", SchemaDefs_OSTypeId_Windows95},
136 {"Windows 98", SchemaDefs_OSTypeId_Windows98},
137 {"Windows Me", SchemaDefs_OSTypeId_WindowsMe},
138 {"Windows 3.", SchemaDefs_OSTypeId_Windows31},
139 {"DOS", SchemaDefs_OSTypeId_DOS},
140 {"OS2", SchemaDefs_OSTypeId_OS2}
141};
142
143/* These are the 64-Bit ones. They are sorted by priority. */
144static const osTypePattern g_osTypesPattern64[] =
145{
146 {"Windows XP", SchemaDefs_OSTypeId_WindowsXP_64},
147 {"Windows 2003", SchemaDefs_OSTypeId_Windows2003_64},
148 {"Windows Vista", SchemaDefs_OSTypeId_WindowsVista_64},
149 {"Windows 2008", SchemaDefs_OSTypeId_Windows2008_64},
150 {"SUSE", SchemaDefs_OSTypeId_OpenSUSE_64},
151 {"Novell", SchemaDefs_OSTypeId_OpenSUSE_64},
152 {"Red Hat", SchemaDefs_OSTypeId_RedHat_64},
153 {"Mandriva", SchemaDefs_OSTypeId_Mandriva_64},
154 {"Ubuntu", SchemaDefs_OSTypeId_Ubuntu_64},
155 {"Debian", SchemaDefs_OSTypeId_Debian_64},
156 {"Linux 2.4", SchemaDefs_OSTypeId_Linux24_64},
157 {"Linux 2.6", SchemaDefs_OSTypeId_Linux26_64},
158 {"Linux", SchemaDefs_OSTypeId_Linux26_64},
159 {"OpenSolaris", SchemaDefs_OSTypeId_OpenSolaris_64},
160 {"Solaris", SchemaDefs_OSTypeId_OpenSolaris_64},
161 {"FreeBSD", SchemaDefs_OSTypeId_FreeBSD_64},
162};
163
164/**
165 * Private helper func that suggests a VirtualBox guest OS type
166 * for the given OVF operating system type.
167 * @param osTypeVBox
168 * @param c
169 * @param cStr
170 */
171void convertCIMOSType2VBoxOSType(Utf8Str &strType, ovf::CIMOSType_T c, const Utf8Str &cStr)
172{
173 /* First check if the type is other/other_64 */
174 if (c == ovf::CIMOSType_CIMOS_Other)
175 {
176 for (size_t i=0; i < RT_ELEMENTS(g_osTypesPattern); ++i)
177 if (cStr.contains (g_osTypesPattern[i].pcszPattern, Utf8Str::CaseInsensitive))
178 {
179 strType = g_osTypesPattern[i].pcszVbox;
180 return;
181 }
182 }
183 else if (c == ovf::CIMOSType_CIMOS_Other_64)
184 {
185 for (size_t i=0; i < RT_ELEMENTS(g_osTypesPattern64); ++i)
186 if (cStr.contains (g_osTypesPattern64[i].pcszPattern, Utf8Str::CaseInsensitive))
187 {
188 strType = g_osTypesPattern64[i].pcszVbox;
189 return;
190 }
191 }
192
193 for (size_t i = 0; i < RT_ELEMENTS(g_osTypes); ++i)
194 {
195 if (c == g_osTypes[i].cim)
196 {
197 strType = g_osTypes[i].pcszVbox;
198 return;
199 }
200 }
201
202 strType = SchemaDefs_OSTypeId_Other;
203}
204
205/**
206 * Private helper func that suggests a VirtualBox guest OS type
207 * for the given OVF operating system type.
208 * @param osTypeVBox
209 * @param c
210 */
211ovf::CIMOSType_T convertVBoxOSType2CIMOSType(const char *pcszVbox)
212{
213 for (size_t i = 0; i < RT_ELEMENTS(g_osTypes); ++i)
214 {
215 if (!RTStrICmp(pcszVbox, g_osTypes[i].pcszVbox))
216 return g_osTypes[i].cim;
217 }
218
219 return ovf::CIMOSType_CIMOS_Other;
220}
221
222////////////////////////////////////////////////////////////////////////////////
223//
224// IVirtualBox public methods
225//
226////////////////////////////////////////////////////////////////////////////////
227
228// This code is here so we won't have to include the appliance headers in the
229// IVirtualBox implementation.
230
231/**
232 * Implementation for IVirtualBox::createAppliance.
233 *
234 * @param anAppliance IAppliance object created if S_OK is returned.
235 * @return S_OK or error.
236 */
237STDMETHODIMP VirtualBox::CreateAppliance(IAppliance** anAppliance)
238{
239 HRESULT rc;
240
241 ComObjPtr<Appliance> appliance;
242 appliance.createObject();
243 rc = appliance->init(this);
244
245 if (SUCCEEDED(rc))
246 appliance.queryInterfaceTo(anAppliance);
247
248 return rc;
249}
250
251////////////////////////////////////////////////////////////////////////////////
252//
253// Appliance constructor / destructor
254//
255////////////////////////////////////////////////////////////////////////////////
256
257Appliance::Appliance()
258 : mVirtualBox(NULL)
259{
260}
261
262Appliance::~Appliance()
263{
264}
265
266/**
267 * Appliance COM initializer.
268 * @param
269 * @return
270 */
271HRESULT Appliance::init(VirtualBox *aVirtualBox)
272{
273 /* Enclose the state transition NotReady->InInit->Ready */
274 AutoInitSpan autoInitSpan(this);
275 AssertReturn(autoInitSpan.isOk(), E_FAIL);
276
277 /* Weak reference to a VirtualBox object */
278 unconst(mVirtualBox) = aVirtualBox;
279
280 // initialize data
281 m = new Data;
282
283 /* Confirm a successful initialization */
284 autoInitSpan.setSucceeded();
285
286 return S_OK;
287}
288
289/**
290 * Appliance COM uninitializer.
291 * @return
292 */
293void Appliance::uninit()
294{
295 /* Enclose the state transition Ready->InUninit->NotReady */
296 AutoUninitSpan autoUninitSpan(this);
297 if (autoUninitSpan.uninitDone())
298 return;
299
300 delete m;
301 m = NULL;
302}
303
304////////////////////////////////////////////////////////////////////////////////
305//
306// IAppliance public methods
307//
308////////////////////////////////////////////////////////////////////////////////
309
310/**
311 * Public method implementation.
312 * @param
313 * @return
314 */
315STDMETHODIMP Appliance::COMGETTER(Path)(BSTR *aPath)
316{
317 if (!aPath)
318 return E_POINTER;
319
320 AutoCaller autoCaller(this);
321 if (FAILED(autoCaller.rc())) return autoCaller.rc();
322
323 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
324
325 if (!isApplianceIdle())
326 return E_ACCESSDENIED;
327
328 Bstr bstrPath(m->locInfo.strPath);
329 bstrPath.cloneTo(aPath);
330
331 return S_OK;
332}
333
334/**
335 * Public method implementation.
336 * @param
337 * @return
338 */
339STDMETHODIMP Appliance::COMGETTER(Disks)(ComSafeArrayOut(BSTR, aDisks))
340{
341 CheckComArgOutSafeArrayPointerValid(aDisks);
342
343 AutoCaller autoCaller(this);
344 if (FAILED(autoCaller.rc())) return autoCaller.rc();
345
346 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
347
348 if (!isApplianceIdle())
349 return E_ACCESSDENIED;
350
351 if (m->pReader) // OVFReader instantiated?
352 {
353 size_t c = m->pReader->m_mapDisks.size();
354 com::SafeArray<BSTR> sfaDisks(c);
355
356 ovf::DiskImagesMap::const_iterator it;
357 size_t i = 0;
358 for (it = m->pReader->m_mapDisks.begin();
359 it != m->pReader->m_mapDisks.end();
360 ++it, ++i)
361 {
362 // create a string representing this disk
363 const ovf::DiskImage &d = it->second;
364 char *psz = NULL;
365 RTStrAPrintf(&psz,
366 "%s\t"
367 "%RI64\t"
368 "%RI64\t"
369 "%s\t"
370 "%s\t"
371 "%RI64\t"
372 "%RI64\t"
373 "%s",
374 d.strDiskId.c_str(),
375 d.iCapacity,
376 d.iPopulatedSize,
377 d.strFormat.c_str(),
378 d.strHref.c_str(),
379 d.iSize,
380 d.iChunkSize,
381 d.strCompression.c_str());
382 Utf8Str utf(psz);
383 Bstr bstr(utf);
384 // push to safearray
385 bstr.cloneTo(&sfaDisks[i]);
386 RTStrFree(psz);
387 }
388
389 sfaDisks.detachTo(ComSafeArrayOutArg(aDisks));
390 }
391
392 return S_OK;
393}
394
395/**
396 * Public method implementation.
397 * @param
398 * @return
399 */
400STDMETHODIMP Appliance::COMGETTER(VirtualSystemDescriptions)(ComSafeArrayOut(IVirtualSystemDescription*, aVirtualSystemDescriptions))
401{
402 CheckComArgOutSafeArrayPointerValid(aVirtualSystemDescriptions);
403
404 AutoCaller autoCaller(this);
405 if (FAILED(autoCaller.rc())) return autoCaller.rc();
406
407 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
408
409 if (!isApplianceIdle())
410 return E_ACCESSDENIED;
411
412 SafeIfaceArray<IVirtualSystemDescription> sfaVSD(m->virtualSystemDescriptions);
413 sfaVSD.detachTo(ComSafeArrayOutArg(aVirtualSystemDescriptions));
414
415 return S_OK;
416}
417
418STDMETHODIMP Appliance::CreateVFSExplorer(IN_BSTR aURI, IVFSExplorer **aExplorer)
419{
420 CheckComArgOutPointerValid(aExplorer);
421
422 AutoCaller autoCaller(this);
423 if (FAILED(autoCaller.rc())) return autoCaller.rc();
424
425 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
426
427 ComObjPtr<VFSExplorer> explorer;
428 HRESULT rc = S_OK;
429 try
430 {
431 Utf8Str uri(aURI);
432 /* Check which kind of export the user has requested */
433 LocationInfo li;
434 parseURI(uri, li);
435 /* Create the explorer object */
436 explorer.createObject();
437 rc = explorer->init(li.storageType, li.strPath, li.strHostname, li.strUsername, li.strPassword, mVirtualBox);
438 }
439 catch (HRESULT aRC)
440 {
441 rc = aRC;
442 }
443
444 if (SUCCEEDED(rc))
445 /* Return explorer to the caller */
446 explorer.queryInterfaceTo(aExplorer);
447
448 return rc;
449}
450
451/**
452* Public method implementation.
453 * @return
454 */
455STDMETHODIMP Appliance::GetWarnings(ComSafeArrayOut(BSTR, aWarnings))
456{
457 if (ComSafeArrayOutIsNull(aWarnings))
458 return E_POINTER;
459
460 AutoCaller autoCaller(this);
461 if (FAILED(autoCaller.rc())) return autoCaller.rc();
462
463 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
464
465 com::SafeArray<BSTR> sfaWarnings(m->llWarnings.size());
466
467 list<Utf8Str>::const_iterator it;
468 size_t i = 0;
469 for (it = m->llWarnings.begin();
470 it != m->llWarnings.end();
471 ++it, ++i)
472 {
473 Bstr bstr = *it;
474 bstr.cloneTo(&sfaWarnings[i]);
475 }
476
477 sfaWarnings.detachTo(ComSafeArrayOutArg(aWarnings));
478
479 return S_OK;
480}
481
482////////////////////////////////////////////////////////////////////////////////
483//
484// Appliance private methods
485//
486////////////////////////////////////////////////////////////////////////////////
487
488/**
489 * Returns true if the appliance is in "idle" state. This should always be the
490 * case unless an import or export is currently in progress. Similar to machine
491 * states, this permits the Appliance implementation code to let go of the
492 * Appliance object lock while a time-consuming disk conversion is in progress
493 * without exposing the appliance to conflicting calls.
494 *
495 * This sets an error on "this" (the appliance) and returns false if the appliance
496 * is busy. The caller should then return E_ACCESSDENIED.
497 *
498 * Must be called from under the object lock!
499 *
500 * @return
501 */
502bool Appliance::isApplianceIdle() const
503{
504 if (m->state == Data::ApplianceImporting)
505 setError(VBOX_E_INVALID_OBJECT_STATE, "The appliance is busy importing files");
506 else if (m->state == Data::ApplianceExporting)
507 setError(VBOX_E_INVALID_OBJECT_STATE, "The appliance is busy exporting files");
508 else
509 return true;
510
511 return false;
512}
513
514HRESULT Appliance::searchUniqueVMName(Utf8Str& aName) const
515{
516 IMachine *machine = NULL;
517 char *tmpName = RTStrDup(aName.c_str());
518 int i = 1;
519 /* @todo: Maybe too cost-intensive; try to find a lighter way */
520 while (mVirtualBox->FindMachine(Bstr(tmpName), &machine) != VBOX_E_OBJECT_NOT_FOUND)
521 {
522 RTStrFree(tmpName);
523 RTStrAPrintf(&tmpName, "%s_%d", aName.c_str(), i);
524 ++i;
525 }
526 aName = tmpName;
527 RTStrFree(tmpName);
528
529 return S_OK;
530}
531
532HRESULT Appliance::searchUniqueDiskImageFilePath(Utf8Str& aName) const
533{
534 IMedium *harddisk = NULL;
535 char *tmpName = RTStrDup(aName.c_str());
536 int i = 1;
537 /* Check if the file exists or if a file with this path is registered
538 * already */
539 /* @todo: Maybe too cost-intensive; try to find a lighter way */
540 while ( RTPathExists(tmpName)
541 || mVirtualBox->FindHardDisk(Bstr(tmpName), &harddisk) != VBOX_E_OBJECT_NOT_FOUND
542 )
543 {
544 RTStrFree(tmpName);
545 char *tmpDir = RTStrDup(aName.c_str());
546 RTPathStripFilename(tmpDir);;
547 char *tmpFile = RTStrDup(RTPathFilename(aName.c_str()));
548 RTPathStripExt(tmpFile);
549 const char *tmpExt = RTPathExt(aName.c_str());
550 RTStrAPrintf(&tmpName, "%s%c%s_%d%s", tmpDir, RTPATH_DELIMITER, tmpFile, i, tmpExt);
551 RTStrFree(tmpFile);
552 RTStrFree(tmpDir);
553 ++i;
554 }
555 aName = tmpName;
556 RTStrFree(tmpName);
557
558 return S_OK;
559}
560
561/**
562 * Little shortcut to SystemProperties::DefaultHardDiskFolder.
563 * @param str
564 * @return
565 */
566HRESULT Appliance::getDefaultHardDiskFolder(Utf8Str &str) const
567{
568 /* We need the default path for storing disk images */
569 ComPtr<ISystemProperties> systemProps;
570 HRESULT rc = mVirtualBox->COMGETTER(SystemProperties)(systemProps.asOutParam());
571 if (FAILED(rc)) return rc;
572 Bstr bstrDefaultHardDiskFolder;
573 rc = systemProps->COMGETTER(DefaultHardDiskFolder)(bstrDefaultHardDiskFolder.asOutParam());
574 if (FAILED(rc)) return rc;
575 str = bstrDefaultHardDiskFolder;
576
577 return S_OK;
578}
579
580/**
581 * Called from the import and export background threads to synchronize the second
582 * background disk thread's progress object with the current progress object so
583 * that the user interface sees progress correctly and that cancel signals are
584 * passed on to the second thread.
585 * @param pProgressThis Progress object of the current thread.
586 * @param pProgressAsync Progress object of asynchronous task running in background.
587 */
588void Appliance::waitForAsyncProgress(ComObjPtr<Progress> &pProgressThis,
589 ComPtr<IProgress> &pProgressAsync)
590{
591 HRESULT rc;
592
593 // now loop until the asynchronous operation completes and then report its result
594 BOOL fCompleted;
595 BOOL fCanceled;
596 ULONG currentPercent;
597 while (SUCCEEDED(pProgressAsync->COMGETTER(Completed(&fCompleted))))
598 {
599 rc = pProgressThis->COMGETTER(Canceled)(&fCanceled);
600 if (FAILED(rc)) throw rc;
601 if (fCanceled)
602 {
603 pProgressAsync->Cancel();
604 break;
605 }
606
607 rc = pProgressAsync->COMGETTER(Percent(&currentPercent));
608 if (FAILED(rc)) throw rc;
609 if (!pProgressThis.isNull())
610 pProgressThis->SetCurrentOperationProgress(currentPercent);
611 if (fCompleted)
612 break;
613
614 /* Make sure the loop is not too tight */
615 rc = pProgressAsync->WaitForCompletion(100);
616 if (FAILED(rc)) throw rc;
617 }
618 // report result of asynchronous operation
619 LONG iRc;
620 rc = pProgressAsync->COMGETTER(ResultCode)(&iRc);
621 if (FAILED(rc)) throw rc;
622
623
624 // if the thread of the progress object has an error, then
625 // retrieve the error info from there, or it'll be lost
626 if (FAILED(iRc))
627 {
628 ProgressErrorInfo info(pProgressAsync);
629 Utf8Str str(info.getText());
630 const char *pcsz = str.c_str();
631 HRESULT rc2 = setError(iRc, pcsz);
632 throw rc2;
633 }
634}
635
636void Appliance::addWarning(const char* aWarning, ...)
637{
638 va_list args;
639 va_start(args, aWarning);
640 Utf8StrFmtVA str(aWarning, args);
641 va_end(args);
642 m->llWarnings.push_back(str);
643}
644
645/**
646 * Refreshes the cDisks and ulTotalDisksMB members in the instance data.
647 * Requires that virtual system descriptions are present.
648 */
649void Appliance::disksWeight()
650{
651 m->ulTotalDisksMB = 0;
652 m->cDisks = 0;
653 // weigh the disk images according to their sizes
654 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
655 for (it = m->virtualSystemDescriptions.begin();
656 it != m->virtualSystemDescriptions.end();
657 ++it)
658 {
659 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
660 /* One for every hard disk of the Virtual System */
661 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
662 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
663 for (itH = avsdeHDs.begin();
664 itH != avsdeHDs.end();
665 ++itH)
666 {
667 const VirtualSystemDescriptionEntry *pHD = *itH;
668 m->ulTotalDisksMB += pHD->ulSizeMB;
669 ++m->cDisks;
670 }
671 }
672
673}
674
675HRESULT Appliance::setUpProgress(ComObjPtr<Progress> &pProgress,
676 const Bstr &bstrDescription,
677 SetUpProgressMode mode)
678{
679 HRESULT rc;
680
681 /* Create the progress object */
682 pProgress.createObject();
683
684 // compute the disks weight (this sets ulTotalDisksMB and cDisks in the instance data)
685 disksWeight();
686
687 ULONG cOperations;
688 ULONG ulTotalOperationsWeight;
689
690 cOperations = 1 // one for XML setup
691 + m->cDisks; // plus one per disk
692 if (m->ulTotalDisksMB)
693 {
694 m->ulWeightPerOperation = (ULONG)((double)m->ulTotalDisksMB * 1 / 100); // use 1% of the progress for the XML
695 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightPerOperation;
696 }
697 else
698 {
699 // no disks to export:
700 m->ulWeightPerOperation = 1;
701 ulTotalOperationsWeight = 1;
702 }
703
704 switch (mode)
705 {
706 case Regular:
707 break;
708
709 case ImportS3:
710 {
711 cOperations += 1 + 1; // another one for the manifest file & another one for the import
712 ulTotalOperationsWeight = m->ulTotalDisksMB;
713 if (!m->ulTotalDisksMB)
714 // no disks to export:
715 ulTotalOperationsWeight = 1;
716
717 ULONG ulImportWeight = (ULONG)((double)ulTotalOperationsWeight * 50 / 100); // use 50% for import
718 ulTotalOperationsWeight += ulImportWeight;
719
720 m->ulWeightPerOperation = ulImportWeight; /* save for using later */
721
722 ULONG ulInitWeight = (ULONG)((double)ulTotalOperationsWeight * 0.1 / 100); // use 0.1% for init
723 ulTotalOperationsWeight += ulInitWeight;
724 }
725 break;
726
727 case WriteS3:
728 {
729 cOperations += 1 + 1; // another one for the mf & another one for temporary creation
730
731 if (m->ulTotalDisksMB)
732 {
733 m->ulWeightPerOperation = (ULONG)((double)m->ulTotalDisksMB * 1 / 100); // use 1% of the progress for OVF file upload (we didn't know the size at this point)
734 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightPerOperation;
735 }
736 else
737 {
738 // no disks to export:
739 ulTotalOperationsWeight = 1;
740 m->ulWeightPerOperation = 1;
741 }
742 ULONG ulOVFCreationWeight = (ULONG)((double)ulTotalOperationsWeight * 50.0 / 100.0); /* Use 50% for the creation of the OVF & the disks */
743 ulTotalOperationsWeight += ulOVFCreationWeight;
744 }
745 break;
746 }
747
748 Log(("Setting up progress object: ulTotalMB = %d, cDisks = %d, => cOperations = %d, ulTotalOperationsWeight = %d, m->ulWeightPerOperation = %d\n",
749 m->ulTotalDisksMB, m->cDisks, cOperations, ulTotalOperationsWeight, m->ulWeightPerOperation));
750
751 rc = pProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
752 bstrDescription,
753 TRUE /* aCancelable */,
754 cOperations, // ULONG cOperations,
755 ulTotalOperationsWeight, // ULONG ulTotalOperationsWeight,
756 bstrDescription, // CBSTR bstrFirstOperationDescription,
757 m->ulWeightPerOperation); // ULONG ulFirstOperationWeight,
758 return rc;
759}
760
761void Appliance::parseURI(Utf8Str strUri, LocationInfo &locInfo) const
762{
763 /* Check the URI for the protocol */
764 if (strUri.startsWith("file://", Utf8Str::CaseInsensitive)) /* File based */
765 {
766 locInfo.storageType = VFSType_File;
767 strUri = strUri.substr(sizeof("file://") - 1);
768 }
769 else if (strUri.startsWith("SunCloud://", Utf8Str::CaseInsensitive)) /* Sun Cloud service */
770 {
771 locInfo.storageType = VFSType_S3;
772 strUri = strUri.substr(sizeof("SunCloud://") - 1);
773 }
774 else if (strUri.startsWith("S3://", Utf8Str::CaseInsensitive)) /* S3 service */
775 {
776 locInfo.storageType = VFSType_S3;
777 strUri = strUri.substr(sizeof("S3://") - 1);
778 }
779 else if (strUri.startsWith("webdav://", Utf8Str::CaseInsensitive)) /* webdav service */
780 throw E_NOTIMPL;
781
782 /* Not necessary on a file based URI */
783 if (locInfo.storageType != VFSType_File)
784 {
785 size_t uppos = strUri.find("@"); /* username:password combo */
786 if (uppos != Utf8Str::npos)
787 {
788 locInfo.strUsername = strUri.substr(0, uppos);
789 strUri = strUri.substr(uppos + 1);
790 size_t upos = locInfo.strUsername.find(":");
791 if (upos != Utf8Str::npos)
792 {
793 locInfo.strPassword = locInfo.strUsername.substr(upos + 1);
794 locInfo.strUsername = locInfo.strUsername.substr(0, upos);
795 }
796 }
797 size_t hpos = strUri.find("/"); /* hostname part */
798 if (hpos != Utf8Str::npos)
799 {
800 locInfo.strHostname = strUri.substr(0, hpos);
801 strUri = strUri.substr(hpos);
802 }
803 }
804
805 locInfo.strPath = strUri;
806}
807
808void Appliance::parseBucket(Utf8Str &aPath, Utf8Str &aBucket) const
809{
810 /* Buckets are S3 specific. So parse the bucket out of the file path */
811 if (!aPath.startsWith("/"))
812 throw setError(E_INVALIDARG,
813 tr("The path '%s' must start with /"), aPath.c_str());
814 size_t bpos = aPath.find("/", 1);
815 if (bpos != Utf8Str::npos)
816 {
817 aBucket = aPath.substr(1, bpos - 1); /* The bucket without any slashes */
818 aPath = aPath.substr(bpos); /* The rest of the file path */
819 }
820 /* If there is no bucket name provided reject it */
821 if (aBucket.isEmpty())
822 throw setError(E_INVALIDARG,
823 tr("You doesn't provide a bucket name in the URI '%s'"), aPath.c_str());
824}
825
826Utf8Str Appliance::manifestFileName(Utf8Str aPath) const
827{
828 /* Get the name part */
829 char *pszMfName = RTStrDup(RTPathFilename(aPath.c_str()));
830 /* Strip any extensions */
831 RTPathStripExt(pszMfName);
832 /* Path without the filename */
833 aPath.stripFilename();
834 /* Format the manifest path */
835 Utf8StrFmt strMfFile("%s/%s.mf", aPath.c_str(), pszMfName);
836 RTStrFree(pszMfName);
837 return strMfFile;
838}
839
840/**
841 *
842 * @return
843 */
844int Appliance::TaskOVF::startThread()
845{
846 int vrc = RTThreadCreate(NULL, Appliance::taskThreadImportOrExport, this,
847 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
848 "Appliance::Task");
849
850 ComAssertMsgRCRet(vrc,
851 ("Could not create OVF task thread (%Rrc)\n", vrc), E_FAIL);
852
853 return S_OK;
854}
855
856/**
857 * Thread function for the thread started in Appliance::readImpl() and Appliance::importImpl()
858 * and Appliance::writeImpl().
859 * This will in turn call Appliance::readFS() or Appliance::readS3() or Appliance::importFS()
860 * or Appliance::importS3() or Appliance::writeFS() or Appliance::writeS3().
861 *
862 * @param aThread
863 * @param pvUser
864 */
865/* static */
866DECLCALLBACK(int) Appliance::taskThreadImportOrExport(RTTHREAD /* aThread */, void *pvUser)
867{
868 std::auto_ptr<TaskOVF> task(static_cast<TaskOVF*>(pvUser));
869 AssertReturn(task.get(), VERR_GENERAL_FAILURE);
870
871 Appliance *pAppliance = task->pAppliance;
872
873 LogFlowFuncEnter();
874 LogFlowFunc(("Appliance %p\n", pAppliance));
875
876 HRESULT taskrc = S_OK;
877
878 switch (task->taskType)
879 {
880 case TaskOVF::Read:
881 if (task->locInfo.storageType == VFSType_File)
882 taskrc = pAppliance->readFS(task->locInfo);
883 else if (task->locInfo.storageType == VFSType_S3)
884 taskrc = pAppliance->readS3(task.get());
885 break;
886
887 case TaskOVF::Import:
888 if (task->locInfo.storageType == VFSType_File)
889 taskrc = pAppliance->importFS(task->locInfo, task->pProgress);
890 else if (task->locInfo.storageType == VFSType_S3)
891 taskrc = pAppliance->importS3(task.get());
892 break;
893
894 case TaskOVF::Write:
895 if (task->locInfo.storageType == VFSType_File)
896 taskrc = pAppliance->writeFS(task->locInfo, task->enFormat, task->pProgress);
897 else if (task->locInfo.storageType == VFSType_S3)
898 taskrc = pAppliance->writeS3(task.get());
899 break;
900 }
901
902 task->rc = taskrc;
903
904 if (!task->pProgress.isNull())
905 task->pProgress->notifyComplete(taskrc);
906
907 LogFlowFuncLeave();
908
909 return VINF_SUCCESS;
910}
911
912/* static */
913int Appliance::TaskOVF::updateProgress(unsigned uPercent, void *pvUser)
914{
915 Appliance::TaskOVF* pTask = *(Appliance::TaskOVF**)pvUser;
916
917 if ( pTask
918 && !pTask->pProgress.isNull())
919 {
920 BOOL fCanceled;
921 pTask->pProgress->COMGETTER(Canceled)(&fCanceled);
922 if (fCanceled)
923 return -1;
924 pTask->pProgress->SetCurrentOperationProgress(uPercent);
925 }
926 return VINF_SUCCESS;
927}
928
929////////////////////////////////////////////////////////////////////////////////
930//
931// IVirtualSystemDescription constructor / destructor
932//
933////////////////////////////////////////////////////////////////////////////////
934
935DEFINE_EMPTY_CTOR_DTOR(VirtualSystemDescription)
936
937/**
938 * COM initializer.
939 * @return
940 */
941HRESULT VirtualSystemDescription::init()
942{
943 /* Enclose the state transition NotReady->InInit->Ready */
944 AutoInitSpan autoInitSpan(this);
945 AssertReturn(autoInitSpan.isOk(), E_FAIL);
946
947 /* Initialize data */
948 m = new Data();
949 m->pConfig = NULL;
950
951 /* Confirm a successful initialization */
952 autoInitSpan.setSucceeded();
953 return S_OK;
954}
955
956/**
957* COM uninitializer.
958*/
959
960void VirtualSystemDescription::uninit()
961{
962 if (m->pConfig)
963 delete m->pConfig;
964 delete m;
965 m = NULL;
966}
967
968////////////////////////////////////////////////////////////////////////////////
969//
970// IVirtualSystemDescription public methods
971//
972////////////////////////////////////////////////////////////////////////////////
973
974/**
975 * Public method implementation.
976 * @param
977 * @return
978 */
979STDMETHODIMP VirtualSystemDescription::COMGETTER(Count)(ULONG *aCount)
980{
981 if (!aCount)
982 return E_POINTER;
983
984 AutoCaller autoCaller(this);
985 if (FAILED(autoCaller.rc())) return autoCaller.rc();
986
987 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
988
989 *aCount = (ULONG)m->llDescriptions.size();
990
991 return S_OK;
992}
993
994/**
995 * Public method implementation.
996 * @return
997 */
998STDMETHODIMP VirtualSystemDescription::GetDescription(ComSafeArrayOut(VirtualSystemDescriptionType_T, aTypes),
999 ComSafeArrayOut(BSTR, aRefs),
1000 ComSafeArrayOut(BSTR, aOrigValues),
1001 ComSafeArrayOut(BSTR, aVboxValues),
1002 ComSafeArrayOut(BSTR, aExtraConfigValues))
1003{
1004 if (ComSafeArrayOutIsNull(aTypes) ||
1005 ComSafeArrayOutIsNull(aRefs) ||
1006 ComSafeArrayOutIsNull(aOrigValues) ||
1007 ComSafeArrayOutIsNull(aVboxValues) ||
1008 ComSafeArrayOutIsNull(aExtraConfigValues))
1009 return E_POINTER;
1010
1011 AutoCaller autoCaller(this);
1012 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1013
1014 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1015
1016 ULONG c = (ULONG)m->llDescriptions.size();
1017 com::SafeArray<VirtualSystemDescriptionType_T> sfaTypes(c);
1018 com::SafeArray<BSTR> sfaRefs(c);
1019 com::SafeArray<BSTR> sfaOrigValues(c);
1020 com::SafeArray<BSTR> sfaVboxValues(c);
1021 com::SafeArray<BSTR> sfaExtraConfigValues(c);
1022
1023 list<VirtualSystemDescriptionEntry>::const_iterator it;
1024 size_t i = 0;
1025 for (it = m->llDescriptions.begin();
1026 it != m->llDescriptions.end();
1027 ++it, ++i)
1028 {
1029 const VirtualSystemDescriptionEntry &vsde = (*it);
1030
1031 sfaTypes[i] = vsde.type;
1032
1033 Bstr bstr = vsde.strRef;
1034 bstr.cloneTo(&sfaRefs[i]);
1035
1036 bstr = vsde.strOvf;
1037 bstr.cloneTo(&sfaOrigValues[i]);
1038
1039 bstr = vsde.strVbox;
1040 bstr.cloneTo(&sfaVboxValues[i]);
1041
1042 bstr = vsde.strExtraConfig;
1043 bstr.cloneTo(&sfaExtraConfigValues[i]);
1044 }
1045
1046 sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
1047 sfaRefs.detachTo(ComSafeArrayOutArg(aRefs));
1048 sfaOrigValues.detachTo(ComSafeArrayOutArg(aOrigValues));
1049 sfaVboxValues.detachTo(ComSafeArrayOutArg(aVboxValues));
1050 sfaExtraConfigValues.detachTo(ComSafeArrayOutArg(aExtraConfigValues));
1051
1052 return S_OK;
1053}
1054
1055/**
1056 * Public method implementation.
1057 * @return
1058 */
1059STDMETHODIMP VirtualSystemDescription::GetDescriptionByType(VirtualSystemDescriptionType_T aType,
1060 ComSafeArrayOut(VirtualSystemDescriptionType_T, aTypes),
1061 ComSafeArrayOut(BSTR, aRefs),
1062 ComSafeArrayOut(BSTR, aOrigValues),
1063 ComSafeArrayOut(BSTR, aVboxValues),
1064 ComSafeArrayOut(BSTR, aExtraConfigValues))
1065{
1066 if (ComSafeArrayOutIsNull(aTypes) ||
1067 ComSafeArrayOutIsNull(aRefs) ||
1068 ComSafeArrayOutIsNull(aOrigValues) ||
1069 ComSafeArrayOutIsNull(aVboxValues) ||
1070 ComSafeArrayOutIsNull(aExtraConfigValues))
1071 return E_POINTER;
1072
1073 AutoCaller autoCaller(this);
1074 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1075
1076 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1077
1078 std::list<VirtualSystemDescriptionEntry*> vsd = findByType (aType);
1079 ULONG c = (ULONG)vsd.size();
1080 com::SafeArray<VirtualSystemDescriptionType_T> sfaTypes(c);
1081 com::SafeArray<BSTR> sfaRefs(c);
1082 com::SafeArray<BSTR> sfaOrigValues(c);
1083 com::SafeArray<BSTR> sfaVboxValues(c);
1084 com::SafeArray<BSTR> sfaExtraConfigValues(c);
1085
1086 list<VirtualSystemDescriptionEntry*>::const_iterator it;
1087 size_t i = 0;
1088 for (it = vsd.begin();
1089 it != vsd.end();
1090 ++it, ++i)
1091 {
1092 const VirtualSystemDescriptionEntry *vsde = (*it);
1093
1094 sfaTypes[i] = vsde->type;
1095
1096 Bstr bstr = vsde->strRef;
1097 bstr.cloneTo(&sfaRefs[i]);
1098
1099 bstr = vsde->strOvf;
1100 bstr.cloneTo(&sfaOrigValues[i]);
1101
1102 bstr = vsde->strVbox;
1103 bstr.cloneTo(&sfaVboxValues[i]);
1104
1105 bstr = vsde->strExtraConfig;
1106 bstr.cloneTo(&sfaExtraConfigValues[i]);
1107 }
1108
1109 sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
1110 sfaRefs.detachTo(ComSafeArrayOutArg(aRefs));
1111 sfaOrigValues.detachTo(ComSafeArrayOutArg(aOrigValues));
1112 sfaVboxValues.detachTo(ComSafeArrayOutArg(aVboxValues));
1113 sfaExtraConfigValues.detachTo(ComSafeArrayOutArg(aExtraConfigValues));
1114
1115 return S_OK;
1116}
1117
1118/**
1119 * Public method implementation.
1120 * @return
1121 */
1122STDMETHODIMP VirtualSystemDescription::GetValuesByType(VirtualSystemDescriptionType_T aType,
1123 VirtualSystemDescriptionValueType_T aWhich,
1124 ComSafeArrayOut(BSTR, aValues))
1125{
1126 if (ComSafeArrayOutIsNull(aValues))
1127 return E_POINTER;
1128
1129 AutoCaller autoCaller(this);
1130 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1131
1132 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1133
1134 std::list<VirtualSystemDescriptionEntry*> vsd = findByType (aType);
1135 com::SafeArray<BSTR> sfaValues((ULONG)vsd.size());
1136
1137 list<VirtualSystemDescriptionEntry*>::const_iterator it;
1138 size_t i = 0;
1139 for (it = vsd.begin();
1140 it != vsd.end();
1141 ++it, ++i)
1142 {
1143 const VirtualSystemDescriptionEntry *vsde = (*it);
1144
1145 Bstr bstr;
1146 switch (aWhich)
1147 {
1148 case VirtualSystemDescriptionValueType_Reference: bstr = vsde->strRef; break;
1149 case VirtualSystemDescriptionValueType_Original: bstr = vsde->strOvf; break;
1150 case VirtualSystemDescriptionValueType_Auto: bstr = vsde->strVbox; break;
1151 case VirtualSystemDescriptionValueType_ExtraConfig: bstr = vsde->strExtraConfig; break;
1152 }
1153
1154 bstr.cloneTo(&sfaValues[i]);
1155 }
1156
1157 sfaValues.detachTo(ComSafeArrayOutArg(aValues));
1158
1159 return S_OK;
1160}
1161
1162/**
1163 * Public method implementation.
1164 * @return
1165 */
1166STDMETHODIMP VirtualSystemDescription::SetFinalValues(ComSafeArrayIn(BOOL, aEnabled),
1167 ComSafeArrayIn(IN_BSTR, argVboxValues),
1168 ComSafeArrayIn(IN_BSTR, argExtraConfigValues))
1169{
1170#ifndef RT_OS_WINDOWS
1171 NOREF(aEnabledSize);
1172#endif /* RT_OS_WINDOWS */
1173
1174 CheckComArgSafeArrayNotNull(aEnabled);
1175 CheckComArgSafeArrayNotNull(argVboxValues);
1176 CheckComArgSafeArrayNotNull(argExtraConfigValues);
1177
1178 AutoCaller autoCaller(this);
1179 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1180
1181 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1182
1183 com::SafeArray<BOOL> sfaEnabled(ComSafeArrayInArg(aEnabled));
1184 com::SafeArray<IN_BSTR> sfaVboxValues(ComSafeArrayInArg(argVboxValues));
1185 com::SafeArray<IN_BSTR> sfaExtraConfigValues(ComSafeArrayInArg(argExtraConfigValues));
1186
1187 if ( (sfaEnabled.size() != m->llDescriptions.size())
1188 || (sfaVboxValues.size() != m->llDescriptions.size())
1189 || (sfaExtraConfigValues.size() != m->llDescriptions.size())
1190 )
1191 return E_INVALIDARG;
1192
1193 list<VirtualSystemDescriptionEntry>::iterator it;
1194 size_t i = 0;
1195 for (it = m->llDescriptions.begin();
1196 it != m->llDescriptions.end();
1197 ++it, ++i)
1198 {
1199 VirtualSystemDescriptionEntry& vsde = *it;
1200
1201 if (sfaEnabled[i])
1202 {
1203 vsde.strVbox = sfaVboxValues[i];
1204 vsde.strExtraConfig = sfaExtraConfigValues[i];
1205 }
1206 else
1207 vsde.type = VirtualSystemDescriptionType_Ignore;
1208 }
1209
1210 return S_OK;
1211}
1212
1213/**
1214 * Public method implementation.
1215 * @return
1216 */
1217STDMETHODIMP VirtualSystemDescription::AddDescription(VirtualSystemDescriptionType_T aType,
1218 IN_BSTR aVboxValue,
1219 IN_BSTR aExtraConfigValue)
1220{
1221 AutoCaller autoCaller(this);
1222 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1223
1224 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1225
1226 addEntry(aType, "", aVboxValue, aVboxValue, 0, aExtraConfigValue);
1227
1228 return S_OK;
1229}
1230
1231/**
1232 * Internal method; adds a new description item to the member list.
1233 * @param aType Type of description for the new item.
1234 * @param strRef Reference item; only used with hard disk controllers.
1235 * @param aOrigValue Corresponding original value from OVF.
1236 * @param aAutoValue Initial configuration value (can be overridden by caller with setFinalValues).
1237 * @param ulSizeMB Weight for IProgress
1238 * @param strExtraConfig Extra configuration; meaning dependent on type.
1239 */
1240void VirtualSystemDescription::addEntry(VirtualSystemDescriptionType_T aType,
1241 const Utf8Str &strRef,
1242 const Utf8Str &aOvfValue,
1243 const Utf8Str &aVboxValue,
1244 uint32_t ulSizeMB,
1245 const Utf8Str &strExtraConfig /*= ""*/)
1246{
1247 VirtualSystemDescriptionEntry vsde;
1248 vsde.ulIndex = (uint32_t)m->llDescriptions.size(); // each entry gets an index so the client side can reference them
1249 vsde.type = aType;
1250 vsde.strRef = strRef;
1251 vsde.strOvf = aOvfValue;
1252 vsde.strVbox = aVboxValue;
1253 vsde.strExtraConfig = strExtraConfig;
1254 vsde.ulSizeMB = ulSizeMB;
1255
1256 m->llDescriptions.push_back(vsde);
1257}
1258
1259/**
1260 * Private method; returns a list of description items containing all the items from the member
1261 * description items of this virtual system that match the given type.
1262 * @param aType
1263 * @return
1264 */
1265std::list<VirtualSystemDescriptionEntry*> VirtualSystemDescription::findByType(VirtualSystemDescriptionType_T aType)
1266{
1267 std::list<VirtualSystemDescriptionEntry*> vsd;
1268
1269 list<VirtualSystemDescriptionEntry>::iterator it;
1270 for (it = m->llDescriptions.begin();
1271 it != m->llDescriptions.end();
1272 ++it)
1273 {
1274 if (it->type == aType)
1275 vsd.push_back(&(*it));
1276 }
1277
1278 return vsd;
1279}
1280
1281/**
1282 * Private method; looks thru the member hardware items for the IDE, SATA, or SCSI controller with
1283 * the given reference ID. Useful when needing the controller for a particular
1284 * virtual disk.
1285 * @param id
1286 * @return
1287 */
1288const VirtualSystemDescriptionEntry* VirtualSystemDescription::findControllerFromID(uint32_t id)
1289{
1290 Utf8Str strRef = Utf8StrFmt("%RI32", id);
1291 list<VirtualSystemDescriptionEntry>::const_iterator it;
1292 for (it = m->llDescriptions.begin();
1293 it != m->llDescriptions.end();
1294 ++it)
1295 {
1296 const VirtualSystemDescriptionEntry &d = *it;
1297 switch (d.type)
1298 {
1299 case VirtualSystemDescriptionType_HardDiskControllerIDE:
1300 case VirtualSystemDescriptionType_HardDiskControllerSATA:
1301 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
1302 if (d.strRef == strRef)
1303 return &d;
1304 break;
1305 }
1306 }
1307
1308 return NULL;
1309}
1310
1311/**
1312 * Method called from Appliance::Interpret() if the source OVF for a virtual system
1313 * contains a <vbox:Machine> element. This method then attempts to parse that and
1314 * create a MachineConfigFile instance from it which is stored in this instance data
1315 * and can then be used to create a machine.
1316 *
1317 * This must only be called once per instance.
1318 *
1319 * This rethrows all XML and logic errors from MachineConfigFile.
1320 *
1321 * @param elmMachine <vbox:Machine> element with attributes and subelements from some
1322 * DOM tree.
1323 */
1324void VirtualSystemDescription::importVboxMachineXML(const xml::ElementNode &elmMachine)
1325{
1326 settings::MachineConfigFile *pConfig = NULL;
1327
1328 Assert(m->pConfig == NULL);
1329
1330 try
1331 {
1332 pConfig = new settings::MachineConfigFile(NULL);
1333 pConfig->importMachineXML(elmMachine);
1334
1335 m->pConfig = pConfig;
1336 }
1337 catch (...)
1338 {
1339 if (pConfig)
1340 delete pConfig;
1341 throw;
1342 }
1343}
1344
1345/**
1346 * Returns the machine config created by importVboxMachineXML() or NULL if there's none.
1347 * @return
1348 */
1349const settings::MachineConfigFile* VirtualSystemDescription::getMachineConfig() const
1350{
1351 return m->pConfig;
1352}
1353
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