VirtualBox

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

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

Serial: forward-port of 54411 (serial/rawfile: don't flush after writing a byte to the file)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: DrvRawFile.cpp 24382 2009-11-05 14:02:08Z 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 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 * Queries an interface to the driver.
99 *
100 * @returns Pointer to interface.
101 * @returns NULL if the interface was not supported by the driver.
102 * @param pInterface Pointer to this interface structure.
103 * @param enmInterface The requested interface identification.
104 * @thread Any thread.
105 */
106static DECLCALLBACK(void *) drvRawFileQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
107{
108 PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
109 PDRVRAWFILE pDrv = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
110 switch (enmInterface)
111 {
112 case PDMINTERFACE_BASE:
113 return &pDrvIns->IBase;
114 case PDMINTERFACE_STREAM:
115 return &pDrv->IStream;
116 default:
117 return NULL;
118 }
119}
120
121
122/**
123 * Construct a raw output stream driver instance.
124 *
125 * @copydoc FNPDMDRVCONSTRUCT
126 */
127static DECLCALLBACK(int) drvRawFileConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
128{
129 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
130
131 /*
132 * Init the static parts.
133 */
134 pThis->pDrvIns = pDrvIns;
135 pThis->pszLocation = NULL;
136 pThis->OutputFile = NIL_RTFILE;
137 /* IBase */
138 pDrvIns->IBase.pfnQueryInterface = drvRawFileQueryInterface;
139 /* IStream */
140 pThis->IStream.pfnWrite = drvRawFileWrite;
141
142 /*
143 * Read the configuration.
144 */
145 if (!CFGMR3AreValuesValid(pCfgHandle, "Location\0"))
146 AssertFailedReturn(VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES);
147
148 int rc = CFGMR3QueryStringAlloc(pCfgHandle, "Location", &pThis->pszLocation);
149 if (RT_FAILURE(rc))
150 AssertMsgFailedReturn(("Configuration error: query \"Location\" resulted in %Rrc.\n", rc), rc);
151
152 /*
153 * Open the raw file.
154 */
155 rc = RTFileOpen(&pThis->OutputFile, pThis->pszLocation, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
156 if (RT_FAILURE(rc))
157 {
158 LogRel(("RawFile%d: CreateFile failed rc=%Rrc\n", pDrvIns->iInstance));
159 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("RawFile#%d failed to create the raw output file %s"), pDrvIns->iInstance, pThis->pszLocation);
160 }
161
162 LogFlow(("drvRawFileConstruct: location %s\n", pThis->pszLocation));
163 LogRel(("RawFile#%u: location %s\n", pDrvIns->iInstance, pThis->pszLocation));
164 return VINF_SUCCESS;
165}
166
167
168/**
169 * Destruct a raw output stream driver instance.
170 *
171 * Most VM resources are freed by the VM. This callback is provided so that
172 * any non-VM resources can be freed correctly.
173 *
174 * @param pDrvIns The driver instance data.
175 */
176static DECLCALLBACK(void) drvRawFileDestruct(PPDMDRVINS pDrvIns)
177{
178 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
179 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
180
181 if (pThis->pszLocation)
182 MMR3HeapFree(pThis->pszLocation);
183
184 if (pThis->OutputFile != NIL_RTFILE)
185 {
186 RTFileClose(pThis->OutputFile);
187 pThis->OutputFile = NIL_RTFILE;
188 }
189}
190
191
192/**
193 * Power off a raw output stream driver instance.
194 *
195 * This does most of the destruction work, to avoid ordering dependencies.
196 *
197 * @param pDrvIns The driver instance data.
198 */
199static DECLCALLBACK(void) drvRawFilePowerOff(PPDMDRVINS pDrvIns)
200{
201 PDRVRAWFILE pThis = PDMINS_2_DATA(pDrvIns, PDRVRAWFILE);
202 LogFlow(("%s: %s\n", __FUNCTION__, pThis->pszLocation));
203
204 if (pThis->OutputFile != NIL_RTFILE)
205 {
206 RTFileClose(pThis->OutputFile);
207 pThis->OutputFile = NIL_RTFILE;
208 }
209}
210
211
212/**
213 * Raw file driver registration record.
214 */
215const PDMDRVREG g_DrvRawFile =
216{
217 /* u32Version */
218 PDM_DRVREG_VERSION,
219 /* szDriverName */
220 "RawFile",
221 /* pszDescription */
222 "RawFile stream driver.",
223 /* fFlags */
224 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
225 /* fClass. */
226 PDM_DRVREG_CLASS_STREAM,
227 /* cMaxInstances */
228 ~0,
229 /* cbInstance */
230 sizeof(DRVRAWFILE),
231 /* pfnConstruct */
232 drvRawFileConstruct,
233 /* pfnDestruct */
234 drvRawFileDestruct,
235 /* pfnIOCtl */
236 NULL,
237 /* pfnPowerOn */
238 NULL,
239 /* pfnReset */
240 NULL,
241 /* pfnSuspend */
242 NULL,
243 /* pfnResume */
244 NULL,
245 /* pfnAttach */
246 NULL,
247 /* pfnDetach */
248 NULL,
249 /* pfnPowerOff */
250 drvRawFilePowerOff,
251 /* pfnSoftReset */
252 NULL,
253 /* u32EndVersion */
254 PDM_DRVREG_VERSION
255};
256
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