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 "FloppyImageImpl.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 |
|
---|
35 | DEFINE_EMPTY_CTOR_DTOR (FloppyImage)
|
---|
36 |
|
---|
37 | HRESULT FloppyImage::FinalConstruct()
|
---|
38 | {
|
---|
39 | mAccessible = FALSE;
|
---|
40 | return S_OK;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void FloppyImage::FinalRelease()
|
---|
44 | {
|
---|
45 | uninit();
|
---|
46 | }
|
---|
47 |
|
---|
48 | // public initializer/uninitializer for internal purposes only
|
---|
49 | /////////////////////////////////////////////////////////////////////////////
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Initializes the floppy 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 | */
|
---|
67 | HRESULT FloppyImage::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, 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::OpenFloppyImage(). fail in this case */
|
---|
112 | rc = setError (E_FAIL,
|
---|
113 | tr ("Could not open the floppy 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 | */
|
---|
131 | void FloppyImage::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 | mParent->removeDependentChild (this);
|
---|
141 |
|
---|
142 | unconst (mParent).setNull();
|
---|
143 | }
|
---|
144 |
|
---|
145 | // IFloppyImage properties
|
---|
146 | /////////////////////////////////////////////////////////////////////////////
|
---|
147 |
|
---|
148 | STDMETHODIMP FloppyImage::COMGETTER(Id) (GUIDPARAMOUT aId)
|
---|
149 | {
|
---|
150 | if (!aId)
|
---|
151 | return E_POINTER;
|
---|
152 |
|
---|
153 | AutoCaller autoCaller (this);
|
---|
154 | CheckComRCReturnRC (autoCaller.rc());
|
---|
155 |
|
---|
156 | /* mUuid is constant during life time, no need to lock */
|
---|
157 | mUuid.cloneTo (aId);
|
---|
158 |
|
---|
159 | return S_OK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | STDMETHODIMP FloppyImage::COMGETTER(FilePath) (BSTR *aFilePath)
|
---|
163 | {
|
---|
164 | if (!aFilePath)
|
---|
165 | return E_POINTER;
|
---|
166 |
|
---|
167 | AutoCaller autoCaller (this);
|
---|
168 | CheckComRCReturnRC (autoCaller.rc());
|
---|
169 |
|
---|
170 | AutoReadLock alock (this);
|
---|
171 |
|
---|
172 | mImageFileFull.cloneTo (aFilePath);
|
---|
173 |
|
---|
174 | return S_OK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | STDMETHODIMP FloppyImage::COMGETTER(Accessible) (BOOL *aAccessible)
|
---|
178 | {
|
---|
179 | if (!aAccessible)
|
---|
180 | return E_POINTER;
|
---|
181 |
|
---|
182 | AutoCaller autoCaller (this);
|
---|
183 | CheckComRCReturnRC (autoCaller.rc());
|
---|
184 |
|
---|
185 | AutoWriteLock alock (this);
|
---|
186 |
|
---|
187 | HRESULT rc = S_OK;
|
---|
188 |
|
---|
189 | /* check whether the given image file exists or not */
|
---|
190 | RTFILE file;
|
---|
191 | int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
|
---|
192 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
193 | if (VBOX_FAILURE (vrc))
|
---|
194 | {
|
---|
195 | Log (("FloppyImage::COMGETTER(Accessible): WARNING: '%ls' "
|
---|
196 | "is not accessible (%Vrc)\n", mImageFileFull.raw(), vrc));
|
---|
197 | mAccessible = FALSE;
|
---|
198 | }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | mAccessible = TRUE;
|
---|
202 | RTFileClose (file);
|
---|
203 | }
|
---|
204 |
|
---|
205 | *aAccessible = mAccessible;
|
---|
206 |
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 | STDMETHODIMP FloppyImage::COMGETTER(Size) (ULONG *aSize)
|
---|
211 | {
|
---|
212 | if (!aSize)
|
---|
213 | return E_POINTER;
|
---|
214 |
|
---|
215 | HRESULT rc = S_OK;
|
---|
216 |
|
---|
217 | AutoCaller autoCaller (this);
|
---|
218 | CheckComRCReturnRC (autoCaller.rc());
|
---|
219 |
|
---|
220 | AutoReadLock alock (this);
|
---|
221 |
|
---|
222 | RTFILE file;
|
---|
223 | int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
|
---|
224 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
225 |
|
---|
226 | if (VBOX_FAILURE (vrc))
|
---|
227 | rc = setError (E_FAIL,
|
---|
228 | tr ("Failed to open floppy image '%ls' (%Vrc)\n"),
|
---|
229 | mImageFileFull.raw(), vrc);
|
---|
230 | else
|
---|
231 | {
|
---|
232 | uint64_t u64Size = 0;
|
---|
233 |
|
---|
234 | vrc = RTFileGetSize (file, &u64Size);
|
---|
235 |
|
---|
236 | if (VBOX_SUCCESS (vrc))
|
---|
237 | *aSize = (ULONG) u64Size;
|
---|
238 | else
|
---|
239 | rc = setError (E_FAIL,
|
---|
240 | tr ("Failed to determine size of floppy image '%ls' (%Vrc)\n"),
|
---|
241 | mImageFileFull.raw(), vrc);
|
---|
242 |
|
---|
243 | RTFileClose (file);
|
---|
244 | }
|
---|
245 |
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // public methods for internal purposes only
|
---|
250 | ////////////////////////////////////////////////////////////////////////////////
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Changes the stored path values of this image to reflect the new location.
|
---|
254 | * Intended to be called only by VirtualBox::updateSettings() if a machine's
|
---|
255 | * name change causes directory renaming that affects this image.
|
---|
256 | *
|
---|
257 | * @param aNewFullPath new full path to this image file
|
---|
258 | * @param aNewPath new path to this image file relative to the VirtualBox
|
---|
259 | * settings directory (when possible)
|
---|
260 | *
|
---|
261 | * @note Locks this object for writing.
|
---|
262 | */
|
---|
263 | void FloppyImage::updatePath (const char *aNewFullPath, const char *aNewPath)
|
---|
264 | {
|
---|
265 | AssertReturnVoid (aNewFullPath);
|
---|
266 | AssertReturnVoid (aNewPath);
|
---|
267 |
|
---|
268 | AutoCaller autoCaller (this);
|
---|
269 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
270 |
|
---|
271 | AutoWriteLock alock (this);
|
---|
272 |
|
---|
273 | unconst (mImageFileFull) = aNewFullPath;
|
---|
274 | unconst (mImageFile) = aNewPath;
|
---|
275 | }
|
---|
276 |
|
---|