VirtualBox

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

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

The Big Sun Rebranding Header Change

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