VirtualBox

Ignore:
Timestamp:
Feb 10, 2016 12:47:33 AM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
105459
Message:

IPRT: Added 'off' parameter to RTVfsIoStrmSgWrite and RTVfsIoStrmSgRead so it's easier to write passthru layers. Added RTVfsIoStrmReadAll, RTVfsIoStrmReadAllFree, RTVfsIoStrmFromBuffer, RTManifestPtIosIsInstanceOf, RTCrX509Certificate_ReadFromBuffer and RTCrDigestUpdateFromVfsFile. Updated the manifest passthru read code to handle ReadAt requests which skips parts and jumps back to re-read stuff on streams/files which are seekable.

Location:
trunk/src/VBox/Runtime/common/vfs
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/vfs/vfsbase.cpp

    r57358 r59620  
    21352135
    21362136
    2137 RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
     2137RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
    21382138{
    21392139    AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
     
    21502150    int rc;
    21512151    if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
    2152         rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbRead);
     2152        rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbRead);
    21532153    else
    21542154    {
     
    21622162
    21632163            size_t cbReadSeg = pcbRead ? 0 : pSgBuf->paSegs[iSeg].cbSeg;
    2164             rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
     2164            rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
    21652165            if (RT_FAILURE(rc))
    21662166                break;
     
    21682168            if ((pcbRead && cbReadSeg != SgBuf.paSegs[0].cbSeg) || rc != VINF_SUCCESS)
    21692169                break;
     2170            if (off != -1)
     2171                off += cbReadSeg;
    21702172        }
    21712173
     
    21782180
    21792181
    2180 RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
     2182RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
    21812183{
    21822184    AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
     
    21932195    int rc;
    21942196    if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
    2195         rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbWritten);
     2197        rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbWritten);
    21962198    else
    21972199    {
     
    22052207
    22062208            size_t cbWrittenSeg = 0;
    2207             rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
     2209            rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
    22082210            if (RT_FAILURE(rc))
    22092211                break;
     
    22132215                if (cbWrittenSeg != SgBuf.paSegs[0].cbSeg)
    22142216                    break;
     2217                if (off != -1)
     2218                    off += cbWrittenSeg;
    22152219            }
     2220            else if (off != -1)
     2221                off += pSgBuf->paSegs[iSeg].cbSeg;
    22162222        }
    22172223
  • trunk/src/VBox/Runtime/common/vfs/vfsiosmisc.cpp

    r57358 r59620  
    3333
    3434#include <iprt/err.h>
     35#include <iprt/mem.h>
    3536#include <iprt/string.h>
    3637
     
    124125}
    125126
     127
     128/** Header size.  */
     129#define READ_ALL_HEADER_SIZE    0x20
     130/** The header magic. It's followed by the size (both size_t). */
     131#define READ_ALL_HEADER_MAGIC   UINT32_C(0x11223355)
     132
     133RTDECL(int) RTVfsIoStrmReadAll(RTVFSIOSTREAM hVfsIos, void **ppvBuf, size_t *pcbBuf)
     134{
     135    /*
     136     * Try query the object information and in case the stream has a known
     137     * size we could use for guidance.
     138     */
     139    RTFSOBJINFO ObjInfo;
     140    int    rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
     141    size_t cbAllocated = RT_SUCCESS(rc) && ObjInfo.cbObject > 0 && ObjInfo.cbObject < _1G
     142                       ? (size_t)ObjInfo.cbObject + 1 : _16K;
     143    cbAllocated += READ_ALL_HEADER_SIZE;
     144    void *pvBuf = RTMemAlloc(cbAllocated);
     145    if (pvBuf)
     146    {
     147        memset(pvBuf, 0xfe, READ_ALL_HEADER_SIZE);
     148        size_t off = 0;
     149        for (;;)
     150        {
     151            /*
     152             * Handle buffer growing and detecting the end of it all.
     153             */
     154            size_t cbToRead = cbAllocated - off - READ_ALL_HEADER_SIZE - 1;
     155            if (!cbToRead)
     156            {
     157                /* The end? */
     158                uint8_t bIgn;
     159                size_t cbIgn;
     160                rc = RTVfsIoStrmRead(hVfsIos, &bIgn, 0, true /*fBlocking*/, &cbIgn);
     161                if (rc == VINF_EOF)
     162                    break;
     163
     164                /* Grow the buffer. */
     165                cbAllocated -= READ_ALL_HEADER_SIZE - 1;
     166                cbAllocated  = RT_MAX(RT_MIN(cbAllocated, _32M), _1K);
     167                cbAllocated  = RT_ALIGN_Z(cbAllocated, _4K);
     168                cbAllocated += READ_ALL_HEADER_SIZE + 1;
     169
     170                void *pvNew = RTMemRealloc(pvBuf, cbAllocated);
     171                AssertBreakStmt(pvNew, rc = VERR_NO_MEMORY);
     172
     173                cbToRead = cbAllocated - off - READ_ALL_HEADER_SIZE - 1;
     174            }
     175            Assert(cbToRead < cbAllocated);
     176
     177            /*
     178             * Read.
     179             */
     180            size_t cbActual;
     181            rc = RTVfsIoStrmRead(hVfsIos, (uint8_t *)pvBuf + READ_ALL_HEADER_SIZE + off, cbToRead,
     182                                 true /*fBlocking*/, &cbActual);
     183            if (RT_FAILURE(rc))
     184                break;
     185            Assert(cbActual > 0);
     186            Assert(cbActual <= cbToRead);
     187            off += cbActual;
     188            if (rc == VINF_EOF)
     189                break;
     190        }
     191        Assert(rc != VERR_EOF);
     192        if (RT_SUCCESS(rc))
     193        {
     194            ((size_t *)pvBuf)[0] = READ_ALL_HEADER_MAGIC;
     195            ((size_t *)pvBuf)[1] = off;
     196            ((uint8_t *)pvBuf)[READ_ALL_HEADER_SIZE + off] = 0;
     197
     198            *ppvBuf = (uint8_t *)pvBuf + READ_ALL_HEADER_SIZE;
     199            *pcbBuf = off;
     200            return VINF_SUCCESS;
     201        }
     202
     203        RTMemFree(pvBuf);
     204    }
     205    else
     206        rc = VERR_NO_MEMORY;
     207    *ppvBuf = NULL;
     208    *pcbBuf = 0;
     209    return rc;
     210}
     211
     212
     213RTDECL(void) RTVfsIoStrmReadAllFree(void *pvBuf, size_t cbBuf)
     214{
     215    AssertPtrReturnVoid(pvBuf);
     216
     217    /* Spool back to the start of the header. */
     218    pvBuf = (uint8_t *)pvBuf - READ_ALL_HEADER_SIZE;
     219
     220    /* Make sure the caller isn't messing with us. Hardcoded, but works. */
     221    Assert(((size_t *)pvBuf)[0] == READ_ALL_HEADER_MAGIC);
     222    Assert(((size_t *)pvBuf)[1] == cbBuf);
     223
     224    /* Free it. */
     225    RTMemFree(pvBuf);
     226}
     227
  • trunk/src/VBox/Runtime/common/vfs/vfsmemory.cpp

    r59524 r59620  
    805805
    806806
    807 RTDECL(int) RTVfsFileFromBuffer(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile)
     807RTDECL(int) RTVfsFileFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile)
    808808{
    809809    /*
     
    832832            return VINF_SUCCESS;
    833833        }
     834        RTVfsFileRelease(hVfsFile);
     835    }
     836    return rc;
     837}
     838
     839
     840RTDECL(int) RTVfsIoStrmFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSIOSTREAM phVfsIos)
     841{
     842    RTVFSFILE hVfsFile;
     843    int rc = RTVfsFileFromBuffer(fFlags, pvBuf, cbBuf, &hVfsFile);
     844    if (RT_SUCCESS(rc))
     845    {
     846        *phVfsIos = RTVfsFileToIoStream(hVfsFile);
    834847        RTVfsFileRelease(hVfsFile);
    835848    }
  • trunk/src/VBox/Runtime/common/vfs/vfsstdfile.cpp

    r57635 r59620  
    502502    int rc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
    503503    if (RT_SUCCESS(rc))
     504    {
    504505        *phVfsIos = RTVfsFileToIoStream(hVfsFile);
    505     return rc;
    506 }
    507 
     506        RTVfsFileRelease(hVfsFile);
     507    }
     508    return rc;
     509}
     510
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette