manifest2.cpp revision 49bcf64aaacb2de9703cdd3633eec0a2610c3cf3
/* $Id$ */
/** @file
* IPRT - Manifest, the core.
*/
/*
* Copyright (C) 2010 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 *
*******************************************************************************/
/**
* Manifest attribute.
*
* Used both for entries and manifest attributes.
*/
typedef struct RTMANIFESTATTR
{
/** The string space core (szName). */
/** The property value. */
char *pszValue;
/** The attribute type if applicable, RTMANIFEST_ATTR_UNKNOWN if not. */
/** Whether it was visited by the equals operation or not. */
bool fVisited;
/** The normalized property name that StrCore::pszString points at. */
char szName[1];
/** Pointer to a manifest attribute. */
typedef RTMANIFESTATTR *PRTMANIFESTATTR;
/**
* Manifest entry.
*/
typedef struct RTMANIFESTENTRY
{
/** The string space core (szName). */
/** The entry attributes (hashes, checksums, size, etc) -
* RTMANIFESTATTR. */
/** The number of attributes. */
/** Whether it was visited by the equals operation or not. */
bool fVisited;
/** The normalized entry name that StrCore::pszString points at. */
char szName[1];
/** Pointer to a manifest entry. */
typedef RTMANIFESTENTRY *PRTMANIFESTENTRY;
/**
* Manifest handle data.
*/
typedef struct RTMANIFESTINT
{
/** Magic value (RTMANIFEST_MAGIC). */
/** The number of references to this manifest. */
/** String space of the entries covered by this manifest -
* RTMANIFESTENTRY. */
/** The number of entries. */
/** The entry for the manifest itself. */
/** The value of RTMANIFESTINT::u32Magic. */
/**
* Argument package passed to rtManifestWriteStdAttr by rtManifestWriteStdEntry
* and RTManifestWriteStandard.
*/
typedef struct RTMANIFESTWRITESTDATTR
{
/** The entry name. */
const char *pszEntry;
/** The output I/O stream. */
/**
* Argument package used by RTManifestEqualsEx to pass its arguments to the
* enumeration callback functions.
*/
typedef struct RTMANIFESTEQUALS
{
/** Name of entries to ignore. */
const char * const *papszIgnoreEntries;
/** Name of attributes to ignore. */
const char * const *papszIgnoreAttr;
/** Flags governing the comparision. */
/** Where to return an error message (++) on failure. Can be NULL. */
char *pszError;
/** The size of the buffer pszError points to. Can be 0. */
/** Pointer to the 2nd manifest. */
/** The number of ignored entries from the 1st manifest. */
/** The number of entries processed from the 2nd manifest. */
/** The number of ignored attributes from the 1st manifest. */
/** The number of ignored attributes from the 1st manifest. */
/** The number of attributes processed from the 2nd manifest. */
/** Pointer to the string space to get matching attributes from. */
/** The name of the current entry.
* Points to an empty string it's the manifest attributes. */
const char *pszCurEntry;
/** Pointer to an RTManifestEqualEx argument packet. */
typedef RTMANIFESTEQUALS *PRTMANIFESTEQUALS;
/**
* Argument package used by rtMainfestQueryAttrWorker to pass its search
* criteria to rtMainfestQueryAttrEnumCallback and get a result back.
*/
typedef struct RTMANIFESTQUERYATTRARGS
{
/** The attribute types we're hunting for. */
/** What we've found. */
/** Pointer to a rtMainfestQueryAttrEnumCallback argument packet. */
/**
* Creates an empty manifest.
*
* @returns IPRT status code.
* @param fFlags Flags, MBZ.
* @param phManifest Where to return the handle to the manifest.
*/
{
if (!pThis)
return VERR_NO_MEMORY;
*phManifest = pThis;
return VINF_SUCCESS;
}
/**
* Retains a reference to the manifest handle.
*
* @returns The new reference count, UINT32_MAX if the handle is invalid.
* @param hManifest The handle to retain.
*/
{
return cRefs;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys RTMANIFESTATTR.}
*/
{
return 0;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys RTMANIFESTENTRY.}
*/
{
return 0;
}
/**
* Releases a reference to the manifest handle.
*
* @returns The new reference count, 0 if free. UINT32_MAX is returned if the
* handle is invalid.
* @param hManifest The handle to release.
* NIL is quietly ignored (returns 0).
*/
{
if (pThis == NIL_RTMANIFEST)
return 0;
if (!cRefs)
{
}
return cRefs;
}
/**
* Creates a duplicate of the specified manifest.
*
* @returns IPRT status code
* @param hManifestSrc The manifest to clone.
* @param phManifestDst Where to store the handle to the duplicate.
*/
{
/** @todo implement cloning. */
return VERR_NOT_IMPLEMENTED;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation.}
*/
{
return 0;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation.}
*/
{
return 0;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Finds the first missing.}
*/
{
/*
* Already visited?
*/
return 0;
/*
* Ignore this entry?
*/
if (ppsz)
{
while (*ppsz)
{
return 0;
ppsz++;
}
}
/*
* Gotcha!
*/
if (*pEquals->pszCurEntry)
"Attribute '%s' on '%s' was not found in the 1st manifest",
else
RTStrPrintf(pEquals->pszError, pEquals->cbError, "Attribute '%s' was not found in the 1st manifest", pAttr->szName);
return VERR_NOT_EQUAL;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Finds the first missing.}
*/
{
/*
* Already visited?
*/
return 0;
/*
* Ignore this entry?
*/
if (ppsz)
{
while (*ppsz)
{
return 0;
ppsz++;
}
}
/*
* Gotcha!
*/
RTStrPrintf(pEquals->pszError, pEquals->cbError, "'%s' was not found in the 1st manifest", pEntry->StrCore.pszString);
return VERR_NOT_EQUAL;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Compares attributes.}
*/
{
/*
* Ignore this entry?
*/
if (ppsz)
{
while (*ppsz)
{
{
if (pAttr2)
{
}
return 0;
}
ppsz++;
}
}
/*
* Find the matching attribute.
*/
if (!pAttr2)
{
return 0;
if (*pEquals->pszCurEntry)
"Attribute '%s' on '%s' was not found in the 2nd manifest",
else
RTStrPrintf(pEquals->pszError, pEquals->cbError, "Attribute '%s' was not found in the 2nd manifest", pAttr1->szName);
return VERR_NOT_EQUAL;
}
pEquals->cAttributes2++;
/*
* Compare them.
*/
{
if (*pEquals->pszCurEntry)
"Attribute '%s' on '%s' does not match ('%s' vs. '%s')",
else
"Attribute '%s' does not match ('%s' vs. '%s')",
return VERR_NOT_EQUAL;
}
return 0;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation.}
*/
DECLINLINE (int) rtManifestEntryCompare2(PRTMANIFESTEQUALS pEquals, PRTMANIFESTENTRY pEntry1, PRTMANIFESTENTRY pEntry2)
{
/*
* Compare the attributes. It's a bit ugly with all this counting, but
* how else to efficiently implement RTMANIFEST_EQUALS_IGN_MISSING_ATTRS?
*/
pEquals->cIgnoredAttributes1 = 0;
pEquals->cIgnoredAttributes2 = 0;
pEquals->cAttributes2 = 0;
if (RT_SUCCESS(rc))
{
/*
* Check that we matched all that is required.
*/
}
return rc;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation.}
*/
{
/*
* Ignore this entry.
*/
if (ppsz)
{
while (*ppsz)
{
{
if (pEntry2)
{
}
return 0;
}
ppsz++;
}
}
/*
* Try find the entry in the other manifest.
*/
if (!pEntry2)
{
RTStrPrintf(pEquals->pszError, pEquals->cbError, "'%s' not found in the 2nd manifest", pEntry1->StrCore.pszString);
return VERR_NOT_EQUAL;
}
}
RTDECL(int) RTManifestEqualsEx(RTMANIFEST hManifest1, RTMANIFEST hManifest2, const char * const *papszIgnoreEntries,
{
/*
* Validate input.
*/
*pszError = '\0';
if (pThis1 != NIL_RTMANIFEST)
{
}
if (pThis2 != NIL_RTMANIFEST)
{
}
/*
* The simple cases.
*/
return VINF_SUCCESS;
return VERR_NOT_EQUAL;
/*
* Since we have to use callback style enumeration, we have to mark the
* entries and attributes to make sure we've covered them all.
*/
Equals.cIgnoredEntries2 = 0;
Equals.cAttributes2 = 0;
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/*
* Did we cover all entries of the 2nd manifest?
*/
}
return rc;
}
{
0 /*fFlags*/, NULL, 0);
}
/**
* Worker common to RTManifestSetAttr and RTManifestEntrySetAttr.
*
* @returns IPRT status code.
* @param pEntry Pointer to the entry.
* @param pszAttr The name of the attribute to add.
* @param pszValue The value string.
* @param fType The attribute type type.
*/
static int rtManifestSetAttrWorker(PRTMANIFESTENTRY pEntry, const char *pszAttr, const char *pszValue, uint32_t fType)
{
char *pszValueCopy;
if (RT_FAILURE(rc))
return rc;
/*
* Does the attribute exist already?
*/
if (pAttr)
{
}
else
{
if (!pAttr)
{
return VERR_NO_MEMORY;
}
{
AssertFailed();
return VERR_INTERNAL_ERROR_4;
}
pEntry->cAttributes++;
}
return VINF_SUCCESS;
}
/**
* Sets a manifest attribute.
*
* @returns IPRT status code.
* @param hManifest The manifest handle.
* @param pszAttr The attribute name. If this already exists,
* its value will be replaced.
* @param pszValue The value string.
* @param fType The attribute type, pass
* RTMANIFEST_ATTR_UNKNOWN if not known.
*/
RTDECL(int) RTManifestSetAttr(RTMANIFEST hManifest, const char *pszAttr, const char *pszValue, uint32_t fType)
{
}
/**
* Worker common to RTManifestUnsetAttr and RTManifestEntryUnsetAttr.
*
* @returns IPRT status code.
* @param pEntry Pointer to the entry.
* @param pszAttr The name of the attribute to remove.
*/
{
if (!pStrCore)
return VWRN_NOT_FOUND;
pEntry->cAttributes--;
return VINF_SUCCESS;
}
/**
* Unsets (removes) a manifest attribute if it exists.
*
* @returns IPRT status code.
* @retval VWRN_NOT_FOUND if not found.
*
* @param hManifest The manifest handle.
* @param pszAttr The attribute name.
*/
{
}
/**
* Callback employed by rtManifestQueryAttrWorker to search by attribute type.
*
* @returns VINF_SUCCESS or VINF_CALLBACK_RETURN.
* @param pStr The attribute string node.
* @param pvUser The argument package.
*/
{
{
return VINF_CALLBACK_RETURN;
}
return VINF_SUCCESS;
}
/**
* Worker common to RTManifestQueryAttr and RTManifestEntryQueryAttr.
*
* @returns IPRT status code.
* @param pEntry The entry.
* @param pszAttr The attribute name. If NULL, it will be
* selected by @a fType alone.
* @param fType The attribute types the entry should match. Pass
* Pass RTMANIFEST_ATTR_ANY match any. If more
* than one is given, the first matching one is
* returned.
* @param pszValue Where to return value.
* @param cbValue The size of the buffer @a pszValue points to.
* @param pfType Where to return the attribute type value.
*/
{
/*
* Find the requested attribute.
*/
if (pszAttr)
{
/* By name. */
if (!pAttr)
return VERR_MANIFEST_ATTR_NOT_FOUND;
return VERR_MANIFEST_ATTR_TYPE_MISMATCH;
}
else
{
/* By type. */
if (!pAttr)
}
/*
* Set the return values.
*/
{
return VERR_BUFFER_OVERFLOW;
}
if (pfType)
return VINF_SUCCESS;
}
{
}
/**
* Validates the name entry.
*
* @returns IPRT status code.
* @param pszEntry The entry name to validate.
* @param pfNeedNormalization Where to return whether it needs normalization
* or not. Optional.
* @param pcchEntry Where to return the length. Optional.
*/
static int rtManifestValidateNameEntry(const char *pszEntry, bool *pfNeedNormalization, size_t *pcchEntry)
{
int rc;
bool fNeedNormalization = false;
for (;;)
{
if (RT_FAILURE(rc))
return rc;
if (!uc)
break;
if (uc == '\\')
fNeedNormalization = true;
return VERR_INVALID_NAME;
}
if (pfNeedNormalization)
if (!cchEntry)
if (pcchEntry)
return rc;
}
/**
* Normalizes a entry name.
*
* @param pszEntry The entry name to normalize.
*/
static void rtManifestNormalizeEntry(char *pszEntry)
{
char ch;
{
if (ch == '\\')
*pszEntry = '/';
pszEntry++;
}
}
/**
* Gets an entry.
*
* @returns IPRT status code.
* @param pThis The manifest to work with.
* @param pszEntry The entry name.
* @param fNeedNormalization Whether rtManifestValidateNameEntry said it
* needed normalization.
* @param cchEntry The length of the name.
* @param ppEntry Where to return the entry pointer on success.
*/
static int rtManifestGetEntry(RTMANIFESTINT *pThis, const char *pszEntry, bool fNeedNormalization, size_t cchEntry,
{
if (!fNeedNormalization)
else
{
if (RT_UNLIKELY(!pszCopy))
return VERR_NO_TMP_MEMORY;
}
}
/**
* Sets an attribute of a manifest entry.
*
* @returns IPRT status code.
* @param hManifest The manifest handle.
* @param pszEntry The entry name. This will automatically be
* added if there was no previous call to
* RTManifestEntryAdd for this name. See
* RTManifestEntryAdd for the entry name rules.
* @param pszAttr The attribute name. If this already exists,
* its value will be replaced.
* @param pszValue The value string.
* @param fType The attribute type, pass
* RTMANIFEST_ATTR_UNKNOWN if not known.
*/
{
bool fNeedNormalization;
/*
* Resolve the entry, adding one if necessary.
*/
if (rc == VERR_NOT_FOUND)
{
if (!pEntry)
return VERR_NO_MEMORY;
pEntry->cAttributes = 0;
if (fNeedNormalization)
{
return VERR_INTERNAL_ERROR_4;
}
}
else if (RT_FAILURE(rc))
return rc;
}
/**
* Unsets (removes) an attribute of a manifest entry if they both exist.
*
* @returns IPRT status code.
* @retval VWRN_NOT_FOUND if not found.
*
* @param hManifest The manifest handle.
* @param pszEntry The entry name.
* @param pszAttr The attribute name.
*/
RTDECL(int) RTManifestEntryUnsetAttr(RTMANIFEST hManifest, const char *pszEntry, const char *pszAttr)
{
bool fNeedNormalization;
/*
* Resolve the entry and hand it over to the worker.
*/
if (RT_SUCCESS(rc))
return rc;
}
RTDECL(int) RTManifestEntryQueryAttr(RTMANIFEST hManifest, const char *pszEntry, const char *pszAttr, uint32_t fType,
{
/*
* Look up the entry.
*/
bool fNeedNormalization;
if (RT_SUCCESS(rc))
return rc;
}
/**
* Adds a new entry to a manifest.
*
* The entry name rules:
* - The entry name can contain any character defined by unicode, except
* control characters, ':', '(' and ')'. The exceptions are mainly there
* because of uncertainty around how various formats handles these.
* - It is considered case sensitive.
* - Forward (unix) and backward (dos) slashes are considered path
* separators and converted to forward slashes.
*
* @returns IPRT status code.
* @retval VWRN_ALREADY_EXISTS if the entry already exists.
*
* @param hManifest The manifest handle.
* @param pszEntry The entry name (UTF-8).
*
* @remarks Some manifest formats will not be able to store an entry without
* any attributes. So, this is just here in case it comes in handy
* when dealing with formats which can.
*/
{
bool fNeedNormalization;
/*
* Only add one if it does not already exist.
*/
if (rc == VERR_NOT_FOUND)
{
if (pEntry)
{
pEntry->cAttributes = 0;
if (fNeedNormalization)
{
rc = VINF_SUCCESS;
}
else
{
}
}
else
rc = VERR_NO_MEMORY;
}
else if (RT_SUCCESS(rc))
return rc;
}
/**
* Removes an entry.
*
* @returns IPRT status code.
* @param hManifest The manifest handle.
* @param pszEntry The entry name.
*/
{
bool fNeedNormalization;
/*
* Look it up before removing it.
*/
if (RT_SUCCESS(rc))
{
}
return rc;
}
{
AssertPtrReturn(pThis, false);
bool fNeedNormalization;
AssertRCReturn(rc, false);
/*
* Check if it exists.
*/
return RT_SUCCESS_NP(rc);
}
/**
* Reads a line from a VFS I/O stream.
*
* @todo Replace this with a buffered I/O stream layer.
*
* @returns IPRT status code. VERR_EOF when trying to read beyond the stream
* end.
* @param hVfsIos The I/O stream to read from.
* @param pszLine Where to store what we've read.
* @param cbLine The number of bytes to read.
*/
{
/* This is horribly slow right now, but it's not a biggy as the input is
usually cached in memory somewhere... */
*pszLine = '\0';
while (cbLine > 1)
{
char ch;
if (RT_FAILURE(rc))
return rc;
/* \r\n */
if (ch == '\r')
{
if (cbLine <= 2)
{
return VINF_BUFFER_OVERFLOW;
}
return VINF_SUCCESS;
pszLine[0] = '\r';
if (RT_FAILURE(rc))
}
/* \n */
if (ch == '\n')
return VINF_SUCCESS;
/* add character. */
/* advance */
pszLine++;
cbLine--;
}
return VINF_BUFFER_OVERFLOW;
}
RTDECL(int) RTManifestReadStandardEx(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos, char *pszErr, size_t cbErr)
{
/*
* Validate input.
*/
*pszErr = '\0';
/*
* Process the stream line by line.
*/
for (;;)
{
/*
* Read a line from the input stream.
*/
iLine++;
if (RT_FAILURE(rc))
{
return VINF_SUCCESS;
return rc;
}
if (rc != VINF_SUCCESS)
{
return VERR_OUT_OF_RANGE;
}
/*
* Strip it and skip if empty.
*/
if (!*psz)
continue;
/*
* Read the attribute name.
*/
do
psz++;
if (*psz)
*psz++ = '\0';
/*
* The entry name is enclosed in parenthesis and followed by a '='.
*/
if (*psz != '(')
{
return VERR_PARSE_ERROR;
}
while (*psz)
{
if (*psz == ')')
{
if (*psz2 == '=')
{
*psz = '\0';
break;
}
}
psz++;
}
if (*psz != '=')
{
return VERR_PARSE_ERROR;
}
/*
* The value.
*/
if (!*psz)
{
return VERR_PARSE_ERROR;
}
/*
* Detect attribute type and sanity check the value.
*/
static const struct
{
const char *pszAttr;
unsigned cBits;
unsigned uBase;
} s_aDecAttrs[] =
{
};
for (unsigned i = 0; i < RT_ELEMENTS(s_aDecAttrs); i++)
{
if (rc != VINF_SUCCESS)
{
RTStrPrintf(pszErr, cbErr, "Malformed value ('%s') at %zu on line %u: %Rrc", pszValue, psz - szLine, iLine, rc);
return VERR_PARSE_ERROR;
}
break;
}
if (fType == RTMANIFEST_ATTR_UNKNOWN)
{
static const struct
{
const char *pszAttr;
unsigned cchHex;
} s_aHexAttrs[] =
{
};
for (unsigned i = 0; i < RT_ELEMENTS(s_aHexAttrs); i++)
{
{
return VERR_PARSE_ERROR;
}
break;
}
}
/*
* Finally, add it.
*/
if (RT_FAILURE(rc))
{
return rc;
}
}
}
{
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Writes RTMANIFESTATTR.}
*/
{
size_t cchLine = RTStrPrintf(szLine, sizeof(szLine), "%s (%s) = %s\n", pAttr->szName, pArgs->pszEntry, pAttr->pszValue);
return VERR_BUFFER_OVERFLOW;
}
/**
* @callback_method_impl{FNRTSTRSPACECALLBACK, Writes RTMANIFESTENTRY.}
*/
{
}
{
if (RT_SUCCESS(rc))
return rc;
}