VirtualBox

Changeset 59524 in vbox


Ignore:
Timestamp:
Jan 30, 2016 1:28:29 AM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
105314
Message:

IPRT: Added RTVfsMemFileCreate and RTVfsFileFromBuffer.

Location:
trunk
Files:
3 edited

Legend:

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

    r59480 r59524  
    22072207# define RTVfsDirRetain                                 RT_MANGLER(RTVfsDirRetain)
    22082208# define RTVfsFileFlush                                 RT_MANGLER(RTVfsFileFlush)
     2209# define RTVfsFileFromBuffer                            RT_MANGLER(RTVfsFileFromBuffer)
    22092210# define RTVfsFileFromRTFile                            RT_MANGLER(RTVfsFileFromRTFile)
    22102211# define RTVfsFileGetSize                               RT_MANGLER(RTVfsFileGetSize)
     
    22552256# define RTVfsLockReleaseWriteSlow                      RT_MANGLER(RTVfsLockReleaseWriteSlow)
    22562257# define RTVfsLockRetain                                RT_MANGLER(RTVfsLockRetain)
     2258# define RTVfsMemFileCreate                             RT_MANGLER(RTVfsMemFileCreate)
    22572259# define RTVfsMemorizeIoStreamAsFile                    RT_MANGLER(RTVfsMemorizeIoStreamAsFile)
    22582260# define RTVfsNew                                       RT_MANGLER(RTVfsNew)
  • trunk/include/iprt/vfs.h

    r57944 r59524  
    903903
    904904/**
     905 * Creates a VFS file from a memory buffer.
     906 *
     907 * @returns VBox status code.
     908 *
     909 * @param   hVfsIos         The VFS I/O stream to memorize.  This will be read
     910 *                          to the end on success, on failure its position is
     911 *                          undefined.
     912 * @param   fFlags          A combination of RTFILE_O_READ and RTFILE_O_WRITE.
     913 * @param   pvBuf           The buffer.  This will be copied and not referenced
     914 *                          after this function returns.
     915 * @param   cbBuf           The buffer size.
     916 * @param   phVfsFile       Where to return the handle to the memory file on
     917 *                          success.
     918 */
     919RTDECL(int) RTVfsFileFromBuffer(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile);
     920
     921/**
     922 * Creates a memory backed VFS file object for read and write.
     923 *
     924 * @returns VBox status code.
     925 *
     926 * @param   hVfsIos         The VFS I/O stream to memorize.  This will be read
     927 *                          to the end on success, on failure its position is
     928 *                          undefined.
     929 * @param   cbEstimate      The estimated file size.
     930 * @param   phVfsFile       Where to return the handle to the memory file on
     931 *                          success.
     932 */
     933RTDECL(int) RTVfsMemFileCreate(RTVFSIOSTREAM hVfsIos, size_t cbEstimate, PRTVFSFILE phVfsFile);
     934
     935/**
    905936 * Pumps data from one I/O stream to another.
    906937 *
  • trunk/src/VBox/Runtime/common/vfs/vfsmemory.cpp

    r57358 r59524  
    702702
    703703/**
    704  * Standard file operations.
    705  */
    706 DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_rtVfsStdFileOps =
     704 * Memory file operations.
     705 */
     706DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_rtVfsMemFileOps =
    707707{
    708708    { /* Stream */
     
    742742
    743743
    744 
     744/**
     745 * Initialize the RTVFSMEMFILE specific members.
     746 *
     747 * @param   pThis           The memory file to initialize.
     748 * @param   cbObject        The object size for estimating extent size.
     749 * @param   fFlags          The user specified flags.
     750 */
     751static void rtVfsMemFileInit(PRTVFSMEMFILE pThis, RTFOFF cbObject, uint32_t fFlags)
     752{
     753    pThis->offCurPos    = 0;
     754    pThis->pCurExt      = NULL;
     755    RTListInit(&pThis->ExtentHead);
     756    if (cbObject <= 0)
     757        pThis->cbExtent = _4K;
     758    else if (cbObject < RTVFSMEM_MAX_EXTENT_SIZE)
     759        pThis->cbExtent = fFlags & RTFILE_O_WRITE ? _4K : cbObject;
     760    else
     761        pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
     762}
     763
     764
     765/**
     766 * Rewinds the file to position 0 and clears the WRITE flag if necessary.
     767 *
     768 * @param   pThis           The memory file instance.
     769 * @param   fFlags          The user specified flags.
     770 */
     771static void rtVfsMemFileResetAndFixWriteFlag(PRTVFSMEMFILE pThis, uint32_t fFlags)
     772{
     773    pThis->pCurExt   = RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
     774    pThis->offCurPos = 0;
     775
     776    if (!(fFlags & RTFILE_O_WRITE))
     777    {
     778        /** @todo clear RTFILE_O_WRITE from the resulting. */
     779    }
     780}
     781
     782
     783RTDECL(int) RTVfsMemFileCreate(RTVFSIOSTREAM hVfsIos, size_t cbEstimate, PRTVFSFILE phVfsFile)
     784{
     785    /*
     786     * Create a memory file instance and set the extension size according to the
     787     * buffer size.  Add the WRITE flag so we can use normal write APIs for
     788     * copying the buffer.
     789     */
     790    RTVFSFILE       hVfsFile;
     791    PRTVFSMEMFILE   pThis;
     792    int rc = RTVfsNewFile(&g_rtVfsMemFileOps, sizeof(*pThis), RTFILE_O_READ | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
     793                          &hVfsFile, (void **)&pThis);
     794    if (RT_SUCCESS(rc))
     795    {
     796        pThis->Base.ObjInfo.cbObject   = 0;
     797        pThis->Base.ObjInfo.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | RTFS_UNIX_IRWXU;
     798        rtVfsMemFileInit(pThis, cbEstimate, RTFILE_O_READ | RTFILE_O_WRITE);
     799
     800        *phVfsFile = hVfsFile;
     801        return VINF_SUCCESS;
     802    }
     803    return rc;
     804}
     805
     806
     807RTDECL(int) RTVfsFileFromBuffer(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile)
     808{
     809    /*
     810     * Create a memory file instance and set the extension size according to the
     811     * buffer size.  Add the WRITE flag so we can use normal write APIs for
     812     * copying the buffer.
     813     */
     814    RTVFSFILE       hVfsFile;
     815    PRTVFSMEMFILE   pThis;
     816    int rc = RTVfsNewFile(&g_rtVfsMemFileOps, sizeof(*pThis), fFlags | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
     817                          &hVfsFile, (void **)&pThis);
     818    if (RT_SUCCESS(rc))
     819    {
     820        pThis->Base.ObjInfo.cbObject   = cbBuf;
     821        pThis->Base.ObjInfo.Attr.fMode = RTFS_DOS_NT_NORMAL | RTFS_TYPE_FILE | RTFS_UNIX_IRWXU;
     822        rtVfsMemFileInit(pThis, cbBuf, fFlags);
     823
     824        /*
     825         * Copy the buffer and reposition the file pointer to the start.
     826         */
     827        rc = RTVfsFileWrite(hVfsFile, pvBuf, cbBuf, NULL);
     828        if (RT_SUCCESS(rc))
     829        {
     830            rtVfsMemFileResetAndFixWriteFlag(pThis, fFlags);
     831            *phVfsFile = hVfsFile;
     832            return VINF_SUCCESS;
     833        }
     834        RTVfsFileRelease(hVfsFile);
     835    }
     836    return rc;
     837}
    745838
    746839
     
    757850        RTVFSFILE       hVfsFile;
    758851        PRTVFSMEMFILE   pThis;
    759         rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(*pThis), fFlags | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
     852        rc = RTVfsNewFile(&g_rtVfsMemFileOps, sizeof(*pThis), fFlags | RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
    760853                          &hVfsFile, (void **)&pThis);
    761854        if (RT_SUCCESS(rc))
    762855        {
    763856            pThis->Base.ObjInfo = ObjInfo;
    764             pThis->offCurPos    = 0;
    765             pThis->pCurExt      = NULL;
    766             RTListInit(&pThis->ExtentHead);
    767             if (ObjInfo.cbObject <= 0)
    768                 pThis->cbExtent = _4K;
    769             else if (ObjInfo.cbObject < RTVFSMEM_MAX_EXTENT_SIZE)
    770                 pThis->cbExtent = _4K /* ObjInfo.cbObject */;
    771             else
    772                 pThis->cbExtent = RTVFSMEM_MAX_EXTENT_SIZE;
     857            rtVfsMemFileInit(pThis, ObjInfo.cbObject, fFlags);
    773858
    774859            /*
     
    780865            if (RT_SUCCESS(rc))
    781866            {
    782                 pThis->pCurExt   = RTListGetFirst(&pThis->ExtentHead, RTVFSMEMEXTENT, Entry);
    783                 pThis->offCurPos = 0;
    784 
    785                 if (!(fFlags & RTFILE_O_WRITE))
    786                 {
    787                     /** @todo clear RTFILE_O_WRITE from the resulting. */
    788                 }
     867                rtVfsMemFileResetAndFixWriteFlag(pThis, fFlags);
    789868                *phVfsFile = hVfsFile;
    790869                return VINF_SUCCESS;
     
    795874    return rc;
    796875}
     876
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