VirtualBox

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

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

Main/OVF: isolate disk import code from other import code; clean up progress handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.1 KB
Line 
1/* $Id: ApplianceImpl.cpp 28162 2010-04-11 12:44:21Z 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 * Called from the import and export background threads to synchronize the second
567 * background disk thread's progress object with the current progress object so
568 * that the user interface sees progress correctly and that cancel signals are
569 * passed on to the second thread.
570 * @param pProgressThis Progress object of the current thread.
571 * @param pProgressAsync Progress object of asynchronous task running in background.
572 */
573void Appliance::waitForAsyncProgress(ComObjPtr<Progress> &pProgressThis,
574 ComPtr<IProgress> &pProgressAsync)
575{
576 HRESULT rc;
577
578 // now loop until the asynchronous operation completes and then report its result
579 BOOL fCompleted;
580 BOOL fCanceled;
581 ULONG currentPercent;
582 while (SUCCEEDED(pProgressAsync->COMGETTER(Completed(&fCompleted))))
583 {
584 rc = pProgressThis->COMGETTER(Canceled)(&fCanceled);
585 if (FAILED(rc)) throw rc;
586 if (fCanceled)
587 {
588 pProgressAsync->Cancel();
589 break;
590 }
591
592 rc = pProgressAsync->COMGETTER(Percent(&currentPercent));
593 if (FAILED(rc)) throw rc;
594 if (!pProgressThis.isNull())
595 pProgressThis->SetCurrentOperationProgress(currentPercent);
596 if (fCompleted)
597 break;
598
599 /* Make sure the loop is not too tight */
600 rc = pProgressAsync->WaitForCompletion(100);
601 if (FAILED(rc)) throw rc;
602 }
603 // report result of asynchronous operation
604 LONG iRc;
605 rc = pProgressAsync->COMGETTER(ResultCode)(&iRc);
606 if (FAILED(rc)) throw rc;
607
608
609 // if the thread of the progress object has an error, then
610 // retrieve the error info from there, or it'll be lost
611 if (FAILED(iRc))
612 {
613 ProgressErrorInfo info(pProgressAsync);
614 Utf8Str str(info.getText());
615 const char *pcsz = str.c_str();
616 HRESULT rc2 = setError(iRc, pcsz);
617 throw rc2;
618 }
619}
620
621void Appliance::addWarning(const char* aWarning, ...)
622{
623 va_list args;
624 va_start(args, aWarning);
625 Utf8StrFmtVA str(aWarning, args);
626 va_end(args);
627 m->llWarnings.push_back(str);
628}
629
630/**
631 * Refreshes the cDisks and ulTotalDisksMB members in the instance data.
632 * Requires that virtual system descriptions are present.
633 */
634void Appliance::disksWeight()
635{
636 m->ulTotalDisksMB = 0;
637 m->cDisks = 0;
638 // weigh the disk images according to their sizes
639 list< ComObjPtr<VirtualSystemDescription> >::const_iterator it;
640 for (it = m->virtualSystemDescriptions.begin();
641 it != m->virtualSystemDescriptions.end();
642 ++it)
643 {
644 ComObjPtr<VirtualSystemDescription> vsdescThis = (*it);
645 /* One for every hard disk of the Virtual System */
646 std::list<VirtualSystemDescriptionEntry*> avsdeHDs = vsdescThis->findByType(VirtualSystemDescriptionType_HardDiskImage);
647 std::list<VirtualSystemDescriptionEntry*>::const_iterator itH;
648 for (itH = avsdeHDs.begin();
649 itH != avsdeHDs.end();
650 ++itH)
651 {
652 const VirtualSystemDescriptionEntry *pHD = *itH;
653 m->ulTotalDisksMB += pHD->ulSizeMB;
654 ++m->cDisks;
655 }
656 }
657
658}
659
660HRESULT Appliance::setUpProgress(ComObjPtr<Progress> &pProgress,
661 const Bstr &bstrDescription,
662 SetUpProgressMode mode)
663{
664 HRESULT rc;
665
666 /* Create the progress object */
667 pProgress.createObject();
668
669 // compute the disks weight (this sets ulTotalDisksMB and cDisks in the instance data)
670 disksWeight();
671
672 ULONG cOperations;
673 ULONG ulTotalOperationsWeight;
674
675 cOperations = 1 // one for XML setup
676 + m->cDisks; // plus one per disk
677 if (m->ulTotalDisksMB)
678 {
679 m->ulWeightPerOperation = (ULONG)((double)m->ulTotalDisksMB * 1 / 100); // use 1% of the progress for the XML
680 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightPerOperation;
681 }
682 else
683 {
684 // no disks to export:
685 m->ulWeightPerOperation = 1;
686 ulTotalOperationsWeight = 1;
687 }
688
689 switch (mode)
690 {
691 case Regular:
692 break;
693
694 case ImportS3:
695 {
696 cOperations += 1 + 1; // another one for the manifest file & another one for the import
697 ulTotalOperationsWeight = m->ulTotalDisksMB;
698 if (!m->ulTotalDisksMB)
699 // no disks to export:
700 ulTotalOperationsWeight = 1;
701
702 ULONG ulImportWeight = (ULONG)((double)ulTotalOperationsWeight * 50 / 100); // use 50% for import
703 ulTotalOperationsWeight += ulImportWeight;
704
705 m->ulWeightPerOperation = ulImportWeight; /* save for using later */
706
707 ULONG ulInitWeight = (ULONG)((double)ulTotalOperationsWeight * 0.1 / 100); // use 0.1% for init
708 ulTotalOperationsWeight += ulInitWeight;
709 }
710 break;
711
712 case WriteS3:
713 {
714 cOperations += 1 + 1; // another one for the mf & another one for temporary creation
715
716 if (m->ulTotalDisksMB)
717 {
718 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)
719 ulTotalOperationsWeight = m->ulTotalDisksMB + m->ulWeightPerOperation;
720 }
721 else
722 {
723 // no disks to export:
724 ulTotalOperationsWeight = 1;
725 m->ulWeightPerOperation = 1;
726 }
727 ULONG ulOVFCreationWeight = (ULONG)((double)ulTotalOperationsWeight * 50.0 / 100.0); /* Use 50% for the creation of the OVF & the disks */
728 ulTotalOperationsWeight += ulOVFCreationWeight;
729 }
730 break;
731 }
732
733 Log(("Setting up progress object: ulTotalMB = %d, cDisks = %d, => cOperations = %d, ulTotalOperationsWeight = %d, m->ulWeightPerOperation = %d\n",
734 m->ulTotalDisksMB, m->cDisks, cOperations, ulTotalOperationsWeight, m->ulWeightPerOperation));
735
736 rc = pProgress->init(mVirtualBox, static_cast<IAppliance*>(this),
737 bstrDescription,
738 TRUE /* aCancelable */,
739 cOperations, // ULONG cOperations,
740 ulTotalOperationsWeight, // ULONG ulTotalOperationsWeight,
741 bstrDescription, // CBSTR bstrFirstOperationDescription,
742 m->ulWeightPerOperation); // ULONG ulFirstOperationWeight,
743 return rc;
744}
745
746void Appliance::parseURI(Utf8Str strUri, LocationInfo &locInfo) const
747{
748 /* Check the URI for the protocol */
749 if (strUri.startsWith("file://", Utf8Str::CaseInsensitive)) /* File based */
750 {
751 locInfo.storageType = VFSType_File;
752 strUri = strUri.substr(sizeof("file://") - 1);
753 }
754 else if (strUri.startsWith("SunCloud://", Utf8Str::CaseInsensitive)) /* Sun Cloud service */
755 {
756 locInfo.storageType = VFSType_S3;
757 strUri = strUri.substr(sizeof("SunCloud://") - 1);
758 }
759 else if (strUri.startsWith("S3://", Utf8Str::CaseInsensitive)) /* S3 service */
760 {
761 locInfo.storageType = VFSType_S3;
762 strUri = strUri.substr(sizeof("S3://") - 1);
763 }
764 else if (strUri.startsWith("webdav://", Utf8Str::CaseInsensitive)) /* webdav service */
765 throw E_NOTIMPL;
766
767 /* Not necessary on a file based URI */
768 if (locInfo.storageType != VFSType_File)
769 {
770 size_t uppos = strUri.find("@"); /* username:password combo */
771 if (uppos != Utf8Str::npos)
772 {
773 locInfo.strUsername = strUri.substr(0, uppos);
774 strUri = strUri.substr(uppos + 1);
775 size_t upos = locInfo.strUsername.find(":");
776 if (upos != Utf8Str::npos)
777 {
778 locInfo.strPassword = locInfo.strUsername.substr(upos + 1);
779 locInfo.strUsername = locInfo.strUsername.substr(0, upos);
780 }
781 }
782 size_t hpos = strUri.find("/"); /* hostname part */
783 if (hpos != Utf8Str::npos)
784 {
785 locInfo.strHostname = strUri.substr(0, hpos);
786 strUri = strUri.substr(hpos);
787 }
788 }
789
790 locInfo.strPath = strUri;
791}
792
793void Appliance::parseBucket(Utf8Str &aPath, Utf8Str &aBucket) const
794{
795 /* Buckets are S3 specific. So parse the bucket out of the file path */
796 if (!aPath.startsWith("/"))
797 throw setError(E_INVALIDARG,
798 tr("The path '%s' must start with /"), aPath.c_str());
799 size_t bpos = aPath.find("/", 1);
800 if (bpos != Utf8Str::npos)
801 {
802 aBucket = aPath.substr(1, bpos - 1); /* The bucket without any slashes */
803 aPath = aPath.substr(bpos); /* The rest of the file path */
804 }
805 /* If there is no bucket name provided reject it */
806 if (aBucket.isEmpty())
807 throw setError(E_INVALIDARG,
808 tr("You doesn't provide a bucket name in the URI '%s'"), aPath.c_str());
809}
810
811Utf8Str Appliance::manifestFileName(Utf8Str aPath) const
812{
813 /* Get the name part */
814 char *pszMfName = RTStrDup(RTPathFilename(aPath.c_str()));
815 /* Strip any extensions */
816 RTPathStripExt(pszMfName);
817 /* Path without the filename */
818 aPath.stripFilename();
819 /* Format the manifest path */
820 Utf8StrFmt strMfFile("%s/%s.mf", aPath.c_str(), pszMfName);
821 RTStrFree(pszMfName);
822 return strMfFile;
823}
824
825/**
826 *
827 * @return
828 */
829int Appliance::TaskOVF::startThread()
830{
831 int vrc = RTThreadCreate(NULL, Appliance::taskThreadImportOrExport, this,
832 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
833 "Appliance::Task");
834
835 ComAssertMsgRCRet(vrc,
836 ("Could not create OVF task thread (%Rrc)\n", vrc), E_FAIL);
837
838 return S_OK;
839}
840
841/**
842 * Thread function for the thread started in Appliance::readImpl() and Appliance::importImpl()
843 * and Appliance::writeImpl().
844 * This will in turn call Appliance::readFS() or Appliance::readS3() or Appliance::importFS()
845 * or Appliance::importS3() or Appliance::writeFS() or Appliance::writeS3().
846 *
847 * @param aThread
848 * @param pvUser
849 */
850/* static */
851DECLCALLBACK(int) Appliance::taskThreadImportOrExport(RTTHREAD /* aThread */, void *pvUser)
852{
853 std::auto_ptr<TaskOVF> task(static_cast<TaskOVF*>(pvUser));
854 AssertReturn(task.get(), VERR_GENERAL_FAILURE);
855
856 Appliance *pAppliance = task->pAppliance;
857
858 LogFlowFuncEnter();
859 LogFlowFunc(("Appliance %p\n", pAppliance));
860
861 HRESULT taskrc = S_OK;
862
863 switch (task->taskType)
864 {
865 case TaskOVF::Read:
866 if (task->locInfo.storageType == VFSType_File)
867 taskrc = pAppliance->readFS(task->locInfo);
868 else if (task->locInfo.storageType == VFSType_S3)
869 taskrc = pAppliance->readS3(task.get());
870 break;
871
872 case TaskOVF::Import:
873 if (task->locInfo.storageType == VFSType_File)
874 taskrc = pAppliance->importFS(task->locInfo, task->pProgress);
875 else if (task->locInfo.storageType == VFSType_S3)
876 taskrc = pAppliance->importS3(task.get());
877 break;
878
879 case TaskOVF::Write:
880 if (task->locInfo.storageType == VFSType_File)
881 taskrc = pAppliance->writeFS(task->locInfo, task->enFormat, task->pProgress);
882 else if (task->locInfo.storageType == VFSType_S3)
883 taskrc = pAppliance->writeS3(task.get());
884 break;
885 }
886
887 task->rc = taskrc;
888
889 if (!task->pProgress.isNull())
890 task->pProgress->notifyComplete(taskrc);
891
892 LogFlowFuncLeave();
893
894 return VINF_SUCCESS;
895}
896
897/* static */
898int Appliance::TaskOVF::updateProgress(unsigned uPercent, void *pvUser)
899{
900 Appliance::TaskOVF* pTask = *(Appliance::TaskOVF**)pvUser;
901
902 if ( pTask
903 && !pTask->pProgress.isNull())
904 {
905 BOOL fCanceled;
906 pTask->pProgress->COMGETTER(Canceled)(&fCanceled);
907 if (fCanceled)
908 return -1;
909 pTask->pProgress->SetCurrentOperationProgress(uPercent);
910 }
911 return VINF_SUCCESS;
912}
913
914////////////////////////////////////////////////////////////////////////////////
915//
916// IVirtualSystemDescription constructor / destructor
917//
918////////////////////////////////////////////////////////////////////////////////
919
920DEFINE_EMPTY_CTOR_DTOR(VirtualSystemDescription)
921
922/**
923 * COM initializer.
924 * @return
925 */
926HRESULT VirtualSystemDescription::init()
927{
928 /* Enclose the state transition NotReady->InInit->Ready */
929 AutoInitSpan autoInitSpan(this);
930 AssertReturn(autoInitSpan.isOk(), E_FAIL);
931
932 /* Initialize data */
933 m = new Data();
934 m->pConfig = NULL;
935
936 /* Confirm a successful initialization */
937 autoInitSpan.setSucceeded();
938 return S_OK;
939}
940
941/**
942* COM uninitializer.
943*/
944
945void VirtualSystemDescription::uninit()
946{
947 if (m->pConfig)
948 delete m->pConfig;
949 delete m;
950 m = NULL;
951}
952
953////////////////////////////////////////////////////////////////////////////////
954//
955// IVirtualSystemDescription public methods
956//
957////////////////////////////////////////////////////////////////////////////////
958
959/**
960 * Public method implementation.
961 * @param
962 * @return
963 */
964STDMETHODIMP VirtualSystemDescription::COMGETTER(Count)(ULONG *aCount)
965{
966 if (!aCount)
967 return E_POINTER;
968
969 AutoCaller autoCaller(this);
970 if (FAILED(autoCaller.rc())) return autoCaller.rc();
971
972 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
973
974 *aCount = (ULONG)m->llDescriptions.size();
975
976 return S_OK;
977}
978
979/**
980 * Public method implementation.
981 * @return
982 */
983STDMETHODIMP VirtualSystemDescription::GetDescription(ComSafeArrayOut(VirtualSystemDescriptionType_T, aTypes),
984 ComSafeArrayOut(BSTR, aRefs),
985 ComSafeArrayOut(BSTR, aOrigValues),
986 ComSafeArrayOut(BSTR, aVboxValues),
987 ComSafeArrayOut(BSTR, aExtraConfigValues))
988{
989 if (ComSafeArrayOutIsNull(aTypes) ||
990 ComSafeArrayOutIsNull(aRefs) ||
991 ComSafeArrayOutIsNull(aOrigValues) ||
992 ComSafeArrayOutIsNull(aVboxValues) ||
993 ComSafeArrayOutIsNull(aExtraConfigValues))
994 return E_POINTER;
995
996 AutoCaller autoCaller(this);
997 if (FAILED(autoCaller.rc())) return autoCaller.rc();
998
999 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1000
1001 ULONG c = (ULONG)m->llDescriptions.size();
1002 com::SafeArray<VirtualSystemDescriptionType_T> sfaTypes(c);
1003 com::SafeArray<BSTR> sfaRefs(c);
1004 com::SafeArray<BSTR> sfaOrigValues(c);
1005 com::SafeArray<BSTR> sfaVboxValues(c);
1006 com::SafeArray<BSTR> sfaExtraConfigValues(c);
1007
1008 list<VirtualSystemDescriptionEntry>::const_iterator it;
1009 size_t i = 0;
1010 for (it = m->llDescriptions.begin();
1011 it != m->llDescriptions.end();
1012 ++it, ++i)
1013 {
1014 const VirtualSystemDescriptionEntry &vsde = (*it);
1015
1016 sfaTypes[i] = vsde.type;
1017
1018 Bstr bstr = vsde.strRef;
1019 bstr.cloneTo(&sfaRefs[i]);
1020
1021 bstr = vsde.strOvf;
1022 bstr.cloneTo(&sfaOrigValues[i]);
1023
1024 bstr = vsde.strVbox;
1025 bstr.cloneTo(&sfaVboxValues[i]);
1026
1027 bstr = vsde.strExtraConfig;
1028 bstr.cloneTo(&sfaExtraConfigValues[i]);
1029 }
1030
1031 sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
1032 sfaRefs.detachTo(ComSafeArrayOutArg(aRefs));
1033 sfaOrigValues.detachTo(ComSafeArrayOutArg(aOrigValues));
1034 sfaVboxValues.detachTo(ComSafeArrayOutArg(aVboxValues));
1035 sfaExtraConfigValues.detachTo(ComSafeArrayOutArg(aExtraConfigValues));
1036
1037 return S_OK;
1038}
1039
1040/**
1041 * Public method implementation.
1042 * @return
1043 */
1044STDMETHODIMP VirtualSystemDescription::GetDescriptionByType(VirtualSystemDescriptionType_T aType,
1045 ComSafeArrayOut(VirtualSystemDescriptionType_T, aTypes),
1046 ComSafeArrayOut(BSTR, aRefs),
1047 ComSafeArrayOut(BSTR, aOrigValues),
1048 ComSafeArrayOut(BSTR, aVboxValues),
1049 ComSafeArrayOut(BSTR, aExtraConfigValues))
1050{
1051 if (ComSafeArrayOutIsNull(aTypes) ||
1052 ComSafeArrayOutIsNull(aRefs) ||
1053 ComSafeArrayOutIsNull(aOrigValues) ||
1054 ComSafeArrayOutIsNull(aVboxValues) ||
1055 ComSafeArrayOutIsNull(aExtraConfigValues))
1056 return E_POINTER;
1057
1058 AutoCaller autoCaller(this);
1059 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1060
1061 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1062
1063 std::list<VirtualSystemDescriptionEntry*> vsd = findByType (aType);
1064 ULONG c = (ULONG)vsd.size();
1065 com::SafeArray<VirtualSystemDescriptionType_T> sfaTypes(c);
1066 com::SafeArray<BSTR> sfaRefs(c);
1067 com::SafeArray<BSTR> sfaOrigValues(c);
1068 com::SafeArray<BSTR> sfaVboxValues(c);
1069 com::SafeArray<BSTR> sfaExtraConfigValues(c);
1070
1071 list<VirtualSystemDescriptionEntry*>::const_iterator it;
1072 size_t i = 0;
1073 for (it = vsd.begin();
1074 it != vsd.end();
1075 ++it, ++i)
1076 {
1077 const VirtualSystemDescriptionEntry *vsde = (*it);
1078
1079 sfaTypes[i] = vsde->type;
1080
1081 Bstr bstr = vsde->strRef;
1082 bstr.cloneTo(&sfaRefs[i]);
1083
1084 bstr = vsde->strOvf;
1085 bstr.cloneTo(&sfaOrigValues[i]);
1086
1087 bstr = vsde->strVbox;
1088 bstr.cloneTo(&sfaVboxValues[i]);
1089
1090 bstr = vsde->strExtraConfig;
1091 bstr.cloneTo(&sfaExtraConfigValues[i]);
1092 }
1093
1094 sfaTypes.detachTo(ComSafeArrayOutArg(aTypes));
1095 sfaRefs.detachTo(ComSafeArrayOutArg(aRefs));
1096 sfaOrigValues.detachTo(ComSafeArrayOutArg(aOrigValues));
1097 sfaVboxValues.detachTo(ComSafeArrayOutArg(aVboxValues));
1098 sfaExtraConfigValues.detachTo(ComSafeArrayOutArg(aExtraConfigValues));
1099
1100 return S_OK;
1101}
1102
1103/**
1104 * Public method implementation.
1105 * @return
1106 */
1107STDMETHODIMP VirtualSystemDescription::GetValuesByType(VirtualSystemDescriptionType_T aType,
1108 VirtualSystemDescriptionValueType_T aWhich,
1109 ComSafeArrayOut(BSTR, aValues))
1110{
1111 if (ComSafeArrayOutIsNull(aValues))
1112 return E_POINTER;
1113
1114 AutoCaller autoCaller(this);
1115 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1116
1117 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1118
1119 std::list<VirtualSystemDescriptionEntry*> vsd = findByType (aType);
1120 com::SafeArray<BSTR> sfaValues((ULONG)vsd.size());
1121
1122 list<VirtualSystemDescriptionEntry*>::const_iterator it;
1123 size_t i = 0;
1124 for (it = vsd.begin();
1125 it != vsd.end();
1126 ++it, ++i)
1127 {
1128 const VirtualSystemDescriptionEntry *vsde = (*it);
1129
1130 Bstr bstr;
1131 switch (aWhich)
1132 {
1133 case VirtualSystemDescriptionValueType_Reference: bstr = vsde->strRef; break;
1134 case VirtualSystemDescriptionValueType_Original: bstr = vsde->strOvf; break;
1135 case VirtualSystemDescriptionValueType_Auto: bstr = vsde->strVbox; break;
1136 case VirtualSystemDescriptionValueType_ExtraConfig: bstr = vsde->strExtraConfig; break;
1137 }
1138
1139 bstr.cloneTo(&sfaValues[i]);
1140 }
1141
1142 sfaValues.detachTo(ComSafeArrayOutArg(aValues));
1143
1144 return S_OK;
1145}
1146
1147/**
1148 * Public method implementation.
1149 * @return
1150 */
1151STDMETHODIMP VirtualSystemDescription::SetFinalValues(ComSafeArrayIn(BOOL, aEnabled),
1152 ComSafeArrayIn(IN_BSTR, argVboxValues),
1153 ComSafeArrayIn(IN_BSTR, argExtraConfigValues))
1154{
1155#ifndef RT_OS_WINDOWS
1156 NOREF(aEnabledSize);
1157#endif /* RT_OS_WINDOWS */
1158
1159 CheckComArgSafeArrayNotNull(aEnabled);
1160 CheckComArgSafeArrayNotNull(argVboxValues);
1161 CheckComArgSafeArrayNotNull(argExtraConfigValues);
1162
1163 AutoCaller autoCaller(this);
1164 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1165
1166 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1167
1168 com::SafeArray<BOOL> sfaEnabled(ComSafeArrayInArg(aEnabled));
1169 com::SafeArray<IN_BSTR> sfaVboxValues(ComSafeArrayInArg(argVboxValues));
1170 com::SafeArray<IN_BSTR> sfaExtraConfigValues(ComSafeArrayInArg(argExtraConfigValues));
1171
1172 if ( (sfaEnabled.size() != m->llDescriptions.size())
1173 || (sfaVboxValues.size() != m->llDescriptions.size())
1174 || (sfaExtraConfigValues.size() != m->llDescriptions.size())
1175 )
1176 return E_INVALIDARG;
1177
1178 list<VirtualSystemDescriptionEntry>::iterator it;
1179 size_t i = 0;
1180 for (it = m->llDescriptions.begin();
1181 it != m->llDescriptions.end();
1182 ++it, ++i)
1183 {
1184 VirtualSystemDescriptionEntry& vsde = *it;
1185
1186 if (sfaEnabled[i])
1187 {
1188 vsde.strVbox = sfaVboxValues[i];
1189 vsde.strExtraConfig = sfaExtraConfigValues[i];
1190 }
1191 else
1192 vsde.type = VirtualSystemDescriptionType_Ignore;
1193 }
1194
1195 return S_OK;
1196}
1197
1198/**
1199 * Public method implementation.
1200 * @return
1201 */
1202STDMETHODIMP VirtualSystemDescription::AddDescription(VirtualSystemDescriptionType_T aType,
1203 IN_BSTR aVboxValue,
1204 IN_BSTR aExtraConfigValue)
1205{
1206 AutoCaller autoCaller(this);
1207 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1208
1209 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1210
1211 addEntry(aType, "", aVboxValue, aVboxValue, 0, aExtraConfigValue);
1212
1213 return S_OK;
1214}
1215
1216/**
1217 * Internal method; adds a new description item to the member list.
1218 * @param aType Type of description for the new item.
1219 * @param strRef Reference item; only used with hard disk controllers.
1220 * @param aOrigValue Corresponding original value from OVF.
1221 * @param aAutoValue Initial configuration value (can be overridden by caller with setFinalValues).
1222 * @param ulSizeMB Weight for IProgress
1223 * @param strExtraConfig Extra configuration; meaning dependent on type.
1224 */
1225void VirtualSystemDescription::addEntry(VirtualSystemDescriptionType_T aType,
1226 const Utf8Str &strRef,
1227 const Utf8Str &aOrigValue,
1228 const Utf8Str &aAutoValue,
1229 uint32_t ulSizeMB,
1230 const Utf8Str &strExtraConfig /*= ""*/)
1231{
1232 VirtualSystemDescriptionEntry vsde;
1233 vsde.ulIndex = (uint32_t)m->llDescriptions.size(); // each entry gets an index so the client side can reference them
1234 vsde.type = aType;
1235 vsde.strRef = strRef;
1236 vsde.strOvf = aOrigValue;
1237 vsde.strVbox = aAutoValue;
1238 vsde.strExtraConfig = strExtraConfig;
1239 vsde.ulSizeMB = ulSizeMB;
1240
1241 m->llDescriptions.push_back(vsde);
1242}
1243
1244/**
1245 * Private method; returns a list of description items containing all the items from the member
1246 * description items of this virtual system that match the given type.
1247 * @param aType
1248 * @return
1249 */
1250std::list<VirtualSystemDescriptionEntry*> VirtualSystemDescription::findByType(VirtualSystemDescriptionType_T aType)
1251{
1252 std::list<VirtualSystemDescriptionEntry*> vsd;
1253
1254 list<VirtualSystemDescriptionEntry>::iterator it;
1255 for (it = m->llDescriptions.begin();
1256 it != m->llDescriptions.end();
1257 ++it)
1258 {
1259 if (it->type == aType)
1260 vsd.push_back(&(*it));
1261 }
1262
1263 return vsd;
1264}
1265
1266/**
1267 * Private method; looks thru the member hardware items for the IDE, SATA, or SCSI controller with
1268 * the given reference ID. Useful when needing the controller for a particular
1269 * virtual disk.
1270 * @param id
1271 * @return
1272 */
1273const VirtualSystemDescriptionEntry* VirtualSystemDescription::findControllerFromID(uint32_t id)
1274{
1275 Utf8Str strRef = Utf8StrFmt("%RI32", id);
1276 list<VirtualSystemDescriptionEntry>::const_iterator it;
1277 for (it = m->llDescriptions.begin();
1278 it != m->llDescriptions.end();
1279 ++it)
1280 {
1281 const VirtualSystemDescriptionEntry &d = *it;
1282 switch (d.type)
1283 {
1284 case VirtualSystemDescriptionType_HardDiskControllerIDE:
1285 case VirtualSystemDescriptionType_HardDiskControllerSATA:
1286 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
1287 if (d.strRef == strRef)
1288 return &d;
1289 break;
1290 }
1291 }
1292
1293 return NULL;
1294}
1295
1296/**
1297 * Method called from Appliance::Interpret() if the source OVF for a virtual system
1298 * contains a <vbox:Machine> element. This method then attempts to parse that and
1299 * create a MachineConfigFile instance from it which is stored in this instance data
1300 * and can then be used to create a machine.
1301 *
1302 * This must only be called once per instance.
1303 *
1304 * This rethrows all XML and logic errors from MachineConfigFile.
1305 *
1306 * @param elmMachine <vbox:Machine> element with attributes and subelements from some
1307 * DOM tree.
1308 */
1309void VirtualSystemDescription::importVboxMachineXML(const xml::ElementNode &elmMachine)
1310{
1311 settings::MachineConfigFile *pConfig = NULL;
1312
1313 Assert(m->pConfig == NULL);
1314
1315 try
1316 {
1317 pConfig = new settings::MachineConfigFile(NULL);
1318 pConfig->importMachineXML(elmMachine);
1319
1320 m->pConfig = pConfig;
1321 }
1322 catch (...)
1323 {
1324 if (pConfig)
1325 delete pConfig;
1326 throw;
1327 }
1328}
1329
1330/**
1331 * Returns the machine config created by importVboxMachineXML() or NULL if there's none.
1332 * @return
1333 */
1334const settings::MachineConfigFile* VirtualSystemDescription::getMachineConfig() const
1335{
1336 return m->pConfig;
1337}
1338
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