VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/manifest2.cpp@ 62564

Last change on this file since 62564 was 62564, checked in by vboxsync, 8 years ago

IPRT: Mark unused parameters.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.1 KB
Line 
1/* $Id: manifest2.cpp 62564 2016-07-26 14:43:03Z vboxsync $ */
2/** @file
3 * IPRT - Manifest, the core.
4 */
5
6/*
7 * Copyright (C) 2010-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/manifest.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/mem.h>
39#include <iprt/param.h>
40#include <iprt/md5.h>
41#include <iprt/sha.h>
42#include <iprt/string.h>
43#include <iprt/vfs.h>
44
45#include "internal/magics.h"
46
47
48/*********************************************************************************************************************************
49* Structures and Typedefs *
50*********************************************************************************************************************************/
51/**
52 * Manifest attribute.
53 *
54 * Used both for entries and manifest attributes.
55 */
56typedef struct RTMANIFESTATTR
57{
58 /** The string space core (szName). */
59 RTSTRSPACECORE StrCore;
60 /** The property value. */
61 char *pszValue;
62 /** The attribute type if applicable, RTMANIFEST_ATTR_UNKNOWN if not. */
63 uint32_t fType;
64 /** Whether it was visited by the equals operation or not. */
65 bool fVisited;
66 /** The normalized property name that StrCore::pszString points at. */
67 char szName[1];
68} RTMANIFESTATTR;
69/** Pointer to a manifest attribute. */
70typedef RTMANIFESTATTR *PRTMANIFESTATTR;
71
72
73/**
74 * Manifest entry.
75 */
76typedef struct RTMANIFESTENTRY
77{
78 /** The string space core (szName). */
79 RTSTRSPACECORE StrCore;
80 /** The entry attributes (hashes, checksums, size, etc) -
81 * RTMANIFESTATTR. */
82 RTSTRSPACE Attributes;
83 /** The number of attributes. */
84 uint32_t cAttributes;
85 /** Whether it was visited by the equals operation or not. */
86 bool fVisited;
87 /** The normalized entry name that StrCore::pszString points at. */
88 char szName[1];
89} RTMANIFESTENTRY;
90/** Pointer to a manifest entry. */
91typedef RTMANIFESTENTRY *PRTMANIFESTENTRY;
92
93
94/**
95 * Manifest handle data.
96 */
97typedef struct RTMANIFESTINT
98{
99 /** Magic value (RTMANIFEST_MAGIC). */
100 uint32_t u32Magic;
101 /** The number of references to this manifest. */
102 uint32_t volatile cRefs;
103 /** String space of the entries covered by this manifest -
104 * RTMANIFESTENTRY. */
105 RTSTRSPACE Entries;
106 /** The number of entries. */
107 uint32_t cEntries;
108 /** The entry for the manifest itself. */
109 RTMANIFESTENTRY SelfEntry;
110} RTMANIFESTINT;
111
112/** The value of RTMANIFESTINT::u32Magic. */
113#define RTMANIFEST_MAGIC UINT32_C(0x99998866)
114
115/**
116 * Argument package passed to rtManifestWriteStdAttr by rtManifestWriteStdEntry
117 * and RTManifestWriteStandard.
118 */
119typedef struct RTMANIFESTWRITESTDATTR
120{
121 /** The entry name. */
122 const char *pszEntry;
123 /** The output I/O stream. */
124 RTVFSIOSTREAM hVfsIos;
125} RTMANIFESTWRITESTDATTR;
126
127
128/**
129 * Argument package used by RTManifestEqualsEx to pass its arguments to the
130 * enumeration callback functions.
131 */
132typedef struct RTMANIFESTEQUALS
133{
134 /** Name of entries to ignore. */
135 const char * const *papszIgnoreEntries;
136 /** Name of attributes to ignore. */
137 const char * const *papszIgnoreAttrs;
138 /** Flags governing the comparision. */
139 uint32_t fFlags;
140 /** Where to return an error message (++) on failure. Can be NULL. */
141 char *pszError;
142 /** The size of the buffer pszError points to. Can be 0. */
143 size_t cbError;
144
145 /** Pointer to the 2nd manifest. */
146 RTMANIFESTINT *pThis2;
147
148 /** The number of ignored entries from the 1st manifest. */
149 uint32_t cIgnoredEntries2;
150 /** The number of entries processed from the 2nd manifest. */
151 uint32_t cEntries2;
152
153 /** The number of ignored attributes from the 1st manifest. */
154 uint32_t cIgnoredAttributes1;
155 /** The number of ignored attributes from the 1st manifest. */
156 uint32_t cIgnoredAttributes2;
157 /** The number of attributes processed from the 2nd manifest. */
158 uint32_t cAttributes2;
159 /** Pointer to the string space to get matching attributes from. */
160 PRTSTRSPACE pAttributes2;
161 /** The name of the current entry.
162 * Points to an empty string it's the manifest attributes. */
163 const char *pszCurEntry;
164} RTMANIFESTEQUALS;
165/** Pointer to an RTManifestEqualEx argument packet. */
166typedef RTMANIFESTEQUALS *PRTMANIFESTEQUALS;
167
168/**
169 * Argument package used by rtMainfestQueryAttrWorker to pass its search
170 * criteria to rtMainfestQueryAttrEnumCallback and get a result back.
171 */
172typedef struct RTMANIFESTQUERYATTRARGS
173{
174 /** The attribute types we're hunting for. */
175 uint32_t fType;
176 /** What we've found. */
177 PRTMANIFESTATTR pAttr;
178} RTMANIFESTQUERYATTRARGS;
179/** Pointer to a rtMainfestQueryAttrEnumCallback argument packet. */
180typedef RTMANIFESTQUERYATTRARGS *PRTMANIFESTQUERYATTRARGS;
181
182
183/**
184 * Creates an empty manifest.
185 *
186 * @returns IPRT status code.
187 * @param fFlags Flags, MBZ.
188 * @param phManifest Where to return the handle to the manifest.
189 */
190RTDECL(int) RTManifestCreate(uint32_t fFlags, PRTMANIFEST phManifest)
191{
192 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
193 AssertPtr(phManifest);
194
195 RTMANIFESTINT *pThis = (RTMANIFESTINT *)RTMemAlloc(sizeof(*pThis));
196 if (!pThis)
197 return VERR_NO_MEMORY;
198
199 pThis->u32Magic = RTMANIFEST_MAGIC;
200 pThis->cRefs = 1;
201 pThis->Entries = NULL;
202 pThis->cEntries = 0;
203 pThis->SelfEntry.StrCore.pszString = "main";
204 pThis->SelfEntry.StrCore.cchString = 4;
205 pThis->SelfEntry.Attributes = NULL;
206 pThis->SelfEntry.cAttributes = 0;
207 pThis->SelfEntry.fVisited = false;
208 pThis->SelfEntry.szName[0] = '\0';
209
210 *phManifest = pThis;
211 return VINF_SUCCESS;
212}
213
214/**
215 * Retains a reference to the manifest handle.
216 *
217 * @returns The new reference count, UINT32_MAX if the handle is invalid.
218 * @param hManifest The handle to retain.
219 */
220RTDECL(uint32_t) RTManifestRetain(RTMANIFEST hManifest)
221{
222 RTMANIFESTINT *pThis = hManifest;
223 AssertPtrReturn(pThis, UINT32_MAX);
224 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, UINT32_MAX);
225
226 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
227 Assert(cRefs > 1 && cRefs < _1M);
228
229 return cRefs;
230}
231
232
233/**
234 * @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys RTMANIFESTATTR.}
235 */
236static DECLCALLBACK(int) rtManifestDestroyAttribute(PRTSTRSPACECORE pStr, void *pvUser)
237{
238 PRTMANIFESTATTR pAttr = RT_FROM_MEMBER(pStr, RTMANIFESTATTR, StrCore);
239 RTStrFree(pAttr->pszValue);
240 pAttr->pszValue = NULL;
241 RTMemFree(pAttr);
242 NOREF(pvUser);
243 return 0;
244}
245
246
247/**
248 * @callback_method_impl{FNRTSTRSPACECALLBACK, Destroys RTMANIFESTENTRY.}
249 */
250static DECLCALLBACK(int) rtManifestDestroyEntry(PRTSTRSPACECORE pStr, void *pvUser)
251{
252 PRTMANIFESTENTRY pEntry = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
253 RTStrSpaceDestroy(&pEntry->Attributes, rtManifestDestroyAttribute, pvUser);
254 RTMemFree(pEntry);
255 return 0;
256}
257
258
259/**
260 * Releases a reference to the manifest handle.
261 *
262 * @returns The new reference count, 0 if free. UINT32_MAX is returned if the
263 * handle is invalid.
264 * @param hManifest The handle to release.
265 * NIL is quietly ignored (returns 0).
266 */
267RTDECL(uint32_t) RTManifestRelease(RTMANIFEST hManifest)
268{
269 RTMANIFESTINT *pThis = hManifest;
270 if (pThis == NIL_RTMANIFEST)
271 return 0;
272 AssertPtrReturn(pThis, UINT32_MAX);
273 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, UINT32_MAX);
274
275 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
276 Assert(cRefs < _1M);
277 if (!cRefs)
278 {
279 ASMAtomicWriteU32(&pThis->u32Magic, ~RTMANIFEST_MAGIC);
280 RTStrSpaceDestroy(&pThis->Entries, rtManifestDestroyEntry,pThis);
281 RTStrSpaceDestroy(&pThis->SelfEntry.Attributes, rtManifestDestroyAttribute, pThis);
282 RTMemFree(pThis);
283 }
284
285 return cRefs;
286}
287
288
289/**
290 * Creates a duplicate of the specified manifest.
291 *
292 * @returns IPRT status code
293 * @param hManifestSrc The manifest to clone.
294 * @param phManifestDst Where to store the handle to the duplicate.
295 */
296RTDECL(int) RTManifestDup(RTMANIFEST hManifestSrc, PRTMANIFEST phManifestDst)
297{
298 RTMANIFESTINT *pThis = hManifestSrc;
299 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
300 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
301 AssertPtr(phManifestDst);
302
303 RT_NOREF_PV(phManifestDst); /** @todo implement cloning. */
304
305 return VERR_NOT_IMPLEMENTED;
306}
307
308
309/**
310 * @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation}
311 */
312static DECLCALLBACK(int) rtManifestAttributeClearVisited(PRTSTRSPACECORE pStr, void *pvUser)
313{
314 PRTMANIFESTATTR pAttr = RT_FROM_MEMBER(pStr, RTMANIFESTATTR, StrCore);
315 pAttr->fVisited = false;
316 NOREF(pvUser);
317 return 0;
318}
319
320
321/**
322 * @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation}
323 */
324static DECLCALLBACK(int) rtManifestEntryClearVisited(PRTSTRSPACECORE pStr, void *pvUser)
325{
326 PRTMANIFESTENTRY pEntry = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
327 RTStrSpaceEnumerate(&pEntry->Attributes, rtManifestAttributeClearVisited, NULL);
328 pEntry->fVisited = false;
329 NOREF(pvUser);
330 return 0;
331}
332
333
334/**
335 * @callback_method_impl{FNRTSTRSPACECALLBACK, Finds the first missing}
336 */
337static DECLCALLBACK(int) rtManifestAttributeFindMissing2(PRTSTRSPACECORE pStr, void *pvUser)
338{
339 PRTMANIFESTEQUALS pEquals = (PRTMANIFESTEQUALS)pvUser;
340 PRTMANIFESTATTR pAttr = RT_FROM_MEMBER(pStr, RTMANIFESTATTR, StrCore);
341
342 /*
343 * Already visited?
344 */
345 if (pAttr->fVisited)
346 return 0;
347
348 /*
349 * Ignore this entry?
350 */
351 char const * const *ppsz = pEquals->papszIgnoreAttrs;
352 if (ppsz)
353 {
354 while (*ppsz)
355 {
356 if (!strcmp(*ppsz, pAttr->szName))
357 return 0;
358 ppsz++;
359 }
360 }
361
362 /*
363 * Gotcha!
364 */
365 if (*pEquals->pszCurEntry)
366 RTStrPrintf(pEquals->pszError, pEquals->cbError,
367 "Attribute '%s' on '%s' was not found in the 1st manifest",
368 pAttr->szName, pEquals->pszCurEntry);
369 else
370 RTStrPrintf(pEquals->pszError, pEquals->cbError, "Attribute '%s' was not found in the 1st manifest", pAttr->szName);
371 return VERR_NOT_EQUAL;
372}
373
374
375/**
376 * @callback_method_impl{FNRTSTRSPACECALLBACK, Finds the first missing}
377 */
378static DECLCALLBACK(int) rtManifestEntryFindMissing2(PRTSTRSPACECORE pStr, void *pvUser)
379{
380 PRTMANIFESTEQUALS pEquals = (PRTMANIFESTEQUALS)pvUser;
381 PRTMANIFESTENTRY pEntry = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
382
383 /*
384 * Already visited?
385 */
386 if (pEntry->fVisited)
387 return 0;
388
389 /*
390 * Ignore this entry?
391 */
392 char const * const *ppsz = pEquals->papszIgnoreEntries;
393 if (ppsz)
394 {
395 while (*ppsz)
396 {
397 if (!strcmp(*ppsz, pEntry->StrCore.pszString))
398 return 0;
399 ppsz++;
400 }
401 }
402
403 /*
404 * Gotcha!
405 */
406 RTStrPrintf(pEquals->pszError, pEquals->cbError, "'%s' was not found in the 1st manifest", pEntry->StrCore.pszString);
407 return VERR_NOT_EQUAL;
408}
409
410
411/**
412 * @callback_method_impl{FNRTSTRSPACECALLBACK, Compares attributes}
413 */
414static DECLCALLBACK(int) rtManifestAttributeCompare(PRTSTRSPACECORE pStr, void *pvUser)
415{
416 PRTMANIFESTEQUALS pEquals = (PRTMANIFESTEQUALS)pvUser;
417 PRTMANIFESTATTR pAttr1 = RT_FROM_MEMBER(pStr, RTMANIFESTATTR, StrCore);
418 PRTMANIFESTATTR pAttr2;
419
420 Assert(!pAttr1->fVisited);
421 pAttr1->fVisited = true;
422
423 /*
424 * Ignore this entry?
425 */
426 char const * const *ppsz = pEquals->papszIgnoreAttrs;
427 if (ppsz)
428 {
429 while (*ppsz)
430 {
431 if (!strcmp(*ppsz, pAttr1->szName))
432 {
433 pAttr2 = (PRTMANIFESTATTR)RTStrSpaceGet(pEquals->pAttributes2, pAttr1->szName);
434 if (pAttr2)
435 {
436 Assert(!pAttr2->fVisited);
437 pAttr2->fVisited = true;
438 pEquals->cIgnoredAttributes2++;
439 }
440 pEquals->cIgnoredAttributes1++;
441 return 0;
442 }
443 ppsz++;
444 }
445 }
446
447 /*
448 * Find the matching attribute.
449 */
450 pAttr2 = (PRTMANIFESTATTR)RTStrSpaceGet(pEquals->pAttributes2, pAttr1->szName);
451 if (!pAttr2)
452 {
453 if (pEquals->fFlags & RTMANIFEST_EQUALS_IGN_MISSING_ATTRS)
454 return 0;
455
456 if (*pEquals->pszCurEntry)
457 RTStrPrintf(pEquals->pszError, pEquals->cbError,
458 "Attribute '%s' on '%s' was not found in the 2nd manifest",
459 pAttr1->szName, pEquals->pszCurEntry);
460 else
461 RTStrPrintf(pEquals->pszError, pEquals->cbError, "Attribute '%s' was not found in the 2nd manifest", pAttr1->szName);
462 return VERR_NOT_EQUAL;
463 }
464
465 Assert(!pAttr2->fVisited);
466 pAttr2->fVisited = true;
467 pEquals->cAttributes2++;
468
469 /*
470 * Compare them.
471 */
472 if (strcmp(pAttr1->pszValue, pAttr2->pszValue))
473 {
474 if (*pEquals->pszCurEntry)
475 RTStrPrintf(pEquals->pszError, pEquals->cbError,
476 "Attribute '%s' on '%s' does not match ('%s' vs. '%s')",
477 pAttr1->szName, pEquals->pszCurEntry, pAttr1->pszValue, pAttr2->pszValue);
478 else
479 RTStrPrintf(pEquals->pszError, pEquals->cbError,
480 "Attribute '%s' does not match ('%s' vs. '%s')",
481 pAttr1->szName, pAttr1->pszValue, pAttr2->pszValue);
482 return VERR_NOT_EQUAL;
483 }
484
485 return 0;
486}
487
488
489/**
490 * @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation}
491 */
492DECLINLINE (int) rtManifestEntryCompare2(PRTMANIFESTEQUALS pEquals, PRTMANIFESTENTRY pEntry1, PRTMANIFESTENTRY pEntry2)
493{
494 /*
495 * Compare the attributes. It's a bit ugly with all this counting, but
496 * how else to efficiently implement RTMANIFEST_EQUALS_IGN_MISSING_ATTRS?
497 */
498 pEquals->cIgnoredAttributes1 = 0;
499 pEquals->cIgnoredAttributes2 = 0;
500 pEquals->cAttributes2 = 0;
501 pEquals->pszCurEntry = &pEntry2->szName[0];
502 pEquals->pAttributes2 = &pEntry2->Attributes;
503 int rc = RTStrSpaceEnumerate(&pEntry1->Attributes, rtManifestAttributeCompare, pEquals);
504 if (RT_SUCCESS(rc))
505 {
506 /*
507 * Check that we matched all that is required.
508 */
509 if ( pEquals->cAttributes2 + pEquals->cIgnoredAttributes2 != pEntry2->cAttributes
510 && ( !(pEquals->fFlags & RTMANIFEST_EQUALS_IGN_MISSING_ATTRS)
511 || pEquals->cIgnoredAttributes1 == pEntry1->cAttributes))
512 rc = RTStrSpaceEnumerate(&pEntry2->Attributes, rtManifestAttributeFindMissing2, pEquals);
513 }
514 return rc;
515}
516
517
518/**
519 * @callback_method_impl{FNRTSTRSPACECALLBACK, Prepare equals operation}
520 */
521static DECLCALLBACK(int) rtManifestEntryCompare(PRTSTRSPACECORE pStr, void *pvUser)
522{
523 PRTMANIFESTEQUALS pEquals = (PRTMANIFESTEQUALS)pvUser;
524 PRTMANIFESTENTRY pEntry1 = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
525 PRTMANIFESTENTRY pEntry2;
526
527 /*
528 * Ignore this entry?
529 */
530 char const * const *ppsz = pEquals->papszIgnoreEntries;
531 if (ppsz)
532 {
533 while (*ppsz)
534 {
535 if (!strcmp(*ppsz, pStr->pszString))
536 {
537 pEntry2 = (PRTMANIFESTENTRY)RTStrSpaceGet(&pEquals->pThis2->Entries, pStr->pszString);
538 if (pEntry2)
539 {
540 pEntry2->fVisited = true;
541 pEquals->cIgnoredEntries2++;
542 }
543 pEntry1->fVisited = true;
544 return 0;
545 }
546 ppsz++;
547 }
548 }
549
550 /*
551 * Try find the entry in the other manifest.
552 */
553 pEntry2 = (PRTMANIFESTENTRY)RTStrSpaceGet(&pEquals->pThis2->Entries, pEntry1->StrCore.pszString);
554 if (!pEntry2)
555 {
556 RTStrPrintf(pEquals->pszError, pEquals->cbError, "'%s' not found in the 2nd manifest", pEntry1->StrCore.pszString);
557 return VERR_NOT_EQUAL;
558 }
559
560 Assert(!pEntry1->fVisited);
561 Assert(!pEntry2->fVisited);
562 pEntry1->fVisited = true;
563 pEntry2->fVisited = true;
564 pEquals->cEntries2++;
565
566 return rtManifestEntryCompare2(pEquals, pEntry1, pEntry2);
567}
568
569
570
571RTDECL(int) RTManifestEqualsEx(RTMANIFEST hManifest1, RTMANIFEST hManifest2, const char * const *papszIgnoreEntries,
572 const char * const *papszIgnoreAttrs, uint32_t fFlags, char *pszError, size_t cbError)
573{
574 /*
575 * Validate input.
576 */
577 AssertPtrNullReturn(pszError, VERR_INVALID_POINTER);
578 if (pszError && cbError)
579 *pszError = '\0';
580 RTMANIFESTINT *pThis1 = hManifest1;
581 RTMANIFESTINT *pThis2 = hManifest2;
582 if (pThis1 != NIL_RTMANIFEST)
583 {
584 AssertPtrReturn(pThis1, VERR_INVALID_HANDLE);
585 AssertReturn(pThis1->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
586 }
587 if (pThis2 != NIL_RTMANIFEST)
588 {
589 AssertPtrReturn(pThis2, VERR_INVALID_HANDLE);
590 AssertReturn(pThis2->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
591 }
592 AssertReturn(!(fFlags & ~(RTMANIFEST_EQUALS_IGN_MISSING_ATTRS)), VERR_INVALID_PARAMETER);
593
594 /*
595 * The simple cases.
596 */
597 if (pThis1 == pThis2)
598 return VINF_SUCCESS;
599 if (pThis1 == NIL_RTMANIFEST || pThis2 == NIL_RTMANIFEST)
600 return VERR_NOT_EQUAL;
601
602 /*
603 * Since we have to use callback style enumeration, we have to mark the
604 * entries and attributes to make sure we've covered them all.
605 */
606 RTStrSpaceEnumerate(&pThis1->Entries, rtManifestEntryClearVisited, NULL);
607 RTStrSpaceEnumerate(&pThis2->Entries, rtManifestEntryClearVisited, NULL);
608 RTStrSpaceEnumerate(&pThis1->SelfEntry.Attributes, rtManifestAttributeClearVisited, NULL);
609 RTStrSpaceEnumerate(&pThis2->SelfEntry.Attributes, rtManifestAttributeClearVisited, NULL);
610
611 RTMANIFESTEQUALS Equals;
612 Equals.pThis2 = pThis2;
613 Equals.fFlags = fFlags;
614 Equals.papszIgnoreEntries = papszIgnoreEntries;
615 Equals.papszIgnoreAttrs = papszIgnoreAttrs;
616 Equals.pszError = pszError;
617 Equals.cbError = cbError;
618
619 Equals.cIgnoredEntries2 = 0;
620 Equals.cEntries2 = 0;
621 Equals.cIgnoredAttributes1 = 0;
622 Equals.cIgnoredAttributes2 = 0;
623 Equals.cAttributes2 = 0;
624 Equals.pAttributes2 = NULL;
625 Equals.pszCurEntry = NULL;
626
627 int rc = rtManifestEntryCompare2(&Equals, &pThis1->SelfEntry, &pThis2->SelfEntry);
628 if (RT_SUCCESS(rc))
629 rc = RTStrSpaceEnumerate(&pThis1->Entries, rtManifestEntryCompare, &Equals);
630 if (RT_SUCCESS(rc))
631 {
632 /*
633 * Did we cover all entries of the 2nd manifest?
634 */
635 if (Equals.cEntries2 + Equals.cIgnoredEntries2 != pThis2->cEntries)
636 rc = RTStrSpaceEnumerate(&pThis1->Entries, rtManifestEntryFindMissing2, &Equals);
637 }
638
639 return rc;
640}
641
642
643RTDECL(int) RTManifestEquals(RTMANIFEST hManifest1, RTMANIFEST hManifest2)
644{
645 return RTManifestEqualsEx(hManifest1, hManifest2,
646 NULL /*papszIgnoreEntries*/, NULL /*papszIgnoreAttrs*/,
647 0 /*fFlags*/, NULL, 0);
648}
649
650
651/**
652 * Translates a attribyte type to a attribute name.
653 *
654 * @returns Attribute name for fFlags, NULL if not translatable.
655 * @param fType The type flags. Only one bit should be set.
656 */
657static const char *rtManifestTypeToAttrName(uint32_t fType)
658{
659 switch (fType)
660 {
661 case RTMANIFEST_ATTR_SIZE: return "SIZE";
662 case RTMANIFEST_ATTR_MD5: return "MD5";
663 case RTMANIFEST_ATTR_SHA1: return "SHA1";
664 case RTMANIFEST_ATTR_SHA256: return "SHA256";
665 case RTMANIFEST_ATTR_SHA512: return "SHA512";
666 default: return NULL;
667 }
668}
669
670
671/**
672 * Worker common to RTManifestSetAttr and RTManifestEntrySetAttr.
673 *
674 * @returns IPRT status code.
675 * @param pEntry Pointer to the entry.
676 * @param pszAttr The name of the attribute to add.
677 * @param pszValue The value string.
678 * @param fType The attribute type type.
679 */
680static int rtManifestSetAttrWorker(PRTMANIFESTENTRY pEntry, const char *pszAttr, const char *pszValue, uint32_t fType)
681{
682 char *pszValueCopy;
683 int rc = RTStrDupEx(&pszValueCopy, pszValue);
684 if (RT_FAILURE(rc))
685 return rc;
686
687 /*
688 * Does the attribute exist already?
689 */
690 AssertCompileMemberOffset(RTMANIFESTATTR, StrCore, 0);
691 PRTMANIFESTATTR pAttr = (PRTMANIFESTATTR)RTStrSpaceGet(&pEntry->Attributes, pszAttr);
692 if (pAttr)
693 {
694 RTStrFree(pAttr->pszValue);
695 pAttr->pszValue = pszValueCopy;
696 pAttr->fType = fType;
697 }
698 else
699 {
700 size_t cbName = strlen(pszAttr) + 1;
701 pAttr = (PRTMANIFESTATTR)RTMemAllocVar(RT_OFFSETOF(RTMANIFESTATTR, szName[cbName]));
702 if (!pAttr)
703 {
704 RTStrFree(pszValueCopy);
705 return VERR_NO_MEMORY;
706 }
707 memcpy(pAttr->szName, pszAttr, cbName);
708 pAttr->StrCore.pszString = pAttr->szName;
709 pAttr->StrCore.cchString = cbName - 1;
710 pAttr->pszValue = pszValueCopy;
711 pAttr->fType = fType;
712 if (RT_UNLIKELY(!RTStrSpaceInsert(&pEntry->Attributes, &pAttr->StrCore)))
713 {
714 AssertFailed();
715 RTStrFree(pszValueCopy);
716 RTMemFree(pAttr);
717 return VERR_INTERNAL_ERROR_4;
718 }
719 pEntry->cAttributes++;
720 }
721
722 return VINF_SUCCESS;
723}
724
725
726/**
727 * Sets a manifest attribute.
728 *
729 * @returns IPRT status code.
730 * @param hManifest The manifest handle.
731 * @param pszAttr The attribute name. If this already exists,
732 * its value will be replaced.
733 * @param pszValue The value string.
734 * @param fType The attribute type, pass
735 * RTMANIFEST_ATTR_UNKNOWN if not known.
736 */
737RTDECL(int) RTManifestSetAttr(RTMANIFEST hManifest, const char *pszAttr, const char *pszValue, uint32_t fType)
738{
739 RTMANIFESTINT *pThis = hManifest;
740 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
741 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
742 AssertPtr(pszValue);
743 AssertReturn(RT_IS_POWER_OF_TWO(fType) && fType < RTMANIFEST_ATTR_END, VERR_INVALID_PARAMETER);
744 if (!pszAttr)
745 pszAttr = rtManifestTypeToAttrName(fType);
746 AssertPtr(pszAttr);
747
748 return rtManifestSetAttrWorker(&pThis->SelfEntry, pszAttr, pszValue, fType);
749}
750
751
752/**
753 * Worker common to RTManifestUnsetAttr and RTManifestEntryUnsetAttr.
754 *
755 * @returns IPRT status code.
756 * @param pEntry Pointer to the entry.
757 * @param pszAttr The name of the attribute to remove.
758 */
759static int rtManifestUnsetAttrWorker(PRTMANIFESTENTRY pEntry, const char *pszAttr)
760{
761 PRTSTRSPACECORE pStrCore = RTStrSpaceRemove(&pEntry->Attributes, pszAttr);
762 if (!pStrCore)
763 return VWRN_NOT_FOUND;
764 pEntry->cAttributes--;
765 rtManifestDestroyAttribute(pStrCore, NULL);
766 return VINF_SUCCESS;
767}
768
769
770/**
771 * Unsets (removes) a manifest attribute if it exists.
772 *
773 * @returns IPRT status code.
774 * @retval VWRN_NOT_FOUND if not found.
775 *
776 * @param hManifest The manifest handle.
777 * @param pszAttr The attribute name.
778 */
779RTDECL(int) RTManifestUnsetAttr(RTMANIFEST hManifest, const char *pszAttr)
780{
781 RTMANIFESTINT *pThis = hManifest;
782 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
783 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
784 AssertPtr(pszAttr);
785
786 return rtManifestUnsetAttrWorker(&pThis->SelfEntry, pszAttr);
787}
788
789
790/**
791 * Callback employed by rtManifestQueryAttrWorker to search by attribute type.
792 *
793 * @returns VINF_SUCCESS or VINF_CALLBACK_RETURN.
794 * @param pStr The attribute string node.
795 * @param pvUser The argument package.
796 */
797static DECLCALLBACK(int) rtMainfestQueryAttrEnumCallback(PRTSTRSPACECORE pStr, void *pvUser)
798{
799 PRTMANIFESTATTR pAttr = (PRTMANIFESTATTR)pStr;
800 PRTMANIFESTQUERYATTRARGS pArgs = (PRTMANIFESTQUERYATTRARGS)pvUser;
801
802 if (pAttr->fType & pArgs->fType)
803 {
804 pArgs->pAttr = pAttr;
805 return VINF_CALLBACK_RETURN;
806 }
807 return VINF_SUCCESS;
808}
809
810
811/**
812 * Worker common to RTManifestQueryAttr and RTManifestEntryQueryAttr.
813 *
814 * @returns IPRT status code.
815 * @param pEntry The entry.
816 * @param pszAttr The attribute name. If NULL, it will be
817 * selected by @a fType alone.
818 * @param fType The attribute types the entry should match. Pass
819 * Pass RTMANIFEST_ATTR_ANY match any. If more
820 * than one is given, the first matching one is
821 * returned.
822 * @param pszValue Where to return value.
823 * @param cbValue The size of the buffer @a pszValue points to.
824 * @param pfType Where to return the attribute type value.
825 */
826static int rtManifestQueryAttrWorker(PRTMANIFESTENTRY pEntry, const char *pszAttr, uint32_t fType,
827 char *pszValue, size_t cbValue, uint32_t *pfType)
828{
829 /*
830 * Find the requested attribute.
831 */
832 PRTMANIFESTATTR pAttr;
833 if (pszAttr)
834 {
835 /* By name. */
836 pAttr = (PRTMANIFESTATTR)RTStrSpaceGet(&pEntry->Attributes, pszAttr);
837 if (!pAttr)
838 return VERR_MANIFEST_ATTR_NOT_FOUND;
839 if (!(pAttr->fType & fType))
840 return VERR_MANIFEST_ATTR_TYPE_MISMATCH;
841 }
842 else
843 {
844 /* By type. */
845 RTMANIFESTQUERYATTRARGS Args;
846 Args.fType = fType;
847 Args.pAttr = NULL;
848 int rc = RTStrSpaceEnumerate(&pEntry->Attributes, rtMainfestQueryAttrEnumCallback, &Args);
849 AssertRCReturn(rc, rc);
850 pAttr = Args.pAttr;
851 if (!pAttr)
852 return VERR_MANIFEST_ATTR_TYPE_NOT_FOUND;
853 }
854
855 /*
856 * Set the return values.
857 */
858 if (cbValue || pszValue)
859 {
860 size_t cbNeeded = strlen(pAttr->pszValue) + 1;
861 if (cbNeeded > cbValue)
862 return VERR_BUFFER_OVERFLOW;
863 memcpy(pszValue, pAttr->pszValue, cbNeeded);
864 }
865
866 if (pfType)
867 *pfType = pAttr->fType;
868
869 return VINF_SUCCESS;
870}
871
872
873RTDECL(int) RTManifestQueryAttr(RTMANIFEST hManifest, const char *pszAttr, uint32_t fType,
874 char *pszValue, size_t cbValue, uint32_t *pfType)
875{
876 RTMANIFESTINT *pThis = hManifest;
877 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
878 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
879 AssertPtrNull(pszAttr);
880 AssertPtr(pszValue);
881
882 return rtManifestQueryAttrWorker(&pThis->SelfEntry, pszAttr, fType, pszValue, cbValue, pfType);
883}
884
885
886/**
887 * Callback employed by RTManifestQueryAllAttrTypes to collect attribute types.
888 *
889 * @returns VINF_SUCCESS.
890 * @param pStr The attribute string node.
891 * @param pvUser Pointer to type flags (uint32_t).
892 */
893static DECLCALLBACK(int) rtMainfestQueryAllAttrTypesEnumAttrCallback(PRTSTRSPACECORE pStr, void *pvUser)
894{
895 PRTMANIFESTATTR pAttr = (PRTMANIFESTATTR)pStr;
896 uint32_t *pfTypes = (uint32_t *)pvUser;
897 *pfTypes |= pAttr->fType;
898 return VINF_SUCCESS;
899}
900
901
902/**
903 * Callback employed by RTManifestQueryAllAttrTypes to collect attribute types
904 * for an entry.
905 *
906 * @returns VINF_SUCCESS.
907 * @param pStr The attribute string node.
908 * @param pvUser Pointer to type flags (uint32_t).
909 */
910static DECLCALLBACK(int) rtMainfestQueryAllAttrTypesEnumEntryCallback(PRTSTRSPACECORE pStr, void *pvUser)
911{
912 PRTMANIFESTENTRY pEntry = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
913 return RTStrSpaceEnumerate(&pEntry->Attributes, rtMainfestQueryAllAttrTypesEnumAttrCallback, pvUser);
914}
915
916
917RTDECL(int) RTManifestQueryAllAttrTypes(RTMANIFEST hManifest, bool fEntriesOnly, uint32_t *pfTypes)
918{
919 RTMANIFESTINT *pThis = hManifest;
920 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
921 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
922 AssertPtr(pfTypes);
923
924 *pfTypes = 0;
925 int rc = RTStrSpaceEnumerate(&pThis->Entries, rtMainfestQueryAllAttrTypesEnumEntryCallback, pfTypes);
926 if (RT_SUCCESS(rc) && fEntriesOnly)
927 rc = rtMainfestQueryAllAttrTypesEnumAttrCallback(&pThis->SelfEntry.StrCore, pfTypes);
928 return VINF_SUCCESS;
929}
930
931
932/**
933 * Validates the name entry.
934 *
935 * @returns IPRT status code.
936 * @param pszEntry The entry name to validate.
937 * @param pfNeedNormalization Where to return whether it needs normalization
938 * or not. Optional.
939 * @param pcchEntry Where to return the length. Optional.
940 */
941static int rtManifestValidateNameEntry(const char *pszEntry, bool *pfNeedNormalization, size_t *pcchEntry)
942{
943 int rc;
944 bool fNeedNormalization = false;
945 const char *pszCur = pszEntry;
946
947 for (;;)
948 {
949 RTUNICP uc;
950 rc = RTStrGetCpEx(&pszCur, &uc);
951 if (RT_FAILURE(rc))
952 return rc;
953 if (!uc)
954 break;
955 if (uc == '\\')
956 fNeedNormalization = true;
957 else if (uc < 32 || uc == ':' || uc == '(' || uc == ')')
958 return VERR_INVALID_NAME;
959 }
960
961 if (pfNeedNormalization)
962 *pfNeedNormalization = fNeedNormalization;
963
964 size_t cchEntry = pszCur - pszEntry - 1;
965 if (!cchEntry)
966 rc = VERR_INVALID_NAME;
967 if (pcchEntry)
968 *pcchEntry = cchEntry;
969
970 return rc;
971}
972
973
974/**
975 * Normalizes a entry name.
976 *
977 * @param pszEntry The entry name to normalize.
978 */
979static void rtManifestNormalizeEntry(char *pszEntry)
980{
981 char ch;
982 while ((ch = *pszEntry))
983 {
984 if (ch == '\\')
985 *pszEntry = '/';
986 pszEntry++;
987 }
988}
989
990
991/**
992 * Gets an entry.
993 *
994 * @returns IPRT status code.
995 * @param pThis The manifest to work with.
996 * @param pszEntry The entry name.
997 * @param fNeedNormalization Whether rtManifestValidateNameEntry said it
998 * needed normalization.
999 * @param cchEntry The length of the name.
1000 * @param ppEntry Where to return the entry pointer on success.
1001 */
1002static int rtManifestGetEntry(RTMANIFESTINT *pThis, const char *pszEntry, bool fNeedNormalization, size_t cchEntry,
1003 PRTMANIFESTENTRY *ppEntry)
1004{
1005 PRTMANIFESTENTRY pEntry;
1006
1007 AssertCompileMemberOffset(RTMANIFESTATTR, StrCore, 0);
1008 if (!fNeedNormalization)
1009 pEntry = (PRTMANIFESTENTRY)RTStrSpaceGet(&pThis->Entries, pszEntry);
1010 else
1011 {
1012 char *pszCopy = (char *)RTMemTmpAlloc(cchEntry + 1);
1013 if (RT_UNLIKELY(!pszCopy))
1014 return VERR_NO_TMP_MEMORY;
1015 memcpy(pszCopy, pszEntry, cchEntry + 1);
1016 rtManifestNormalizeEntry(pszCopy);
1017
1018 pEntry = (PRTMANIFESTENTRY)RTStrSpaceGet(&pThis->Entries, pszCopy);
1019 RTMemTmpFree(pszCopy);
1020 }
1021
1022 *ppEntry = pEntry;
1023 return pEntry ? VINF_SUCCESS : VERR_NOT_FOUND;
1024}
1025
1026
1027/**
1028 * Sets an attribute of a manifest entry.
1029 *
1030 * @returns IPRT status code.
1031 * @param hManifest The manifest handle.
1032 * @param pszEntry The entry name. This will automatically be
1033 * added if there was no previous call to
1034 * RTManifestEntryAdd for this name. See
1035 * RTManifestEntryAdd for the entry name rules.
1036 * @param pszAttr The attribute name. If this already exists,
1037 * its value will be replaced.
1038 * @param pszValue The value string.
1039 * @param fType The attribute type, pass
1040 * RTMANIFEST_ATTR_UNKNOWN if not known.
1041 */
1042RTDECL(int) RTManifestEntrySetAttr(RTMANIFEST hManifest, const char *pszEntry, const char *pszAttr,
1043 const char *pszValue, uint32_t fType)
1044{
1045 RTMANIFESTINT *pThis = hManifest;
1046 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1047 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1048 AssertPtr(pszEntry);
1049 AssertPtr(pszValue);
1050 AssertReturn(RT_IS_POWER_OF_TWO(fType) && fType < RTMANIFEST_ATTR_END, VERR_INVALID_PARAMETER);
1051 if (!pszAttr)
1052 pszAttr = rtManifestTypeToAttrName(fType);
1053 AssertPtr(pszAttr);
1054
1055 bool fNeedNormalization;
1056 size_t cchEntry;
1057 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1058 AssertRCReturn(rc, rc);
1059
1060 /*
1061 * Resolve the entry, adding one if necessary.
1062 */
1063 PRTMANIFESTENTRY pEntry;
1064 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1065 if (rc == VERR_NOT_FOUND)
1066 {
1067 pEntry = (PRTMANIFESTENTRY)RTMemAlloc(RT_OFFSETOF(RTMANIFESTENTRY, szName[cchEntry + 1]));
1068 if (!pEntry)
1069 return VERR_NO_MEMORY;
1070
1071 pEntry->StrCore.cchString = cchEntry;
1072 pEntry->StrCore.pszString = pEntry->szName;
1073 pEntry->Attributes = NULL;
1074 pEntry->cAttributes = 0;
1075 memcpy(pEntry->szName, pszEntry, cchEntry + 1);
1076 if (fNeedNormalization)
1077 rtManifestNormalizeEntry(pEntry->szName);
1078
1079 if (!RTStrSpaceInsert(&pThis->Entries, &pEntry->StrCore))
1080 {
1081 RTMemFree(pEntry);
1082 return VERR_INTERNAL_ERROR_4;
1083 }
1084 pThis->cEntries++;
1085 }
1086 else if (RT_FAILURE(rc))
1087 return rc;
1088
1089 return rtManifestSetAttrWorker(pEntry, pszAttr, pszValue, fType);
1090}
1091
1092
1093/**
1094 * Unsets (removes) an attribute of a manifest entry if they both exist.
1095 *
1096 * @returns IPRT status code.
1097 * @retval VWRN_NOT_FOUND if not found.
1098 *
1099 * @param hManifest The manifest handle.
1100 * @param pszEntry The entry name.
1101 * @param pszAttr The attribute name.
1102 */
1103RTDECL(int) RTManifestEntryUnsetAttr(RTMANIFEST hManifest, const char *pszEntry, const char *pszAttr)
1104{
1105 RTMANIFESTINT *pThis = hManifest;
1106 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1107 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1108 AssertPtr(pszEntry);
1109 AssertPtr(pszAttr);
1110
1111 bool fNeedNormalization;
1112 size_t cchEntry;
1113 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1114 AssertRCReturn(rc, rc);
1115
1116 /*
1117 * Resolve the entry and hand it over to the worker.
1118 */
1119 PRTMANIFESTENTRY pEntry;
1120 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1121 if (RT_SUCCESS(rc))
1122 rc = rtManifestUnsetAttrWorker(pEntry, pszAttr);
1123 return rc;
1124}
1125
1126
1127RTDECL(int) RTManifestEntryQueryAttr(RTMANIFEST hManifest, const char *pszEntry, const char *pszAttr, uint32_t fType,
1128 char *pszValue, size_t cbValue, uint32_t *pfType)
1129{
1130 RTMANIFESTINT *pThis = hManifest;
1131 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1132 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1133 AssertPtr(pszEntry);
1134 AssertPtrNull(pszAttr);
1135 AssertPtr(pszValue);
1136
1137 /*
1138 * Look up the entry.
1139 */
1140 bool fNeedNormalization;
1141 size_t cchEntry;
1142 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1143 AssertRCReturn(rc, rc);
1144
1145 PRTMANIFESTENTRY pEntry;
1146 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1147 if (RT_SUCCESS(rc))
1148 rc = rtManifestQueryAttrWorker(pEntry, pszAttr, fType, pszValue, cbValue, pfType);
1149 return rc;
1150}
1151
1152
1153/**
1154 * Adds a new entry to a manifest.
1155 *
1156 * The entry name rules:
1157 * - The entry name can contain any character defined by unicode, except
1158 * control characters, ':', '(' and ')'. The exceptions are mainly there
1159 * because of uncertainty around how various formats handles these.
1160 * - It is considered case sensitive.
1161 * - Forward (unix) and backward (dos) slashes are considered path
1162 * separators and converted to forward slashes.
1163 *
1164 * @returns IPRT status code.
1165 * @retval VWRN_ALREADY_EXISTS if the entry already exists.
1166 *
1167 * @param hManifest The manifest handle.
1168 * @param pszEntry The entry name (UTF-8).
1169 *
1170 * @remarks Some manifest formats will not be able to store an entry without
1171 * any attributes. So, this is just here in case it comes in handy
1172 * when dealing with formats which can.
1173 */
1174RTDECL(int) RTManifestEntryAdd(RTMANIFEST hManifest, const char *pszEntry)
1175{
1176 RTMANIFESTINT *pThis = hManifest;
1177 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1178 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1179 AssertPtr(pszEntry);
1180
1181 bool fNeedNormalization;
1182 size_t cchEntry;
1183 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1184 AssertRCReturn(rc, rc);
1185
1186 /*
1187 * Only add one if it does not already exist.
1188 */
1189 PRTMANIFESTENTRY pEntry;
1190 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1191 if (rc == VERR_NOT_FOUND)
1192 {
1193 pEntry = (PRTMANIFESTENTRY)RTMemAlloc(RT_OFFSETOF(RTMANIFESTENTRY, szName[cchEntry + 1]));
1194 if (pEntry)
1195 {
1196 pEntry->StrCore.cchString = cchEntry;
1197 pEntry->StrCore.pszString = pEntry->szName;
1198 pEntry->Attributes = NULL;
1199 pEntry->cAttributes = 0;
1200 memcpy(pEntry->szName, pszEntry, cchEntry + 1);
1201 if (fNeedNormalization)
1202 rtManifestNormalizeEntry(pEntry->szName);
1203
1204 if (RTStrSpaceInsert(&pThis->Entries, &pEntry->StrCore))
1205 {
1206 pThis->cEntries++;
1207 rc = VINF_SUCCESS;
1208 }
1209 else
1210 {
1211 RTMemFree(pEntry);
1212 rc = VERR_INTERNAL_ERROR_4;
1213 }
1214 }
1215 else
1216 rc = VERR_NO_MEMORY;
1217 }
1218 else if (RT_SUCCESS(rc))
1219 rc = VWRN_ALREADY_EXISTS;
1220
1221 return rc;
1222}
1223
1224
1225/**
1226 * Removes an entry.
1227 *
1228 * @returns IPRT status code.
1229 * @param hManifest The manifest handle.
1230 * @param pszEntry The entry name.
1231 */
1232RTDECL(int) RTManifestEntryRemove(RTMANIFEST hManifest, const char *pszEntry)
1233{
1234 RTMANIFESTINT *pThis = hManifest;
1235 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1236 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1237 AssertPtr(pszEntry);
1238
1239 bool fNeedNormalization;
1240 size_t cchEntry;
1241 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1242 AssertRCReturn(rc, rc);
1243
1244 /*
1245 * Look it up before removing it.
1246 */
1247 PRTMANIFESTENTRY pEntry;
1248 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1249 if (RT_SUCCESS(rc))
1250 {
1251 PRTSTRSPACECORE pStrCore = RTStrSpaceRemove(&pThis->Entries, pEntry->StrCore.pszString);
1252 AssertReturn(pStrCore, VERR_INTERNAL_ERROR_3);
1253 pThis->cEntries--;
1254 rtManifestDestroyEntry(pStrCore, pThis);
1255 }
1256
1257 return rc;
1258}
1259
1260
1261RTDECL(bool) RTManifestEntryExists(RTMANIFEST hManifest, const char *pszEntry)
1262{
1263 RTMANIFESTINT *pThis = hManifest;
1264 AssertPtrReturn(pThis, false);
1265 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, false);
1266 AssertPtr(pszEntry);
1267
1268 bool fNeedNormalization;
1269 size_t cchEntry;
1270 int rc = rtManifestValidateNameEntry(pszEntry, &fNeedNormalization, &cchEntry);
1271 AssertRCReturn(rc, false);
1272
1273 /*
1274 * Check if it exists.
1275 */
1276 PRTMANIFESTENTRY pEntry;
1277 rc = rtManifestGetEntry(pThis, pszEntry, fNeedNormalization, cchEntry, &pEntry);
1278 return RT_SUCCESS_NP(rc);
1279}
1280
1281
1282/**
1283 * Reads a line from a VFS I/O stream.
1284 *
1285 * @todo Replace this with a buffered I/O stream layer.
1286 *
1287 * @returns IPRT status code. VERR_EOF when trying to read beyond the stream
1288 * end.
1289 * @param hVfsIos The I/O stream to read from.
1290 * @param pszLine Where to store what we've read.
1291 * @param cbLine The number of bytes to read.
1292 */
1293static int rtManifestReadLine(RTVFSIOSTREAM hVfsIos, char *pszLine, size_t cbLine)
1294{
1295 /* This is horribly slow right now, but it's not a biggy as the input is
1296 usually cached in memory somewhere... */
1297 *pszLine = '\0';
1298 while (cbLine > 1)
1299 {
1300 char ch;
1301 int rc = RTVfsIoStrmRead(hVfsIos, &ch, 1, true /*fBLocking*/, NULL);
1302 if (RT_FAILURE(rc))
1303 return rc;
1304
1305 /* \r\n */
1306 if (ch == '\r')
1307 {
1308 if (cbLine <= 2)
1309 {
1310 pszLine[0] = ch;
1311 pszLine[1] = '\0';
1312 return VINF_BUFFER_OVERFLOW;
1313 }
1314
1315 rc = RTVfsIoStrmRead(hVfsIos, &ch, 1, true /*fBLocking*/, NULL);
1316 if (RT_SUCCESS(rc) && ch == '\n')
1317 return VINF_SUCCESS;
1318 pszLine[0] = '\r';
1319 pszLine[1] = ch;
1320 pszLine[2] = '\0';
1321 if (RT_FAILURE(rc))
1322 return rc == VERR_EOF ? VINF_EOF : rc;
1323 }
1324
1325 /* \n */
1326 if (ch == '\n')
1327 return VINF_SUCCESS;
1328
1329 /* add character. */
1330 pszLine[0] = ch;
1331 pszLine[1] = '\0';
1332
1333 /* advance */
1334 pszLine++;
1335 cbLine--;
1336 }
1337
1338 return VINF_BUFFER_OVERFLOW;
1339}
1340
1341
1342RTDECL(int) RTManifestReadStandardEx(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos, char *pszErr, size_t cbErr)
1343{
1344 /*
1345 * Validate input.
1346 */
1347 AssertPtrNull(pszErr);
1348 if (pszErr && cbErr)
1349 *pszErr = '\0';
1350 RTMANIFESTINT *pThis = hManifest;
1351 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1352 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1353
1354 /*
1355 * Process the stream line by line.
1356 */
1357 uint32_t iLine = 0;
1358 for (;;)
1359 {
1360 /*
1361 * Read a line from the input stream.
1362 */
1363 iLine++;
1364 char szLine[RTPATH_MAX + RTSHA512_DIGEST_LEN + 32];
1365 int rc = rtManifestReadLine(hVfsIos, szLine, sizeof(szLine));
1366 if (RT_FAILURE(rc))
1367 {
1368 if (rc == VERR_EOF)
1369 return VINF_SUCCESS;
1370 RTStrPrintf(pszErr, cbErr, "Error reading line #%u: %Rrc", iLine, rc);
1371 return rc;
1372 }
1373 if (rc != VINF_SUCCESS)
1374 {
1375 RTStrPrintf(pszErr, cbErr, "Line number %u is too long", iLine);
1376 return VERR_OUT_OF_RANGE;
1377 }
1378
1379 /*
1380 * Strip it and skip if empty.
1381 */
1382 char *psz = RTStrStrip(szLine);
1383 if (!*psz)
1384 continue;
1385
1386 /*
1387 * Read the attribute name.
1388 */
1389 char ch;
1390 const char * const pszAttr = psz;
1391 do
1392 psz++;
1393 while (!RT_C_IS_BLANK((ch = *psz)) && ch && ch != '(');
1394 if (ch)
1395 *psz++ = '\0';
1396
1397 /*
1398 * The entry name is enclosed in parenthesis and followed by a '='.
1399 */
1400 if (ch != '(')
1401 {
1402 psz = RTStrStripL(psz);
1403 ch = *psz++;
1404 if (ch != '(')
1405 {
1406 RTStrPrintf(pszErr, cbErr, "Expected '(' after %zu on line %u", psz - szLine - 1, iLine);
1407 return VERR_PARSE_ERROR;
1408 }
1409 }
1410 const char * const pszName = psz;
1411 while ((ch = *psz) != '\0')
1412 {
1413 if (ch == ')')
1414 {
1415 char *psz2 = RTStrStripL(psz + 1);
1416 if (*psz2 == '=')
1417 {
1418 *psz = '\0';
1419 psz = psz2;
1420 break;
1421 }
1422 }
1423 psz++;
1424 }
1425
1426 if (*psz != '=')
1427 {
1428 RTStrPrintf(pszErr, cbErr, "Expected ')=' at %zu on line %u", psz - szLine, iLine);
1429 return VERR_PARSE_ERROR;
1430 }
1431
1432 /*
1433 * The value.
1434 */
1435 psz = RTStrStrip(psz + 1);
1436 const char * const pszValue = psz;
1437 if (!*psz)
1438 {
1439 RTStrPrintf(pszErr, cbErr, "Expected value at %zu on line %u", psz - szLine, iLine);
1440 return VERR_PARSE_ERROR;
1441 }
1442
1443 /*
1444 * Detect attribute type and sanity check the value.
1445 */
1446 uint32_t fType = RTMANIFEST_ATTR_UNKNOWN;
1447 static const struct
1448 {
1449 const char *pszAttr;
1450 uint32_t fType;
1451 unsigned cBits;
1452 unsigned uBase;
1453 } s_aDecAttrs[] =
1454 {
1455 { "SIZE", RTMANIFEST_ATTR_SIZE, 64, 10}
1456 };
1457 for (unsigned i = 0; i < RT_ELEMENTS(s_aDecAttrs); i++)
1458 if (!strcmp(s_aDecAttrs[i].pszAttr, pszAttr))
1459 {
1460 fType = s_aDecAttrs[i].fType;
1461 rc = RTStrToUInt64Full(pszValue, s_aDecAttrs[i].uBase, NULL);
1462 if (rc != VINF_SUCCESS)
1463 {
1464 RTStrPrintf(pszErr, cbErr, "Malformed value ('%s') at %zu on line %u: %Rrc", pszValue, psz - szLine, iLine, rc);
1465 return VERR_PARSE_ERROR;
1466 }
1467 break;
1468 }
1469
1470 if (fType == RTMANIFEST_ATTR_UNKNOWN)
1471 {
1472 static const struct
1473 {
1474 const char *pszAttr;
1475 uint32_t fType;
1476 unsigned cchHex;
1477 } s_aHexAttrs[] =
1478 {
1479 { "MD5", RTMANIFEST_ATTR_MD5, RTMD5_DIGEST_LEN },
1480 { "SHA1", RTMANIFEST_ATTR_SHA1, RTSHA1_DIGEST_LEN },
1481 { "SHA256", RTMANIFEST_ATTR_SHA256, RTSHA256_DIGEST_LEN },
1482 { "SHA512", RTMANIFEST_ATTR_SHA512, RTSHA512_DIGEST_LEN }
1483 };
1484 for (unsigned i = 0; i < RT_ELEMENTS(s_aHexAttrs); i++)
1485 if (!strcmp(s_aHexAttrs[i].pszAttr, pszAttr))
1486 {
1487 fType = s_aHexAttrs[i].fType;
1488 for (unsigned off = 0; off < s_aHexAttrs[i].cchHex; off++)
1489 if (!RT_C_IS_XDIGIT(pszValue[off]))
1490 {
1491 RTStrPrintf(pszErr, cbErr, "Expected hex digit at %zu on line %u (value '%s', pos %u)",
1492 pszValue - szLine + off, iLine, pszValue, off);
1493 return VERR_PARSE_ERROR;
1494 }
1495 break;
1496 }
1497 }
1498
1499 /*
1500 * Finally, add it.
1501 */
1502 rc = RTManifestEntrySetAttr(hManifest, pszName, pszAttr, pszValue, fType);
1503 if (RT_FAILURE(rc))
1504 {
1505 RTStrPrintf(pszErr, cbErr, "RTManifestEntrySetAttr(,'%s','%s', '%s', %#x) failed on line %u: %Rrc",
1506 pszName, pszAttr, pszValue, fType, iLine, rc);
1507 return rc;
1508 }
1509 }
1510}
1511
1512RTDECL(int) RTManifestReadStandard(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos)
1513{
1514 return RTManifestReadStandardEx(hManifest, hVfsIos, NULL, 0);
1515}
1516
1517
1518/**
1519 * @callback_method_impl{FNRTSTRSPACECALLBACK, Writes RTMANIFESTATTR.}
1520 */
1521static DECLCALLBACK(int) rtManifestWriteStdAttr(PRTSTRSPACECORE pStr, void *pvUser)
1522{
1523 PRTMANIFESTATTR pAttr = RT_FROM_MEMBER(pStr, RTMANIFESTATTR, StrCore);
1524 RTMANIFESTWRITESTDATTR *pArgs = (RTMANIFESTWRITESTDATTR *)pvUser;
1525 char szLine[RTPATH_MAX + RTSHA512_DIGEST_LEN + 32];
1526 size_t cchLine = RTStrPrintf(szLine, sizeof(szLine), "%s (%s) = %s\n", pAttr->szName, pArgs->pszEntry, pAttr->pszValue);
1527 if (cchLine >= sizeof(szLine) - 1)
1528 return VERR_BUFFER_OVERFLOW;
1529 return RTVfsIoStrmWrite(pArgs->hVfsIos, szLine, cchLine, true /*fBlocking*/, NULL);
1530}
1531
1532
1533/**
1534 * @callback_method_impl{FNRTSTRSPACECALLBACK, Writes RTMANIFESTENTRY.}
1535 */
1536static DECLCALLBACK(int) rtManifestWriteStdEntry(PRTSTRSPACECORE pStr, void *pvUser)
1537{
1538 PRTMANIFESTENTRY pEntry = RT_FROM_MEMBER(pStr, RTMANIFESTENTRY, StrCore);
1539
1540 RTMANIFESTWRITESTDATTR Args;
1541 Args.hVfsIos = (RTVFSIOSTREAM)pvUser;
1542 Args.pszEntry = pStr->pszString;
1543 return RTStrSpaceEnumerate(&pEntry->Attributes, rtManifestWriteStdAttr, &Args);
1544}
1545
1546
1547RTDECL(int) RTManifestWriteStandard(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos)
1548{
1549 RTMANIFESTINT *pThis = hManifest;
1550 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1551 AssertReturn(pThis->u32Magic == RTMANIFEST_MAGIC, VERR_INVALID_HANDLE);
1552
1553 RTMANIFESTWRITESTDATTR Args;
1554 Args.hVfsIos = hVfsIos;
1555 Args.pszEntry = "main";
1556 int rc = RTStrSpaceEnumerate(&pThis->SelfEntry.Attributes, rtManifestWriteStdAttr, &Args);
1557 if (RT_SUCCESS(rc))
1558 rc = RTStrSpaceEnumerate(&pThis->Entries, rtManifestWriteStdEntry, hVfsIos);
1559 return rc;
1560}
1561
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette