VirtualBox

source: vbox/trunk/src/VBox/Storage/VDIfVfs.cpp@ 47444

Last change on this file since 47444 was 47345, checked in by vboxsync, 11 years ago

Added VDIfCreateVfsStream for OVA importing/exporting - completely untested.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: VDIfVfs.cpp 47345 2013-07-23 15:29:30Z 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
34/*******************************************************************************
35* Structures and Typedefs *
36*******************************************************************************/
37
38/**
39 * The internal data of a DVM volume I/O stream.
40 */
41typedef struct VDIFVFSIOS
42{
43 /** The VD I/O interface we wrap. */
44 PVDINTERFACEIO pVDIfsIo;
45 /** User pointer to pass to the VD I/O interface methods. */
46 void *pvStorage;
47 /** The current stream position relative to the VDIfCreateVfsStream call. */
48 uint64_t offCurPos;
49} VDIFVFSIOS;
50/** Pointer to a the internal data of a DVM volume file. */
51typedef VDIFVFSIOS *PVDIFVFSIOS;
52
53
54
55/**
56 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
57 */
58static DECLCALLBACK(int) vdIfVfsIos_Close(void *pvThis)
59{
60 /* We don't close anything. */
61 return VINF_SUCCESS;
62}
63
64
65/**
66 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
67 */
68static DECLCALLBACK(int) vdIfVfsIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
69{
70 NOREF(pvThis);
71 NOREF(pObjInfo);
72 NOREF(enmAddAttr);
73 return VERR_NOT_SUPPORTED;
74}
75
76
77/**
78 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
79 */
80static DECLCALLBACK(int) vdIfVfsIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
81{
82 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
83 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
84
85 /*
86 * This may end up being a little more complicated, esp. wrt VERR_EOF.
87 */
88 int rc = vdIfIoFileReadSync(pThis->pVDIfsIo, pThis->pvStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg, pcbRead);
89 if (RT_SUCCESS(rc))
90 pThis->offCurPos = off + (pcbRead ? *pcbRead : pSgBuf->paSegs[0].cbSeg);
91 return rc;
92}
93
94
95/**
96 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
97 */
98static DECLCALLBACK(int) vdIfVfsIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
99{
100 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
101 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
102
103 /*
104 * This may end up being a little more complicated, esp. wrt VERR_EOF.
105 */
106 int rc = vdIfIoFileWriteSync(pThis->pVDIfsIo, pThis->pvStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg, pcbWritten);
107 if (RT_SUCCESS(rc))
108 pThis->offCurPos = off + (pcbWritten ? *pcbWritten : pSgBuf->paSegs[0].cbSeg);
109 return rc;
110}
111
112
113/**
114 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
115 */
116static DECLCALLBACK(int) vdIfVfsIos_Flush(void *pvThis)
117{
118 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
119 return vdIfIoFileFlushSync(pThis->pVDIfsIo, pThis->pvStorage);
120}
121
122
123/**
124 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
125 */
126static DECLCALLBACK(int) vdIfVfsIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
127 uint32_t *pfRetEvents)
128{
129 NOREF(pvThis);
130 int rc;
131 if (fEvents != RTPOLL_EVT_ERROR)
132 {
133 *pfRetEvents = fEvents & ~RTPOLL_EVT_ERROR;
134 rc = VINF_SUCCESS;
135 }
136 else
137 rc = RTVfsUtilDummyPollOne(fEvents, cMillies, fIntr, pfRetEvents);
138 return rc;
139}
140
141
142/**
143 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
144 */
145static DECLCALLBACK(int) vdIfVfsIos_Tell(void *pvThis, PRTFOFF poffActual)
146{
147 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
148 *poffActual = pThis->offCurPos;
149 return VINF_SUCCESS;
150}
151
152
153
154
155/**
156 * Standard file operations.
157 */
158DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_vdIfVfsStdIosOps =
159{
160 { /* Obj */
161 RTVFSOBJOPS_VERSION,
162 RTVFSOBJTYPE_FILE,
163 "VDIfIos",
164 vdIfVfsIos_Close,
165 vdIfVfsIos_QueryInfo,
166 RTVFSOBJOPS_VERSION
167 },
168 RTVFSIOSTREAMOPS_VERSION,
169 RTVFSIOSTREAMOPS_FEAT_NO_SG,
170 vdIfVfsIos_Read,
171 vdIfVfsIos_Write,
172 vdIfVfsIos_Flush,
173 vdIfVfsIos_PollOne,
174 vdIfVfsIos_Tell,
175 NULL /*Skip*/,
176 NULL /*ZeroFill*/,
177 RTVFSIOSTREAMOPS_VERSION,
178};
179
180
181VBOXDDU_DECL(int) VDIfCreateVfsStream(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos)
182{
183 AssertPtrReturn(pVDIfsIo, VERR_INVALID_HANDLE);
184 AssertPtrReturn(phVfsIos, VERR_INVALID_POINTER);
185
186 /*
187 * Create the volume file.
188 */
189 RTVFSIOSTREAM hVfsIos;
190 PVDIFVFSIOS pThis;
191 int rc = RTVfsNewIoStream(&g_vdIfVfsStdIosOps, sizeof(*pThis), fFlags,
192 NIL_RTVFS, NIL_RTVFSLOCK, &hVfsIos, (void **)&pThis);
193 if (RT_SUCCESS(rc))
194 {
195 pThis->pVDIfsIo = pVDIfsIo;
196 pThis->pvStorage = pvStorage;
197 pThis->offCurPos = 0;
198
199 *phVfsIos = hVfsIos;
200 return VINF_SUCCESS;
201 }
202
203 return rc;
204}
205
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