Changeset 35239 in vbox for trunk/src/VBox/Frontends/VBoxManage
- Timestamp:
- Dec 20, 2010 12:45:14 PM (14 years ago)
- Location:
- trunk/src/VBox/Frontends/VBoxManage
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/VBoxManage.h
r34971 r35239 215 215 216 216 /* VBoxManageDisk.cpp */ 217 HRESULT findMedium(HandlerArg *a, const char *pszFilenameOrUuid, 218 DeviceType_T enmDevType, bool fSilent, 219 ComPtr<IMedium> &pMedium); 220 HRESULT findOrOpenMedium(HandlerArg *a, const char *pszFilenameOrUuid, 221 DeviceType_T enmDevType, ComPtr<IMedium> &pMedium, 222 bool *pfWasUnknown); 217 223 int handleCreateHardDisk(HandlerArg *a); 218 224 int handleModifyHardDisk(HandlerArg *a); -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageDisk.cpp
r35085 r35239 149 149 } 150 150 151 HRESULT findMedium(HandlerArg *a, const char *pszFilenameOrUuid, 152 DeviceType_T enmDevType, bool fSilent, 153 ComPtr<IMedium> &pMedium) 154 { 155 HRESULT rc; 156 Guid id(pszFilenameOrUuid); 157 char szFilenameAbs[RTPATH_MAX] = ""; 158 159 /* If it is no UUID, convert the filename to an absolute one. */ 160 if (id.isEmpty()) 161 { 162 int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs)); 163 if (RT_FAILURE(irc)) 164 { 165 if (!fSilent) 166 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid); 167 return E_FAIL; 168 } 169 pszFilenameOrUuid = szFilenameAbs; 170 } 171 172 if (!fSilent) 173 CHECK_ERROR(a->virtualBox, FindMedium(Bstr(pszFilenameOrUuid).raw(), 174 enmDevType, pMedium.asOutParam())); 175 else 176 rc = a->virtualBox->FindMedium(Bstr(pszFilenameOrUuid).raw(), 177 enmDevType, pMedium.asOutParam()); 178 return rc; 179 } 180 181 HRESULT findOrOpenMedium(HandlerArg *a, const char *pszFilenameOrUuid, 182 DeviceType_T enmDevType, ComPtr<IMedium> &pMedium, 183 bool *pfWasUnknown) 184 { 185 HRESULT rc; 186 bool fWasUnknown = false; 187 Guid id(pszFilenameOrUuid); 188 char szFilenameAbs[RTPATH_MAX] = ""; 189 190 /* If it is no UUID, convert the filename to an absolute one. */ 191 if (id.isEmpty()) 192 { 193 int irc = RTPathAbs(pszFilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs)); 194 if (RT_FAILURE(irc)) 195 { 196 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilenameOrUuid); 197 return E_FAIL; 198 } 199 pszFilenameOrUuid = szFilenameAbs; 200 } 201 202 rc = a->virtualBox->FindMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, 203 pMedium.asOutParam()); 204 /* If the medium is unknown try to open it. */ 205 if (!pMedium) 206 { 207 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(pszFilenameOrUuid).raw(), 208 enmDevType, AccessMode_ReadWrite, 209 pMedium.asOutParam())); 210 if (SUCCEEDED(rc)) 211 fWasUnknown = true; 212 } 213 if (RT_VALID_PTR(pfWasUnknown)) 214 *pfWasUnknown = fWasUnknown; 215 return rc; 216 } 217 218 static HRESULT createHardDisk(HandlerArg *a, const char *pszFormat, 219 const char *pszFilename, ComPtr<IMedium> &pMedium) 220 { 221 HRESULT rc; 222 char szFilenameAbs[RTPATH_MAX] = ""; 223 224 /** @todo laziness shortcut. should really check the MediumFormatCapabilities */ 225 if (RTStrICmp(pszFormat, "iSCSI")) 226 { 227 int irc = RTPathAbs(pszFilename, szFilenameAbs, sizeof(szFilenameAbs)); 228 if (RT_FAILURE(irc)) 229 { 230 RTMsgError("Cannot convert filename \"%s\" to absolute path", pszFilename); 231 return E_FAIL; 232 } 233 pszFilename = szFilenameAbs; 234 } 235 236 CHECK_ERROR(a->virtualBox, CreateHardDisk(Bstr(pszFormat).raw(), 237 Bstr(pszFilename).raw(), 238 pMedium.asOutParam())); 239 return rc; 240 } 241 151 242 static const RTGETOPTDEF g_aCreateHardDiskOptions[] = 152 243 { … … 168 259 HRESULT rc; 169 260 int vrc; 170 Bstr filename;261 const char *filename = NULL; 171 262 uint64_t size = 0; 172 Bstrformat = "VDI";263 const char *format = "VDI"; 173 264 MediumVariant_T DiskVariant = MediumVariant_Standard; 174 265 … … 234 325 235 326 /* check the outcome */ 236 if ( filename.isEmpty() 327 if ( !filename 328 || !*filename 237 329 || size == 0) 238 330 return errorSyntax(USAGE_CREATEHD, "Parameters --filename and --size are required"); 239 331 240 332 /* check for filename extension */ 333 /** @todo use IMediumFormat to cover all extensions generically */ 241 334 Utf8Str strName(filename); 242 335 if (!RTPathHaveExt(strName.c_str())) … … 249 342 else 250 343 strName.append(".vdi"); 251 filename = Bstr(strName);344 filename = strName.c_str(); 252 345 } 253 346 254 347 ComPtr<IMedium> hardDisk; 255 CHECK_ERROR(a->virtualBox, CreateHardDisk(format.raw(), filename.raw(), 256 hardDisk.asOutParam())); 348 rc = createHardDisk(a, format, filename, hardDisk); 257 349 if (SUCCEEDED(rc) && hardDisk) 258 350 { … … 381 473 * may be opened on demand. */ 382 474 if (fModifyDiskType || fModifyAutoReset) 383 { 384 CHECK_ERROR(a->virtualBox, FindMedium(Bstr(FilenameOrUuid).raw(), 385 DeviceType_HardDisk, 386 hardDisk.asOutParam())); 387 } 475 rc = findMedium(a, FilenameOrUuid, DeviceType_HardDisk, false /* fSilent */, hardDisk); 388 476 else 389 { 390 rc = a->virtualBox->FindMedium(Bstr(FilenameOrUuid).raw(), 391 DeviceType_HardDisk, 392 hardDisk.asOutParam()); 393 /* the hard disk image might not be registered */ 394 if (!hardDisk) 395 { 396 unknown = true; 397 rc = a->virtualBox->OpenMedium(Bstr(FilenameOrUuid).raw(), 398 DeviceType_HardDisk, 399 AccessMode_ReadWrite, 400 hardDisk.asOutParam()); 401 if (rc == VBOX_E_FILE_ERROR) 402 { 403 char szFilenameAbs[RTPATH_MAX] = ""; 404 int irc = RTPathAbs(FilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs)); 405 if (RT_FAILURE(irc)) 406 { 407 RTMsgError("Cannot convert filename \"%s\" to absolute path", FilenameOrUuid); 408 return 1; 409 } 410 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(szFilenameAbs).raw(), 411 DeviceType_HardDisk, 412 AccessMode_ReadWrite, 413 hardDisk.asOutParam())); 414 } 415 } 416 } 477 rc = findOrOpenMedium(a, FilenameOrUuid, DeviceType_HardDisk, 478 hardDisk, &unknown); 417 479 if (FAILED(rc)) 418 480 return 1; … … 492 554 HRESULT rc; 493 555 int vrc; 494 Bstr src, dst; 556 const char *pszSrc = NULL; 557 const char *pszDst = NULL; 495 558 Bstr format; 496 559 MediumVariant_T DiskVariant = MediumVariant_Standard; … … 530 593 531 594 case VINF_GETOPT_NOT_OPTION: 532 if ( src.isEmpty())533 src = ValueUnion.psz;534 else if ( dst.isEmpty())535 dst = ValueUnion.psz;595 if (!pszSrc) 596 pszSrc = ValueUnion.psz; 597 else if (!pszDst) 598 pszDst = ValueUnion.psz; 536 599 else 537 600 return errorSyntax(USAGE_CLONEHD, "Invalid parameter '%s'", ValueUnion.psz); … … 555 618 } 556 619 557 if ( src.isEmpty())620 if (!pszSrc) 558 621 return errorSyntax(USAGE_CLONEHD, "Mandatory UUID or input file parameter missing"); 559 if ( dst.isEmpty())622 if (!pszDst) 560 623 return errorSyntax(USAGE_CLONEHD, "Mandatory output file parameter missing"); 561 624 if (fExisting && (!format.isEmpty() || DiskVariant != MediumType_Normal)) … … 567 630 bool fDstUnknown = false; 568 631 569 rc = a->virtualBox->FindMedium(src.raw(), DeviceType_HardDisk, 570 srcDisk.asOutParam()); 571 /* no? well, then it's an unknown image */ 572 if (FAILED (rc)) 573 { 574 rc = a->virtualBox->OpenMedium(src.raw(), DeviceType_HardDisk, 575 AccessMode_ReadWrite, 576 srcDisk.asOutParam()); 577 if (rc == VBOX_E_FILE_ERROR) 578 { 579 char szFilenameAbs[RTPATH_MAX] = ""; 580 int irc = RTPathAbs(Utf8Str(src).c_str(), szFilenameAbs, sizeof(szFilenameAbs)); 581 if (RT_FAILURE(irc)) 582 { 583 RTMsgError("Cannot convert filename \"%s\" to absolute path", Utf8Str(src).c_str()); 584 return 1; 585 } 586 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(szFilenameAbs).raw(), 587 DeviceType_HardDisk, 588 AccessMode_ReadWrite, 589 srcDisk.asOutParam())); 590 } 591 else if (SUCCEEDED(rc)) 592 fSrcUnknown = true; 593 else 594 { 595 com::GluePrintRCMessage(rc); 596 return 1; 597 } 598 } 632 rc = findOrOpenMedium(a, pszSrc, DeviceType_HardDisk, srcDisk, &fSrcUnknown); 633 if (FAILED(rc)) 634 return 1; 599 635 600 636 do … … 603 639 if (fExisting) 604 640 { 605 rc = a->virtualBox->FindMedium(dst.raw(), DeviceType_HardDisk, 606 dstDisk.asOutParam()); 607 /* no? well, then it's an unknown image */ 641 rc = findOrOpenMedium(a, pszDst, DeviceType_HardDisk, dstDisk, &fDstUnknown); 608 642 if (FAILED(rc)) 609 { 610 rc = a->virtualBox->OpenMedium(dst.raw(), DeviceType_HardDisk, 611 AccessMode_ReadWrite, 612 dstDisk.asOutParam()); 613 if (rc == VBOX_E_FILE_ERROR) 614 { 615 char szFilenameAbs[RTPATH_MAX] = ""; 616 int irc = RTPathAbs(Utf8Str(dst).c_str(), szFilenameAbs, sizeof(szFilenameAbs)); 617 if (RT_FAILURE(irc)) 618 { 619 RTMsgError("Cannot convert filename \"%s\" to absolute path", Utf8Str(dst).c_str()); 620 return 1; 621 } 622 CHECK_ERROR_BREAK(a->virtualBox, OpenMedium(Bstr(szFilenameAbs).raw(), 623 DeviceType_HardDisk, 624 AccessMode_ReadWrite, 625 dstDisk.asOutParam())); 626 } 627 else if (FAILED(rc)) 628 { 629 com::GluePrintRCMessage(rc); 630 break; 631 } 632 633 /* If the image wasn't opened before, close it at the end. */ 634 if (SUCCEEDED(rc)) 635 fDstUnknown = true; 636 } 637 if (SUCCEEDED(rc)) 638 { 639 /* Perform accessibility check now. */ 640 MediumState_T state; 641 CHECK_ERROR_BREAK(dstDisk, RefreshState(&state)); 642 CHECK_ERROR_BREAK(dstDisk, COMGETTER(Format)(format.asOutParam())); 643 } 643 break; 644 645 /* Perform accessibility check now. */ 646 MediumState_T state; 647 CHECK_ERROR_BREAK(dstDisk, RefreshState(&state)); 648 CHECK_ERROR_BREAK(dstDisk, COMGETTER(Format)(format.asOutParam())); 644 649 } 645 650 else … … 648 653 if (format.isEmpty()) 649 654 CHECK_ERROR_BREAK(srcDisk, COMGETTER(Format)(format.asOutParam())); 650 CHECK_ERROR_BREAK(a->virtualBox, CreateHardDisk(format.raw(),651 dst.raw(),652 dstDisk.asOutParam()));655 rc = createHardDisk(a, Utf8Str(format).c_str(), pszDst, dstDisk); 656 if (FAILED(rc)) 657 break; 653 658 } 654 659 … … 919 924 ComPtr<IMedium> hardDisk; 920 925 bool unknown = false; 921 /* first guess is that it's a UUID */ 922 rc = a->virtualBox->FindMedium(Bstr(FilenameOrUuid).raw(), 923 DeviceType_HardDisk, 924 hardDisk.asOutParam()); 925 /* no? well, then it's an unknown image */ 926 927 rc = findOrOpenMedium(a, FilenameOrUuid, DeviceType_HardDisk, hardDisk, &unknown); 926 928 if (FAILED(rc)) 927 { 928 rc = a->virtualBox->OpenMedium(Bstr(FilenameOrUuid).raw(), 929 DeviceType_HardDisk, 930 AccessMode_ReadWrite, 931 hardDisk.asOutParam()); 932 if (rc == VBOX_E_FILE_ERROR) 933 { 934 char szFilenameAbs[RTPATH_MAX] = ""; 935 int vrc = RTPathAbs(FilenameOrUuid, szFilenameAbs, sizeof(szFilenameAbs)); 936 if (RT_FAILURE(vrc)) 937 { 938 RTMsgError("Cannot convert filename \"%s\" to absolute path", FilenameOrUuid); 939 return 1; 940 } 941 CHECK_ERROR(a->virtualBox, OpenMedium(Bstr(szFilenameAbs).raw(), 942 DeviceType_HardDisk, 943 AccessMode_ReadWrite, 944 hardDisk.asOutParam())); 945 } 946 else if (SUCCEEDED(rc)) 947 unknown = true; 948 else 949 { 950 com::GluePrintRCMessage(rc); 951 return 1; 952 } 953 } 929 return 1; 954 930 955 931 do … … 1177 1153 1178 1154 if (cmd == CMD_DISK) 1179 CHECK_ERROR(a->virtualBox, FindMedium(Bstr(FilenameOrUuid).raw(), 1180 DeviceType_HardDisk, 1181 medium.asOutParam())); 1155 rc = findMedium(a, FilenameOrUuid, DeviceType_HardDisk, false /* fSilent */, medium); 1182 1156 else if (cmd == CMD_DVD) 1183 CHECK_ERROR(a->virtualBox, FindMedium(Bstr(FilenameOrUuid).raw(), 1184 DeviceType_DVD, 1185 medium.asOutParam())); 1157 rc = findMedium(a, FilenameOrUuid, DeviceType_DVD, false /* fSilent */, medium); 1186 1158 else if (cmd == CMD_FLOPPY) 1187 CHECK_ERROR(a->virtualBox, FindMedium(Bstr(FilenameOrUuid).raw(), 1188 DeviceType_Floppy, 1189 medium.asOutParam())); 1159 rc = findMedium(a, FilenameOrUuid, DeviceType_Floppy, false /* fSilent */, medium); 1190 1160 1191 1161 if (SUCCEEDED(rc) && medium) -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageModifyVM.cpp
r35146 r35239 683 683 { 684 684 ComPtr<IMedium> hardDisk; 685 rc = a->virtualBox->FindMedium(Bstr(ValueUnion.psz).raw(), 686 DeviceType_HardDisk, 687 hardDisk.asOutParam()); 685 rc = findOrOpenMedium(a, ValueUnion.psz, DeviceType_HardDisk, 686 hardDisk, NULL); 688 687 if (FAILED(rc)) 689 { 690 /* open the new hard disk object */ 691 CHECK_ERROR(a->virtualBox, 692 OpenMedium(Bstr(ValueUnion.psz).raw(), 693 DeviceType_HardDisk, 694 AccessMode_ReadWrite, 695 hardDisk.asOutParam())); 696 } 688 break; 697 689 if (hardDisk) 698 690 { … … 785 777 else 786 778 { 787 /* first guess is that it's a UUID */788 779 ComPtr<IMedium> hardDisk; 789 rc = a->virtualBox->FindMedium(Bstr(ValueUnion.psz).raw(), 790 DeviceType_HardDisk, 791 hardDisk.asOutParam()); 792 /* not successful? Then it must be a filename */ 780 rc = findOrOpenMedium(a, ValueUnion.psz, DeviceType_HardDisk, 781 hardDisk, NULL); 793 782 if (FAILED(rc)) 794 { 795 /* open the new hard disk object */ 796 CHECK_ERROR(a->virtualBox, 797 OpenMedium(Bstr(ValueUnion.psz).raw(), 798 DeviceType_HardDisk, 799 AccessMode_ReadWrite, 800 hardDisk.asOutParam())); 801 } 783 break; 802 784 if (hardDisk) 803 785 { … … 923 905 else 924 906 { 925 /* first assume it's a UUID */ 926 rc = a->virtualBox->FindMedium(Bstr(ValueUnion.psz).raw(), 927 DeviceType_DVD, 928 dvdMedium.asOutParam()); 929 if (FAILED(rc) || !dvdMedium) 930 { 931 /* not registered, do that on the fly */ 932 Bstr emptyUUID; 933 CHECK_ERROR(a->virtualBox, 934 OpenMedium(Bstr(ValueUnion.psz).raw(), 935 DeviceType_DVD, 936 AccessMode_ReadWrite, 937 dvdMedium.asOutParam())); 938 } 907 rc = findOrOpenMedium(a, ValueUnion.psz, DeviceType_DVD, 908 dvdMedium, NULL); 909 if (FAILED(rc)) 910 break; 939 911 if (!dvdMedium) 940 912 { … … 996 968 else 997 969 { 998 /* first assume it's a UUID */ 999 rc = a->virtualBox->FindMedium(Bstr(ValueUnion.psz).raw(), 1000 DeviceType_Floppy, 1001 floppyMedium.asOutParam()); 1002 if (FAILED(rc) || !floppyMedium) 1003 { 1004 /* not registered, do that on the fly */ 1005 Bstr emptyUUID; 1006 CHECK_ERROR(a->virtualBox, 1007 OpenMedium(Bstr(ValueUnion.psz).raw(), 1008 DeviceType_Floppy, 1009 AccessMode_ReadWrite, 1010 floppyMedium.asOutParam())); 1011 } 970 rc = findOrOpenMedium(a, ValueUnion.psz, DeviceType_Floppy, 971 floppyMedium, NULL); 972 if (FAILED(rc)) 973 break; 1012 974 if (!floppyMedium) 1013 975 { -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageStorageController.cpp
r35137 r35239 395 395 396 396 ComPtr<IMedium> pExistingMedium; 397 rc = a->virtualBox->FindMedium(Bstr(pszMedium).raw(), 398 deviceType, 399 pExistingMedium.asOutParam()); 397 rc = findMedium(a, pszMedium, deviceType, true /* fSilent */, 398 pExistingMedium); 400 399 if (pExistingMedium) 401 400 { … … 544 543 throw Utf8Str("Missing --medium argument"); 545 544 546 rc = a->virtualBox->FindMedium(bstrMedium.raw(), 547 devTypeRequested, 548 pMedium2Mount.asOutParam()); 549 if (FAILED(rc) || !pMedium2Mount) 550 { 551 /* not registered, do that on the fly */ 552 CHECK_ERROR(a->virtualBox, 553 OpenMedium(bstrMedium.raw(), 554 devTypeRequested, 555 AccessMode_ReadWrite, 556 pMedium2Mount.asOutParam())); 557 } 545 rc = findOrOpenMedium(a, pszMedium, devTypeRequested, 546 pMedium2Mount, NULL); 558 547 if (!pMedium2Mount) 559 548 throw Utf8StrFmt("Invalid UUID or filename \"%s\"", pszMedium);
Note:
See TracChangeset
for help on using the changeset viewer.