VirtualBox

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

Last change on this file since 96324 was 94291, checked in by vboxsync, 3 years ago

IPRT,Storage: Adding RTVfsQueryLabel and internally a generic pfnQueryInfoEx method to the RTVFSOBJOPS function table. Untested implementation of the latter for iso/udf. bugref:9781

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: VDIfVfs.cpp 94291 2022-03-17 13:29:52Z 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-2022 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
36/*********************************************************************************************************************************
37* Structures and Typedefs *
38*********************************************************************************************************************************/
39
40/**
41 * The internal data of an VD I/O to VFS file or I/O stream wrapper.
42 */
43typedef struct VDIFVFSIOSFILE
44{
45 /** The VD I/O interface we prefer wrap.
46 * Can be NULL, in which case pVDIfsIoInt must be valid. */
47 PVDINTERFACEIO pVDIfsIo;
48 /** The VD I/O interface we alternatively can wrap.
49 Can be NULL, in which case pVDIfsIo must be valid. */
50 PVDINTERFACEIOINT pVDIfsIoInt;
51 /** User pointer to pass to the VD I/O interface methods. */
52 PVDIOSTORAGE pStorage;
53 /** The current stream position. */
54 RTFOFF offCurPos;
55} VDIFVFSIOSFILE;
56/** Pointer to a the internal data of a DVM volume file. */
57typedef VDIFVFSIOSFILE *PVDIFVFSIOSFILE;
58
59
60
61/**
62 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
63 */
64static DECLCALLBACK(int) vdIfVfsIos_Close(void *pvThis)
65{
66 /* We don't close anything. */
67 RT_NOREF1(pvThis);
68 return VINF_SUCCESS;
69}
70
71
72/**
73 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
74 */
75static DECLCALLBACK(int) vdIfVfsIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
76{
77 NOREF(pvThis);
78 NOREF(pObjInfo);
79 NOREF(enmAddAttr);
80 return VERR_NOT_SUPPORTED;
81}
82
83
84/**
85 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
86 */
87static DECLCALLBACK(int) vdIfVfsIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
88{
89 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
90 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
91 Assert(off >= -1);
92
93 /*
94 * This may end up being a little more complicated, esp. wrt VERR_EOF.
95 */
96 if (off == -1)
97 off = pThis->offCurPos;
98 int rc;
99 if (pThis->pVDIfsIo)
100 rc = vdIfIoFileReadSync(pThis->pVDIfsIo, pThis->pStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg, pcbRead);
101 else
102 {
103 rc = vdIfIoIntFileReadSync(pThis->pVDIfsIoInt, (PVDIOSTORAGE)pThis->pStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg);
104 if (pcbRead)
105 *pcbRead = RT_SUCCESS(rc) ? pSgBuf->paSegs[0].cbSeg : 0;
106 }
107 if (RT_SUCCESS(rc))
108 {
109 size_t cbAdvance = pcbRead ? *pcbRead : pSgBuf->paSegs[0].cbSeg;
110 pThis->offCurPos = off + cbAdvance;
111 if (pcbRead && !cbAdvance)
112 rc = VINF_EOF;
113 }
114 return rc;
115}
116
117
118/**
119 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
120 */
121static DECLCALLBACK(int) vdIfVfsIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
122{
123 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
124 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
125 Assert(off >= -1);
126
127 /*
128 * This may end up being a little more complicated, esp. wrt VERR_EOF.
129 */
130 if (off == -1)
131 off = pThis->offCurPos;
132 int rc;
133 if (pThis->pVDIfsIo)
134 rc = vdIfIoFileWriteSync(pThis->pVDIfsIo, pThis->pStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg, pcbWritten);
135 else
136 {
137 rc = vdIfIoIntFileWriteSync(pThis->pVDIfsIoInt, pThis->pStorage, off, pSgBuf[0].pvSegCur, pSgBuf->paSegs[0].cbSeg);
138 if (pcbWritten)
139 *pcbWritten = RT_SUCCESS(rc) ? pSgBuf->paSegs[0].cbSeg : 0;
140 }
141 if (RT_SUCCESS(rc))
142 pThis->offCurPos = off + (pcbWritten ? *pcbWritten : pSgBuf->paSegs[0].cbSeg);
143 return rc;
144}
145
146
147/**
148 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
149 */
150static DECLCALLBACK(int) vdIfVfsIos_Flush(void *pvThis)
151{
152 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
153 int rc;
154 if (pThis->pVDIfsIo)
155 rc = vdIfIoFileFlushSync(pThis->pVDIfsIo, pThis->pStorage);
156 else
157 rc = vdIfIoIntFileFlushSync(pThis->pVDIfsIoInt, pThis->pStorage);
158 return rc;
159}
160
161
162/**
163 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
164 */
165static DECLCALLBACK(int) vdIfVfsIos_Tell(void *pvThis, PRTFOFF poffActual)
166{
167 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
168 *poffActual = pThis->offCurPos;
169 return VINF_SUCCESS;
170}
171
172
173/**
174 * VFS I/O stream operations for a VD file or stream.
175 */
176DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_vdIfVfsIosOps =
177{
178 { /* Obj */
179 RTVFSOBJOPS_VERSION,
180 RTVFSOBJTYPE_IO_STREAM,
181 "VDIfIos",
182 vdIfVfsIos_Close,
183 vdIfVfsIos_QueryInfo,
184 NULL,
185 RTVFSOBJOPS_VERSION
186 },
187 RTVFSIOSTREAMOPS_VERSION,
188 RTVFSIOSTREAMOPS_FEAT_NO_SG,
189 vdIfVfsIos_Read,
190 vdIfVfsIos_Write,
191 vdIfVfsIos_Flush,
192 NULL /*PollOne*/,
193 vdIfVfsIos_Tell,
194 NULL /*Skip*/,
195 NULL /*ZeroFill*/,
196 RTVFSIOSTREAMOPS_VERSION,
197
198};
199
200VBOXDDU_DECL(int) VDIfCreateVfsStream(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos)
201{
202 AssertPtrReturn(pVDIfsIo, VERR_INVALID_HANDLE);
203 AssertPtrReturn(phVfsIos, VERR_INVALID_POINTER);
204
205 /*
206 * Create the volume file.
207 */
208 RTVFSIOSTREAM hVfsIos;
209 PVDIFVFSIOSFILE pThis;
210 int rc = RTVfsNewIoStream(&g_vdIfVfsIosOps, sizeof(*pThis), fFlags,
211 NIL_RTVFS, NIL_RTVFSLOCK, &hVfsIos, (void **)&pThis);
212 if (RT_SUCCESS(rc))
213 {
214 pThis->pVDIfsIo = pVDIfsIo;
215 pThis->pVDIfsIoInt = NULL;
216 pThis->pStorage = (PVDIOSTORAGE)pvStorage;
217 pThis->offCurPos = 0;
218
219 *phVfsIos = hVfsIos;
220 return VINF_SUCCESS;
221 }
222
223 return rc;
224}
225
226
227
228/**
229 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetMode}
230 */
231static DECLCALLBACK(int) vdIfVfsFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
232{
233 NOREF(pvThis);
234 NOREF(fMode);
235 NOREF(fMask);
236 return VERR_NOT_SUPPORTED;
237}
238
239
240/**
241 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
242 */
243static DECLCALLBACK(int) vdIfVfsFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
244 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
245{
246 NOREF(pvThis);
247 NOREF(pAccessTime);
248 NOREF(pModificationTime);
249 NOREF(pChangeTime);
250 NOREF(pBirthTime);
251 return VERR_NOT_SUPPORTED;
252}
253
254
255/**
256 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
257 */
258static DECLCALLBACK(int) vdIfVfsFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
259{
260 NOREF(pvThis);
261 NOREF(uid);
262 NOREF(gid);
263 return VERR_NOT_SUPPORTED;
264}
265
266
267/**
268 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
269 */
270static DECLCALLBACK(int) vdIfVfsFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
271{
272 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
273
274 uint64_t cbFile;
275 int rc;
276 if (pThis->pVDIfsIo)
277 rc = vdIfIoFileGetSize(pThis->pVDIfsIo, pThis->pStorage, &cbFile);
278 else
279 rc = vdIfIoIntFileGetSize(pThis->pVDIfsIoInt, pThis->pStorage, &cbFile);
280 if (RT_FAILURE(rc))
281 return rc;
282 if (cbFile >= (uint64_t)RTFOFF_MAX)
283 cbFile = RTFOFF_MAX;
284
285 /* Recalculate the request to RTFILE_SEEK_BEGIN. */
286 switch (uMethod)
287 {
288 case RTFILE_SEEK_BEGIN:
289 break;
290 case RTFILE_SEEK_CURRENT:
291 offSeek += pThis->offCurPos;
292 break;
293 case RTFILE_SEEK_END:
294 offSeek = cbFile + offSeek;
295 break;
296 default:
297 AssertFailedReturn(VERR_INVALID_PARAMETER);
298 }
299
300 /* Do limit checks. */
301 if (offSeek < 0)
302 offSeek = 0;
303 else if (offSeek > (RTFOFF)cbFile)
304 offSeek = cbFile;
305
306 /* Apply and return. */
307 pThis->offCurPos = offSeek;
308 if (poffActual)
309 *poffActual = offSeek;
310
311 return VINF_SUCCESS;
312}
313
314
315/**
316 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
317 */
318static DECLCALLBACK(int) vdIfVfsFile_QuerySize(void *pvThis, uint64_t *pcbFile)
319{
320 PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
321 int rc;
322 if (pThis->pVDIfsIo)
323 rc = vdIfIoFileGetSize(pThis->pVDIfsIo, pThis->pStorage, pcbFile);
324 else
325 rc = vdIfIoIntFileGetSize(pThis->pVDIfsIoInt, pThis->pStorage, pcbFile);
326 return rc;
327}
328
329
330
331/**
332 * VFS file operations for a VD file.
333 */
334DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_vdIfVfsFileOps =
335{
336 { /* I/O stream */
337 { /* Obj */
338 RTVFSOBJOPS_VERSION,
339 RTVFSOBJTYPE_FILE,
340 "VDIfFile",
341 vdIfVfsIos_Close,
342 vdIfVfsIos_QueryInfo,
343 NULL,
344 RTVFSOBJOPS_VERSION
345 },
346 RTVFSIOSTREAMOPS_VERSION,
347 RTVFSIOSTREAMOPS_FEAT_NO_SG,
348 vdIfVfsIos_Read,
349 vdIfVfsIos_Write,
350 vdIfVfsIos_Flush,
351 NULL /*PollOne*/,
352 vdIfVfsIos_Tell,
353 NULL /*Skip*/,
354 NULL /*ZeroFill*/,
355 RTVFSIOSTREAMOPS_VERSION,
356 },
357 RTVFSFILEOPS_VERSION,
358 0,
359 { /* ObjSet */
360 RTVFSOBJSETOPS_VERSION,
361 RT_UOFFSETOF(RTVFSFILEOPS, ObjSet) - RT_UOFFSETOF(RTVFSFILEOPS, Stream.Obj),
362 vdIfVfsFile_SetMode,
363 vdIfVfsFile_SetTimes,
364 vdIfVfsFile_SetOwner,
365 RTVFSOBJSETOPS_VERSION
366 },
367 vdIfVfsFile_Seek,
368 vdIfVfsFile_QuerySize,
369 NULL /*SetSize*/,
370 NULL /*QueryMaxSize*/,
371 RTVFSFILEOPS_VERSION,
372};
373
374
375VBOXDDU_DECL(int) VDIfCreateVfsFile(PVDINTERFACEIO pVDIfs, struct VDINTERFACEIOINT *pVDIfsInt, void *pvStorage, uint32_t fFlags, PRTVFSFILE phVfsFile)
376{
377 AssertReturn((pVDIfs != NULL) != (pVDIfsInt != NULL), VERR_INVALID_PARAMETER); /* Exactly one needs to be specified. */
378 AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
379
380 /*
381 * Create the volume file.
382 */
383 RTVFSFILE hVfsFile;
384 PVDIFVFSIOSFILE pThis;
385 int rc = RTVfsNewFile(&g_vdIfVfsFileOps, sizeof(*pThis), fFlags,
386 NIL_RTVFS, NIL_RTVFSLOCK, &hVfsFile, (void **)&pThis);
387 if (RT_SUCCESS(rc))
388 {
389 pThis->pVDIfsIo = pVDIfs;
390 pThis->pVDIfsIoInt = pVDIfsInt;
391 pThis->pStorage = (PVDIOSTORAGE)pvStorage;
392 pThis->offCurPos = 0;
393
394 *phVfsFile = hVfsFile;
395 return VINF_SUCCESS;
396 }
397
398 return rc;
399}
400
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