VirtualBox

Changeset 33822 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Nov 7, 2010 8:25:30 PM (14 years ago)
Author:
vboxsync
Message:

iprt/vfs: I/O stream code.

File:
1 edited

Legend:

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

    r33820 r33822  
    184184
    185185
     186RTDECL(uint32_t)    RTVfsIoStrmRetain(RTVFSIOSTREAM hVfsIos)
     187{
     188    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     189    AssertPtrReturn(pThis, UINT32_MAX);
     190    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
     191    return rtVfsRetain(&pThis->cRefs);
     192}
     193
     194
     195RTDECL(uint32_t)    RTVfsIoStrmRelease(RTVFSIOSTREAM hVfsIos)
     196{
     197    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     198    AssertPtrReturn(pThis, UINT32_MAX);
     199    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, UINT32_MAX);
     200
     201    uint32_t cRefs = rtVfsRelease(&pThis->cRefs);
     202    if (!cRefs)
     203    {
     204        /*
     205         * That was the last reference, close the stream.
     206         *
     207         * This is a little bit more complicated than when releasing a file or
     208         * directory handle because the I/O stream can be a sub-object and we
     209         * need to get to the real one before handing it to RTMemFree.
     210         */
     211        ASMAtomicWriteU32(&pThis->uMagic, RTVFSIOSTREAM_MAGIC_DEAD);
     212        pThis->pOps->Obj.pfnClose(pThis->pvThis);
     213
     214        switch (pThis->pOps->Obj.enmType)
     215        {
     216            case RTVFSOBJTYPE_IOSTREAM:
     217                RTMemFree(pThis);
     218                break;
     219
     220            case RTVFSOBJTYPE_FILE:
     221            {
     222                RTVFSFILEINTERNAL *pThisFile = RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
     223                ASMAtomicWriteU32(&pThisFile->uMagic, RTVFSIOSTREAM_MAGIC_DEAD);
     224                RTMemFree(pThisFile);
     225                break;
     226            }
     227
     228            /* Add new I/O stream compatible handle types here. */
     229
     230            default:
     231                AssertMsgFailed(("%d\n", pThis->pOps->Obj.enmType));
     232                break;
     233        }
     234    }
     235
     236    return cRefs;
     237
     238}
     239
     240
     241RTDECL(RTVFSFILE)   RTVfsIoStrmToFile(RTVFSIOSTREAM hVfsIos)
     242{
     243    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     244    AssertPtrReturn(pThis, NIL_RTVFSFILE);
     245    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, NIL_RTVFSFILE);
     246
     247    if (pThis->pOps->Obj.enmType == RTVFSOBJTYPE_FILE)
     248    {
     249        rtVfsRetainVoid(&pThis->cRefs);
     250        return RT_FROM_MEMBER(pThis, RTVFSFILEINTERNAL, Stream);
     251    }
     252
     253    /* this is no crime, so don't assert. */
     254    return NIL_RTVFSFILE;
     255}
     256
     257
     258RTDECL(int)         RTVfsIoStrmQueryInfo(RTVFSIOSTREAM hVfsIos, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
     259{
     260    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     261    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     262    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     263
     264    return pThis->pOps->Obj.pfnQueryInfo(pThis->pvThis, pObjInfo, enmAddAttr);
     265}
     266
     267
     268RTDECL(int)         RTVfsIoStrmRead(RTVFSIOSTREAM hVfsIos, void *pvBuf, size_t cbToRead, size_t *pcbRead)
     269{
     270    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     271    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     272    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     273
     274    RTSGSEG Seg = { pvBuf, cbToRead };
     275    RTSGBUF SgBuf;
     276    RTSgBufInit(&SgBuf, &Seg, 1);
     277    return pThis->pOps->pfnRead(pThis->pvThis, -1 /*off*/, &SgBuf, pcbRead == NULL /*fBlocking*/, pcbRead);
     278}
     279
     280
     281RTDECL(int)         RTVfsIoStrmWrite(RTVFSIOSTREAM hVfsIos, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
     282{
     283    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     284    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     285    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     286
     287    RTSGSEG Seg = { (void *)pvBuf, cbToWrite };
     288    RTSGBUF SgBuf;
     289    RTSgBufInit(&SgBuf, &Seg, 1);
     290    return pThis->pOps->pfnWrite(pThis->pvThis, -1 /*off*/, &SgBuf, pcbWritten == NULL /*fBlocking*/, pcbWritten);
     291}
     292
     293
     294RTDECL(int)         RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
     295{
     296    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     297    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     298    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     299    AssertPtr(pSgBuf);
     300    AssertReturn(fBlocking || VALID_PTR(pcbRead), VERR_INVALID_PARAMETER);
     301
     302    return pThis->pOps->pfnRead(pThis->pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbRead);
     303}
     304
     305
     306RTDECL(int)         RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
     307{
     308    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     309    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     310    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     311    AssertPtr(pSgBuf);
     312    AssertReturn(fBlocking || VALID_PTR(pcbWritten), VERR_INVALID_PARAMETER);
     313
     314    return pThis->pOps->pfnWrite(pThis->pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbWritten);
     315}
     316
     317
     318RTDECL(int)         RTVfsIoStrmFlush(RTVFSIOSTREAM hVfsIos)
     319{
     320    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     321    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     322    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     323
     324    return pThis->pOps->pfnFlush(pThis->pvThis);
     325}
     326
     327
     328RTDECL(RTFOFF)      RTVfsIoStrmPoll(RTVFSIOSTREAM hVfsIos, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
     329                                    uint32_t *pfRetEvents)
     330{
     331    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     332    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     333    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, VERR_INVALID_HANDLE);
     334
     335    return pThis->pOps->pfnPollOne(pThis->pvThis, fEvents, cMillies, fIntr, pfRetEvents);
     336}
     337
     338
     339RTDECL(RTFOFF)      RTVfsIoStrmTell(RTVFSIOSTREAM hVfsIos)
     340{
     341    RTVFSIOSTREAMINTERNAL *pThis = hVfsIos;
     342    AssertPtrReturn(pThis, -1);
     343    AssertReturn(pThis->uMagic == RTVFSIOSTREAM_MAGIC, -1);
     344
     345    RTFOFF off;
     346    int rc = pThis->pOps->pfnTell(pThis->pvThis, &off);
     347    if (RT_FAILURE(rc))
     348        off = rc;
     349    return off;
     350}
     351
     352
     353
    186354RTDECL(int) RTVfsNewFile(PCRTVFSFILEOPS pFileOps, size_t cbInstance, uint32_t fOpen, RTVFS hVfs,
    187355                         PRTVFSFILE phVfsFile, void **ppvInstance)
Note: See TracChangeset for help on using the changeset viewer.

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