VirtualBox

Changeset 46245 in vbox


Ignore:
Timestamp:
May 23, 2013 7:15:19 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
85974
Message:

Runtime: RTFileSg{Read|Write}At API

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/file.h

    r45678 r46245  
    3131#include <iprt/stdarg.h>
    3232#include <iprt/fs.h>
     33#include <iprt/sg.h>
    3334
    3435RT_C_DECLS_BEGIN
     
    382383
    383384/**
     385 * Read bytes from a file at a given offset into a S/G buffer.
     386 * This function may modify the file position.
     387 *
     388 * @returns iprt status code.
     389 * @param   hFile       Handle to the file.
     390 * @param   off         Where to read.
     391 * @param   pSgBuf      Pointer to the S/G buffer to read into.
     392 * @param   cbToRead    How much to read.
     393 * @param   *pcbRead    How much we actually read .
     394 *                      If NULL an error will be returned for a partial read.
     395 */
     396RTDECL(int)  RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead);
     397
     398/**
    384399 * Write bytes to a file.
    385400 *
     
    406421 */
    407422RTDECL(int)  RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
     423
     424/**
     425 * Write bytes from a S/G buffer to a file at a given offset.
     426 * This function may modify the file position.
     427 *
     428 * @returns iprt status code.
     429 * @param   hFile       Handle to the file.
     430 * @param   off         Where to write.
     431 * @param   pSgBuf      What to write.
     432 * @param   cbToWrite   How much to write.
     433 * @param   *pcbWritten How much we actually wrote.
     434 *                      If NULL an error will be returned for a partial write.
     435 */
     436RTDECL(int)  RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten);
    408437
    409438/**
  • trunk/src/VBox/Runtime/r3/fileio.cpp

    r44528 r46245  
    209209
    210210/**
     211 * Read bytes from a file at a given offset into a S/G buffer.
     212 * This function may modify the file position.
     213 *
     214 * @returns iprt status code.
     215 * @param   hFile       Handle to the file.
     216 * @param   off         Where to read.
     217 * @param   pSgBuf      Pointer to the S/G buffer to read into.
     218 * @param   cbToRead    How much to read.
     219 * @param   pcbRead     How much we actually read.
     220 *                      If NULL an error will be returned for a partial read.
     221 */
     222RTR3DECL(int)  RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
     223{
     224    int rc;
     225    size_t cbRead = 0;
     226
     227    while (cbToRead)
     228    {
     229        size_t cbThisRead = 0;
     230        size_t cbBuf = cbToRead;
     231        void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
     232
     233        rc = RTFileReadAt(hFile, off, pvBuf, cbBuf, pcbRead ? &cbThisRead : NULL);
     234        if (RT_SUCCESS(rc))
     235            cbRead += cbThisRead;
     236
     237        if (   RT_FAILURE(rc)
     238            || cbThisRead < cbBuf)
     239            break;
     240
     241        cbToRead -= cbBuf;
     242        off += cbBuf;
     243    }
     244
     245    if (pcbRead)
     246        *pcbRead = cbRead;
     247
     248    return rc;
     249}
     250
     251
     252/**
    211253 * Write bytes to a file at a given offset.
    212254 * This function may modify the file position.
     
    225267    if (RT_SUCCESS(rc))
    226268        rc = RTFileWrite(File, pvBuf, cbToWrite, pcbWritten);
     269    return rc;
     270}
     271
     272
     273/**
     274 * Write bytes from a S/G buffer to a file at a given offset.
     275 * This function may modify the file position.
     276 *
     277 * @returns iprt status code.
     278 * @param   hFile       Handle to the file.
     279 * @param   off         Where to write.
     280 * @param   pSgBuf      What to write.
     281 * @param   cbToWrite   How much to write.
     282 * @param   pcbWritten  How much we actually wrote.
     283 *                      If NULL an error will be returned for a partial write.
     284 */
     285RTR3DECL(int)  RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
     286{
     287    int rc;
     288    size_t cbWritten = 0;
     289
     290    while (cbToWrite)
     291    {
     292        size_t cbThisWritten = 0;
     293        size_t cbBuf = cbToWrite;
     294        void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
     295
     296        rc = RTFileWriteAt(hFile, off, pvBuf, cbBuf, pcbWritten ? &cbThisWritten : NULL);
     297        if (RT_SUCCESS(rc))
     298            cbWritten += cbThisWritten;
     299
     300        if (   RT_FAILURE(rc)
     301            || cbThisWritten < cbBuf)
     302            break;
     303
     304        cbToWrite -= cbBuf;
     305        off += cbBuf;
     306    }
     307
     308    if (pcbWritten)
     309        *pcbWritten = cbWritten;
     310
    227311    return rc;
    228312}
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