VirtualBox

source: vbox/trunk/src/VBox/Main/FloppyImageImpl.cpp@ 2981

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

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 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 "FloppyImageImpl.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 (FloppyImage)
35
36HRESULT FloppyImage::FinalConstruct()
37{
38 mAccessible = FALSE;
39 return S_OK;
40}
41
42void FloppyImage::FinalRelease()
43{
44 uninit();
45}
46
47// public initializer/uninitializer for internal purposes only
48/////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Initializes the floppy 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 FloppyImage::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, tr ("Invalid image file path: '%ls' (%Vrc)"),
96 aFilePath, vrc);
97
98 unconst (mImageFileFull) = filePathFull;
99 LogFlowThisFunc (("...filePathFull={%ls}\n", mImageFileFull.raw()));
100
101 if (!aRegistered)
102 {
103 /* check whether the given file exists or not */
104 RTFILE file;
105 vrc = RTFileOpen (&file, filePathFull,
106 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
107 if (VBOX_FAILURE (vrc))
108 {
109 /* here we come when the image was just opened by
110 * IVirtualBox::OpenFloppyImage(). fail in this case */
111 rc = setError (E_FAIL,
112 tr ("Could not open the floppy image '%ls' (%Vrc)"),
113 mImageFileFull.raw(), vrc);
114 }
115 else
116 RTFileClose (file);
117 }
118
119 /* Confirm a successful initialization when it's the case */
120 if (SUCCEEDED (rc))
121 autoInitSpan.setSucceeded();
122
123 return rc;
124}
125
126/**
127 * Uninitializes the instance and sets the ready flag to FALSE.
128 * Called either from FinalRelease() or by the parent when it gets destroyed.
129 */
130void FloppyImage::uninit()
131{
132 LogFlowThisFunc (("\n"));
133
134 /* Enclose the state transition Ready->InUninit->NotReady */
135 AutoUninitSpan autoUninitSpan (this);
136 if (autoUninitSpan.uninitDone())
137 return;
138
139 mParent->removeDependentChild (this);
140
141 unconst (mParent).setNull();
142}
143
144// IFloppyImage properties
145/////////////////////////////////////////////////////////////////////////////
146
147STDMETHODIMP FloppyImage::COMGETTER(Id) (GUIDPARAMOUT aId)
148{
149 if (!aId)
150 return E_POINTER;
151
152 AutoCaller autoCaller (this);
153 CheckComRCReturnRC (autoCaller.rc());
154
155 /* mUuid is constant during life time, no need to lock */
156 mUuid.cloneTo (aId);
157
158 return S_OK;
159}
160
161STDMETHODIMP FloppyImage::COMGETTER(FilePath) (BSTR *aFilePath)
162{
163 if (!aFilePath)
164 return E_POINTER;
165
166 AutoCaller autoCaller (this);
167 CheckComRCReturnRC (autoCaller.rc());
168
169 AutoReaderLock alock (this);
170
171 mImageFileFull.cloneTo (aFilePath);
172
173 return S_OK;
174}
175
176STDMETHODIMP FloppyImage::COMGETTER(Accessible) (BOOL *aAccessible)
177{
178 if (!aAccessible)
179 return E_POINTER;
180
181 AutoCaller autoCaller (this);
182 CheckComRCReturnRC (autoCaller.rc());
183
184 AutoLock alock (this);
185
186 HRESULT rc = S_OK;
187
188 /* check whether the given image file exists or not */
189 RTFILE file;
190 int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
191 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
192 if (VBOX_FAILURE (vrc))
193 {
194 Log (("FloppyImage::COMGETTER(Accessible): WARNING: '%ls' "
195 "is not accessible (%Vrc)\n", mImageFileFull.raw(), vrc));
196 mAccessible = FALSE;
197 }
198 else
199 {
200 mAccessible = TRUE;
201 RTFileClose (file);
202 }
203
204 *aAccessible = mAccessible;
205
206 return rc;
207}
208
209STDMETHODIMP FloppyImage::COMGETTER(Size) (ULONG *aSize)
210{
211 if (!aSize)
212 return E_POINTER;
213
214 HRESULT rc = S_OK;
215
216 AutoCaller autoCaller (this);
217 CheckComRCReturnRC (autoCaller.rc());
218
219 AutoReaderLock alock (this);
220
221 RTFILE file;
222 int vrc = RTFileOpen (&file, Utf8Str (mImageFileFull),
223 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
224
225 if (VBOX_FAILURE (vrc))
226 rc = setError (E_FAIL,
227 tr ("Failed to open floppy image '%ls' (%Vrc)\n"),
228 mImageFileFull.raw(), vrc);
229 else
230 {
231 uint64_t u64Size = 0;
232
233 vrc = RTFileGetSize (file, &u64Size);
234
235 if (VBOX_SUCCESS (vrc))
236 *aSize = (ULONG) u64Size;
237 else
238 rc = setError (E_FAIL,
239 tr ("Failed to determine size of floppy image '%ls' (%Vrc)\n"),
240 mImageFileFull.raw(), vrc);
241
242 RTFileClose (file);
243 }
244
245 return rc;
246}
247
248// public methods for internal purposes only
249////////////////////////////////////////////////////////////////////////////////
250
251/**
252 * Changes the stored path values of this image to reflect the new location.
253 * Intended to be called only by VirtualBox::updateSettings() if a machine's
254 * name change causes directory renaming that affects this image.
255 *
256 * @param aNewFullPath new full path to this image file
257 * @param aNewPath new path to this image file relative to the VirtualBox
258 * settings directory (when possible)
259 *
260 * @note Locks this object for writing.
261 */
262void FloppyImage::updatePath (const char *aNewFullPath, const char *aNewPath)
263{
264 AssertReturnVoid (aNewFullPath);
265 AssertReturnVoid (aNewPath);
266
267 AutoCaller autoCaller (this);
268 AssertComRCReturnVoid (autoCaller.rc());
269
270 AutoLock alock (this);
271
272 unconst (mImageFileFull) = aNewFullPath;
273 unconst (mImageFile) = aNewPath;
274}
275
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