diff options
Diffstat (limited to 'src/VBox/Runtime/r3/fileio.cpp')
-rw-r--r-- | src/VBox/Runtime/r3/fileio.cpp | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/src/VBox/Runtime/r3/fileio.cpp b/src/VBox/Runtime/r3/fileio.cpp index 5b4e8873..fd1a2898 100644 --- a/src/VBox/Runtime/r3/fileio.cpp +++ b/src/VBox/Runtime/r3/fileio.cpp @@ -4,7 +4,7 @@ */ /* - * Copyright (C) 2006-2007 Oracle Corporation + * Copyright (C) 2006-2011 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; @@ -208,6 +208,49 @@ RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRea /** + * Read bytes from a file at a given offset into a S/G buffer. + * This function may modify the file position. + * + * @returns iprt status code. + * @param hFile Handle to the file. + * @param off Where to read. + * @param pSgBuf Pointer to the S/G buffer to read into. + * @param cbToRead How much to read. + * @param pcbRead How much we actually read. + * If NULL an error will be returned for a partial read. + */ +RTR3DECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead) +{ + int rc = VINF_SUCCESS; + size_t cbRead = 0; + + while (cbToRead) + { + size_t cbThisRead = 0; + size_t cbBuf = cbToRead; + void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf); + + rc = RTFileReadAt(hFile, off, pvBuf, cbBuf, pcbRead ? &cbThisRead : NULL); + if (RT_SUCCESS(rc)) + cbRead += cbThisRead; + + if ( RT_FAILURE(rc) + || ( cbThisRead < cbBuf + && pcbRead)) + break; + + cbToRead -= cbBuf; + off += cbBuf; + } + + if (pcbRead) + *pcbRead = cbRead; + + return rc; +} + + +/** * Write bytes to a file at a given offset. * This function may modify the file position. * @@ -229,6 +272,49 @@ RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t /** + * Write bytes from a S/G buffer to a file at a given offset. + * This function may modify the file position. + * + * @returns iprt status code. + * @param hFile Handle to the file. + * @param off Where to write. + * @param pSgBuf What to write. + * @param cbToWrite How much to write. + * @param pcbWritten How much we actually wrote. + * If NULL an error will be returned for a partial write. + */ +RTR3DECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten) +{ + int rc = VINF_SUCCESS; + size_t cbWritten = 0; + + while (cbToWrite) + { + size_t cbThisWritten = 0; + size_t cbBuf = cbToWrite; + void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf); + + rc = RTFileWriteAt(hFile, off, pvBuf, cbBuf, pcbWritten ? &cbThisWritten : NULL); + if (RT_SUCCESS(rc)) + cbWritten += cbThisWritten; + + if ( RT_FAILURE(rc) + || ( cbThisWritten < cbBuf + && pcbWritten)) + break; + + cbToWrite -= cbBuf; + off += cbBuf; + } + + if (pcbWritten) + *pcbWritten = cbWritten; + + return rc; +} + + +/** * Gets the current file position. * * @returns File offset. |