VirtualBox

Changeset 76905 in vbox


Ignore:
Timestamp:
Jan 20, 2019 7:49:15 PM (6 years ago)
Author:
vboxsync
Message:

Runtime/testcase/tstRTDvm: Resurrect testcase as an example on how to use the current API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTDvm.cpp

    r76553 r76905  
    3535#include <iprt/file.h>
    3636#include <iprt/string.h>
     37#include <iprt/vfs.h>
    3738
    3839
     
    4041*   Structures and Typedefs                                                                                                      *
    4142*********************************************************************************************************************************/
    42 /**
    43  * Disk structure.
    44  */
    45 typedef struct TSTRTDVMDISK
    46 {
    47     /** Flag whether this disk uses the image file or a volume. */
    48     bool            fUseImage;
    49     /** Data dependent on the flag. */
    50     union
    51     {
    52         /** File handle of the image. */
    53         RTVFSFILE   hImage;
    54         /** Handle of the volume. */
    55         RTDVMVOLUME hVol;
    56     };
    57 } TSTRTDVMDISK, *PTSTRTDVMDISK;
    5843
    5944
    60 #if 0
    61 static DECLCALLBACK(int) dvmDiskRead(void *pvUser, uint64_t off, void *pvBuf, size_t cbRead)
    62 {
    63     PTSTRTDVMDISK pDisk = (PTSTRTDVMDISK)pvUser;
    64 
    65     if (pDisk->fUseImage)
    66         return RTFileReadAt(pDisk->hImage, off, pvBuf, cbRead, NULL);
    67     return RTDvmVolumeRead(pDisk->hVol, off, pvBuf, cbRead);
    68 }
    69 
    70 static DECLCALLBACK(int) dvmDiskWrite(void *pvUser, uint64_t off, const void *pvBuf, size_t cbWrite)
    71 {
    72     PTSTRTDVMDISK pDisk = (PTSTRTDVMDISK)pvUser;
    73 
    74     if (pDisk->fUseImage)
    75         return RTFileWriteAt(pDisk->hImage, off, pvBuf, cbWrite, NULL);
    76     return RTDvmVolumeWrite(pDisk->hVol, off, pvBuf, cbWrite);
    77 }
    78 
    79 static int tstRTDvmVolume(RTTEST hTest, PTSTRTDVMDISK pDisk, uint64_t cb, unsigned cNesting)
     45static int tstRTDvmVolume(RTTEST hTest, RTVFSFILE hVfsDisk, unsigned cNesting)
    8046{
    8147    char szPrefix[100];
     
    9258    RTTestSubF(hTest, "Create DVM");
    9359    RTDVM hVolMgr;
    94     rc = RTDvmCreate(&hVolMgr, dvmDiskRead, dvmDiskWrite, cb, 512, 0, pDisk);
     60    rc = RTDvmCreate(&hVolMgr, hVfsDisk, 512, 0 /*fFlags*/);
    9561    if (RT_FAILURE(rc))
    9662    {
     
    11076        return VINF_SUCCESS;
    11177
    112     RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Successfully opened map with format: %s.\n", szPrefix, RTDvmMapGetFormat(hVolMgr));
     78    RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Successfully opened map with format: %s.\n", szPrefix, RTDvmMapGetFormatName(hVolMgr));
    11379
    11480    /* Dump all volumes. */
     
    147113         * (think of MBR partitions with a bsdlabel inside)
    148114         */
    149         TSTRTDVMDISK Disk;
    150         Disk.fUseImage = false;
    151         Disk.hVol      = hVol;
    152         rc = tstRTDvmVolume(hTest, &Disk, RTDvmVolumeGetSize(hVol), cNesting + 1);
     115        RTVFSFILE hVfsVol;
     116        rc = RTDvmVolumeCreateVfsFile(hVol, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsVol);
     117        if (RT_SUCCESS(rc))
     118        {
     119            rc = tstRTDvmVolume(hTest, hVfsVol, cNesting + 1);
     120            RTVfsFileRelease(hVfsVol);
     121        }
     122        else
     123            RTTestIFailed("RTDvmVolumeCreateVfsFile -> %Rrc", rc);
    153124
    154125        RTDVMVOLUME hVolNext;
     
    171142    return rc;
    172143}
    173 #endif
     144
    174145
    175146int main(int argc, char **argv)
     
    193164    }
    194165
    195 #if 1
    196     RTTestFailed(hTest, "Needs updating to RTDvm API changes!");
    197 #else
    198     /* Open image. */
    199     RTFILE hFile;
    200     uint64_t cb = 0;
    201     rc = RTFileOpen(&hFile, argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE);
     166    RTVFSFILE hVfsDisk;
     167    rc = RTVfsFileOpenNormal(argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsDisk);
    202168    if (RT_FAILURE(rc))
    203169    {
    204         RTTestIFailed("RTFileOpen -> %Rrc", rc);
     170        RTTestIFailed("RTVfsFileOpenNormal -> %Rrc", rc);
    205171        return RTTestSummaryAndDestroy(hTest);
    206172    }
    207173
    208     rc = RTFileGetSize(hFile, &cb);
     174    uint64_t cb = 0;
     175    rc = RTVfsFileGetSize(hVfsDisk, &cb);
    209176    if (   RT_FAILURE(rc)
    210177        || cb % 512 != 0) /* Assume 512 byte sector size. */
    211178    {
    212         RTTestIFailed("RTFileGetSize -> %Rrc", rc);
     179        RTTestIFailed("RTVfsFileGetSize -> %Rrc", rc);
    213180        return RTTestSummaryAndDestroy(hTest);
    214181    }
    215182
    216     TSTRTDVMDISK Disk;
    217 
    218     Disk.fUseImage = true;
    219     Disk.hImage    = hFile;
    220     rc = tstRTDvmVolume(hTest, &Disk, cb, 0);
     183    rc = tstRTDvmVolume(hTest, hVfsDisk, 0);
    221184
    222185    RTTESTI_CHECK(rc == VINF_SUCCESS);
     
    225188     * Summary
    226189     */
    227 #endif
    228190    return RTTestSummaryAndDestroy(hTest);
    229191}
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