VirtualBox

Changeset 32568 in vbox


Ignore:
Timestamp:
Sep 16, 2010 3:26:13 PM (14 years ago)
Author:
vboxsync
Message:

OVA: parse ovf in memory on ova import (no need for a temporary ovf anymore)

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/sha.h

    r30079 r32568  
    114114
    115115/**
     116 * Creates a SHA1 digest for the given memory buffer.
     117 *
     118 * @returns iprt status code.
     119 *
     120 * @param   pvBuf                 Memory buffer to create a SHA1 digest for.
     121 * @param   cbBuf                 The amount of data (in bytes).
     122 * @param   ppszDigest            On success the SHA1 digest.
     123 * @param   pfnProgressCallback   optional callback for the progress indication
     124 * @param   pvUser                user defined pointer for the callback
     125 */
     126RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser);
     127
     128/**
    116129 * Creates a SHA1 digest for the given file.
    117130 *
     
    123136 * @param   pvUser                user defined pointer for the callback
    124137 */
    125 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser);
    126 
     138RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser);
    127139
    128140
  • trunk/src/VBox/Main/ApplianceImplImport.cpp

    r31676 r32568  
    691691        /* Create the SHA1 sum of the OVF file for later validation */
    692692        char *pszDigest;
    693         int vrc = RTSha1Digest(locInfo.strPath.c_str(), &pszDigest, NULL, NULL);
     693        int vrc = RTSha1DigestFromFile(locInfo.strPath.c_str(), &pszDigest, NULL, NULL);
    694694        if (RT_FAILURE(vrc))
    695695            DebugBreakThrow(setError(VBOX_E_FILE_ERROR,
     
    724724
    725725    AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS);
    726 
    727726    HRESULT rc = S_OK;
    728     int vrc = VINF_SUCCESS;
    729     char szOSTmpDir[RTPATH_MAX];
    730     RTPathTemp(szOSTmpDir, sizeof(szOSTmpDir));
    731     /* The template for the temporary directory created below */
    732     char *pszTmpDir;
    733     RTStrAPrintf(&pszTmpDir, "%s"RTPATH_SLASH_STR"vbox-ovf-XXXXXX", szOSTmpDir);
    734     list< pair<Utf8Str, ULONG> > filesList;
    735     Utf8Str strTmpOvf;
     727    void *pvBuf = 0;
    736728
    737729    try
    738730    {
    739         /* Extract the path */
    740731        Utf8Str tmpPath = locInfo.strPath;
    741732        /* Remove the ova extension */
    742733        tmpPath.stripExt();
     734        /* add the ovf extension. */
    743735        tmpPath += ".ovf";
    744 
    745         /* We need a temporary directory which we can put the OVF file & all
    746          * disk images in */
    747         vrc = RTDirCreateTemp(pszTmpDir);
     736        char* pcszOVFName = RTPathFilename(tmpPath.c_str());
     737
     738        /* Read the OVF into a memory buffer */
     739        uint64_t cbSize;
     740        int vrc = RTTarExtractFileToBuf(locInfo.strPath.c_str(), &pvBuf, &cbSize, pcszOVFName, 0, 0);
    748741        if (RT_FAILURE(vrc))
    749             DebugBreakThrow(setError(VBOX_E_FILE_ERROR,
    750                            tr("Cannot create temporary directory '%s' (%Rrc)"), pszTmpDir, vrc));
    751 
    752         /* The temporary name of the target OVF file */
    753         strTmpOvf = Utf8StrFmt("%s/%s", pszTmpDir, RTPathFilename(tmpPath.c_str()));
    754 
    755         /* Next we have to download the OVF */
    756         char *papszFile = RTPathFilename(strTmpOvf.c_str());
    757         vrc = RTTarExtractFiles(locInfo.strPath.c_str(), pszTmpDir, &papszFile, 1, 0, 0);
     742        {
     743            if (vrc == VERR_FILE_NOT_FOUND)
     744                throw setError(VBOX_E_IPRT_ERROR,
     745                               tr("Can't find ovf file '%s' in archive '%s' (%Rrc)"), pcszOVFName, locInfo.strPath.c_str(), vrc);
     746            else
     747                throw setError(VBOX_E_IPRT_ERROR,
     748                               tr("Can't unpack the archive file '%s' (%Rrc)"), locInfo.strPath.c_str(), vrc);
     749        }
     750
     751        /* Read & parse the XML structure of the OVF file */
     752        m->pReader = new ovf::OVFReader(pvBuf, cbSize, locInfo.strPath);
     753        /* Create the SHA1 sum of the OVF file for later validation */
     754        char *pszDigest;
     755        vrc = RTSha1Digest(pvBuf, cbSize, &pszDigest, 0, 0);
    758756        if (RT_FAILURE(vrc))
    759         {
    760             if (vrc == VERR_FILE_NOT_FOUND)
    761                 DebugBreakThrow(setError(VBOX_E_IPRT_ERROR,
    762                                          tr("Can't find ovf file '%s' in archive '%s' (%Rrc)"), papszFile, locInfo.strPath.c_str(), vrc));
    763             else
    764                 DebugBreakThrow(setError(VBOX_E_IPRT_ERROR,
    765                                          tr("Can't unpack the archive file '%s' (%Rrc)"), locInfo.strPath.c_str(), vrc));
    766         }
    767 
    768         // todo: check this out
    769 //        pTask->pProgress->SetNextOperation(Bstr(tr("Reading")), 1);
    770 
    771         /* Prepare the temporary reading of the OVF */
    772         ComObjPtr<Progress> progress;
    773         LocationInfo li;
    774         li.strPath = strTmpOvf;
    775         /* Start the reading from the fs */
    776         rc = readImpl(li, progress);
    777         if (FAILED(rc)) DebugBreakThrow(rc);
    778 
    779         /* Unlock the appliance for the reading thread */
    780         appLock.release();
    781         /* Wait until the reading is done, but report the progress back to the
    782            caller */
    783         ComPtr<IProgress> progressInt(progress);
    784         waitForAsyncProgress(pProgress, progressInt); /* Any errors will be thrown */
    785 
    786         /* Again lock the appliance for the next steps */
    787         appLock.acquire();
    788     }
    789     catch(HRESULT aRC)
     757            throw setError(VBOX_E_FILE_ERROR,
     758                           tr("Couldn't calculate SHA1 digest for file '%s' (%Rrc)"),
     759                           RTPathFilename(locInfo.strPath.c_str()), vrc);
     760        m->strOVFSHA1Digest = pszDigest;
     761        RTStrFree(pszDigest);
     762
     763    }
     764    catch (iprt::Error &x)      // includes all XML exceptions
     765    {
     766        rc = setError(VBOX_E_FILE_ERROR,
     767                      x.what());
     768    }
     769    catch (HRESULT aRC)
    790770    {
    791771        rc = aRC;
    792772    }
    793     /* Delete all files which where temporary created */
    794     if (RTPathExists(strTmpOvf.c_str()))
    795     {
    796         vrc = RTFileDelete(strTmpOvf.c_str());
    797         if (RT_FAILURE(vrc))
    798             rc = setError(VBOX_E_FILE_ERROR,
    799                           tr("Cannot delete file '%s' (%Rrc)"), strTmpOvf.c_str(), vrc);
    800     }
    801     /* Delete the temporary directory */
    802     if (RTPathExists(pszTmpDir))
    803     {
    804         vrc = RTDirRemove(pszTmpDir);
    805         if (RT_FAILURE(vrc))
    806             rc = setError(VBOX_E_FILE_ERROR,
    807                           tr("Cannot delete temporary directory '%s' (%Rrc)"), pszTmpDir, vrc);
    808     }
    809     if (pszTmpDir)
    810         RTStrFree(pszTmpDir);
     773
     774    /* Cleanup the OVF memory buffer */
     775    if (pvBuf)
     776        RTMemFree(pvBuf);
    811777
    812778    LogFlowFunc(("rc=%Rhrc\n", rc));
     
    11571123        {
    11581124            char* pszDigest;
    1159             vrc = RTSha1Digest((*it1).c_str(), &pszDigest, NULL, NULL);
     1125            vrc = RTSha1DigestFromFile((*it1).c_str(), &pszDigest, NULL, NULL);
    11601126            pTestList[i].pszTestFile = (char*)(*it1).c_str();
    11611127            pTestList[i].pszTestDigest = pszDigest;
  • trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp

    r30080 r32568  
    4141
    4242
    43 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
     43RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser)
     44{
     45    /* Validate input */
     46    AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
     47    AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
     48    AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
     49
     50    int rc = VINF_SUCCESS;
     51    *ppszDigest = NULL;
     52
     53    /* Initialize OpenSSL. */
     54    SHA_CTX ctx;
     55    if (!SHA1_Init(&ctx))
     56        return VERR_INTERNAL_ERROR;
     57
     58    /* Buffer size for progress callback */
     59    double rdMulti = 100.0 / cbBuf;
     60
     61    /* Working buffer */
     62    char *pvTmp = (char*)pvBuf;
     63
     64    /* Process the memory in blocks */
     65    size_t cbRead;
     66    size_t cbReadTotal = 0;
     67    for (;;)
     68    {
     69        cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
     70        if(!SHA1_Update(&ctx, pvTmp, cbRead))
     71        {
     72            rc = VERR_INTERNAL_ERROR;
     73            break;
     74        }
     75        cbReadTotal += cbRead;
     76        pvTmp += cbRead;
     77
     78        /* Call the progress callback if one is defined */
     79        if (pfnProgressCallback)
     80        {
     81            rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
     82            if (RT_FAILURE(rc))
     83                break; /* canceled */
     84        }
     85        /* Finished? */
     86        if (cbReadTotal == cbBuf)
     87            break;
     88    }
     89    if (RT_SUCCESS(rc))
     90    {
     91        /* Finally calculate & format the SHA1 sum */
     92        unsigned char auchDig[RTSHA1_HASH_SIZE];
     93        if (!SHA1_Final(auchDig, &ctx))
     94            return VERR_INTERNAL_ERROR;
     95
     96        char *pszDigest;
     97        rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
     98        if (RT_SUCCESS(rc))
     99        {
     100            rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);
     101            if (RT_SUCCESS(rc))
     102                *ppszDigest = pszDigest;
     103            else
     104                RTStrFree(pszDigest);
     105        }
     106    }
     107
     108    return rc;
     109}
     110
     111RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
    44112{
    45113    /* Validate input */
  • trunk/src/VBox/Runtime/common/checksum/manifest.cpp

    r30079 r32568  
    253253        {
    254254            callback.cCurrentFile = i;
    255             rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
     255            rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
    256256        }
    257257        else
    258             rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
     258            rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, NULL, NULL);
    259259        if (RT_FAILURE(rc))
    260260            break;
     
    301301        {
    302302            callback.cCurrentFile = i;
    303             rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
     303            rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
    304304        }
    305305        else
    306             rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
     306            rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, NULL, NULL);
    307307        if (RT_FAILURE(rc))
    308308            break;
  • trunk/src/VBox/Runtime/testcase/tstRTDigest.cpp

    r31847 r32568  
    152152                             {
    153153                                 char *pszDigest;
    154                                  int rc = RTSha1Digest(ValueUnion.psz, &pszDigest, NULL, NULL);
     154                                 int rc = RTSha1DigestFromFile(ValueUnion.psz, &pszDigest, NULL, NULL);
    155155                                 if (RT_FAILURE(rc))
    156156                                     return Error("RTSha1Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc);
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