VirtualBox

Changeset 48849 in vbox


Ignore:
Timestamp:
Oct 3, 2013 7:52:53 PM (11 years ago)
Author:
vboxsync
Message:

VDIfVfs.cpp: Added file interface wrapper - VDIfCreateVfsFile.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/vd-ifs.h

    r47349 r48849  
    621621 */
    622622VBOXDDU_DECL(int) VDIfCreateVfsStream(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos);
     623
     624/**
     625 * Create a VFS file handle around a VD I/O interface.
     626 *
     627 * The I/O interface will not be closed or free by the VFS file, the caller will
     628 * do so after it is done with the VFS file and has released the instances of
     629 * the VFS object returned by this API.
     630 *
     631 * @return  VBox status code.
     632 * @param   pVDIfsIo        Pointer to the VD I/O interface.
     633 * @param   pvStorage       The storage argument to pass to the interface
     634 *                          methods.
     635 * @param   fFlags          RTFILE_O_XXX, access mask requied.
     636 * @param   phVfsFile       Where to return the VFS file handle on success.
     637 */
     638VBOXDDU_DECL(int) VDIfCreateVfsFile(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSFILE phVfsFile);
    623639
    624640
  • trunk/src/VBox/Storage/VDIfVfs.cpp

    r48176 r48849  
    3737
    3838/**
    39  * The internal data of a DVM volume I/O stream.
    40  */
    41 typedef struct VDIFVFSIOS
     39 * The internal data of an VD I/O to VFS file or I/O stream wrapper.
     40 */
     41typedef struct VDIFVFSIOSFILE
    4242{
    4343    /** The VD I/O interface we wrap. */
     
    4545    /** User pointer to pass to the VD I/O interface methods. */
    4646    void           *pvStorage;
    47     /** The current stream position relative to the VDIfCreateVfsStream call. */
    48     uint64_t        offCurPos;
    49 } VDIFVFSIOS;
     47    /** The current stream position. */
     48    RTFOFF          offCurPos;
     49} VDIFVFSIOSFILE;
    5050/** Pointer to a the internal data of a DVM volume file. */
    51 typedef VDIFVFSIOS *PVDIFVFSIOS;
     51typedef VDIFVFSIOSFILE *PVDIFVFSIOSFILE;
    5252
    5353
     
    8080static DECLCALLBACK(int) vdIfVfsIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
    8181{
    82     PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
     82    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
    8383    Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
     84    Assert(off >= -1);
    8485
    8586    /*
     
    9192    if (RT_SUCCESS(rc))
    9293    {
    93         size_t cbAdvance = (pcbRead ? *pcbRead : pSgBuf->paSegs[0].cbSeg);
     94        size_t cbAdvance = pcbRead ? *pcbRead : pSgBuf->paSegs[0].cbSeg;
    9495        pThis->offCurPos = off + cbAdvance;
    9596        if (pcbRead && !cbAdvance)
     
    105106static DECLCALLBACK(int) vdIfVfsIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
    106107{
    107     PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
     108    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
    108109    Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
     110    Assert(off >= -1);
    109111
    110112    /*
     
    125127static DECLCALLBACK(int) vdIfVfsIos_Flush(void *pvThis)
    126128{
    127     PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
     129    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
    128130    return vdIfIoFileFlushSync(pThis->pVDIfsIo, pThis->pvStorage);
    129131}
     
    134136 */
    135137static DECLCALLBACK(int) vdIfVfsIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
    136                                               uint32_t *pfRetEvents)
     138                                            uint32_t *pfRetEvents)
    137139{
    138140    NOREF(pvThis);
     
    154156static DECLCALLBACK(int) vdIfVfsIos_Tell(void *pvThis, PRTFOFF poffActual)
    155157{
    156     PVDIFVFSIOS pThis = (PVDIFVFSIOS)pvThis;
     158    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
    157159    *poffActual = pThis->offCurPos;
    158160    return VINF_SUCCESS;
    159161}
    160162
    161 /**
    162  * Standard file operations.
    163  */
    164 DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_vdIfVfsStdIosOps =
     163
     164/**
     165 * VFS I/O stream operations for a VD file or stream.
     166 */
     167DECL_HIDDEN_CONST(const RTVFSIOSTREAMOPS) g_vdIfVfsIosOps =
    165168{
    166169    { /* Obj */
     
    194197     */
    195198    RTVFSIOSTREAM hVfsIos;
    196     PVDIFVFSIOS pThis;
    197     int rc = RTVfsNewIoStream(&g_vdIfVfsStdIosOps, sizeof(*pThis), fFlags,
     199    PVDIFVFSIOSFILE pThis;
     200    int rc = RTVfsNewIoStream(&g_vdIfVfsIosOps, sizeof(*pThis), fFlags,
    198201                              NIL_RTVFS, NIL_RTVFSLOCK, &hVfsIos, (void **)&pThis);
    199202    if (RT_SUCCESS(rc))
     
    210213}
    211214
     215
     216
     217/**
     218 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
     219 */
     220static DECLCALLBACK(int) vdIfVfsFile_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
     221{
     222    NOREF(pvThis);
     223    NOREF(fMode);
     224    NOREF(fMask);
     225    return VERR_NOT_SUPPORTED;
     226}
     227
     228
     229/**
     230 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
     231 */
     232static DECLCALLBACK(int) vdIfVfsFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
     233                                              PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
     234{
     235    NOREF(pvThis);
     236    NOREF(pAccessTime);
     237    NOREF(pModificationTime);
     238    NOREF(pChangeTime);
     239    NOREF(pBirthTime);
     240    return VERR_NOT_SUPPORTED;
     241}
     242
     243
     244/**
     245 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
     246 */
     247static DECLCALLBACK(int) vdIfVfsFile_SetOwner(void *pvThis, RTUID uid, RTGID gid)
     248{
     249    NOREF(pvThis);
     250    NOREF(uid);
     251    NOREF(gid);
     252    return VERR_NOT_SUPPORTED;
     253}
     254
     255
     256/**
     257 * @interface_method_impl{RTVFSFILEOPS,pfnSeek}
     258 */
     259static DECLCALLBACK(int) vdIfVfsFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
     260{
     261    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
     262
     263    uint64_t cbFile;
     264    int rc = vdIfIoFileGetSize(pThis->pVDIfsIo, pThis->pvStorage, &cbFile);
     265    if (RT_FAILURE(rc))
     266        return rc;
     267    if (cbFile >= (uint64_t)RTFOFF_MAX)
     268        cbFile = RTFOFF_MAX;
     269
     270    /* Recalculate the request to RTFILE_SEEK_BEGIN. */
     271    switch (uMethod)
     272    {
     273        case RTFILE_SEEK_BEGIN:
     274            break;
     275        case RTFILE_SEEK_CURRENT:
     276            offSeek += pThis->offCurPos;
     277            break;
     278        case RTFILE_SEEK_END:
     279            offSeek = cbFile + offSeek;
     280            break;
     281        default:
     282            AssertFailedReturn(VERR_INVALID_PARAMETER);
     283    }
     284
     285    /* Do limit checks. */
     286    if (offSeek < 0)
     287        offSeek = 0;
     288    else if (offSeek > (RTFOFF)cbFile)
     289        offSeek = cbFile;
     290
     291    /* Apply and return. */
     292    pThis->offCurPos = offSeek;
     293    if (poffActual)
     294        *poffActual = offSeek;
     295
     296    return VINF_SUCCESS;
     297}
     298
     299
     300/**
     301 * @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
     302 */
     303static DECLCALLBACK(int) vdIfVfsFile_QuerySize(void *pvThis, uint64_t *pcbFile)
     304{
     305    PVDIFVFSIOSFILE pThis = (PVDIFVFSIOSFILE)pvThis;
     306    return vdIfIoFileGetSize(pThis->pVDIfsIo, pThis->pvStorage, pcbFile);
     307}
     308
     309
     310
     311/**
     312 * VFS file operations for a VD file.
     313 */
     314DECL_HIDDEN_CONST(const RTVFSFILEOPS) g_vdIfVfsFileOps =
     315{
     316    { /* I/O stream */
     317        { /* Obj */
     318            RTVFSOBJOPS_VERSION,
     319            RTVFSOBJTYPE_FILE,
     320            "VDIfFile",
     321            vdIfVfsIos_Close,
     322            vdIfVfsIos_QueryInfo,
     323            RTVFSOBJOPS_VERSION
     324        },
     325        RTVFSIOSTREAMOPS_VERSION,
     326        RTVFSIOSTREAMOPS_FEAT_NO_SG,
     327        vdIfVfsIos_Read,
     328        vdIfVfsIos_Write,
     329        vdIfVfsIos_Flush,
     330        vdIfVfsIos_PollOne,
     331        vdIfVfsIos_Tell,
     332        NULL /*Skip*/,
     333        NULL /*ZeroFill*/,
     334        RTVFSIOSTREAMOPS_VERSION,
     335    },
     336    RTVFSFILEOPS_VERSION,
     337    0,
     338    { /* ObjSet */
     339        RTVFSOBJSETOPS_VERSION,
     340        RT_OFFSETOF(RTVFSFILEOPS, Stream.Obj) - RT_OFFSETOF(RTVFSFILEOPS, ObjSet),
     341        vdIfVfsFile_SetMode,
     342        vdIfVfsFile_SetTimes,
     343        vdIfVfsFile_SetOwner,
     344        RTVFSOBJSETOPS_VERSION
     345    },
     346    vdIfVfsFile_Seek,
     347    vdIfVfsFile_QuerySize,
     348    RTVFSFILEOPS_VERSION,
     349};
     350
     351
     352VBOXDDU_DECL(int) VDIfCreateVfsFile(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSFILE phVfsFile)
     353{
     354    AssertPtrReturn(pVDIfsIo, VERR_INVALID_HANDLE);
     355    AssertPtrReturn(phVfsFile, VERR_INVALID_POINTER);
     356
     357    /*
     358     * Create the volume file.
     359     */
     360    RTVFSFILE hVfsFile;
     361    PVDIFVFSIOSFILE pThis;
     362    int rc = RTVfsNewFile(&g_vdIfVfsFileOps, sizeof(*pThis), fFlags,
     363                          NIL_RTVFS, NIL_RTVFSLOCK, &hVfsFile, (void **)&pThis);
     364    if (RT_SUCCESS(rc))
     365    {
     366        pThis->pVDIfsIo  = pVDIfsIo;
     367        pThis->pvStorage = pvStorage;
     368        pThis->offCurPos = 0;
     369
     370        *phVfsFile = hVfsFile;
     371        return VINF_SUCCESS;
     372    }
     373
     374    return rc;
     375}
     376
     377
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