manifest.cpp revision 510dd1bd989d25f95680fc2a3b51bf11854bc6a4
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * IPRT - Manifest file handling.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2009 Oracle Corporation
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * available from http://www.virtualbox.org. This file is free software;
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * General Public License (GPL) as published by the Free Software
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * The contents of this file may alternatively be used under the terms
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * of the Common Development and Distribution License Version 1.0
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * CDDL are applicable instead of those of the GPL.
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * You may elect to license modified versions of this file under the
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * terms and conditions of either the GPL or the CDDL or both.
70dd77b2f7732defac9d929109f20d92676f0570vboxsync/*******************************************************************************
70dd77b2f7732defac9d929109f20d92676f0570vboxsync* Header Files *
70dd77b2f7732defac9d929109f20d92676f0570vboxsync*******************************************************************************/
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync/*******************************************************************************
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync* Structures and Typedefs *
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync*******************************************************************************/
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * Internal per file structure used by RTManifestVerify
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync * Internal structure used for the progress callback
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsynctypedef RTMANIFESTCALLBACKDATA* PRTMANIFESTCALLBACKDATA;
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsyncint rtSHAProgressCallback(unsigned uPercent, void *pvUser)
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync PRTMANIFESTCALLBACKDATA pData = (PRTMANIFESTCALLBACKDATA)pvUser;
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync return pData->pfnProgressCallback((unsigned)((uPercent + (float)pData->cCurrentFile * 100.0) / (float)pData->cMaxFiles), pData->pvUser);
0aa6851583f4cb074ccc163d5b53948b9429ab77vboxsyncRTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Validate input */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Open the manifest file */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync int rc = RTStrmOpen(pszManifestFile, "r", &pStream);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Fill our compare list */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Parse the manifest file line by line */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Skip empty lines */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /** @todo r=bird:
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * -# The SHA1 test should probably include a blank space check.
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * -# If there is a specific order to the elements in the string, it would be
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * good if the delimiter searching checked for it.
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * -# Deal with filenames containing delimiter characters.
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Check for the digest algorithm */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Digest unsupported */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Try to find the filename */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Copy the filename part */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync char *pszName = (char *)RTMemTmpAlloc(cchName + 1);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Try to find the digest sum */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Check our file list against the extracted data */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync bool fFound = false;
70dd77b2f7732defac9d929109f20d92676f0570vboxsync if (!RTStrCmp(RTPathFilename(paFiles[i].pTestPattern->pszTestFile), RTStrStrip(pszName)))
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Add the data of the manifest file to the file list */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync paFiles[i].pszManifestFile = RTStrDup(RTStrStrip(pszName));
70dd77b2f7732defac9d929109f20d92676f0570vboxsync paFiles[i].pszManifestDigest = RTStrDup(RTStrStrip(pszDigest));
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* There have to be an entry in the file list */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* If there is an entry in the file list, which hasn't an
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * equivalent in the manifest file, its an error. */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Do the manifest SHA1 digest match against the actual digest? */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync if (RTStrICmp(paFiles[i].pszManifestDigest, paFiles[i].pTestPattern->pszTestDigest))
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Cleanup */
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsyncRTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Validate input */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Create our compare list */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync PRTMANIFESTTEST paFiles = (PRTMANIFESTTEST)RTMemTmpAllocZ(sizeof(RTMANIFESTTEST) * cFiles);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Fill our compare list */
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Do the verification */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync rc = RTManifestVerify(pszManifestFile, paFiles, cFiles, piFailed);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Cleanup */
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsyncRTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Validate input */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Open a file to stream in */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync int rc = RTStrmOpen(pszManifestFile, "w", &pStream);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Calculate the SHA1 digest of every file */
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Add the entry to the manifest file */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync int cch = RTStrmPrintf(pStream, "SHA1 (%s)= %s\n", RTPathFilename(papszFiles[i]), pszDigest);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Delete the manifest file on failure */