VirtualBox

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

Last change on this file since 47544 was 47515, checked in by vboxsync, 11 years ago

tiny fix I forgot committing last week.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: VDIfVfs.cpp 47515 2013-08-01 18:29:36Z 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 == -1 ? pThis->offCurPos : off)
91 + (pcbRead ? *pcbRead : pSgBuf->paSegs[0].cbSeg);
92 return rc;
93}
94
95
96/**
97 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
98 */
99static DECLCALLBACK(int) vdIfVfsIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
100{
101 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
102 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
103
104 /*
105 * This may end up being a little more complicated, esp. wrt VERR_EOF.
106 */
107 int rc = vdIfIoFileWriteSync(pThis->pVDIfsIo, pThis->pvStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg, pcbWritten);
108 if (RT_SUCCESS(rc))
109 pThis->offCurPos = (off == -1 ? pThis->offCurPos : off)
110 + (pcbWritten ? *pcbWritten : pSgBuf->paSegs[0].cbSeg);
111 return rc;
112}
113
114
115/**
116 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
117 */
118static DECLCALLBACK(int) vdIfVfsIos_Flush(void *pvThis)
119{
120 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
121 return vdIfIoFileFlushSync(pThis->pVDIfsIo, pThis->pvStorage);
122}
123
124
125/**
126 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
127 */
128static DECLCALLBACK(int) vdIfVfsIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
129 uint32_t *pfRetEvents)
130{
131 NOREF(pvThis);
132 int rc;
133 if (fEvents != RTPOLL_EVT_ERROR)
134 {
135 *pfRetEvents = fEvents & ~RTPOLL_EVT_ERROR;
136 rc = VINF_SUCCESS;
137 }
138 else
139 rc = RTVfsUtilDummyPollOne(fEvents, cMillies, fIntr, pfRetEvents);
140 return rc;
141}
142
143
144/**
145 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
146 */
147static DECLCALLBACK(int) vdIfVfsIos_Tell(void *pvThis, PRTFOFF poffActual)
148{
149 PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
150 *poffActual = pThis->offCurPos;
151 return VINF_SUCCESS;
152}
153
154
155
156
157/**
158 * Standard file operations.
159 */
160DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_vdIfVfsStdIosOps =
161{
162 { /* Obj */
163 RTVFSOBJOPS_VERSION,
164 RTVFSOBJTYPE_FILE,
165 "VDIfIos",
166 vdIfVfsIos_Close,
167 vdIfVfsIos_QueryInfo,
168 RTVFSOBJOPS_VERSION
169 },
170 RTVFSIOSTREAMOPS_VERSION,
171 RTVFSIOSTREAMOPS_FEAT_NO_SG,
172 vdIfVfsIos_Read,
173 vdIfVfsIos_Write,
174 vdIfVfsIos_Flush,
175 vdIfVfsIos_PollOne,
176 vdIfVfsIos_Tell,
177 NULL /*Skip*/,
178 NULL /*ZeroFill*/,
179 RTVFSIOSTREAMOPS_VERSION,
180};
181
182
183VBOXDDU_DECL(int) VDIfCreateVfsStream(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos)
184{
185 AssertPtrReturn(pVDIfsIo, VERR_INVALID_HANDLE);
186 AssertPtrReturn(phVfsIos, VERR_INVALID_POINTER);
187
188 /*
189 * Create the volume file.
190 */
191 RTVFSIOSTREAM hVfsIos;
192 PVDIFVFSIOS pThis;
193 int rc = RTVfsNewIoStream(&g_vdIfVfsStdIosOps, sizeof(*pThis), fFlags,
194 NIL_RTVFS, NIL_RTVFSLOCK, &hVfsIos, (void **)&pThis);
195 if (RT_SUCCESS(rc))
196 {
197 pThis->pVDIfsIo = pVDIfsIo;
198 pThis->pvStorage = pvStorage;
199 pThis->offCurPos = 0;
200
201 *phVfsIos = hVfsIos;
202 return VINF_SUCCESS;
203 }
204
205 return rc;
206}
207
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