manifest.cpp revision 5c90ca78888c7bb1632ef4c131cc668b03227271
/* $Id$ */
/** @file
* IPRT - Manifest file handling.
*/
/*
* Copyright (C) 2009 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/manifest.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Internal per file structure used by RTManifestVerify
*/
typedef struct RTMANIFESTFILEENTRY
{
char *pszManifestFile;
char *pszManifestDigest;
typedef RTMANIFESTFILEENTRY* PRTMANIFESTFILEENTRY;
/**
* Internal structure used for the progress callback
*/
typedef struct RTMANIFESTCALLBACKDATA
{
void *pvUser;
{
return pData->pfnProgressCallback((unsigned)((uPercent + (float)pData->cCurrentFile * 100.0) / (float)pData->cMaxFiles), pData->pvUser);
}
RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
{
/* Validate input */
/* Open the manifest file */
if (RT_FAILURE(rc))
return rc;
PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
if (!paFiles)
{
return VERR_NO_MEMORY;
}
/* Fill our compare list */
/* Parse the manifest file line by line */
char szLine[1024];
for (;;)
{
if (RT_FAILURE(rc))
break;
/* Skip empty lines */
if (cch == 0)
continue;
/** @todo r=bird:
* -# The SHA1 test should probably include a blank space check.
* -# If there is a specific order to the elements in the string, it would be
* good if the delimiter searching checked for it.
* -# Deal with filenames containing delimiter characters.
*/
/* Check for the digest algorithm */
if ( cch < 4
|| !( szLine[0] == 'S'
{
/* Digest unsupported */
break;
}
/* Try to find the filename */
if (!pszNameStart)
{
break;
}
if (!pszNameEnd)
{
break;
}
/* Copy the filename part */
if (!pszName)
{
rc = VERR_NO_MEMORY;
break;
}
/* Try to find the digest sum */
if (!pszDigestStart)
{
break;
}
char *pszDigest = ++pszDigestStart;
/* Check our file list against the extracted data */
bool fFound = false;
{
{
/* Add the data of the manifest file to the file list */
fFound = true;
break;
}
}
if (!fFound)
{
/* There have to be an entry in the file list */
break;
}
}
if ( rc == VINF_SUCCESS
{
rc = VINF_SUCCESS;
{
/* If there is an entry in the file list, which hasn't an
* equivalent in the manifest file, its an error. */
if ( !paFiles[i].pszManifestFile
|| !paFiles[i].pszManifestDigest)
{
break;
}
/* Do the manifest SHA1 digest match against the actual digest? */
{
if (piFailed)
*piFailed = i;
break;
}
}
}
/* Cleanup */
{
if (paFiles[i].pszManifestFile)
if (paFiles[i].pszManifestDigest)
}
return rc;
}
RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
{
/* Validate input */
int rc = VINF_SUCCESS;
/* Create our compare list */
if (!paFiles)
return VERR_NO_MEMORY;
/* Fill our compare list */
{
char *pszDigest;
if (pfnProgressCallback)
{
callback.cCurrentFile = i;
}
else
if (RT_FAILURE(rc))
break;
}
/* Do the verification */
if (RT_SUCCESS(rc))
/* Cleanup */
{
if (paFiles[i].pszTestDigest)
}
return rc;
}
RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser)
{
/* Validate input */
/* Open a file to stream in */
if (RT_FAILURE(rc))
return rc;
{
/* Calculate the SHA1 digest of every file */
char *pszDigest;
if (pfnProgressCallback)
{
callback.cCurrentFile = i;
}
else
if (RT_FAILURE(rc))
break;
/* Add the entry to the manifest file */
if (RT_UNLIKELY(cch < 0))
{
break;
}
}
/* Delete the manifest file on failure */
if (RT_FAILURE(rc))
return rc;
}