VirtualBox

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

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

pdmifs.h: the final batch of refactored interface ID code.

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