manifest.cpp revision 5c90ca78888c7bb1632ef4c131cc668b03227271
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;
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsyncint rtSHAProgressCallback(unsigned uPercent, void *pvUser)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync PRTMANIFESTCALLBACKDATA pData = (PRTMANIFESTCALLBACKDATA)pvUser;
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync return pData->pfnProgressCallback((unsigned)((uPercent + (float)pData->cCurrentFile * 100.0) / (float)pData->cMaxFiles), pData->pvUser);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsyncRTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Validate input */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Open the manifest file */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync int rc = RTStrmOpen(pszManifestFile, "r", &pStream);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
44918d7b9fb45f0d3e2ba56bf6d2cede9a2ae566vboxsync /* Fill our compare list */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Parse the manifest file line by line */
0aa6851583f4cb074ccc163d5b53948b9429ab77vboxsync rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Skip empty lines */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /** @todo r=bird:
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * -# The SHA1 test should probably include a blank space check.
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * -# If there is a specific order to the elements in the string, it would be
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * good if the delimiter searching checked for it.
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * -# Deal with filenames containing delimiter characters.
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Check for the digest algorithm */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Digest unsupported */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Try to find the filename */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Copy the filename part */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync char *pszName = (char *)RTMemTmpAlloc(cchName + 1);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Try to find the digest sum */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Check our file list against the extracted data */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync bool fFound = false;
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync if (!RTStrCmp(RTPathFilename(paFiles[i].pTestPattern->pszTestFile), RTStrStrip(pszName)))
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Add the data of the manifest file to the file list */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync paFiles[i].pszManifestFile = RTStrDup(RTStrStrip(pszName));
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync paFiles[i].pszManifestDigest = RTStrDup(RTStrStrip(pszDigest));
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* There have to be an entry in the file list */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* If there is an entry in the file list, which hasn't an
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * equivalent in the manifest file, its an error. */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Do the manifest SHA1 digest match against the actual digest? */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync if (RTStrICmp(paFiles[i].pszManifestDigest, paFiles[i].pTestPattern->pszTestDigest))
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Cleanup */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsyncRTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Validate input */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Create our compare list */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync PRTMANIFESTTEST paFiles = (PRTMANIFESTTEST)RTMemTmpAllocZ(sizeof(RTMANIFESTTEST) * cFiles);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Fill our compare list */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Do the verification */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync rc = RTManifestVerify(pszManifestFile, paFiles, cFiles, piFailed);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Cleanup */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsyncRTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Validate input */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Open a file to stream in */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync int rc = RTStrmOpen(pszManifestFile, "w", &pStream);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Calculate the SHA1 digest of every file */
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Add the entry to the manifest file */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync int cch = RTStrmPrintf(pStream, "SHA1 (%s)= %s\n", RTPathFilename(papszFiles[i]), pszDigest);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /* Delete the manifest file on failure */