VirtualBox

source: vbox/trunk/src/VBox/Main/DVDImageImpl.cpp@ 2603

Last change on this file since 2603 was 2567, checked in by vboxsync, 18 years ago

Main & All Frontends: replaced the IGuestOSType IMachine::OSType property with the wstring IMachine::OSTypeId property (+ converted IGuest and IGuestOSType to VirtualBoxBaseNEXT semantics).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "DVDImageImpl.h"
23#include "VirtualBoxImpl.h"
24#include "Logging.h"
25
26#include <iprt/file.h>
27#include <iprt/path.h>
28#include <VBox/err.h>
29#include <VBox/param.h>
30
31// constructor / destructor
32/////////////////////////////////////////////////////////////////////////////
33
34DEFINE_EMPTY_CTOR_DTOR (DVDImage)
35
36HRESULT DVDImage::FinalConstruct()
37{
38 mAccessible = FALSE;
39 return S_OK;
40}
41
42void DVDImage::FinalRelease()
43{
44 uninit();
45}
46
47// public initializer/uninitializer for internal purposes only
48/////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Initializes the DVD image object.
52 *
53 * @param aParent
54 * parent object
55 * @param aFilePath
56 * local file system path to the image file
57 * (can be relative to the VirtualBox config dir)
58 * @param aRegistered
59 * whether this object is being initialized by the VirtualBox init code
60 * because it is present in the registry
61 * @param aId
62 * ID of the DVD image to assign
63 *
64 * @return COM result indicator
65 */
66HRESULT DVDImage::init (VirtualBox *aParent, const BSTR aFilePath,
67 BOOL aRegistered, const Guid &aId)
68{
69 LogFlowThisFunc (("aFilePath={%ls}, aId={%s}\n",
70 aFilePath, aId.toString().raw()));
71
72 ComAssertRet (aParent && aFilePath && !!aId, E_INVALIDARG);
73
74 /* Enclose the state transition NotReady->InInit->Ready */
75 AutoInitSpan autoInitSpan (this);
76 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
77
78 HRESULT rc = S_OK;
79
80 /* share the parent weakly */
81 unconst (mParent) = aParent;
82
83 /* register with parent early, since uninit() will unconditionally
84 * unregister on failure */
85 mParent->addDependentChild (this);
86
87 unconst (mImageFile) = aFilePath;
88 unconst (mUuid) = aId;
89
90 /* get the full file name */
91 char filePathFull [RTPATH_MAX];
92 int vrc = RTPathAbsEx (mParent->homeDir(), Utf8Str (aFilePath),
93 filePathFull, sizeof (filePathFull));
94 if (VBOX_FAILURE (vrc))
95 return setError (E_FAIL,
96 tr ("Invalid image file path: '%ls' (%Vrc)"),
97 aFilePath, vrc);
98
99 unconst (mImageFileFull) = filePathFull;
100 LogFlowThisFunc (("...filePathFull={%ls}\n", mImageFileFull.raw()));
101
102 if (!aRegistered)
103 {
104 /* check whether the given file exists or not */
105 RTFILE file;
106 vrc = RTFileOpen (&file, filePathFull,
107 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
108 if (VBOX_FAILURE (vrc))
109 {
110 /* here we come when the image was just opened by
111 * IVirtualBox::OpenDVDImage(). fail in this case */
112 rc = setError (E_FAIL,
113 tr ("Could not open the CD/DVD image '%ls' (%Vrc)"),
114 mImageFileFull.raw(), vrc);
115 }
116 else
117 RTFileClose (file);
118 }
119
120 /* Confirm a successful initialization when it's the case */
121 if (SUCCEEDED (rc))
122 autoInitSpan.setSucceeded();
123
124 return rc;
125}
126
127/**
128 * Uninitializes the instance and sets the ready flag to FALSE.
129 * Called either from FinalRelease() or by the parent when it gets destroyed.
130 */
131void DVDImage::uninit()
132{
133 LogFlowThisFunc (("\n"));
134
135 /* Enclose the state transition Ready->InUninit->NotReady */
136 AutoUninitSpan autoUninitSpan (this);
137 if (autoUninitSpan.uninitDone())
138 return;
139
140 LogFlowThisFunc (("initFailed()=%RTbool\n", autoUninitSpan.initFailed()));
141
142 mParent->removeDependentChild (this);
143
144 unconst (mParent).setNull();
145}
146
147// IDVDImage properties
148/////////////////////////////////////////////////////////////////////////////
149
150STDMETHODIMP DVDImage::COMGETTER(Id) (GUIDPARAMOUT aId)
151{
152 if (!aId)
153 return E_POINTER;
154
155 AutoCaller autoCaller (this);
156 CheckComRCReturnRC (autoCaller.rc());
157
158 /* mUuid is constant during life time, no need to lock */
159 mUuid.cloneTo (aId);
160
161 return S_OK;
162}
163
164STDMETHODIMP DVDImage::COMGETTER(FilePath) (BSTR *aFilePath)
165{
166 if (!aFilePath)
167 return E_POINTER;
168
169 AutoCaller autoCaller (this);
170 CheckComRCReturnRC (autoCaller.rc());
171
172 AutoReaderLock alock (this);
173
174 mImageFileFull.cloneTo (aFilePath);
175
176 return S_OK;
177}
178
179STDMETHODIMP DVDImage::COMGETTER(Accessible) (BOOL *aAccessible)
180{
181 if (!aAccessible)
182 return E_POINTER;
183
184 AutoCaller autoCaller (this);
185 CheckComRCReturnRC (autoCaller.rc());
186
187 AutoLock alock (this);
188
189 HRESULT rc = S_OK;
190
191 /* check whether the given image file exists or not */
192 RTFILE file;
193 int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
194 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
195 if (VBOX_FAILURE (vrc))
196 {
197 Log (("DVDImage::COMGETTER(Accessible): WARNING: '%ls' "
198 "is not accessible (%Vrc)\n", mImageFileFull.raw(), vrc));
199 mAccessible = FALSE;
200 }
201 else
202 {
203 mAccessible = TRUE;
204 RTFileClose (file);
205 }
206
207 *aAccessible = mAccessible;
208
209 return rc;
210}
211
212STDMETHODIMP DVDImage::COMGETTER(Size) (ULONG64 *aSize)
213{
214 if (!aSize)
215 return E_POINTER;
216
217 HRESULT rc = S_OK;
218
219 AutoCaller autoCaller (this);
220 CheckComRCReturnRC (autoCaller.rc());
221
222 AutoReaderLock alock (this);
223
224 RTFILE file;
225 int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
226 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
227
228 if (VBOX_FAILURE (vrc))
229 rc = setError (E_FAIL, tr("Failed to open ISO image '%ls' (%Vrc)\n"),
230 mImageFileFull.raw(), vrc);
231 else
232 {
233 AssertCompile (sizeof (uint64_t) == sizeof (ULONG64));
234
235 uint64_t u64Size = 0;
236
237 vrc = RTFileGetSize (file, &u64Size);
238
239 if (VBOX_SUCCESS (vrc))
240 *aSize = u64Size;
241 else
242 rc = setError (E_FAIL,
243 tr ("Failed to determine size of ISO image '%ls' (%Vrc)\n"),
244 mImageFileFull.raw(), vrc);
245
246 RTFileClose (file);
247 }
248
249 return rc;
250}
251
252// public methods for internal purposes only
253////////////////////////////////////////////////////////////////////////////////
254
255/**
256 * Changes the stored path values of this image to reflect the new location.
257 * Intended to be called only by VirtualBox::updateSettings() if a machine's
258 * name change causes directory renaming that affects this image.
259 *
260 * @param aNewFullPath new full path to this image file
261 * @param aNewPath new path to this image file relative to the VirtualBox
262 * settings directory (when possible)
263 *
264 * @note Locks this object for writing.
265 */
266void DVDImage::updatePath (const char *aNewFullPath, const char *aNewPath)
267{
268 AssertReturnVoid (aNewFullPath);
269 AssertReturnVoid (aNewPath);
270
271 AutoCaller autoCaller (this);
272 AssertComRCReturnVoid (autoCaller.rc());
273
274 AutoLock alock (this);
275
276 unconst (mImageFileFull) = aNewFullPath;
277 unconst (mImageFile) = aNewPath;
278}
279
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