manifest.cpp revision 382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8
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;
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync/*******************************************************************************
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync* Private functions
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync*******************************************************************************/
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsyncDECLINLINE(char *) rtManifestPosOfCharInBuf(char const *pv, size_t cb, char c)
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsyncDECLINLINE(size_t) rtManifestIndexOfCharInBuf(char const *pv, size_t cb, char c)
70dd77b2f7732defac9d929109f20d92676f0570vboxsyncint rtSHAProgressCallback(unsigned uPercent, void *pvUser)
70dd77b2f7732defac9d929109f20d92676f0570vboxsync PRTMANIFESTCALLBACKDATA pData = (PRTMANIFESTCALLBACKDATA)pvUser;
70dd77b2f7732defac9d929109f20d92676f0570vboxsync return pData->pfnProgressCallback((unsigned)( (uPercent + (float)pData->cCurrentFile * 100.0)
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync/*******************************************************************************
70dd77b2f7732defac9d929109f20d92676f0570vboxsync* Public functions
70dd77b2f7732defac9d929109f20d92676f0570vboxsync*******************************************************************************/
70dd77b2f7732defac9d929109f20d92676f0570vboxsyncRTR3DECL(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 = RTFileOpen(&file, pszManifestFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Cast down for the case size_t < uint64_t. This isn't really correct,
70dd77b2f7732defac9d929109f20d92676f0570vboxsync but we consider manifest files bigger than size_t as not supported
70dd77b2f7732defac9d929109f20d92676f0570vboxsync rc = RTManifestVerifyFilesBuf(pvBuf, cbRead, paTests, cTests, piFailed);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync }while (0);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Cleanup */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsyncRTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed,
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Validate input */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_POINTER);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Create our compare list */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync PRTMANIFESTTEST paFiles = (PRTMANIFESTTEST)RTMemTmpAllocZ(sizeof(RTMANIFESTTEST) * cFiles);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Fill our compare list */
70dd77b2f7732defac9d929109f20d92676f0570vboxsync rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync rc = RTSha1DigestFromFile(papszFiles[i], &pszDigest, NULL, NULL);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Do the verification */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync rc = RTManifestVerify(pszManifestFile, paFiles, cFiles, piFailed);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Cleanup */
70dd77b2f7732defac9d929109f20d92676f0570vboxsyncRTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles,
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Validate input */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_POINTER);
0aa6851583f4cb074ccc163d5b53948b9429ab77vboxsync int rc = RTFileOpen(&file, pszManifestFile, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_ALL);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync paFiles = (PRTMANIFESTTEST)RTMemAllocZ(sizeof(RTMANIFESTTEST) * cFiles);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 };
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Calculate the SHA1 digest of every file */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync rc = RTSha1DigestFromFile(paFiles[i].pszTestFile, (char**)&paFiles[i].pszTestDigest, rtSHAProgressCallback, &callback);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync rc = RTSha1DigestFromFile(paFiles[i].pszTestFile, (char**)&paFiles[i].pszTestDigest, NULL, NULL);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync rc = RTManifestWriteFilesBuf(&pvBuf, &cbSize, paFiles, cFiles);
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync }while (0);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync /* Cleanup */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Delete the manifest file on failure */
70dd77b2f7732defac9d929109f20d92676f0570vboxsyncRTR3DECL(int) RTManifestVerifyFilesBuf(void *pvBuf, size_t cbSize, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Validate input */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync AssertPtrNullReturn(piFailed, VERR_INVALID_POINTER);
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Fill our compare list */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Parse the manifest file line by line */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync size_t cch = rtManifestIndexOfCharInBuf(pcBuf, cbSize - cbRead, '\n') + 1;
510dd1bd989d25f95680fc2a3b51bf11854bc6a4vboxsync /* Skip empty lines (UNIX/DOS format) */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /** @todo r=bird:
70dd77b2f7732defac9d929109f20d92676f0570vboxsync * -# Better deal with this EOF line platform dependency
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * -# The SHA1 test should probably include a blank space check.
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * -# If there is a specific order to the elements in the string, it would be
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * good if the delimiter searching checked for it.
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync * -# Deal with filenames containing delimiter characters.
70dd77b2f7732defac9d929109f20d92676f0570vboxsync /* Check for the digest algorithm */
c40afa339b10a23d6fffcbeb7d4572bb494685f0vboxsync /* Digest unsupported */
if (!pszNameStart)
if (!pszNameEnd)
if (!pszName)
if (!pszDigestStart)
if (!pszDigestEnd)
if (!pszDigestEnd)
if (!pszDigest)
bool fFound = false;
fFound = true;
if (!fFound)
if (piFailed)
*piFailed = i;
return rc;
RTR3DECL(int) RTManifestWriteFilesBuf(void **ppvBuf, size_t *pcbSize, PRTMANIFESTTEST paFiles, size_t cFiles)
size_t cbTmp = strlen(RTPathFilename(paFiles[i].pszTestFile)) + strlen(paFiles[i].pszTestDigest) + 10;
if (!pvBuf)
return VERR_NO_MEMORY;
size_t cch = RTStrPrintf(pszTmp, cbMaxSize + 1, "SHA1 (%s)= %s\n", RTPathFilename(paFiles[i].pszTestFile), paFiles[i].pszTestDigest);
return VINF_SUCCESS;