VirtualBox

source: vbox/trunk/src/VBox/Storage/VDIfVfs2.cpp@ 59601

Last change on this file since 59601 was 59601, checked in by vboxsync, 9 years ago

StorageLib: Added VDIfCreateFromVfsStream and VDIfDestroyFromVfsStream (untested).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1/* $Id: VDIfVfs2.cpp 59601 2016-02-06 16:19:51Z vboxsync $ */
2/** @file
3 * Virtual Disk Image (VDI), I/O interface to IPRT VFS I/O stream glue.
4 */
5
6/*
7 * Copyright (C) 2012-2013 Oracle Corporation
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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/types.h>
23#include <iprt/assert.h>
24#include <iprt/mem.h>
25#include <iprt/err.h>
26#include <iprt/asm.h>
27#include <iprt/string.h>
28#include <iprt/file.h>
29#include <iprt/sg.h>
30#include <iprt/vfslowlevel.h>
31#include <iprt/poll.h>
32#include <VBox/vd.h>
33#include <VBox/vd-ifs-internal.h>
34
35#include <VBox/log.h>
36
37
38/*********************************************************************************************************************************
39* Structures and Typedefs *
40*********************************************************************************************************************************/
41/**
42 * Extended VD I/O interface structure that vdIfFromVfs_xxx uses.
43 *
44 * It's passed as pvUser to each call.
45 */
46typedef struct VDIFFROMVFS
47{
48 VDINTERFACEIO CoreIo;
49
50 /** Magic. */
51 uint32_t u32Magic;
52 /** The stream access mode (RTFILE_O_ACCESS_MASK), possibly others. */
53 uint32_t fAccessMode;
54 /** The I/O stream. This is NIL after it's been closed. */
55 RTVFSIOSTREAM hVfsIos;
56 /** Completion callback. */
57 PFNVDCOMPLETED pfnCompleted;
58 /** User parameter for the completion callback. */
59 void *pvCompletedUser;
60 /** Set if hVfsIos has been opened. */
61 bool fOpened;
62} VDIFFROMVFS;
63/** Magic value for VDIFFROMVFS::u32Magic. */
64#define VDIFFROMVFS_MAGIC UINT32_C(0x11223344)
65
66/** Pointer to the instance data for the vdIfFromVfs_ methods. */
67typedef struct VDIFFROMVFS *PVDIFFROMVFS;
68
69
70typedef struct FILESTORAGEINTERNAL
71{
72 /** File handle. */
73 RTFILE file;
74} FILESTORAGEINTERNAL, *PFILESTORAGEINTERNAL;
75
76
77/*********************************************************************************************************************************
78* Defined Constants And Macros *
79*********************************************************************************************************************************/
80
81#define STATUS_WAIT UINT32_C(0)
82#define STATUS_WRITE UINT32_C(1)
83#define STATUS_WRITING UINT32_C(2)
84#define STATUS_READ UINT32_C(3)
85#define STATUS_READING UINT32_C(4)
86#define STATUS_END UINT32_C(5)
87
88/* Enable for getting some flow history. */
89#if 0
90# define DEBUG_PRINT_FLOW() RTPrintf("%s\n", __FUNCTION__)
91#else
92# define DEBUG_PRINT_FLOW() do {} while (0)
93#endif
94
95
96/*********************************************************************************************************************************
97* Internal Functions *
98*********************************************************************************************************************************/
99
100
101/** @name VDINTERFACEIO stubs returning not-implemented.
102 * @{
103 */
104
105/** @interface_method_impl{VDINTERFACEIO,pfnDelete} */
106static DECLCALLBACK(int) notImpl_Delete(void *pvUser, const char *pcszFilename)
107{
108 NOREF(pvUser); NOREF(pcszFilename);
109 Log(("%s\n", __FUNCTION__));
110 return VERR_NOT_IMPLEMENTED;
111}
112
113/** @interface_method_impl{VDINTERFACEIO,pfnMove} */
114static DECLCALLBACK(int) notImpl_Move(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
115{
116 NOREF(pvUser); NOREF(pcszSrc); NOREF(pcszDst); NOREF(fMove);
117 Log(("%s\n", __FUNCTION__));
118 return VERR_NOT_IMPLEMENTED;
119}
120
121/** @interface_method_impl{VDINTERFACEIO,pfnGetFreeSpace} */
122static DECLCALLBACK(int) notImpl_GetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
123{
124 NOREF(pvUser); NOREF(pcszFilename); NOREF(pcbFreeSpace);
125 Log(("%s\n", __FUNCTION__));
126 return VERR_NOT_IMPLEMENTED;
127}
128
129/** @interface_method_impl{VDINTERFACEIO,pfnGetModificationTime} */
130static DECLCALLBACK(int) notImpl_GetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
131{
132 NOREF(pvUser); NOREF(pcszFilename); NOREF(pModificationTime);
133 Log(("%s\n", __FUNCTION__));
134 return VERR_NOT_IMPLEMENTED;
135}
136
137/** @interface_method_impl{VDINTERFACEIO,pfnSetSize} */
138static DECLCALLBACK(int) notImpl_SetSize(void *pvUser, void *pvStorage, uint64_t cb)
139{
140 NOREF(pvUser); NOREF(pvStorage); NOREF(cb);
141 Log(("%s\n", __FUNCTION__));
142 return VERR_NOT_IMPLEMENTED;
143}
144
145/** @interface_method_impl{VDINTERFACEIO,pfnWriteSync} */
146static DECLCALLBACK(int) notImpl_WriteSync(void *pvUser, void *pvStorage, uint64_t off, const void *pvBuf,
147 size_t cbWrite, size_t *pcbWritten)
148{
149 NOREF(pvUser); NOREF(pvStorage); NOREF(off); NOREF(pvBuf); NOREF(cbWrite); NOREF(pcbWritten);
150 Log(("%s\n", __FUNCTION__));
151 return VERR_NOT_IMPLEMENTED;
152}
153
154/** @interface_method_impl{VDINTERFACEIO,pfnFlushSync} */
155static DECLCALLBACK(int) notImpl_FlushSync(void *pvUser, void *pvStorage)
156{
157 NOREF(pvUser); NOREF(pvStorage);
158 Log(("%s\n", __FUNCTION__));
159 return VERR_NOT_IMPLEMENTED;
160}
161
162/** @} */
163
164
165/** @interface_method_impl{VDINTERFACEIO,pfnOpen} */
166static DECLCALLBACK(int) vdIfFromVfs_Open(void *pvUser, const char *pszLocation, uint32_t fOpen,
167 PFNVDCOMPLETED pfnCompleted, void **ppInt)
168{
169 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
170
171 /*
172 * Validate input.
173 */
174 AssertPtrReturn(ppInt, VERR_INVALID_POINTER);
175 AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
176
177 /*
178 * We ignore the name, assuming the caller is opening the stream/file we're .
179 * serving. Thus, after close, all open calls fail.
180 */
181 AssertReturn(!pThis->fOpened, VERR_FILE_NOT_FOUND);
182 AssertReturn(pThis->hVfsIos != NIL_RTVFSIOSTREAM, VERR_FILE_NOT_FOUND); /* paranoia */
183 AssertMsgReturn((pThis->fAccessMode & fOpen & RTFILE_O_ACCESS_MASK) == (fOpen & RTFILE_O_ACCESS_MASK),
184 ("fAccessMode=%#x fOpen=%#x\n", pThis->fAccessMode, fOpen), VERR_ACCESS_DENIED);
185
186 pThis->fAccessMode = fOpen & RTFILE_O_ACCESS_MASK;
187 pThis->fOpened = true;
188 pThis->pfnCompleted = pfnCompleted;
189 pThis->pvCompletedUser = pvUser;
190
191 *ppInt = pThis->hVfsIos;
192 return VINF_SUCCESS;
193}
194
195/** @interface_method_impl{VDINTERFACEIO,pfnClose} */
196static DECLCALLBACK(int) vdIfFromVfs_Close(void *pvUser, void *pvStorage)
197{
198 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
199 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
200 AssertPtrReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
201 AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
202
203 uint32_t cRefs = RTVfsIoStrmRelease(pThis->hVfsIos);
204 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
205
206 return VINF_SUCCESS;
207}
208
209
210/** @interface_method_impl{VDINTERFACEIO,pfnGetSize} */
211static DECLCALLBACK(int) vdIfFromVfs_GetSize(void *pvUser, void *pvStorage, uint64_t *pcb)
212{
213 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
214 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
215 AssertPtrReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
216 AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
217 AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
218
219 RTFSOBJINFO ObjInfo;
220 int rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
221 if (RT_SUCCESS(rc))
222 *pcb = ObjInfo.cbObject;
223 return rc;
224}
225
226/** @interface_method_impl{VDINTERFACEIO,pfnReadSync} */
227static DECLCALLBACK(int) vdIfFromVfs_ReadSync(void *pvUser, void *pvStorage, uint64_t off, void *pvBuf,
228 size_t cbToRead, size_t *pcbRead)
229{
230 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
231 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
232 AssertPtrReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
233 AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
234 AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
235 AssertReturn(pThis->fAccessMode & RTFILE_O_READ, VERR_ACCESS_DENIED);
236
237 return RTVfsIoStrmReadAt(pThis->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
238}
239
240
241/** @interface_method_impl{VDINTERFACEIO,pfnWriteSync} */
242static DECLCALLBACK(int) vdIfFromVfs_WriteSync(void *pvUser, void *pvStorage, uint64_t off, void const *pvBuf,
243 size_t cbToWrite, size_t *pcbWritten)
244{
245 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
246 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
247 AssertPtrReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
248 AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
249 AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
250 AssertReturn(pThis->fAccessMode & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
251
252 return RTVfsIoStrmWriteAt(pThis->hVfsIos, off, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
253}
254
255
256VBOXDDU_DECL(int) VDIfCreateFromVfsStream(RTVFSIOSTREAM hVfsIos, uint32_t fAccessMode, PVDINTERFACEIO *ppIoIf)
257{
258 /*
259 * Validate input.
260 */
261 AssertPtrReturn(ppIoIf, VERR_INVALID_POINTER);
262 *ppIoIf = NULL;
263 AssertReturn(hVfsIos != NIL_RTVFSIOSTREAM, VERR_INVALID_HANDLE);
264 AssertReturn(fAccessMode & RTFILE_O_ACCESS_MASK, VERR_INVALID_FLAGS);
265
266 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos);
267 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
268
269 /*
270 * Allocate and init a callback + instance data structure.
271 */
272 int rc;
273 PVDIFFROMVFS pThis = (PVDIFFROMVFS)RTMemAllocZ(sizeof(*pThis));
274 if (pThis)
275 {
276 pThis->CoreIo.pfnOpen = vdIfFromVfs_Open;
277 pThis->CoreIo.pfnClose = vdIfFromVfs_Close;
278 pThis->CoreIo.pfnDelete = notImpl_Delete;
279 pThis->CoreIo.pfnMove = notImpl_Move;
280 pThis->CoreIo.pfnGetFreeSpace = notImpl_GetFreeSpace;
281 pThis->CoreIo.pfnGetModificationTime = notImpl_GetModificationTime;
282 pThis->CoreIo.pfnGetSize = vdIfFromVfs_GetSize;
283 pThis->CoreIo.pfnSetSize = notImpl_SetSize;
284 pThis->CoreIo.pfnReadSync = vdIfFromVfs_ReadSync;
285 pThis->CoreIo.pfnWriteSync = vdIfFromVfs_WriteSync;
286 pThis->CoreIo.pfnFlushSync = notImpl_FlushSync;
287
288 pThis->hVfsIos = hVfsIos;
289 pThis->fAccessMode = fAccessMode;
290 pThis->fOpened = false;
291 pThis->u32Magic = VDIFFROMVFS_MAGIC;
292
293 PVDINTERFACE pFakeList = NULL;
294 rc = VDInterfaceAdd(&pThis->CoreIo.Core, "FromVfsStream", VDINTERFACETYPE_IO, pThis, sizeof(pThis->CoreIo), &pFakeList);
295 if (RT_SUCCESS(rc))
296 {
297 *ppIoIf = &pThis->CoreIo;
298 return VINF_SUCCESS;
299 }
300
301 RTMemFree(pThis);
302 }
303 else
304 rc = VERR_NO_MEMORY;
305 RTVfsIoStrmRelease(hVfsIos);
306 return rc;
307}
308
309
310VBOXDDU_DECL(int) VDIfDestroyFromVfsStream(PVDINTERFACEIO pIoIf)
311{
312 if (pIoIf)
313 {
314 PVDIFFROMVFS pThis = (PVDIFFROMVFS)pIoIf;
315 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
316 AssertReturn(pThis->u32Magic == VDIFFROMVFS_MAGIC, VERR_INVALID_MAGIC);
317
318 if (pThis->hVfsIos != NIL_RTVFSIOSTREAM)
319 {
320 RTVfsIoStrmRelease(pThis->hVfsIos);
321 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
322 }
323 pThis->u32Magic = ~VDIFFROMVFS_MAGIC;
324 }
325 return VINF_SUCCESS;
326}
327
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