VirtualBox

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

Last change on this file since 28456 was 28165, checked in by vboxsync, 15 years ago

Main/OVF: import vbox:machine with disks, the rest. Todo some bugfixing.

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