VirtualBox

Changeset 33235 in vbox


Ignore:
Timestamp:
Oct 19, 2010 3:14:06 PM (14 years ago)
Author:
vboxsync
Message:

Storage/vbox-img: implement stdout handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/testcase/vbox-img.cpp

    r33227 r33235  
    254254typedef struct FILEIOSTATE
    255255{
     256    RTFILE file;
    256257    /** Offset in the file. */
    257258    uint64_t off;
     
    273274    AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
    274275    AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ, VERR_INVALID_PARAMETER);
    275 
     276    RTFILE file;
     277    int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDIN);
     278    if (RT_FAILURE(rc))
     279        return rc;
     280
     281    /* No need to clear the buffer, the data will be read from disk. */
    276282    PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAlloc(sizeof(FILEIOSTATE));
    277283    if (!pFS)
    278284        return VERR_NO_MEMORY;
    279285
     286    pFS->file = file;
    280287    pFS->off = 0;
    281288    pFS->offBuffer = UINT64_MAX;
     
    290297    NOREF(pvUser);
    291298    AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
    292 
    293299    PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
    294300
     
    369375        do
    370376        {
    371             rc = RTFileRead(0, pTmp, cbTmp, &cbRead);
     377            rc = RTFileRead(pFS->file, pTmp, cbTmp, &cbRead);
    372378            if (RT_FAILURE(rc))
    373379                return rc;
     
    401407            do
    402408            {
    403                 rc = RTFileRead(0, pTmp, cbTmp, &cbRead);
     409                rc = RTFileRead(pFS->file, pTmp, cbTmp, &cbRead);
    404410                if (RT_FAILURE(rc))
    405411                    return rc;
     
    460466    AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
    461467    AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
    462 
    463     PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAlloc(sizeof(FILEIOSTATE));
     468    RTFILE file;
     469    int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDOUT);
     470    if (RT_FAILURE(rc))
     471        return rc;
     472
     473    /* Must clear buffer, so that skipped over data is initialized properly. */
     474    PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAllocZ(sizeof(FILEIOSTATE));
    464475    if (!pFS)
    465476        return VERR_NO_MEMORY;
    466477
     478    pFS->file = file;
    467479    pFS->off = 0;
    468     pFS->offBuffer = UINT64_MAX;
    469     pFS->cbBuffer = 0;
     480    pFS->offBuffer = 0;
     481    pFS->cbBuffer = sizeof(FILEIOSTATE);
    470482
    471483    *ppStorage = pFS;
     
    477489    NOREF(pvUser);
    478490    AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
    479 
    480491    PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
     492    int rc = VINF_SUCCESS;
     493
     494    /* Flush any remaining buffer contents. */
     495    if (pFS->cbBuffer)
     496        rc = RTFileWrite(pFS->file, &pFS->abBuffer[0], pFS->cbBuffer, NULL);
    481497
    482498    RTMemFree(pFS);
    483499
    484     return VINF_SUCCESS;
     500    return rc;
    485501}
    486502
     
    554570{
    555571    NOREF(pvUser);
    556     NOREF(pStorage);
    557     NOREF(uOffset);
    558     NOREF(cbBuffer);
    559     NOREF(pcbWritten);
     572    AssertPtrReturn(pStorage, VERR_INVALID_POINTER);
    560573    AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER);
    561     AssertFailedReturn(VERR_NOT_SUPPORTED);
     574    PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage;
     575    AssertReturn(uOffset >= pFS->off, VERR_INVALID_PARAMETER);
     576    int rc;
     577
     578    /* Write the data to the buffer, flushing as required. */
     579    size_t cbTotalWritten = 0;
     580    do
     581    {
     582        /* Flush the buffer if we need a new one. */
     583        while (uOffset > pFS->offBuffer + sizeof(pFS->abBuffer) - 1)
     584        {
     585            rc = RTFileWrite(pFS->file, &pFS->abBuffer[0],
     586                             sizeof(pFS->abBuffer), NULL);
     587            RT_ZERO(pFS->abBuffer);
     588            pFS->offBuffer += sizeof(pFS->abBuffer);
     589            pFS->cbBuffer = 0;
     590        }
     591
     592        uint32_t cbThisWrite = RT_MIN(cbBuffer,
     593                                      sizeof(pFS->abBuffer) - uOffset % sizeof(pFS->abBuffer));
     594        memcpy(&pFS->abBuffer[uOffset % sizeof(pFS->abBuffer)], pvBuffer,
     595               cbThisWrite);
     596        uOffset += cbThisWrite;
     597        pvBuffer = (uint8_t *)pvBuffer + cbThisWrite;
     598        cbBuffer -= cbThisWrite;
     599        cbTotalWritten += cbThisWrite;
     600    } while (cbBuffer > 0);
     601
     602    if (pcbWritten)
     603        *pcbWritten = cbTotalWritten;
     604
     605    pFS->cbBuffer = uOffset % sizeof(pFS->abBuffer);
     606    if (!pFS->cbBuffer)
     607        pFS->cbBuffer = sizeof(pFS->abBuffer);
     608    pFS->off = uOffset;
     609
     610    return VINF_SUCCESS;
    562611}
    563612
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