VirtualBox

Ignore:
Timestamp:
Sep 10, 2010 12:17:06 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
65754
Message:

vbox-img: info/compact command and cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/testcase/vbox-img.cpp

    r32380 r32395  
    4747                 "                [--srcformat VDI|VMDK|VHD|RAW|..]\n"
    4848                 "                [--dstformat VDI|VMDK|VHD|RAW|..]\n"
    49                  "                [--variant Standard,Fixed,Split2G,Stream,ESX]\n",
     49                 "                [--variant Standard,Fixed,Split2G,Stream,ESX]\n"
     50                 "\n"
     51                 "   info         --filename <filename>\n"
     52                 "\n"
     53                 "   compact      --filename <filename>\n",
    5054                 g_pszProgName);
    5155}
     
    7377    va_list args;
    7478    va_start(args, pszFormat);
    75     RTMsgWarningV(pszFormat, args);
     79    RTPrintfV(pszFormat, args);
    7680    va_end(args);
    7781    return VINF_SUCCESS;
     
    8993             "Syntax error: %N\n", pszFormat, &args);
    9094    va_end(args);
     95    showUsage();
    9196    return 1;
    9297}
     98int errorRuntime(const char *pszFormat, ...)
     99{
     100    va_list args;
     101   
     102    va_start(args, pszFormat);
     103    RTPrintf("\n"
     104             "Error: %N\n", pszFormat, &args);
     105    va_end(args);
     106    return 1;
     107}
     108
    93109
    94110
     
    163179        rc = VDGetFormat(NULL, pszFilename, &pszFormat);
    164180        if (RT_FAILURE(rc))
    165             return errorSyntax("Format autodetect failed: %Rrc\n", rc);
     181            return errorRuntime("Format autodetect failed: %Rrc\n", rc);
    166182    }
    167183   
     
    169185    rc = VDCreate(pVDIfs, &pVD);
    170186    if (RT_FAILURE(rc))
    171         return errorSyntax("Cannot create the virtual disk container: %Rrc\n", rc);
     187        return errorRuntime("Cannot create the virtual disk container: %Rrc\n", rc);
    172188   
    173189   
    174190    rc = VDOpen(pVD, pszFormat, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
    175191    if (RT_FAILURE(rc))
    176         return errorSyntax("Cannot open the virtual disk image \"%s\": %Rrc\n",
    177                            pszFilename, rc);
     192        return errorRuntime("Cannot open the virtual disk image \"%s\": %Rrc\n",
     193                            pszFilename, rc);
    178194   
    179195    RTUUID oldImageUuid;
    180196    rc = VDGetUuid(pVD, VD_LAST_IMAGE, &oldImageUuid);
    181197    if (RT_FAILURE(rc))
    182         return errorSyntax("Cannot get UUID of virtual disk image \"%s\": %Rrc\n",
    183                            pszFilename, rc);
     198        return errorRuntime("Cannot get UUID of virtual disk image \"%s\": %Rrc\n",
     199                            pszFilename, rc);
    184200   
    185201    RTPrintf("Old image UUID:  %RTuuid\n", &oldImageUuid);
     
    188204    rc = VDGetParentUuid(pVD, VD_LAST_IMAGE, &oldParentUuid);
    189205    if (RT_FAILURE(rc))
    190         return errorSyntax("Cannot get parent UUID of virtual disk image \"%s\": %Rrc\n",
    191                            pszFilename, rc);
     206        return errorRuntime("Cannot get parent UUID of virtual disk image \"%s\": %Rrc\n",
     207                            pszFilename, rc);
    192208   
    193209    RTPrintf("Old parent UUID: %RTuuid\n", &oldParentUuid);
     
    198214        rc = VDSetUuid(pVD, VD_LAST_IMAGE, &imageUuid);
    199215        if (RT_FAILURE(rc))
    200             return errorSyntax("Cannot set UUID of virtual disk image \"%s\": %Rrc\n",
    201                                pszFilename, rc);
     216            return errorRuntime("Cannot set UUID of virtual disk image \"%s\": %Rrc\n",
     217                                pszFilename, rc);
    202218    }
    203219   
     
    207223        rc = VDSetParentUuid(pVD, VD_LAST_IMAGE, &parentUuid);
    208224        if (RT_FAILURE(rc))
    209             return errorSyntax("Cannot set parent UUID of virtual disk image \"%s\": %Rrc\n",
    210                                pszFilename, rc);
     225            return errorRuntime("Cannot set parent UUID of virtual disk image \"%s\": %Rrc\n",
     226                                pszFilename, rc);
    211227    }
    212228   
    213229    rc = VDCloseAll(pVD);
    214230    if (RT_FAILURE(rc))
    215         return errorSyntax("Closing image failed! rc=%Rrc\n", rc);
     231        return errorRuntime("Closing image failed! rc=%Rrc\n", rc);
    216232   
    217233    if (pszFormat)
     
    333349        if (RT_FAILURE(rc))
    334350        {
    335             errorSyntax("Error while creating source disk container: %Rrc\n", rc);
     351            errorRuntime("Error while creating source disk container: %Rrc\n", rc);
    336352            break;
    337353        }
     
    340356        if (RT_FAILURE(rc))
    341357        {
    342             errorSyntax("Error while opening source image: %Rrc\n", rc);
     358            errorRuntime("Error while opening source image: %Rrc\n", rc);
    343359            break;
    344360        }
     
    351367        if (RT_FAILURE(rc))
    352368        {
    353             errorSyntax("Error while creating the destination disk container: %Rrc\n", rc);
     369            errorRuntime("Error while creating the destination disk container: %Rrc\n", rc);
    354370            break;
    355371        }
     
    363379        if (RT_FAILURE(rc))
    364380        {
    365             errorSyntax("Error while copying the image: %Rrc\n", rc);
     381            errorRuntime("Error while copying the image: %Rrc\n", rc);
    366382            break;
    367383        }
     
    376392   
    377393    return RT_SUCCESS(rc) ? 0 : 1;
     394}
     395
     396
     397int handleInfo(HandlerArg *a)
     398{
     399    int rc = VINF_SUCCESS;
     400    PVBOXHDD pDisk = NULL;
     401    const char *pszFilename = NULL;
     402
     403    /* Parse the command line. */
     404    static const RTGETOPTDEF s_aOptions[] =
     405    {
     406        { "--filename", 'f', RTGETOPT_REQ_STRING }
     407    };
     408    int ch;
     409    RTGETOPTUNION ValueUnion;
     410    RTGETOPTSTATE GetState;
     411    RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
     412    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
     413    {
     414        switch (ch)
     415        {
     416            case 'f':   // --filename
     417                pszFilename = ValueUnion.psz;
     418                break;
     419               
     420            default:
     421                ch = RTGetOptPrintError(ch, &ValueUnion);
     422                showUsage();
     423                return ch;
     424        }
     425    }
     426   
     427    /* Check for mandatory parameters. */
     428    if (!pszFilename)
     429        return errorSyntax("Mandatory --filename option missing\n");
     430   
     431    /* just try it */
     432    char *pszFormat = NULL;
     433    rc = VDGetFormat(NULL, pszFilename, &pszFormat);
     434    if (RT_FAILURE(rc))
     435        return errorSyntax("Format autodetect failed: %Rrc\n", rc);
     436
     437    rc = VDCreate(pVDIfs, &pDisk);
     438    if (RT_FAILURE(rc))
     439        return errorRuntime("Error while creating the virtual disk container: %Rrc\n", rc);
     440   
     441    /* Open the image */
     442    rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_INFO, NULL);
     443    if (RT_FAILURE(rc))
     444        return errorRuntime("Error while opening the image: %Rrc\n", rc);
     445   
     446    VDDumpImages(pDisk);
     447   
     448    VDCloseAll(pDisk);
     449   
     450    return rc;
     451}
     452
     453int handleCompact(HandlerArg *a)
     454{
     455    int rc = VINF_SUCCESS;
     456    PVBOXHDD pDisk = NULL;
     457    const char *pszFilename = NULL;
     458   
     459    /* Parse the command line. */
     460    static const RTGETOPTDEF s_aOptions[] =
     461    {
     462        { "--filename", 'f', RTGETOPT_REQ_STRING }
     463    };
     464    int ch;
     465    RTGETOPTUNION ValueUnion;
     466    RTGETOPTSTATE GetState;
     467    RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
     468    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
     469    {
     470        switch (ch)
     471        {
     472            case 'f':   // --filename
     473                pszFilename = ValueUnion.psz;
     474                break;
     475               
     476            default:
     477                ch = RTGetOptPrintError(ch, &ValueUnion);
     478                showUsage();
     479                return ch;
     480        }
     481    }
     482   
     483    /* Check for mandatory parameters. */
     484    if (!pszFilename)
     485        return errorSyntax("Mandatory --filename option missing\n");
     486   
     487    /* just try it */
     488    char *pszFormat = NULL;
     489    rc = VDGetFormat(NULL, pszFilename, &pszFormat);
     490    if (RT_FAILURE(rc))
     491        return errorSyntax("Format autodetect failed: %Rrc\n", rc);
     492   
     493    rc = VDCreate(pVDIfs, &pDisk);
     494    if (RT_FAILURE(rc))
     495        return errorRuntime("Error while creating the virtual disk container: %Rrc\n", rc);
     496   
     497    /* Open the image */
     498    rc = VDOpen(pDisk, pszFormat, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
     499    if (RT_FAILURE(rc))
     500        return errorRuntime("Error while opening the image: %Rrc\n", rc);
     501   
     502    rc = VDCompact(pDisk, 0, NULL);
     503    if (RT_FAILURE(rc))
     504        errorRuntime("Error while compacting image: %Rrc\n", rc);
     505   
     506    VDCloseAll(pDisk);
     507   
     508    return rc;
    378509}
    379510
     
    470601        { "setuuid", handleSetUUID },
    471602        { "convert", handleConvert },
     603        { "info",    handleInfo    },
     604        { "compact", handleCompact },
    472605        { NULL,               NULL }
    473606    };
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