VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsmemory.cpp@ 34507

Last change on this file since 34507 was 34507, checked in by vboxsync, 14 years ago

IPRT: Visual C++ warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.9 KB
Line 
1/* $Id: vfsmemory.cpp 34507 2010-11-30 13:14:14Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, Memory Backed VFS.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/vfs.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/file.h>
38#include <iprt/list.h>
39#include <iprt/poll.h>
40#include <iprt/string.h>
41#include <iprt/vfslowlevel.h>
42
43
44
45/*******************************************************************************
46* Header Files *
47*******************************************************************************/
48#include "internal/iprt.h"
49#include <iprt/vfs.h>
50
51#include <iprt/err.h>
52#include <iprt/mem.h>
53
54
55/*******************************************************************************
56* Defined Constants And Macros *
57*******************************************************************************/
58/** The max extent size. */
59#define RTVFSMEM_MAX_EXTENT_SIZE _2M
60
61
62/*******************************************************************************
63* Structures and Typedefs *
64*******************************************************************************/
65
66/**
67 * Memory base object info.
68 */
69typedef struct RTVFSMEMBASE
70{
71 /** The basic object info. */
72 RTFSOBJINFO ObjInfo;
73} RTVFSMEMBASE;
74
75
76/**
77 * Memory file extent.
78 *
79 * This stores part of the file content.
80 */
81typedef struct RTVFSMEMEXTENT
82{
83 /** Extent list entry. */
84 RTLISTNODE Entry;
85 /** The offset of this extent within the file. */
86 uint64_t off;
87 /** The size of the this extent. */
88 uint32_t cb;
89 /** The data. */
90 uint8_t abData[1];
91} RTVFSMEMEXTENT;
92/** Pointer to a memory file extent. */
93typedef RTVFSMEMEXTENT *PRTVFSMEMEXTENT;
94
95/**
96 * Memory file.
97 */
98typedef struct RTVFSMEMFILE
99{
100 /** The base info. */
101 RTVFSMEMBASE Base;
102 /** The current file position. */
103 uint64_t offCurPos;
104 /** Pointer to the current file extent. */
105 PRTVFSMEMEXTENT pCurExt;
106 /** Linked list of file extents - RTVFSMEMEXTENT. */
107 RTLISTNODE ExtentHead;
108 /** The current extent size.
109 * This is slowly grown to RTVFSMEM_MAX_EXTENT_SIZE as the file grows. */
110 uint32_t cbExtent;
111} RTVFSMEMFILE;
112/** Pointer to a memory file. */
113typedef RTVFSMEMFILE *PRTVFSMEMFILE;
114
115
116
117/**
118 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
119 */
120static DECLCALLBACK(int) rtVfsMemFile_Close(void *pvThis)
121{
122 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
123
124 /*
125 * Free the extent list.
126 */
127 PRTVFSMEMEXTENT pCur, pNext;
128 RTListForEachSafe(&pThis->ExtentHead, pCur, pNext, RTVFSMEMEXTENT, Entry)
129 {
130 pCur->off = RTFOFF_MAX;
131 pCur->cb = UINT32_MAX;
132 RTListNodeRemove(&pCur->Entry);
133 RTMemFree(pCur);
134 }
135 pThis->pCurExt = NULL;
136
137 return VINF_SUCCESS;
138}
139
140
141/**
142 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
143 */
144static DECLCALLBACK(int) rtVfsMemFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
145{
146 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
147 switch (enmAddAttr)
148 {
149 case RTFSOBJATTRADD_NOTHING:
150 case RTFSOBJATTRADD_UNIX:
151 *pObjInfo = pThis->Base.ObjInfo;
152 return VINF_SUCCESS;
153
154 default:
155 return VERR_NOT_SUPPORTED;
156 }
157}
158
159
160/**
161 * The slow paths of rtVfsMemFile_LocateExtent.
162 *
163 * @copydoc rtVfsMemFile_LocateExtent
164 */
165static PRTVFSMEMEXTENT rtVfsMemFile_LocateExtentSlow(PRTVFSMEMFILE pThis, uint64_t off, bool *pfHit)
166{
167 /*
168 * Search from the start or the previously used extent. The heuristics
169 * are very very simple, but whatever.
170 */
171 PRTVFSMEMEXTENT pExtent = pThis->pCurExt;
172 if (!pExtent || pExtent->off < off)
173 {
174 pExtent = RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
175 if (!pExtent)
176 {
177 *pfHit = false;
178 return NULL;
179 }
180 }
181
182 while (off - pExtent->off >= pExtent->cb)
183 {
184 Assert(pExtent->off <= off);
185 PRTVFSMEMEXTENT pNext = RTListNodeGetNext(&pExtent->Entry, RTVFSMEMEXTENT, Entry);
186 if ( RTListNodeIsLast(&pThis->ExtentHead, &pNext->Entry)
187 || pNext->off > off)
188 {
189 *pfHit = false;
190 return pExtent;
191 }
192
193 pExtent = pNext;
194 }
195
196 *pfHit = true;
197 pThis->pCurExt = pExtent;
198 return pExtent;
199}
200
201
202/**
203 * Locates the extent covering the specified offset, or then one before it.
204 *
205 * @returns The closest extent. NULL if off is 0 and there are no extent
206 * covering byte 0 yet.
207 * @param pThis The memory file.
208 * @param off The offset (0-positive).
209 * @param pfHit Where to indicate whether the extent is a
210 * direct hit (@c true) or just a closest match
211 * (@c false).
212 */
213DECLINLINE(PRTVFSMEMEXTENT) rtVfsMemFile_LocateExtent(PRTVFSMEMFILE pThis, uint64_t off, bool *pfHit)
214{
215 /*
216 * The most likely case is that we're hitting the extent we used in the
217 * previous access or the one immediately following it.
218 */
219 PRTVFSMEMEXTENT pExtent = pThis->pCurExt;
220 if (!pExtent)
221 return rtVfsMemFile_LocateExtentSlow(pThis, off, pfHit);
222
223 if (off - pExtent->off >= pExtent->cb)
224 {
225 pExtent = RTListNodeGetNext(&pExtent->Entry, RTVFSMEMEXTENT, Entry);
226 if ( !pExtent
227 || off - pExtent->off >= pExtent->cb)
228 return rtVfsMemFile_LocateExtentSlow(pThis, off, pfHit);
229 pThis->pCurExt = pExtent;
230 }
231
232 *pfHit = true;
233 return pExtent;
234}
235
236
237/**
238 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
239 */
240static DECLCALLBACK(int) rtVfsMemFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
241{
242 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
243
244 Assert(pSgBuf->cSegs == 1);
245 Assert(off < 0);
246 NOREF(fBlocking);
247
248 /*
249 * Find the current position and check if it's within the file.
250 */
251 uint64_t offUnsigned = off < 0 ? pThis->offCurPos : (uint64_t)off;
252 if (offUnsigned >= (uint64_t)pThis->Base.ObjInfo.cbObject)
253 {
254 if (pcbRead)
255 {
256 *pcbRead = 0;
257 pThis->offCurPos = offUnsigned;
258 return VINF_EOF;
259 }
260 return VERR_EOF;
261 }
262
263 size_t cbLeftToRead;
264 if (offUnsigned + pSgBuf->paSegs[0].cbSeg > (uint64_t)pThis->Base.ObjInfo.cbObject)
265 {
266 if (!pcbRead)
267 return VERR_EOF;
268 *pcbRead = cbLeftToRead = (size_t)((uint64_t)pThis->Base.ObjInfo.cbObject - offUnsigned);
269 }
270 else
271 *pcbRead = cbLeftToRead = pSgBuf->paSegs[0].cbSeg;
272
273 /*
274 * Ok, we've got a valid stretch within the file. Do the reading.
275 */
276 if (cbLeftToRead > 0)
277 {
278 uint8_t *pbDst = (uint8_t *)pSgBuf->paSegs[0].pvSeg;
279 bool fHit;
280 PRTVFSMEMEXTENT pExtent = rtVfsMemFile_LocateExtent(pThis, offUnsigned, &fHit);
281 for (;;)
282 {
283 PRTVFSMEMEXTENT pNext;
284 size_t cbThisRead;
285 Assert(!pExtent || pExtent->off <= offUnsigned);
286
287 /*
288 * Do we hit an extent covering the the current file surface?
289 */
290 if (fHit)
291 {
292 size_t const offExtent = (size_t)(offUnsigned - pExtent->off);
293 cbThisRead = pExtent->cb - offExtent;
294 if (cbThisRead >= cbLeftToRead)
295 cbThisRead = cbLeftToRead;
296
297 memcpy(pbDst, &pExtent->abData[offUnsigned - pExtent->off], cbThisRead);
298
299 offUnsigned += cbThisRead;
300 cbLeftToRead -= cbThisRead;
301 if (!cbLeftToRead)
302 break;
303 pbDst += cbThisRead;
304
305 pNext = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
306 if ( pNext
307 && pNext->off == pExtent->off + pExtent->cb)
308 {
309 pExtent = pNext;
310 continue;
311 }
312 fHit = false;
313 }
314
315 /*
316 * No extent of this portion (sparse file).
317 */
318 else if (pExtent)
319 pNext = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
320 else
321 pNext = NULL;
322 Assert(!pNext || pNext->off > pExtent->off);
323
324 if ( !pNext
325 || offUnsigned + cbLeftToRead <= pNext->off)
326 cbThisRead = cbLeftToRead;
327 else
328 cbThisRead = (size_t)(pNext->off - offUnsigned);
329
330 RT_BZERO(pbDst, cbThisRead);
331
332 offUnsigned += cbThisRead;
333 cbLeftToRead -= cbThisRead;
334 if (!cbLeftToRead)
335 break;
336 pbDst += cbThisRead;
337
338 /* Go on and read content from the next extent. */
339 fHit = true;
340 pExtent = pNext;
341 }
342 }
343
344 pThis->offCurPos = offUnsigned;
345 return VINF_SUCCESS;
346}
347
348
349/**
350 * Allocates a new extent covering the ground at @a offUnsigned.
351 *
352 * @returns Pointer to the new extent on success, NULL if we're out of memory.
353 * @param pThis The memory file.
354 * @param offUnsigned The location to allocate the extent at.
355 * @param cbToWrite The number of bytes we're interested in writing
356 * starting at @a offUnsigned.
357 * @param pPrev The extention before @a offUnsigned. NULL if
358 * none.
359 */
360static PRTVFSMEMEXTENT rtVfsMemFile_AllocExtent(PRTVFSMEMFILE pThis, uint64_t offUnsigned, size_t cbToWrite,
361 PRTVFSMEMEXTENT pPrev)
362{
363 /*
364 * Adjust the extent size if we haven't reached the max size yet.
365 */
366 if (pThis->cbExtent != RTVFSMEM_MAX_EXTENT_SIZE)
367 {
368 if (cbToWrite >= RTVFSMEM_MAX_EXTENT_SIZE)
369 pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
370 else if (!RTListIsEmpty(&pThis->ExtentHead))
371 {
372 uint32_t cbNextExtent = pThis->cbExtent;
373 if (RT_IS_POWER_OF_TWO(cbNextExtent))
374 cbNextExtent *= 2;
375 else
376 {
377 /* Make it a power of two (seeRTVfsMemorizeIoStreamAsFile). */
378 cbNextExtent = _4K;
379 while (cbNextExtent < pThis->cbExtent)
380 cbNextExtent *= 2;
381 }
382 if (((pThis->Base.ObjInfo.cbAllocated + cbNextExtent) & (cbNextExtent - 1)) == 0)
383 pThis->cbExtent = cbNextExtent;
384 }
385 }
386
387 /*
388 * Figure out the size and position of the extent we're adding.
389 */
390 uint64_t offExtent = offUnsigned & ~(uint64_t)(pThis->cbExtent - 1);
391 uint32_t cbExtent = pThis->cbExtent;
392
393 uint64_t const offPrev = pPrev ? pPrev->off + pPrev->cb : 0;
394 if (offExtent < offPrev)
395 offExtent = offPrev;
396
397 PRTVFSMEMEXTENT pNext = pPrev
398 ? RTListGetNext(&pThis->ExtentHead, pPrev, RTVFSMEMEXTENT, Entry)
399 : RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
400 if (pNext)
401 {
402 uint64_t cbMaxExtent = pNext->off - offExtent;
403 if (cbMaxExtent < cbExtent)
404 cbExtent = (uint32_t)cbMaxExtent;
405 }
406
407 /*
408 * Allocate, initialize and insert the new extent.
409 */
410 PRTVFSMEMEXTENT pNew = (PRTVFSMEMEXTENT)RTMemAllocZ(RT_OFFSETOF(RTVFSMEMEXTENT, abData[cbExtent]));
411 if (pNew)
412 {
413 pNew->off = offExtent;
414 pNew->cb = cbExtent;
415 if (pPrev)
416 RTListNodeInsertAfter(&pPrev->Entry, &pNew->Entry);
417 else
418 RTListPrepend(&pThis->ExtentHead, &pNew->Entry);
419
420 pThis->Base.ObjInfo.cbAllocated += cbExtent;
421 }
422 /** @todo retry with minimum size. */
423
424 return pNew;
425}
426
427
428/**
429 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
430 */
431static DECLCALLBACK(int) rtVfsMemFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
432{
433 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
434
435 Assert(pSgBuf->cSegs == 1);
436 Assert(off < 0);
437 NOREF(fBlocking);
438
439 /*
440 * Validate the write and set up the write loop.
441 */
442 size_t cbLeftToWrite = pSgBuf->paSegs[0].cbSeg;
443 if (!cbLeftToWrite)
444 return VINF_SUCCESS; /* pcbWritten is already 0. */
445 uint64_t offUnsigned = off < 0 ? pThis->offCurPos : (uint64_t)off;
446 if (offUnsigned + cbLeftToWrite >= (uint64_t)RTFOFF_MAX)
447 return VERR_OUT_OF_RANGE;
448
449 int rc = VINF_SUCCESS;
450 uint8_t const *pbSrc = (uint8_t const *)pSgBuf->paSegs[0].pvSeg;
451 bool fHit;
452 PRTVFSMEMEXTENT pExtent = rtVfsMemFile_LocateExtent(pThis, offUnsigned, &fHit);
453 for (;;)
454 {
455 /*
456 * If we didn't hit an extent, allocate one (unless it's all zeros).
457 */
458 if (!fHit)
459 {
460 Assert(!pExtent || (pExtent->off < offUnsigned && pExtent->off + pExtent->cb <= offUnsigned));
461
462 /* Skip leading zeros if there is a whole bunch of them. */
463 uint8_t const *pbSrcNZ = (uint8_t const *)ASMMemIsAll8(pbSrc, cbLeftToWrite, 0);
464 if (!pbSrcNZ)
465 {
466 offUnsigned += cbLeftToWrite;
467 cbLeftToWrite = 0;
468 break;
469 }
470 size_t const cbZeros = pbSrcNZ - pbSrc;
471 if (cbZeros >= RT_MIN(pThis->cbExtent, _64K))
472 {
473 offUnsigned += cbZeros;
474 cbLeftToWrite -= cbZeros;
475 pbSrc = pbSrcNZ;
476 pExtent = rtVfsMemFile_LocateExtent(pThis, offUnsigned, &fHit);
477 break;
478 }
479
480 fHit = true;
481 pExtent = rtVfsMemFile_AllocExtent(pThis, offUnsigned, cbLeftToWrite, pExtent);
482 if (!pExtent)
483 {
484 rc = VERR_NO_MEMORY;
485 break;
486 }
487 }
488
489 /*
490 * Copy the source data into the current extent.
491 */
492 uint32_t const offDst = (uint32_t)(offUnsigned - pExtent->off);
493 uint32_t cbThisWrite = pExtent->cb - offDst;
494 if (cbThisWrite > cbLeftToWrite)
495 cbThisWrite = (uint32_t)cbLeftToWrite;
496 memcpy(&pExtent->abData[offDst], pbSrc, cbThisWrite);
497
498 offUnsigned += cbLeftToWrite;
499 cbLeftToWrite -= cbThisWrite;
500 if (!cbLeftToWrite)
501 break;
502 pbSrc += cbThisWrite;
503 Assert(offUnsigned == pExtent->off + pExtent->cb);
504
505 /*
506 * Advance to the next extent.
507 */
508 PRTVFSMEMEXTENT pNext = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
509 Assert(!pNext || pNext->off >= offUnsigned);
510 if (pNext && pNext->off == offUnsigned)
511 pExtent = pNext;
512 else
513 fHit = false;
514 }
515
516 /*
517 * Update the state, set return value and return.
518 * Note! There must be no alternative exit path from the loop above.
519 */
520 pThis->offCurPos = offUnsigned;
521 if ((uint64_t)pThis->Base.ObjInfo.cbObject < offUnsigned)
522 pThis->Base.ObjInfo.cbObject = offUnsigned;
523
524 if (pcbWritten)
525 *pcbWritten = pSgBuf->paSegs[0].cbSeg - cbLeftToWrite;
526 return rc;
527}
528
529
530/**
531 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
532 */
533static DECLCALLBACK(int) rtVfsMemFile_Flush(void *pvThis)
534{
535 NOREF(pvThis);
536 return VINF_SUCCESS;
537}
538
539
540/**
541 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
542 */
543static DECLCALLBACK(int) rtVfsMemFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
544 uint32_t *pfRetEvents)
545{
546 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
547 int rc;
548
549 if (fEvents != RTPOLL_EVT_ERROR)
550 {
551 *pfRetEvents = fEvents & ~RTPOLL_EVT_ERROR;
552 rc = VINF_SUCCESS;
553 }
554 else
555 rc = RTVfsUtilDummyPollOne(fEvents, cMillies, fIntr, pfRetEvents);
556 return rc;
557}
558
559
560/**
561 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
562 */
563static DECLCALLBACK(int) rtVfsMemFile_Tell(void *pvThis, PRTFOFF poffActual)
564{
565 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
566 *poffActual = pThis->offCurPos;
567 return VINF_SUCCESS;
568}
569
570
571/**
572 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
573 */
574static DECLCALLBACK(int) rtVfsMemFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
575{
576 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
577 pThis->Base.ObjInfo.Attr.fMode = (pThis->Base.ObjInfo.Attr.fMode & ~fMask) | fMode;
578 return VINF_SUCCESS;
579}
580
581
582/**
583 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
584 */
585static DECLCALLBACK(int) rtVfsMemFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
586 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
587{
588 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
589
590 if (pAccessTime)
591 pThis->Base.ObjInfo.AccessTime = *pAccessTime;
592 if (pModificationTime)
593 pThis->Base.ObjInfo.ModificationTime = *pModificationTime;
594 if (pChangeTime)
595 pThis->Base.ObjInfo.ChangeTime = *pChangeTime;
596 if (pBirthTime)
597 pThis->Base.ObjInfo.BirthTime = *pBirthTime;
598
599 return VINF_SUCCESS;
600}
601
602
603/**
604 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
605 */
606static DECLCALLBACK(int) rtVfsMemFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
607{
608 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
609
610 if (uid != NIL_RTUID)
611 pThis->Base.ObjInfo.Attr.u.Unix.uid = uid;
612 if (gid != NIL_RTUID)
613 pThis->Base.ObjInfo.Attr.u.Unix.gid = gid;
614
615 return VINF_SUCCESS;
616}
617
618
619/**
620 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
621 */
622static DECLCALLBACK(int) rtVfsMemFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
623{
624 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
625
626 /*
627 * Seek relative to which position.
628 */
629 uint64_t offWrt;
630 switch (uMethod)
631 {
632 case RTFILE_SEEK_BEGIN:
633 offWrt = 0;
634 break;
635
636 case RTFILE_SEEK_CURRENT:
637 offWrt = pThis->offCurPos;
638 break;
639
640 case RTFILE_SEEK_END:
641 offWrt = pThis->Base.ObjInfo.cbObject;
642 break;
643
644 default:
645 return VERR_INTERNAL_ERROR_5;
646 }
647
648 /*
649 * Calc new position, take care to stay without bounds.
650 */
651 uint64_t offNew;
652 if (offSeek == 0)
653 offNew = offWrt;
654 else if (offSeek > 0)
655 {
656 offNew = offWrt + offSeek;
657 if ( offNew < offWrt
658 || offNew > RTFOFF_MAX)
659 offNew = RTFOFF_MAX;
660 }
661 else if ((uint64_t)-offSeek < offWrt)
662 offNew = offWrt + offSeek;
663 else
664 offNew = 0;
665
666 /*
667 * Update the state and set return value.
668 */
669 if ( pThis->pCurExt
670 && pThis->pCurExt->off - offNew >= pThis->pCurExt->cb)
671 pThis->pCurExt = NULL;
672 pThis->offCurPos = offNew;
673
674 *poffActual = offNew;
675 return VINF_SUCCESS;
676}
677
678
679/**
680 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
681 */
682static DECLCALLBACK(int) rtVfsMemFile_QuerySize(void *pvThis, uint64_t *pcbFile)
683{
684 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
685 *pcbFile = pThis->Base.ObjInfo.cbObject;
686 return VINF_SUCCESS;
687}
688
689
690/**
691 * Standard file operations.
692 */
693DECLHIDDEN(const RTVFSFILEOPS) g_rtVfsStdFileOps =
694{
695 { /* Stream */
696 { /* Obj */
697 RTVFSOBJOPS_VERSION,
698 RTVFSOBJTYPE_FILE,
699 "MemFile",
700 rtVfsMemFile_Close,
701 rtVfsMemFile_QueryInfo,
702 RTVFSOBJOPS_VERSION
703 },
704 RTVFSIOSTREAMOPS_VERSION,
705 RTVFSIOSTREAMOPS_FEAT_NO_SG,
706 rtVfsMemFile_Read,
707 rtVfsMemFile_Write,
708 rtVfsMemFile_Flush,
709 rtVfsMemFile_PollOne,
710 rtVfsMemFile_Tell,
711 NULL /*Skip*/,
712 NULL /*ZeroFill*/,
713 RTVFSIOSTREAMOPS_VERSION,
714 },
715 RTVFSFILEOPS_VERSION,
716 /*RTVFSIOFILEOPS_FEAT_NO_AT_OFFSET*/ 0,
717 { /* ObjSet */
718 RTVFSOBJSETOPS_VERSION,
719 RT_OFFSETOF(RTVFSFILEOPS, Stream.Obj) - RT_OFFSETOF(RTVFSFILEOPS, ObjSet),
720 rtVfsMemFile_SetMode,
721 rtVfsMemFile_SetTimes,
722 rtVfsMemFile_SetOwner,
723 RTVFSOBJSETOPS_VERSION
724 },
725 rtVfsMemFile_Seek,
726 rtVfsMemFile_QuerySize,
727 RTVFSFILEOPS_VERSION
728};
729
730
731
732
733
734RTDECL(int) RTVfsMemorizeIoStreamAsFile(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, PRTVFSFILE phVfsFile)
735{
736 /*
737 * Create a memory file instance and try set the extension size to match
738 * the length of the I/O stream.
739 */
740 RTFSOBJINFO ObjInfo;
741 int rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_UNIX);
742 if (RT_SUCCESS(rc))
743 {
744 RTVFSFILE hVfsFile;
745 PRTVFSMEMFILE pThis;
746 rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(*pThis), fFlags | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
747 &hVfsFile, (void **)&pThis);
748 if (RT_SUCCESS(rc))
749 {
750 pThis->Base.ObjInfo = ObjInfo;
751 pThis->offCurPos = 0;
752 pThis->pCurExt = NULL;
753 RTListInit(&pThis->ExtentHead);
754 if (ObjInfo.cbObject <= 0)
755 pThis->cbExtent = _4K;
756 else if (ObjInfo.cbObject < RTVFSMEM_MAX_EXTENT_SIZE)
757 pThis->cbExtent = _4K;
758 else
759 pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
760
761 /*
762 * Copy the stream.
763 */
764 RTVFSIOSTREAM hVfsIosDst = RTVfsFileToIoStream(hVfsFile);
765 rc = RTVfsUtilPumpIoStreams(hVfsIos, hVfsIosDst, pThis->cbExtent);
766 RTVfsIoStrmRelease(hVfsIosDst);
767 if (RT_SUCCESS(rc))
768 {
769 if (!(fFlags & RTFILE_O_WRITE))
770 {
771 /** @todo clear RTFILE_O_WRITE from the resulting. */
772 }
773 *phVfsFile = hVfsFile;
774 return VINF_SUCCESS;
775 }
776 RTVfsFileRelease(hVfsFile);
777 }
778 }
779 return rc;
780}
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