VirtualBox

Changeset 23589 in vbox for trunk/src/VBox/VMM


Ignore:
Timestamp:
Oct 6, 2009 9:33:17 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
53255
Message:

SSM: Generalizing the file handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/SSM.cpp

    r23542 r23589  
    353353} SSMSTATE;
    354354
     355
    355356/** Pointer to a SSM stream buffer. */
    356357typedef struct SSMSTRMBUF *PSSMSTRMBUF;
     
    381382typedef struct SSMSTRM
    382383{
    383     /** The file handle. */
    384     RTFILE                  hFile;
     384    /** The stream method table. */
     385    PCSSMSTRMOPS            pOps;
     386    /** The user argument for the stream methods.
     387     * For file based streams, this is the file handle and not a pointer. */
     388    void                   *pvUser;
    385389
    386390    /** Write (set) or read (clear) stream. */
     
    16271631
    16281632/**
     1633 * @copydoc SSMSTRMOPS::pfnWrite
     1634 */
     1635static DECLCALLBACK(int) ssmR3FileWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
     1636{
     1637    Assert(RTFileTell((RTFILE)(uintptr_t)pvUser) == offStream); NOREF(offStream);
     1638    return RTFileWriteAt((RTFILE)(uintptr_t)pvUser, offStream, pvBuf, cbToWrite, NULL); /** @todo use RTFileWrite */
     1639}
     1640
     1641
     1642/**
     1643 * @copydoc SSMSTRMOPS::pfnRead
     1644 */
     1645static DECLCALLBACK(int) ssmR3FileRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
     1646{
     1647    Assert(RTFileTell((RTFILE)(uintptr_t)pvUser) == offStream); NOREF(offStream);
     1648    return RTFileRead((RTFILE)(uintptr_t)pvUser, pvBuf, cbToRead, pcbRead);
     1649}
     1650
     1651
     1652/**
     1653 * @copydoc SSMSTRMOPS::pfnSeek
     1654 */
     1655static DECLCALLBACK(int) ssmR3FileSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
     1656{
     1657    return RTFileSeek((RTFILE)(uintptr_t)pvUser, offSeek, uMethod, poffActual);
     1658}
     1659
     1660
     1661/**
     1662 * @copydoc SSMSTRMOPS::pfnTell
     1663 */
     1664static DECLCALLBACK(uint64_t) ssmR3FileTell(void *pvUser)
     1665{
     1666    return RTFileTell((RTFILE)(uintptr_t)pvUser);
     1667}
     1668
     1669
     1670/**
     1671 * @copydoc SSMSTRMOPS::pfnSize
     1672 */
     1673static DECLCALLBACK(int) ssmR3FileSize(void *pvUser, uint64_t *pcb)
     1674{
     1675    return RTFileGetSize((RTFILE)(uintptr_t)pvUser, pcb);
     1676}
     1677
     1678
     1679/**
     1680 * @copydoc SSMSTRMOPS::pfnClose
     1681 */
     1682static DECLCALLBACK(int) ssmR3FileClose(void *pvUser)
     1683{
     1684    return RTFileClose((RTFILE)(uintptr_t)pvUser);
     1685}
     1686
     1687
     1688/**
     1689 * Method table for a file based stream.
     1690 */
     1691static SSMSTRMOPS const g_ssmR3FileOps =
     1692{
     1693    SSMSTRMOPS_VERSION,
     1694    ssmR3FileWrite,
     1695    ssmR3FileRead,
     1696    ssmR3FileSeek,
     1697    ssmR3FileTell,
     1698    ssmR3FileSize,
     1699    ssmR3FileClose,
     1700    SSMSTRMOPS_VERSION
     1701};
     1702
     1703
     1704/**
    16291705 * Opens a file stream.
    16301706 *
     
    16451721                        ? RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE
    16461722                        : RTFILE_O_READ      | RTFILE_O_OPEN           | RTFILE_O_DENY_WRITE;
    1647         rc = RTFileOpen(&pStrm->hFile, pszFilename, fFlags);
     1723        RTFILE hFile;
     1724        rc = RTFileOpen(&hFile, pszFilename, fFlags);
    16481725        if (RT_SUCCESS(rc))
    16491726        {
     1727            pStrm->pOps   = &g_ssmR3FileOps;
     1728            pStrm->pvUser = (void *)(uintptr_t)hFile;
    16501729            pStrm->fWrite = fWrite;
    16511730            return VINF_SUCCESS;
     
    19181997
    19191998        /* flush */
    1920         int rc = RTFileWriteAt(pStrm->hFile, pCur->offStream, &pCur->abData[0], pCur->cb, NULL);
     1999        int rc = pStrm->pOps->pfnWrite(pStrm->pvUser, pCur->offStream, &pCur->abData[0], pCur->cb);
    19212000        if (    RT_FAILURE(rc)
    19222001            &&  ssmR3StrmSetError(pStrm, rc))
     
    19632042    }
    19642043
    1965     int rc = RTFileClose(pStrm->hFile);
     2044    int rc = pStrm->pOps->pfnClose(pStrm->pvUser);
    19662045    if (RT_FAILURE(rc))
    19672046        ssmR3StrmSetError(pStrm, rc);
    1968     pStrm->hFile = NIL_RTFILE;
     2047    pStrm->pOps   = NULL;
     2048    pStrm->pvUser = NULL;
    19692049
    19702050    rc = pStrm->rc;
     
    21522232    if (pStrm->fNeedSeek)
    21532233    {
    2154         rc = RTFileSeek(pStrm->hFile, pStrm->offNeedSeekTo, RTFILE_SEEK_BEGIN, NULL);
     2234        rc = pStrm->pOps->pfnSeek(pStrm->pvUser, pStrm->offNeedSeekTo, RTFILE_SEEK_BEGIN, NULL);
    21552235        if (RT_FAILURE(rc))
    21562236        {
     
    21702250        return pStrm->rc;
    21712251
    2172     pBuf->offStream = RTFileTell(pStrm->hFile);
     2252    pBuf->offStream = pStrm->pOps->pfnTell(pStrm->pvUser);
    21732253    size_t cbRead   = sizeof(pBuf->abData);
    2174     rc = RTFileRead(pStrm->hFile, &pBuf->abData[0], cbRead, &cbRead);
     2254    rc = pStrm->pOps->pfnRead(pStrm->pvUser, pBuf->offStream, &pBuf->abData[0], cbRead, &cbRead);
    21752255    if (    RT_SUCCESS(rc)
    21762256        &&  cbRead > 0)
     
    24062486
    24072487    uint64_t offStream;
    2408     int rc = RTFileSeek(pStrm->hFile, off, uMethod, &offStream);
     2488    int rc = pStrm->pOps->pfnSeek(pStrm->pvUser, off, uMethod, &offStream);
    24092489    if (RT_SUCCESS(rc))
    24102490    {
     
    24662546{
    24672547    uint64_t cbFile;
    2468     int rc = RTFileGetSize(pStrm->hFile, &cbFile);
     2548    int rc = pStrm->pOps->pfnSize(pStrm->pvUser, &cbFile);
    24692549    AssertLogRelRCReturn(rc, UINT64_MAX);
    24702550    return cbFile;
     
    24802560static bool ssmR3StrmIsFile(PSSMSTRM pStrm)
    24812561{
    2482     return pStrm->hFile != NIL_RTFILE;
     2562    return pStrm->pOps == &g_ssmR3FileOps;
    24832563}
    24842564
     
    25012581static int ssmR3StrmPeekAt(PSSMSTRM pStrm, RTFOFF off, void *pvBuf, size_t cbToRead, uint64_t *poff)
    25022582{
    2503     AssertReturn(!pStrm->fWrite && pStrm->hFile != NIL_RTFILE, VERR_NOT_SUPPORTED);
     2583    AssertReturn(!pStrm->fWrite, VERR_NOT_SUPPORTED);
    25042584    AssertReturn(pStrm->hIoThread == NIL_RTTHREAD, VERR_WRONG_ORDER);
    25052585
     
    25092589        pStrm->offNeedSeekTo = pStrm->offCurStream + (pStrm->pCur ? pStrm->pCur->cb : 0);
    25102590    }
    2511 
    2512     int rc = RTFileSeek(pStrm->hFile, off, off >= 0 ? RTFILE_SEEK_BEGIN : RTFILE_SEEK_END, poff);
     2591    uint64_t offActual;
     2592    int rc = pStrm->pOps->pfnSeek(pStrm->pvUser, off, off >= 0 ? RTFILE_SEEK_BEGIN : RTFILE_SEEK_END, &offActual);
    25132593    if (RT_SUCCESS(rc))
    2514         rc = RTFileRead(pStrm->hFile, pvBuf, cbToRead, NULL);
     2594    {
     2595        if (poff)
     2596            *poff = offActual;
     2597        rc = pStrm->pOps->pfnRead(pStrm->pvUser, offActual, pvBuf, cbToRead, NULL);
     2598    }
    25152599
    25162600    return rc;
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