VirtualBox

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

Last change on this file since 40127 was 40127, checked in by vboxsync, 13 years ago

vfsmemory: Fixed zero optimization in the write routine.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.1 KB
Line 
1/* $Id: vfsmemory.cpp 40127 2012-02-14 12:01:04Z vboxsync $ */
2/** @file
3 * IPRT - Virtual File System, Memory Backed VFS.
4 */
5
6/*
7 * Copyright (C) 2010-2012 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 * 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 RTLISTANCHOR 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 = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
186 if ( !pNext
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 = RTListGetNext(&pThis->ExtentHead, pExtent, 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 {
272 cbLeftToRead = pSgBuf->paSegs[0].cbSeg;
273 if (pcbRead)
274 *pcbRead = cbLeftToRead;
275 }
276
277 /*
278 * Ok, we've got a valid stretch within the file. Do the reading.
279 */
280 if (cbLeftToRead > 0)
281 {
282 uint8_t *pbDst = (uint8_t *)pSgBuf->paSegs[0].pvSeg;
283 bool fHit;
284 PRTVFSMEMEXTENT pExtent = rtVfsMemFile_LocateExtent(pThis, offUnsigned, &fHit);
285 for (;;)
286 {
287 PRTVFSMEMEXTENT pNext;
288 size_t cbThisRead;
289 Assert(!pExtent || pExtent->off <= offUnsigned);
290
291 /*
292 * Do we hit an extent covering the the current file surface?
293 */
294 if (fHit)
295 {
296 size_t const offExtent = (size_t)(offUnsigned - pExtent->off);
297 cbThisRead = pExtent->cb - offExtent;
298 if (cbThisRead >= cbLeftToRead)
299 cbThisRead = cbLeftToRead;
300
301 memcpy(pbDst, &pExtent->abData[offUnsigned - pExtent->off], cbThisRead);
302
303 offUnsigned += cbThisRead;
304 cbLeftToRead -= cbThisRead;
305 if (!cbLeftToRead)
306 break;
307 pbDst += cbThisRead;
308
309 pNext = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
310 if ( pNext
311 && pNext->off == pExtent->off + pExtent->cb)
312 {
313 pExtent = pNext;
314 continue;
315 }
316 fHit = false;
317 }
318
319 /*
320 * No extent of this portion (sparse file).
321 */
322 else if (pExtent)
323 pNext = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
324 else
325 pNext = NULL;
326 Assert(!pNext || pNext->off > pExtent->off);
327
328 if ( !pNext
329 || offUnsigned + cbLeftToRead <= pNext->off)
330 cbThisRead = cbLeftToRead;
331 else
332 cbThisRead = (size_t)(pNext->off - offUnsigned);
333
334 RT_BZERO(pbDst, cbThisRead);
335
336 offUnsigned += cbThisRead;
337 cbLeftToRead -= cbThisRead;
338 if (!cbLeftToRead)
339 break;
340 pbDst += cbThisRead;
341
342 /* Go on and read content from the next extent. */
343 fHit = true;
344 pExtent = pNext;
345 }
346 }
347
348 pThis->offCurPos = offUnsigned;
349 return VINF_SUCCESS;
350}
351
352
353/**
354 * Allocates a new extent covering the ground at @a offUnsigned.
355 *
356 * @returns Pointer to the new extent on success, NULL if we're out of memory.
357 * @param pThis The memory file.
358 * @param offUnsigned The location to allocate the extent at.
359 * @param cbToWrite The number of bytes we're interested in writing
360 * starting at @a offUnsigned.
361 * @param pPrev The extention before @a offUnsigned. NULL if
362 * none.
363 */
364static PRTVFSMEMEXTENT rtVfsMemFile_AllocExtent(PRTVFSMEMFILE pThis, uint64_t offUnsigned, size_t cbToWrite,
365 PRTVFSMEMEXTENT pPrev)
366{
367 /*
368 * Adjust the extent size if we haven't reached the max size yet.
369 */
370 if (pThis->cbExtent != RTVFSMEM_MAX_EXTENT_SIZE)
371 {
372 if (cbToWrite >= RTVFSMEM_MAX_EXTENT_SIZE)
373 pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
374 else if (!RTListIsEmpty(&pThis->ExtentHead))
375 {
376 uint32_t cbNextExtent = pThis->cbExtent;
377 if (RT_IS_POWER_OF_TWO(cbNextExtent))
378 cbNextExtent *= 2;
379 else
380 {
381 /* Make it a power of two (seeRTVfsMemorizeIoStreamAsFile). */
382 cbNextExtent = _4K;
383 while (cbNextExtent < pThis->cbExtent)
384 cbNextExtent *= 2;
385 }
386 if (((pThis->Base.ObjInfo.cbAllocated + cbNextExtent) & (cbNextExtent - 1)) == 0)
387 pThis->cbExtent = cbNextExtent;
388 }
389 }
390
391 /*
392 * Figure out the size and position of the extent we're adding.
393 */
394 uint64_t offExtent = offUnsigned & ~(uint64_t)(pThis->cbExtent - 1);
395 uint32_t cbExtent = pThis->cbExtent;
396
397 uint64_t const offPrev = pPrev ? pPrev->off + pPrev->cb : 0;
398 if (offExtent < offPrev)
399 offExtent = offPrev;
400
401 PRTVFSMEMEXTENT pNext = pPrev
402 ? RTListGetNext(&pThis->ExtentHead, pPrev, RTVFSMEMEXTENT, Entry)
403 : RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
404 if (pNext)
405 {
406 uint64_t cbMaxExtent = pNext->off - offExtent;
407 if (cbMaxExtent < cbExtent)
408 cbExtent = (uint32_t)cbMaxExtent;
409 }
410
411 /*
412 * Allocate, initialize and insert the new extent.
413 */
414 PRTVFSMEMEXTENT pNew = (PRTVFSMEMEXTENT)RTMemAllocZ(RT_OFFSETOF(RTVFSMEMEXTENT, abData[cbExtent]));
415 if (pNew)
416 {
417 pNew->off = offExtent;
418 pNew->cb = cbExtent;
419 if (pPrev)
420 RTListNodeInsertAfter(&pPrev->Entry, &pNew->Entry);
421 else
422 RTListPrepend(&pThis->ExtentHead, &pNew->Entry);
423
424 pThis->Base.ObjInfo.cbAllocated += cbExtent;
425 }
426 /** @todo retry with minimum size. */
427
428 return pNew;
429}
430
431
432/**
433 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
434 */
435static DECLCALLBACK(int) rtVfsMemFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
436{
437 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
438
439 Assert(pSgBuf->cSegs == 1);
440 Assert(off < 0);
441 NOREF(fBlocking);
442
443 /*
444 * Validate the write and set up the write loop.
445 */
446 size_t cbLeftToWrite = pSgBuf->paSegs[0].cbSeg;
447 if (!cbLeftToWrite)
448 return VINF_SUCCESS; /* pcbWritten is already 0. */
449 uint64_t offUnsigned = off < 0 ? pThis->offCurPos : (uint64_t)off;
450 if (offUnsigned + cbLeftToWrite >= (uint64_t)RTFOFF_MAX)
451 return VERR_OUT_OF_RANGE;
452
453 int rc = VINF_SUCCESS;
454 uint8_t const *pbSrc = (uint8_t const *)pSgBuf->paSegs[0].pvSeg;
455 bool fHit;
456 PRTVFSMEMEXTENT pExtent = rtVfsMemFile_LocateExtent(pThis, offUnsigned, &fHit);
457 for (;;)
458 {
459 /*
460 * If we didn't hit an extent, allocate one (unless it's all zeros).
461 */
462 if (!fHit)
463 {
464 Assert(!pExtent || offUnsigned < pExtent->off);
465
466 /* Skip leading zeros if there is a whole bunch of them. */
467 uint8_t const *pbSrcNZ = (uint8_t const *)ASMMemIsAll8(pbSrc, cbLeftToWrite, 0);
468 size_t cbZeros = pbSrcNZ ? pbSrcNZ - pbSrc : cbLeftToWrite;
469 if (cbZeros)
470 {
471 uint64_t const cbToNext = pExtent ? pExtent->off - offUnsigned : UINT64_MAX;
472 if (cbZeros > cbToNext)
473 cbZeros = (size_t)cbToNext;
474 offUnsigned += cbZeros;
475 cbLeftToWrite -= cbZeros;
476 if (!cbLeftToWrite)
477 break;
478
479 Assert(!pExtent || offUnsigned <= pExtent->off);
480 if (pExtent && pExtent->off == offUnsigned)
481 {
482 fHit = true;
483 continue;
484 }
485 }
486
487 fHit = true;
488 pExtent = rtVfsMemFile_AllocExtent(pThis, offUnsigned, cbLeftToWrite, pExtent);
489 if (!pExtent)
490 {
491 rc = VERR_NO_MEMORY;
492 break;
493 }
494 }
495
496 /*
497 * Copy the source data into the current extent.
498 */
499 uint32_t const offDst = (uint32_t)(offUnsigned - pExtent->off);
500 uint32_t cbThisWrite = pExtent->cb - offDst;
501 if (cbThisWrite > cbLeftToWrite)
502 cbThisWrite = (uint32_t)cbLeftToWrite;
503 memcpy(&pExtent->abData[offDst], pbSrc, cbThisWrite);
504
505 offUnsigned += cbLeftToWrite;
506 cbLeftToWrite -= cbThisWrite;
507 if (!cbLeftToWrite)
508 break;
509 pbSrc += cbThisWrite;
510 Assert(offUnsigned == pExtent->off + pExtent->cb);
511
512 /*
513 * Advance to the next extent (emulate the lookup).
514 */
515 pExtent = RTListGetNext(&pThis->ExtentHead, pExtent, RTVFSMEMEXTENT, Entry);
516 fHit = pExtent && (offUnsigned - pExtent->off < pExtent->cb);
517 }
518
519 /*
520 * Update the state, set return value and return.
521 * Note! There must be no alternative exit path from the loop above.
522 */
523 pThis->offCurPos = offUnsigned;
524 if ((uint64_t)pThis->Base.ObjInfo.cbObject < offUnsigned)
525 pThis->Base.ObjInfo.cbObject = offUnsigned;
526
527 if (pcbWritten)
528 *pcbWritten = pSgBuf->paSegs[0].cbSeg - cbLeftToWrite;
529 return rc;
530}
531
532
533/**
534 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
535 */
536static DECLCALLBACK(int) rtVfsMemFile_Flush(void *pvThis)
537{
538 NOREF(pvThis);
539 return VINF_SUCCESS;
540}
541
542
543/**
544 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
545 */
546static DECLCALLBACK(int) rtVfsMemFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
547 uint32_t *pfRetEvents)
548{
549 NOREF(pvThis);
550 int rc;
551 if (fEvents != RTPOLL_EVT_ERROR)
552 {
553 *pfRetEvents = fEvents & ~RTPOLL_EVT_ERROR;
554 rc = VINF_SUCCESS;
555 }
556 else
557 rc = RTVfsUtilDummyPollOne(fEvents, cMillies, fIntr, pfRetEvents);
558 return rc;
559}
560
561
562/**
563 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
564 */
565static DECLCALLBACK(int) rtVfsMemFile_Tell(void *pvThis, PRTFOFF poffActual)
566{
567 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
568 *poffActual = pThis->offCurPos;
569 return VINF_SUCCESS;
570}
571
572
573/**
574 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
575 */
576static DECLCALLBACK(int) rtVfsMemFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
577{
578 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
579 pThis->Base.ObjInfo.Attr.fMode = (pThis->Base.ObjInfo.Attr.fMode & ~fMask) | fMode;
580 return VINF_SUCCESS;
581}
582
583
584/**
585 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
586 */
587static DECLCALLBACK(int) rtVfsMemFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
588 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
589{
590 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
591
592 if (pAccessTime)
593 pThis->Base.ObjInfo.AccessTime = *pAccessTime;
594 if (pModificationTime)
595 pThis->Base.ObjInfo.ModificationTime = *pModificationTime;
596 if (pChangeTime)
597 pThis->Base.ObjInfo.ChangeTime = *pChangeTime;
598 if (pBirthTime)
599 pThis->Base.ObjInfo.BirthTime = *pBirthTime;
600
601 return VINF_SUCCESS;
602}
603
604
605/**
606 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
607 */
608static DECLCALLBACK(int) rtVfsMemFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
609{
610 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
611
612 if (uid != NIL_RTUID)
613 pThis->Base.ObjInfo.Attr.u.Unix.uid = uid;
614 if (gid != NIL_RTUID)
615 pThis->Base.ObjInfo.Attr.u.Unix.gid = gid;
616
617 return VINF_SUCCESS;
618}
619
620
621/**
622 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
623 */
624static DECLCALLBACK(int) rtVfsMemFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
625{
626 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
627
628 /*
629 * Seek relative to which position.
630 */
631 uint64_t offWrt;
632 switch (uMethod)
633 {
634 case RTFILE_SEEK_BEGIN:
635 offWrt = 0;
636 break;
637
638 case RTFILE_SEEK_CURRENT:
639 offWrt = pThis->offCurPos;
640 break;
641
642 case RTFILE_SEEK_END:
643 offWrt = pThis->Base.ObjInfo.cbObject;
644 break;
645
646 default:
647 return VERR_INTERNAL_ERROR_5;
648 }
649
650 /*
651 * Calc new position, take care to stay without bounds.
652 */
653 uint64_t offNew;
654 if (offSeek == 0)
655 offNew = offWrt;
656 else if (offSeek > 0)
657 {
658 offNew = offWrt + offSeek;
659 if ( offNew < offWrt
660 || offNew > RTFOFF_MAX)
661 offNew = RTFOFF_MAX;
662 }
663 else if ((uint64_t)-offSeek < offWrt)
664 offNew = offWrt + offSeek;
665 else
666 offNew = 0;
667
668 /*
669 * Update the state and set return value.
670 */
671 if ( pThis->pCurExt
672 && pThis->pCurExt->off - offNew >= pThis->pCurExt->cb)
673 pThis->pCurExt = NULL;
674 pThis->offCurPos = offNew;
675
676 *poffActual = offNew;
677 return VINF_SUCCESS;
678}
679
680
681/**
682 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
683 */
684static DECLCALLBACK(int) rtVfsMemFile_QuerySize(void *pvThis, uint64_t *pcbFile)
685{
686 PRTVFSMEMFILE pThis = (PRTVFSMEMFILE)pvThis;
687 *pcbFile = pThis->Base.ObjInfo.cbObject;
688 return VINF_SUCCESS;
689}
690
691
692/**
693 * Standard file operations.
694 */
695DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_rtVfsStdFileOps =
696{
697 { /* Stream */
698 { /* Obj */
699 RTVFSOBJOPS_VERSION,
700 RTVFSOBJTYPE_FILE,
701 "MemFile",
702 rtVfsMemFile_Close,
703 rtVfsMemFile_QueryInfo,
704 RTVFSOBJOPS_VERSION
705 },
706 RTVFSIOSTREAMOPS_VERSION,
707 RTVFSIOSTREAMOPS_FEAT_NO_SG,
708 rtVfsMemFile_Read,
709 rtVfsMemFile_Write,
710 rtVfsMemFile_Flush,
711 rtVfsMemFile_PollOne,
712 rtVfsMemFile_Tell,
713 NULL /*Skip*/,
714 NULL /*ZeroFill*/,
715 RTVFSIOSTREAMOPS_VERSION,
716 },
717 RTVFSFILEOPS_VERSION,
718 /*RTVFSIOFILEOPS_FEAT_NO_AT_OFFSET*/ 0,
719 { /* ObjSet */
720 RTVFSOBJSETOPS_VERSION,
721 RT_OFFSETOF(RTVFSFILEOPS, Stream.Obj) - RT_OFFSETOF(RTVFSFILEOPS, ObjSet),
722 rtVfsMemFile_SetMode,
723 rtVfsMemFile_SetTimes,
724 rtVfsMemFile_SetOwner,
725 RTVFSOBJSETOPS_VERSION
726 },
727 rtVfsMemFile_Seek,
728 rtVfsMemFile_QuerySize,
729 RTVFSFILEOPS_VERSION
730};
731
732
733
734
735
736RTDECL(int) RTVfsMemorizeIoStreamAsFile(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, PRTVFSFILE phVfsFile)
737{
738 /*
739 * Create a memory file instance and try set the extension size to match
740 * the length of the I/O stream.
741 */
742 RTFSOBJINFO ObjInfo;
743 int rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_UNIX);
744 if (RT_SUCCESS(rc))
745 {
746 RTVFSFILE hVfsFile;
747 PRTVFSMEMFILE pThis;
748 rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(*pThis), fFlags | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
749 &hVfsFile, (void **)&pThis);
750 if (RT_SUCCESS(rc))
751 {
752 pThis->Base.ObjInfo = ObjInfo;
753 pThis->offCurPos = 0;
754 pThis->pCurExt = NULL;
755 RTListInit(&pThis->ExtentHead);
756 if (ObjInfo.cbObject <= 0)
757 pThis->cbExtent = _4K;
758 else if (ObjInfo.cbObject < RTVFSMEM_MAX_EXTENT_SIZE)
759 pThis->cbExtent = _4K /* ObjInfo.cbObject */;
760 else
761 pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
762
763 /*
764 * Copy the stream.
765 */
766 RTVFSIOSTREAM hVfsIosDst = RTVfsFileToIoStream(hVfsFile);
767 rc = RTVfsUtilPumpIoStreams(hVfsIos, hVfsIosDst, pThis->cbExtent);
768 RTVfsIoStrmRelease(hVfsIosDst);
769 if (RT_SUCCESS(rc))
770 {
771 pThis->pCurExt = RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
772 pThis->offCurPos = 0;
773
774 if (!(fFlags & RTFILE_O_WRITE))
775 {
776 /** @todo clear RTFILE_O_WRITE from the resulting. */
777 }
778 *phVfsFile = hVfsFile;
779 return VINF_SUCCESS;
780 }
781 RTVfsFileRelease(hVfsFile);
782 }
783 }
784 return rc;
785}
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