VirtualBox

Changeset 43787 in vbox for trunk/src/VBox/Storage


Ignore:
Timestamp:
Oct 31, 2012 4:27:29 PM (12 years ago)
Author:
vboxsync
Message:

Storage: Repair images when opening if they are corrupted and can be repaired

Location:
trunk/src/VBox/Storage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Storage/VD.cpp

    r43739 r43787  
    54515451                                      pDisk->enmType,
    54525452                                      &pImage->pBackendData);
     5453        /*
     5454         * If the image is corrupted and there is a repair method try to repair it
     5455         * first if it was openend in read-write mode and open again afterwards.
     5456         */
     5457        if (   RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED)
     5458            && pImage->Backend->pfnRepair)
     5459        {
     5460            rc = pImage->Backend->pfnRepair(pszFilename, pDisk->pVDIfsDisk, pImage->pVDIfsImage, 0 /* fFlags */);
     5461            if (RT_SUCCESS(rc))
     5462                rc = pImage->Backend->pfnOpen(pImage->pszFilename,
     5463                                              uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
     5464                                              pDisk->pVDIfsDisk,
     5465                                              pImage->pVDIfsImage,
     5466                                              pDisk->enmType,
     5467                                              &pImage->pBackendData);
     5468            else
     5469            {
     5470                rc = vdError(pDisk, rc, RT_SRC_POS,
     5471                             N_("VD: error %Rrc repairing corrupted image file '%s'"), rc, pszFilename);
     5472                break;
     5473            }
     5474        }
     5475        else if (RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED))
     5476        {
     5477            rc = vdError(pDisk, rc, RT_SRC_POS,
     5478                         N_("VD: Image file '%s' is corrupted and can't be opened"), pszFilename);
     5479            break;
     5480        }
     5481
    54535482        /* If the open in read-write mode failed, retry in read-only mode. */
    54545483        if (RT_FAILURE(rc))
  • trunk/src/VBox/Storage/VHD.cpp

    r40843 r43787  
    793793    rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->uCurrentEndOfFile,
    794794                               &vhdFooter, sizeof(VHDFooter), NULL);
    795     if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
    796         return VERR_VD_VHD_INVALID_HEADER;
     795    if (RT_SUCCESS(rc))
     796    {
     797        if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
     798        {
     799            /*
     800             * There is also a backup header at the beginning in case the image got corrupted.
     801             * Such corrupted images are detected here to let the open handler repair it later.
     802             */
     803            rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0,
     804                                       &vhdFooter, sizeof(VHDFooter), NULL);
     805            if (RT_SUCCESS(rc))
     806            {
     807                if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
     808                    rc = VERR_VD_VHD_INVALID_HEADER;
     809                else
     810                    rc = VERR_VD_IMAGE_CORRUPTED;
     811            }
     812        }
     813    }
     814
     815    if (RT_FAILURE(rc))
     816        return rc;
    797817
    798818    switch (RT_BE2H_U32(vhdFooter.DiskType))
     
    12221242    rc = vdIfIoIntFileReadSync(pIfIo, pStorage, cbFile - sizeof(VHDFooter),
    12231243                               &vhdFooter, sizeof(VHDFooter), NULL);
    1224     if (RT_FAILURE(rc) || (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0))
     1244    if (RT_SUCCESS(rc))
     1245    {
     1246        if (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0)
     1247        {
     1248            /*
     1249             * There is also a backup header at the beginning in case the image got corrupted.
     1250             * Such corrupted images are detected here to let the open handler repair it later.
     1251             */
     1252            rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0,
     1253                                       &vhdFooter, sizeof(VHDFooter), NULL);
     1254            if (   RT_FAILURE(rc)
     1255                || (memcmp(vhdFooter.Cookie, VHD_FOOTER_COOKIE, VHD_FOOTER_COOKIE_SIZE) != 0))
     1256                   rc = VERR_VD_VHD_INVALID_HEADER;
     1257        }
     1258
     1259        if (RT_SUCCESS(rc))
     1260            *penmType = VDTYPE_HDD;
     1261    }
     1262    else
    12251263        rc = VERR_VD_VHD_INVALID_HEADER;
    1226     else
    1227     {
    1228         *penmType = VDTYPE_HDD;
    1229         rc = VINF_SUCCESS;
    1230     }
    12311264
    12321265    vdIfIoIntFileClose(pIfIo, pStorage);
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