VirtualBox

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

Last change on this file since 30681 was 30681, checked in by vboxsync, 14 years ago

Main: COM header cleanup (remove obscure and unused templates), second try

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