VirtualBox

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

Last change on this file since 13538 was 8155, checked in by vboxsync, 16 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.5 KB
RevLine 
[1]1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
[8155]7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
[1]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
[5999]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.
[8155]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.
[1]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>
[3007]28#include <iprt/cpputils.h>
[1]29#include <VBox/err.h>
30#include <VBox/param.h>
31
32// constructor / destructor
33/////////////////////////////////////////////////////////////////////////////
34
[2567]35DEFINE_EMPTY_CTOR_DTOR (FloppyImage)
36
[1]37HRESULT FloppyImage::FinalConstruct()
38{
39 mAccessible = FALSE;
40 return S_OK;
41}
42
43void FloppyImage::FinalRelease()
44{
[2567]45 uninit();
[1]46}
47
48// public initializer/uninitializer for internal purposes only
49/////////////////////////////////////////////////////////////////////////////
50
51/**
52 * Initializes the floppy image object.
53 *
[2567]54 * @param aParent
[1]55 * parent object
[2567]56 * @param aFilePath
[1]57 * local file system path to the image file
58 * (can be relative to the VirtualBox config dir)
[2567]59 * @param aRegistered
[1]60 * whether this object is being initialized by the VirtualBox init code
61 * because it is present in the registry
[2567]62 * @param aId
[1]63 * ID of the DVD image to assign
64 *
65 * @return COM result indicator
66 */
[2567]67HRESULT FloppyImage::init (VirtualBox *aParent, const BSTR aFilePath,
68 BOOL aRegistered, const Guid &aId)
[1]69{
[2567]70 LogFlowThisFunc (("aFilePath={%ls}, aId={%s}\n",
71 aFilePath, aId.toString().raw()));
[1]72
[2567]73 ComAssertRet (aParent && aFilePath && !!aId, E_INVALIDARG);
[1]74
[2567]75 /* Enclose the state transition NotReady->InInit->Ready */
76 AutoInitSpan autoInitSpan (this);
77 AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
[1]78
79 HRESULT rc = S_OK;
80
[2567]81 /* share the parent weakly */
82 unconst (mParent) = aParent;
[1]83
[2567]84 /* register with parent early, since uninit() will unconditionally
85 * unregister on failure */
86 mParent->addDependentChild (this);
[1]87
[2567]88 unconst (mImageFile) = aFilePath;
89 unconst (mUuid) = aId;
90
[1]91 /* get the full file name */
92 char filePathFull [RTPATH_MAX];
[2567]93 int vrc = RTPathAbsEx (mParent->homeDir(), Utf8Str (aFilePath),
[1]94 filePathFull, sizeof (filePathFull));
95 if (VBOX_FAILURE (vrc))
96 return setError (E_FAIL, tr ("Invalid image file path: '%ls' (%Vrc)"),
[2567]97 aFilePath, vrc);
[1]98
99 unconst (mImageFileFull) = filePathFull;
[2567]100 LogFlowThisFunc (("...filePathFull={%ls}\n", mImageFileFull.raw()));
[1]101
[2567]102 if (!aRegistered)
[1]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
[2567]120 /* Confirm a successful initialization when it's the case */
[1]121 if (SUCCEEDED (rc))
[2567]122 autoInitSpan.setSucceeded();
[1]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 */
131void FloppyImage::uninit()
132{
[2567]133 LogFlowThisFunc (("\n"));
[1]134
[2567]135 /* Enclose the state transition Ready->InUninit->NotReady */
136 AutoUninitSpan autoUninitSpan (this);
137 if (autoUninitSpan.uninitDone())
[1]138 return;
139
140 mParent->removeDependentChild (this);
141
[2567]142 unconst (mParent).setNull();
[1]143}
144
145// IFloppyImage properties
146/////////////////////////////////////////////////////////////////////////////
147
[2567]148STDMETHODIMP FloppyImage::COMGETTER(Id) (GUIDPARAMOUT aId)
[1]149{
[2567]150 if (!aId)
[1]151 return E_POINTER;
152
[2567]153 AutoCaller autoCaller (this);
154 CheckComRCReturnRC (autoCaller.rc());
[1]155
[2567]156 /* mUuid is constant during life time, no need to lock */
157 mUuid.cloneTo (aId);
158
[1]159 return S_OK;
160}
161
[2567]162STDMETHODIMP FloppyImage::COMGETTER(FilePath) (BSTR *aFilePath)
[1]163{
[2567]164 if (!aFilePath)
[1]165 return E_POINTER;
166
[2567]167 AutoCaller autoCaller (this);
168 CheckComRCReturnRC (autoCaller.rc());
[1]169
[8083]170 AutoReadLock alock (this);
[2567]171
172 mImageFileFull.cloneTo (aFilePath);
173
[1]174 return S_OK;
175}
176
[2567]177STDMETHODIMP FloppyImage::COMGETTER(Accessible) (BOOL *aAccessible)
[1]178{
[2567]179 if (!aAccessible)
[1]180 return E_POINTER;
181
[2567]182 AutoCaller autoCaller (this);
183 CheckComRCReturnRC (autoCaller.rc());
184
[8083]185 AutoWriteLock alock (this);
[1]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
[2567]205 *aAccessible = mAccessible;
[1]206
207 return rc;
208}
209
[2567]210STDMETHODIMP FloppyImage::COMGETTER(Size) (ULONG *aSize)
[1]211{
[2567]212 if (!aSize)
[1]213 return E_POINTER;
214
215 HRESULT rc = S_OK;
216
[2567]217 AutoCaller autoCaller (this);
218 CheckComRCReturnRC (autoCaller.rc());
[1]219
[8083]220 AutoReadLock alock (this);
[2567]221
[1]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))
[2567]227 rc = setError (E_FAIL,
228 tr ("Failed to open floppy image '%ls' (%Vrc)\n"),
229 mImageFileFull.raw(), vrc);
[1]230 else
231 {
232 uint64_t u64Size = 0;
233
234 vrc = RTFileGetSize (file, &u64Size);
235
236 if (VBOX_SUCCESS (vrc))
[2567]237 *aSize = (ULONG) u64Size;
[1]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 */
263void FloppyImage::updatePath (const char *aNewFullPath, const char *aNewPath)
264{
265 AssertReturnVoid (aNewFullPath);
266 AssertReturnVoid (aNewPath);
267
[2567]268 AutoCaller autoCaller (this);
269 AssertComRCReturnVoid (autoCaller.rc());
270
[8083]271 AutoWriteLock alock (this);
[1]272
273 unconst (mImageFileFull) = aNewFullPath;
274 unconst (mImageFile) = aNewPath;
275}
276
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use