VirtualBox

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

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

pdmifs.h: another batch of _IID changes.

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