From 220adf02ded4c6b499d10a8d589df73424fc4931 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 22 Dec 2009 12:11:42 +0200 Subject: [PATCH] Move the higher level convenience functions out of header.c - header.c is big enough as it is, and none of these things needs header struct internals for anything - no functional changes, just moving stuff around --- lib/header.c | 177 ------------------------------------------------------- lib/headerutil.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 177 deletions(-) diff --git a/lib/header.c b/lib/header.c index 77e2473..a88f205 100644 --- a/lib/header.c +++ b/lib/header.c @@ -1486,100 +1486,6 @@ int headerPut(Header h, rpmtd td, headerPutFlags flags) return rc; } -/* - * Sanity check data types against tag table before putting. Assume - * append on all array-types. - */ -static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype, - rpm_constdata_t data, rpm_count_t size) -{ - struct rpmtd_s td; - rpmTagType type = rpmTagGetType(tag); - headerPutFlags flags = HEADERPUT_APPEND; - int valid = 1; - - /* Basic sanity checks: type must match and there must be data to put */ - if ((type & RPM_MASK_TYPE) != reqtype - || size < 1 || data == NULL || h == NULL) { - valid = 0; - } - - /* - * Non-array types can't be appended to. Binary types use size - * for data length, for other non-array types size must be 1. - */ - if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) { - flags = HEADERPUT_DEFAULT; - if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) { - valid = 0; - } - } - - if (valid) { - rpmtdReset(&td); - td.tag = tag; - td.type = type & RPM_MASK_TYPE; - td.data = (void *) data; - td.count = size; - - valid = headerPut(h, &td, flags); - } - - return valid; -} - -int headerPutString(Header h, rpmTag tag, const char *val) -{ - rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE; - const void *sptr = NULL; - - /* string arrays expect char **, arrange that */ - if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) { - sptr = &val; - } else if (type == RPM_STRING_TYPE) { - sptr = val; - } else { - return 0; - } - - return headerPutType(h, tag, type, sptr, 1); -} - -int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size); -} - -int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_CHAR_TYPE, val, size); -} - -int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_INT8_TYPE, val, size); -} - -int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_INT16_TYPE, val, size); -} - -int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_INT32_TYPE, val, size); -} - -int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_INT64_TYPE, val, size); -} - -int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size) -{ - return headerPutType(h, tag, RPM_BIN_TYPE, val, size); -} - int headerAddI18NString(Header h, rpmTag tag, const char * string, const char * lang) { @@ -1822,48 +1728,6 @@ int headerNext(HeaderIterator hi, rpmtd td) return ((rc == 1) ? 1 : 0); } -/** \ingroup header - * Duplicate a header. - * @param h header - * @return new header instance - */ -Header headerCopy(Header h) -{ - Header nh = headerNew(); - HeaderIterator hi; - struct rpmtd_s td; - - hi = headerInitIterator(h); - while (headerNext(hi, &td)) { - if (rpmtdCount(&td) > 0) { - (void) headerPut(nh, &td, HEADERPUT_DEFAULT); - } - rpmtdFreeData(&td); - } - hi = headerFreeIterator(hi); - - return headerReload(nh, HEADER_IMAGE); -} - -void headerCopyTags(Header headerFrom, Header headerTo, - const rpmTag * tagstocopy) -{ - const rpmTag * p; - struct rpmtd_s td; - - if (headerFrom == headerTo) - return; - - for (p = tagstocopy; *p != 0; p++) { - if (headerIsEntry(headerTo, *p)) - continue; - if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM)) - continue; - (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT); - rpmtdFreeData(&td); - } -} - unsigned int headerGetInstance(Header h) { return h ? h->instance : 0; @@ -1874,44 +1738,3 @@ void headerSetInstance(Header h, unsigned int instance) h->instance = instance; } -char * headerGetAsString(Header h, rpmTag tag) -{ - char *res = NULL; - struct rpmtd_s td; - - if (headerGet(h, tag, &td, HEADERGET_EXT)) { - if (rpmtdCount(&td) == 1) { - res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL); - } - rpmtdFreeData(&td); - } - return res; -} - -const char * headerGetString(Header h, rpmTag tag) -{ - const char *res = NULL; - struct rpmtd_s td; - - if (headerGet(h, tag, &td, HEADERGET_MINMEM)) { - if (rpmtdCount(&td) == 1) { - res = rpmtdGetString(&td); - } - rpmtdFreeData(&td); - } - return res; -} - -uint64_t headerGetNumber(Header h, rpmTag tag) -{ - uint64_t res = 0; - struct rpmtd_s td; - - if (headerGet(h, tag, &td, HEADERGET_EXT)) { - if (rpmtdCount(&td) == 1) { - res = rpmtdGetNumber(&td); - } - rpmtdFreeData(&td); - } - return res; -} diff --git a/lib/headerutil.c b/lib/headerutil.c index 3687372..aa0c443 100644 --- a/lib/headerutil.c +++ b/lib/headerutil.c @@ -69,3 +69,176 @@ int headerIsSource(Header h) return (!headerIsEntry(h, RPMTAG_SOURCERPM)); } +Header headerCopy(Header h) +{ + Header nh = headerNew(); + HeaderIterator hi; + struct rpmtd_s td; + + hi = headerInitIterator(h); + while (headerNext(hi, &td)) { + if (rpmtdCount(&td) > 0) { + (void) headerPut(nh, &td, HEADERPUT_DEFAULT); + } + rpmtdFreeData(&td); + } + hi = headerFreeIterator(hi); + + return headerReload(nh, HEADER_IMAGE); +} + +void headerCopyTags(Header headerFrom, Header headerTo, + const rpmTag * tagstocopy) +{ + const rpmTag * p; + struct rpmtd_s td; + + if (headerFrom == headerTo) + return; + + for (p = tagstocopy; *p != 0; p++) { + if (headerIsEntry(headerTo, *p)) + continue; + if (!headerGet(headerFrom, *p, &td, HEADERGET_MINMEM)) + continue; + (void) headerPut(headerTo, &td, HEADERPUT_DEFAULT); + rpmtdFreeData(&td); + } +} + +char * headerGetAsString(Header h, rpmTag tag) +{ + char *res = NULL; + struct rpmtd_s td; + + if (headerGet(h, tag, &td, HEADERGET_EXT)) { + if (rpmtdCount(&td) == 1) { + res = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL); + } + rpmtdFreeData(&td); + } + return res; +} + +const char * headerGetString(Header h, rpmTag tag) +{ + const char *res = NULL; + struct rpmtd_s td; + + if (headerGet(h, tag, &td, HEADERGET_MINMEM)) { + if (rpmtdCount(&td) == 1) { + res = rpmtdGetString(&td); + } + rpmtdFreeData(&td); + } + return res; +} + +uint64_t headerGetNumber(Header h, rpmTag tag) +{ + uint64_t res = 0; + struct rpmtd_s td; + + if (headerGet(h, tag, &td, HEADERGET_EXT)) { + if (rpmtdCount(&td) == 1) { + res = rpmtdGetNumber(&td); + } + rpmtdFreeData(&td); + } + return res; +} + +/* + * Sanity check data types against tag table before putting. Assume + * append on all array-types. + */ +static int headerPutType(Header h, rpmTag tag, rpmTagType reqtype, + rpm_constdata_t data, rpm_count_t size) +{ + struct rpmtd_s td; + rpmTagType type = rpmTagGetType(tag); + headerPutFlags flags = HEADERPUT_APPEND; + int valid = 1; + + /* Basic sanity checks: type must match and there must be data to put */ + if ((type & RPM_MASK_TYPE) != reqtype + || size < 1 || data == NULL || h == NULL) { + valid = 0; + } + + /* + * Non-array types can't be appended to. Binary types use size + * for data length, for other non-array types size must be 1. + */ + if ((type & RPM_MASK_RETURN_TYPE) != RPM_ARRAY_RETURN_TYPE) { + flags = HEADERPUT_DEFAULT; + if ((type & RPM_MASK_TYPE) != RPM_BIN_TYPE && size != 1) { + valid = 0; + } + } + + if (valid) { + rpmtdReset(&td); + td.tag = tag; + td.type = type & RPM_MASK_TYPE; + td.data = (void *) data; + td.count = size; + + valid = headerPut(h, &td, flags); + } + + return valid; +} + +int headerPutString(Header h, rpmTag tag, const char *val) +{ + rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE; + const void *sptr = NULL; + + /* string arrays expect char **, arrange that */ + if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE) { + sptr = &val; + } else if (type == RPM_STRING_TYPE) { + sptr = val; + } else { + return 0; + } + + return headerPutType(h, tag, type, sptr, 1); +} + +int headerPutStringArray(Header h, rpmTag tag, const char **array, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_STRING_ARRAY_TYPE, array, size); +} + +int headerPutChar(Header h, rpmTag tag, char *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_CHAR_TYPE, val, size); +} + +int headerPutUint8(Header h, rpmTag tag, uint8_t *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_INT8_TYPE, val, size); +} + +int headerPutUint16(Header h, rpmTag tag, uint16_t *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_INT16_TYPE, val, size); +} + +int headerPutUint32(Header h, rpmTag tag, uint32_t *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_INT32_TYPE, val, size); +} + +int headerPutUint64(Header h, rpmTag tag, uint64_t *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_INT64_TYPE, val, size); +} + +int headerPutBin(Header h, rpmTag tag, uint8_t *val, rpm_count_t size) +{ + return headerPutType(h, tag, RPM_BIN_TYPE, val, size); +} + -- 2.7.4