VirtualBox

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

Last change on this file since 3007 was 3007, checked in by vboxsync, 17 years ago

Moved the template code out of cdefs.h, partly because it didn't belong there but mostly because it was at the end of the file and would screw up any attempts made by the object cache at avoid recompiling on cdefs.h changes.

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