VirtualBox

source: vbox/trunk/src/VBox/Devices/Serial/DrvRawFile.cpp@ 25966

Last change on this file since 25966 was 25966, checked in by vboxsync, 15 years ago

PDMIBASE refactoring; use UUID as interface IDs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: DrvRawFile.cpp 25966 2010-01-22 11:15:43Z vboxsync $ */
2/** @file
3 * VBox stream drivers - Raw file output.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DEFAULT
27#include <VBox/pdmdrv.h>
28#include <iprt/assert.h>
29#include <iprt/file.h>
30#include <iprt/mem.h>
31#include <iprt/semaphore.h>
32#include <iprt/stream.h>
33#include <iprt/string.h>
34#include <iprt/uuid.h>
35
36#include "Builtins.h"
37
38
39/*******************************************************************************
40* Defined Constants And Macros *
41*******************************************************************************/
42/** Converts a pointer to DRVRAWFILE::IMedia to a PDRVRAWFILE. */
43#define PDMISTREAM_2_DRVRAWFILE(pInterface) ( (PDRVRAWFILE)((uintptr_t)pInterface - RT_OFFSETOF(DRVRAWFILE, IStream)) )
44
45/** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
46#define PDMIBASE_2_DRVINS(pInterface) ( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * Raw file output driver instance data.
54 *
55 * @implements PDMISTREAM
56 */
57typedef struct DRVRAWFILE
58{
59 /** The stream interface. */
60 PDMISTREAM IStream;
61 /** Pointer to the driver instance. */
62 PPDMDRVINS pDrvIns;
63 /** Pointer to the file name. (Freed by MM) */
64 char *pszLocation;
65 /** Flag whether VirtualBox represents the server or client side. */
66 RTFILE OutputFile;
67} DRVRAWFILE, *PDRVRAWFILE;
68
69
70
71
72/** @copydoc PDMISTREAM::pfnWrite */
73static DECLCALLBACK(int) drvRawFileWrite(PPDMISTREAM pInterface, const void *pvBuf, size_t *pcbWrite)
74{
75 int rc = VINF_SUCCESS;
76 PDRVRAWFILE pThis = PDMISTREAM_2_DRVRAWFILE(pInterface);
77 LogFlow(("%s: pvBuf=%p *pcbWrite=%#x (%s)\n", __FUNCTION__, pvBuf, *pcbWrite, pThis->pszLocation));
78
79 Assert(pvBuf);
80 if (pThis->OutputFile != NIL_RTFILE)
81 {
82 size_t cbWritten;
83 rc = RTFileWrite(pThis->OutputFile, pvBuf, *pcbWrite, &cbWritten);
84#if 0
85 /* don't flush here, takes too long and we will loose characters */
86 if (RT_SUCCESS(rc))
87 RTFileFlush(pThis->OutputFile);
88#endif
89 *pcbWrite = cbWritten;
90 }
91
92 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
93 return rc;
94}
95
96
97/**
98 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
99 */
100static DECLCALLBACK(void *) drvRawFileQueryInterface(PPDMIBASE pInterface, const char *pszIID)
101{
102 PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
103 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
104
105 if (RTUuidCompare2Strs(pszIID, PDMIBASE_IID) == 0)
106 return &pDrvIns->IBase;
107 if (RTUuidCompare2Strs(pszIID, PDMINTERFACE_STREAM) == 0)
108 return &pThis->IStream;
109 return NULL;
110}
111
112
113/**
114 * Construct a raw output stream driver instance.
115 *
116 * @copydoc FNPDMDRVCONSTRUCT
117 */
118static DECLCALLBACK(int) drvRawFileConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
119{
120 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
121
122 /*
123 * Init the static parts.
124 */
125 pThis->pDrvIns = pDrvIns;
126 pThis->pszLocation = NULL;
127 pThis->OutputFile = NIL_RTFILE;
128 /* IBase */
129 pDrvIns->IBase.pfnQueryInterface = drvRawFileQueryInterface;
130 /* IStream */
131 pThis->IStream.pfnWrite = drvRawFileWrite;
132
133 /*
134 * Read the configuration.
135 */
136 if (!CFGMR3AreValuesValid(pCfgHandle, "Location\0"))
137 AssertFailedReturn(VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
138
139 int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Location", &pThis->pszLocation);
140 if (RT_FAILURE(rc))
141 AssertMsgFailedReturn(("Configuration error: query \"Location\" resulted in %Rrc.\n", rc), rc);
142
143 /*
144 * Open the raw file.
145 */
146 rc = RTFileOpen(&pThis->OutputFile, pThis->pszLocation, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
147 if (RT_FAILURE(rc))
148 {
149 LogRel(("RawFile%d: CreateFile failed rc=%Rrc\n", pDrvIns->iInstance));
150 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("RawFile#%d failed to create the raw output file %s"), pDrvIns->iInstance, pThis->pszLocation);
151 }
152
153 LogFlow(("drvRawFileConstruct: location %s\n", pThis->pszLocation));
154 LogRel(("RawFile#%u: location %s\n", pDrvIns->iInstance, pThis->pszLocation));
155 return VINF_SUCCESS;
156}
157
158
159/**
160 * Destruct a raw output stream driver instance.
161 *
162 * Most VM resources are freed by the VM. This callback is provided so that
163 * any non-VM resources can be freed correctly.
164 *
165 * @param pDrvIns The driver instance data.
166 */
167static DECLCALLBACK(void) drvRawFileDestruct(PPDMDRVINS pDrvIns)
168{
169 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
170 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
171
172 if (pThis->pszLocation)
173 MMR3HeapFree(pThis->pszLocation);
174
175 if (pThis->OutputFile != NIL_RTFILE)
176 {
177 RTFileClose(pThis->OutputFile);
178 pThis->OutputFile = NIL_RTFILE;
179 }
180}
181
182
183/**
184 * Power off a raw output stream driver instance.
185 *
186 * This does most of the destruction work, to avoid ordering dependencies.
187 *
188 * @param pDrvIns The driver instance data.
189 */
190static DECLCALLBACK(void) drvRawFilePowerOff(PPDMDRVINS pDrvIns)
191{
192 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
193 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
194
195 if (pThis->OutputFile != NIL_RTFILE)
196 {
197 RTFileClose(pThis->OutputFile);
198 pThis->OutputFile = NIL_RTFILE;
199 }
200}
201
202
203/**
204 * Raw file driver registration record.
205 */
206const PDMDRVREG g_DrvRawFile =
207{
208 /* u32Version */
209 PDM_DRVREG_VERSION,
210 /* szDriverName */
211 "RawFile",
212 /* szRCMod */
213 "",
214 /* szR0Mod */
215 "",
216 /* pszDescription */
217 "RawFile stream driver.",
218 /* fFlags */
219 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
220 /* fClass. */
221 PDM_DRVREG_CLASS_STREAM,
222 /* cMaxInstances */
223 ~0,
224 /* cbInstance */
225 sizeof(DRVRAWFILE),
226 /* pfnConstruct */
227 drvRawFileConstruct,
228 /* pfnDestruct */
229 drvRawFileDestruct,
230 /* pfnRelocate */
231 NULL,
232 /* pfnIOCtl */
233 NULL,
234 /* pfnPowerOn */
235 NULL,
236 /* pfnReset */
237 NULL,
238 /* pfnSuspend */
239 NULL,
240 /* pfnResume */
241 NULL,
242 /* pfnAttach */
243 NULL,
244 /* pfnDetach */
245 NULL,
246 /* pfnPowerOff */
247 drvRawFilePowerOff,
248 /* pfnSoftReset */
249 NULL,
250 /* u32EndVersion */
251 PDM_DRVREG_VERSION
252};
253
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette