VirtualBox

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

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

OSE headers fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: DrvRawFile.cpp 22653 2009-09-01 12:02:32Z vboxsync $ */
2/** @file
3 * VBox stream drivers - Raw file output.
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
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/stream.h>
31#include <iprt/alloc.h>
32#include <iprt/string.h>
33#include <iprt/semaphore.h>
34
35#include "Builtins.h"
36
37
38/*******************************************************************************
39* Defined Constants And Macros *
40*******************************************************************************/
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* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * Raw file output driver instance data.
53 */
54typedef struct DRVRAWFILE
55{
56 /** The stream interface. */
57 PDMISTREAM IStream;
58 /** Pointer to the driver instance. */
59 PPDMDRVINS pDrvIns;
60 /** Pointer to the file name. (Freed by MM) */
61 char *pszLocation;
62 /** Flag whether VirtualBox represents the server or client side. */
63 RTFILE OutputFile;
64} DRVRAWFILE, *PDRVRAWFILE;
65
66
67/*******************************************************************************
68* Internal Functions *
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 (RT_SUCCESS(rc))
85 RTFileFlush(pThis->OutputFile);
86 *pcbWrite = cbWritten;
87 }
88
89 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
90 return rc;
91}
92
93
94/**
95 * Queries an interface to the driver.
96 *
97 * @returns Pointer to interface.
98 * @returns NULL if the interface was not supported by the driver.
99 * @param pInterface Pointer to this interface structure.
100 * @param enmInterface The requested interface identification.
101 * @thread Any thread.
102 */
103static DECLCALLBACK(void *) drvRawFileQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
104{
105 PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
106 PDRVRAWFILE pDrv = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
107 switch (enmInterface)
108 {
109 case PDMINTERFACE_BASE:
110 return &pDrvIns->IBase;
111 case PDMINTERFACE_STREAM:
112 return &pDrv->IStream;
113 default:
114 return NULL;
115 }
116}
117
118
119/**
120 * Construct a raw output stream driver instance.
121 *
122 * @copydoc FNPDMDRVCONSTRUCT
123 */
124static DECLCALLBACK(int) drvRawFileConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
125{
126 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
127
128 /*
129 * Init the static parts.
130 */
131 pThis->pDrvIns = pDrvIns;
132 pThis->pszLocation = NULL;
133 pThis->OutputFile = NIL_RTFILE;
134 /* IBase */
135 pDrvIns->IBase.pfnQueryInterface = drvRawFileQueryInterface;
136 /* IStream */
137 pThis->IStream.pfnWrite = drvRawFileWrite;
138
139 /*
140 * Read the configuration.
141 */
142 if (!CFGMR3AreValuesValid(pCfgHandle, "Location\0"))
143 AssertFailedReturn(VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
144
145 int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Location", &pThis->pszLocation);
146 if (RT_FAILURE(rc))
147 AssertMsgFailedReturn(("Configuration error: query \"Location\" resulted in %Rrc.\n", rc), rc);
148
149 /*
150 * Open the raw file.
151 */
152 rc = RTFileOpen(&pThis->OutputFile, pThis->pszLocation, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
153 if (RT_FAILURE(rc))
154 {
155 LogRel(("RawFile%d: CreateFile failed rc=%Rrc\n", pDrvIns->iInstance));
156 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("RawFile#%d failed to create the raw output file %s"), pDrvIns->iInstance, pThis->pszLocation);
157 }
158
159 LogFlow(("drvRawFileConstruct: location %s\n", pThis->pszLocation));
160 LogRel(("RawFile#%u: location %s\n", pDrvIns->iInstance, pThis->pszLocation));
161 return VINF_SUCCESS;
162}
163
164
165/**
166 * Destruct a raw output stream driver instance.
167 *
168 * Most VM resources are freed by the VM. This callback is provided so that
169 * any non-VM resources can be freed correctly.
170 *
171 * @param pDrvIns The driver instance data.
172 */
173static DECLCALLBACK(void) drvRawFileDestruct(PPDMDRVINS pDrvIns)
174{
175 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
176 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
177
178 if (pThis->pszLocation)
179 MMR3HeapFree(pThis->pszLocation);
180
181 if (pThis->OutputFile != NIL_RTFILE)
182 {
183 RTFileClose(pThis->OutputFile);
184 pThis->OutputFile = NIL_RTFILE;
185 }
186}
187
188
189/**
190 * Power off a raw output stream driver instance.
191 *
192 * This does most of the destruction work, to avoid ordering dependencies.
193 *
194 * @param pDrvIns The driver instance data.
195 */
196static DECLCALLBACK(void) drvRawFilePowerOff(PPDMDRVINS pDrvIns)
197{
198 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
199 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
200
201 if (pThis->OutputFile != NIL_RTFILE)
202 {
203 RTFileClose(pThis->OutputFile);
204 pThis->OutputFile = NIL_RTFILE;
205 }
206}
207
208
209/**
210 * Raw file driver registration record.
211 */
212const PDMDRVREG g_DrvRawFile =
213{
214 /* u32Version */
215 PDM_DRVREG_VERSION,
216 /* szDriverName */
217 "RawFile",
218 /* pszDescription */
219 "RawFile stream driver.",
220 /* fFlags */
221 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
222 /* fClass. */
223 PDM_DRVREG_CLASS_STREAM,
224 /* cMaxInstances */
225 ~0,
226 /* cbInstance */
227 sizeof(DRVRAWFILE),
228 /* pfnConstruct */
229 drvRawFileConstruct,
230 /* pfnDestruct */
231 drvRawFileDestruct,
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.

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