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