From 0ad1873796883c0f66b2ad8526766d68ed28dd0b Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Thu, 30 Aug 2007 15:49:31 +0300 Subject: [PATCH] Move rollback IDTX out of cli-stuff, its needed by core ts handling too. --- lib/Makefile.am | 4 +- lib/idtx.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/idtx.h | 93 ++++++++++++++++++++++++++ lib/rpmcli.h | 88 +------------------------ lib/rpminstall.c | 183 ---------------------------------------------------- lib/transaction.c | 7 +- python/rpmts-py.c | 2 +- 7 files changed, 289 insertions(+), 278 deletions(-) create mode 100644 lib/idtx.c create mode 100644 lib/idtx.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 8c1d678..1d2c2e6 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -18,7 +18,7 @@ check_PROGRAMS = pkgincdir = $(pkgincludedir) pkginc_HEADERS = \ - misc.h rpmcli.h rpmlib.h \ + idtx.h misc.h rpmcli.h rpmlib.h \ rpmal.h rpmds.h rpmfi.h rpmgi.h rpmps.h rpmsx.h rpmte.h rpmts.h \ stringbuf.h @@ -26,7 +26,7 @@ usrlibdir = $(libdir) usrlib_LTLIBRARIES = librpm.la librpm_la_SOURCES = \ cpio.c cpio.h depends.c formats.c fs.c fsm.c fsm.h getdate.c \ - manifest.c manifest.h misc.c package.c \ + idtx.c manifest.c manifest.h misc.c package.c \ poptALL.c poptI.c poptQV.c psm.c psm.h query.c \ rpmal.c rpmchecksig.c rpmds.c rpmfi.c rpmgi.c rpminstall.c \ rpmlead.c rpmlead.h rpmlibprov.c rpmps.c rpmrc.c rpmsx.c rpmte.c rpmts.c \ diff --git a/lib/idtx.c b/lib/idtx.c new file mode 100644 index 0000000..6f58541 --- /dev/null +++ b/lib/idtx.c @@ -0,0 +1,190 @@ + +#include "idtx.h" +#include "rpmlib.h" +#include "rpmdb.h" +#include "rpmts.h" +#include "rpmmacro.h" + +/*@unchecked@*/ +static int reverse = -1; + +/** + */ +static int IDTintcmp(const void * a, const void * b) + /*@*/ +{ + /*@-castexpose@*/ + return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) ); + /*@=castexpose@*/ +} + +IDTX IDTXfree(IDTX idtx) +{ + if (idtx) { + int i; + if (idtx->idt) + for (i = 0; i < idtx->nidt; i++) { + IDT idt = idtx->idt + i; + idt->h = headerFree(idt->h); + idt->key = _free(idt->key); + } + idtx->idt = _free(idtx->idt); + idtx = _free(idtx); + } + return NULL; +} + +IDTX IDTXnew(void) +{ + IDTX idtx = xcalloc(1, sizeof(*idtx)); + idtx->delta = 10; + idtx->size = sizeof(*((IDT)0)); + return idtx; +} + +IDTX IDTXgrow(IDTX idtx, int need) +{ + if (need < 0) return NULL; + if (idtx == NULL) + idtx = IDTXnew(); + if (need == 0) return idtx; + + if ((idtx->nidt + need) > idtx->alloced) { + while (need > 0) { + idtx->alloced += idtx->delta; + need -= idtx->delta; + } + idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) ); + } + return idtx; +} + +IDTX IDTXsort(IDTX idtx) +{ + if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0) + qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp); + return idtx; +} + +IDTX IDTXload(rpmts ts, rpmTag tag) +{ + IDTX idtx = NULL; + rpmdbMatchIterator mi; + HGE_t hge = (HGE_t) headerGetEntry; + Header h; + + /*@-branchstate@*/ + mi = rpmtsInitIterator(ts, tag, NULL, 0); +#ifdef NOTYET + (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey'); +#endif + while ((h = rpmdbNextIterator(mi)) != NULL) { + rpmTagType type = RPM_NULL_TYPE; + int_32 count = 0; + int_32 * tidp; + + tidp = NULL; + if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL) + continue; + + if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1)) + continue; + + idtx = IDTXgrow(idtx, 1); + if (idtx == NULL) + continue; + if (idtx->idt == NULL) + continue; + + { IDT idt; + /*@-nullderef@*/ + idt = idtx->idt + idtx->nidt; + /*@=nullderef@*/ + idt->h = headerLink(h); + idt->key = NULL; + idt->instance = rpmdbGetIteratorOffset(mi); + idt->val.u32 = *tidp; + } + idtx->nidt++; + } + mi = rpmdbFreeIterator(mi); + /*@=branchstate@*/ + + return IDTXsort(idtx); +} + +IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag) +{ + IDTX idtx = NULL; + HGE_t hge = (HGE_t) headerGetEntry; + Header h; + int_32 * tidp; + FD_t fd; + const char ** av = NULL; + int ac = 0; + rpmRC rpmrc; + int xx; + int i; + + av = NULL; ac = 0; + xx = rpmGlob(globstr, &ac, &av); + + if (xx == 0) + for (i = 0; i < ac; i++) { + rpmTagType type; + int_32 count; + int isSource; + + fd = Fopen(av[i], "r.ufdio"); + if (fd == NULL || Ferror(fd)) { + rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i], + Fstrerror(fd)); + if (fd != NULL) (void) Fclose(fd); + continue; + } + + rpmrc = rpmReadPackageFile(ts, fd, av[i], &h); + (void) Fclose(fd); + switch (rpmrc) { + default: + goto bottom; + /*@notreached@*/ /*@switchbreak@*/ break; + case RPMRC_NOTTRUSTED: + case RPMRC_NOKEY: + case RPMRC_OK: + isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE); + if (isSource) + goto bottom; + /*@switchbreak@*/ break; + } + + tidp = NULL; + /*@-branchstate@*/ + if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) { + + idtx = IDTXgrow(idtx, 1); + if (idtx == NULL || idtx->idt == NULL) + goto bottom; + + { IDT idt; + idt = idtx->idt + idtx->nidt; + idt->h = headerLink(h); + idt->key = av[i]; + av[i] = NULL; + idt->instance = 0; + idt->val.u32 = *tidp; + } + idtx->nidt++; + } + /*@=branchstate@*/ +bottom: + h = headerFree(h); + } + + for (i = 0; i < ac; i++) + av[i] = _free(av[i]); + av = _free(av); ac = 0; + + return IDTXsort(idtx); +} + diff --git a/lib/idtx.h b/lib/idtx.h new file mode 100644 index 0000000..79276d0 --- /dev/null +++ b/lib/idtx.h @@ -0,0 +1,93 @@ +#ifndef H_RPMIDTX +#define H_RPMIDTX + +#include "system.h" +#include "rpmlib.h" + +/** + * * A rollback transaction id element. + * */ +/*@-fielduse@*/ +typedef /*@abstract@*/ struct IDT_s { + unsigned int instance; /*!< installed package transaction id. */ +/*@owned@*/ /*@null@*/ + const char * key; /*! removed package file name. */ + Header h; /*!< removed package header. */ + union { + uint_32 u32; /*!< install/remove transaction id */ + } val; +} * IDT; +/*@=fielduse@*/ + +/** + * A rollback transaction id index. + */ +typedef /*@abstract@*/ struct IDTindex_s { + int delta; /*!< no. elements to realloc as a chunk. */ + int size; /*!< size of id index element. */ + int alloced; /*!< current number of elements allocated. */ + int nidt; /*!< current number of elements initialized. */ +/*@only@*/ /*@null@*/ + IDT idt; /*!< id index elements. */ +} * IDTX; + +/** + * Destroy id index. + * @param idtx id index + * @return NULL always + */ +/*@null@*/ +IDTX IDTXfree(/*@only@*/ /*@null@*/ IDTX idtx) + /*@modifies idtx @*/; + +/** + * Create id index. + * @return new id index + */ +/*@only@*/ +IDTX IDTXnew(void) + /*@*/; + +/** + * Insure that index has room for "need" elements. + * @param idtx id index + * @param need additional no. of elements needed + * @return id index (with room for "need" elements) + */ +/*@only@*/ /*@null@*/ +IDTX IDTXgrow(/*@only@*/ /*@null@*/ IDTX idtx, int need) + /*@modifies idtx @*/; + +/** + * Sort tag (instance,value) pairs. + * @param idtx id index + * @return id index + */ +/*@only@*/ /*@null@*/ +IDTX IDTXsort(/*@only@*/ /*@null@*/ IDTX idtx) + /*@modifies idtx @*/; + +/** + * Load tag (instance,value) pairs from rpm databse, and return sorted id index. + * @param ts transaction set + * @param tag rpm tag + * @return id index + */ +/*@only@*/ /*@null@*/ +IDTX IDTXload(rpmts ts, rpmTag tag) + /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/ + /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/; + +/** + * Load tag (instance,value) pairs from packages, and return sorted id index. + * @param ts transaction set + * @param globstr glob expression + * @param tag rpm tag + * @return id index + */ +/*@only@*/ /*@null@*/ +IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag) + /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/ + /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/; + +#endif /* H_RPMIDTX */ diff --git a/lib/rpmcli.h b/lib/rpmcli.h index c24f518..d972d9a 100644 --- a/lib/rpmcli.h +++ b/lib/rpmcli.h @@ -9,6 +9,7 @@ #include "rpmurl.h" #include "rpmmacro.h" #include "argv.h" +#include "idtx.h" /** \ingroup rpmcli * Should version 3 packages be produced? @@ -606,98 +607,13 @@ int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, * @param argv array of package file names (NULL terminated) * @return 0 on success */ + int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, /*@null@*/ const char ** argv) /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/ /*@modifies ts, ia, rpmGlobalMacroContext, fileSystem, internalState @*/; -/** - * A rollback transaction id element. - */ -/*@-fielduse@*/ -typedef /*@abstract@*/ struct IDT_s { - unsigned int instance; /*!< installed package transaction id. */ -/*@owned@*/ /*@null@*/ - const char * key; /*! removed package file name. */ - Header h; /*!< removed package header. */ - union { - uint_32 u32; /*!< install/remove transaction id */ - } val; -} * IDT; -/*@=fielduse@*/ - -/** - * A rollback transaction id index. - */ -typedef /*@abstract@*/ struct IDTindex_s { - int delta; /*!< no. elements to realloc as a chunk. */ - int size; /*!< size of id index element. */ - int alloced; /*!< current number of elements allocated. */ - int nidt; /*!< current number of elements initialized. */ -/*@only@*/ /*@null@*/ - IDT idt; /*!< id index elements. */ -} * IDTX; - -/** - * Destroy id index. - * @param idtx id index - * @return NULL always - */ -/*@null@*/ -IDTX IDTXfree(/*@only@*/ /*@null@*/ IDTX idtx) - /*@modifies idtx @*/; - -/** - * Create id index. - * @return new id index - */ -/*@only@*/ -IDTX IDTXnew(void) - /*@*/; - -/** - * Insure that index has room for "need" elements. - * @param idtx id index - * @param need additional no. of elements needed - * @return id index (with room for "need" elements) - */ -/*@only@*/ /*@null@*/ -IDTX IDTXgrow(/*@only@*/ /*@null@*/ IDTX idtx, int need) - /*@modifies idtx @*/; - -/** - * Sort tag (instance,value) pairs. - * @param idtx id index - * @return id index - */ -/*@only@*/ /*@null@*/ -IDTX IDTXsort(/*@only@*/ /*@null@*/ IDTX idtx) - /*@modifies idtx @*/; - -/** - * Load tag (instance,value) pairs from rpm databse, and return sorted id index. - * @param ts transaction set - * @param tag rpm tag - * @return id index - */ -/*@only@*/ /*@null@*/ -IDTX IDTXload(rpmts ts, rpmTag tag) - /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/ - /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/; - -/** - * Load tag (instance,value) pairs from packages, and return sorted id index. - * @param ts transaction set - * @param globstr glob expression - * @param tag rpm tag - * @return id index - */ -/*@only@*/ /*@null@*/ -IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag) - /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/ - /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/; - /** \ingroup rpmcli * Rollback transactions, erasing new, reinstalling old, package(s). * @param ts transaction set diff --git a/lib/rpminstall.c b/lib/rpminstall.c index 88c3db9..a30feb3 100644 --- a/lib/rpminstall.c +++ b/lib/rpminstall.c @@ -906,189 +906,6 @@ int rpmInstallSource(rpmts ts, const char * arg, return rc; } -/*@unchecked@*/ -static int reverse = -1; - -/** - */ -static int IDTintcmp(const void * a, const void * b) - /*@*/ -{ - /*@-castexpose@*/ - return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) ); - /*@=castexpose@*/ -} - -IDTX IDTXfree(IDTX idtx) -{ - if (idtx) { - int i; - if (idtx->idt) - for (i = 0; i < idtx->nidt; i++) { - IDT idt = idtx->idt + i; - idt->h = headerFree(idt->h); - idt->key = _free(idt->key); - } - idtx->idt = _free(idtx->idt); - idtx = _free(idtx); - } - return NULL; -} - -IDTX IDTXnew(void) -{ - IDTX idtx = xcalloc(1, sizeof(*idtx)); - idtx->delta = 10; - idtx->size = sizeof(*((IDT)0)); - return idtx; -} - -IDTX IDTXgrow(IDTX idtx, int need) -{ - if (need < 0) return NULL; - if (idtx == NULL) - idtx = IDTXnew(); - if (need == 0) return idtx; - - if ((idtx->nidt + need) > idtx->alloced) { - while (need > 0) { - idtx->alloced += idtx->delta; - need -= idtx->delta; - } - idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) ); - } - return idtx; -} - -IDTX IDTXsort(IDTX idtx) -{ - if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0) - qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp); - return idtx; -} - -IDTX IDTXload(rpmts ts, rpmTag tag) -{ - IDTX idtx = NULL; - rpmdbMatchIterator mi; - HGE_t hge = (HGE_t) headerGetEntry; - Header h; - - /*@-branchstate@*/ - mi = rpmtsInitIterator(ts, tag, NULL, 0); -#ifdef NOTYET - (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey'); -#endif - while ((h = rpmdbNextIterator(mi)) != NULL) { - rpmTagType type = RPM_NULL_TYPE; - int_32 count = 0; - int_32 * tidp; - - tidp = NULL; - if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL) - continue; - - if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1)) - continue; - - idtx = IDTXgrow(idtx, 1); - if (idtx == NULL) - continue; - if (idtx->idt == NULL) - continue; - - { IDT idt; - /*@-nullderef@*/ - idt = idtx->idt + idtx->nidt; - /*@=nullderef@*/ - idt->h = headerLink(h); - idt->key = NULL; - idt->instance = rpmdbGetIteratorOffset(mi); - idt->val.u32 = *tidp; - } - idtx->nidt++; - } - mi = rpmdbFreeIterator(mi); - /*@=branchstate@*/ - - return IDTXsort(idtx); -} - -IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag) -{ - IDTX idtx = NULL; - HGE_t hge = (HGE_t) headerGetEntry; - Header h; - int_32 * tidp; - FD_t fd; - const char ** av = NULL; - int ac = 0; - rpmRC rpmrc; - int xx; - int i; - - av = NULL; ac = 0; - xx = rpmGlob(globstr, &ac, &av); - - if (xx == 0) - for (i = 0; i < ac; i++) { - rpmTagType type; - int_32 count; - int isSource; - - fd = Fopen(av[i], "r.ufdio"); - if (fd == NULL || Ferror(fd)) { - rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i], - Fstrerror(fd)); - if (fd != NULL) (void) Fclose(fd); - continue; - } - - rpmrc = rpmReadPackageFile(ts, fd, av[i], &h); - (void) Fclose(fd); - switch (rpmrc) { - default: - goto bottom; - /*@notreached@*/ /*@switchbreak@*/ break; - case RPMRC_NOTTRUSTED: - case RPMRC_NOKEY: - case RPMRC_OK: - isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE); - if (isSource) - goto bottom; - /*@switchbreak@*/ break; - } - - tidp = NULL; - /*@-branchstate@*/ - if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) { - - idtx = IDTXgrow(idtx, 1); - if (idtx == NULL || idtx->idt == NULL) - goto bottom; - - { IDT idt; - idt = idtx->idt + idtx->nidt; - idt->h = headerLink(h); - idt->key = av[i]; - av[i] = NULL; - idt->instance = 0; - idt->val.u32 = *tidp; - } - idtx->nidt++; - } - /*@=branchstate@*/ -bottom: - h = headerFree(h); - } - - for (i = 0; i < ac; i++) - av[i] = _free(av[i]); - av = _free(av); ac = 0; - - return IDTXsort(idtx); -} - /** @todo Transaction handling, more, needs work. */ int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv) { diff --git a/lib/transaction.c b/lib/transaction.c index 35d0ac0..41d43e4 100644 --- a/lib/transaction.c +++ b/lib/transaction.c @@ -32,12 +32,7 @@ #include "debug.h" -/* - * This is needed for the IDTX definitions. I think probably those need - * to be moved into a different source file (idtx.{c,h}), but that is up - * to Jeff Johnson. - */ -#include "rpmcli.h" +#include "idtx.h" /*@access Header @*/ /* XXX ts->notify arg1 is void ptr */ /*@access rpmps @*/ /* XXX need rpmProblemSetOK() */ diff --git a/python/rpmts-py.c b/python/rpmts-py.c index 16af0a6..5c73faf 100644 --- a/python/rpmts-py.c +++ b/python/rpmts-py.c @@ -4,7 +4,7 @@ #include "system.h" -#include +#include #include #include #include -- 2.7.4