rpmdb/rpmdb.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #define _USE_COPY_LOAD  /* XXX don't use DB_DBT_MALLOC (yet) */
00008 
00009 #include <sys/file.h>
00010 
00011 #ifndef DYING   /* XXX already in "system.h" */
00012 /*@-noparams@*/
00013 #include <fnmatch.h>
00014 /*@=noparams@*/
00015 #if defined(__LCLINT__)
00016 /*@-declundef -exportheader -redecl @*/ /* LCL: missing annotation */
00017 extern int fnmatch (const char *__pattern, const char *__name, int __flags)
00018         /*@*/;
00019 /*@=declundef =exportheader =redecl @*/
00020 #endif
00021 #endif
00022 
00023 #include <regex.h>
00024 #if defined(__LCLINT__)
00025 /*@-declundef -exportheader @*/ /* LCL: missing modifies (only is bogus) */
00026 extern void regfree (/*@only@*/ regex_t *preg)
00027         /*@modifies *preg @*/;
00028 /*@=declundef =exportheader @*/
00029 #endif
00030 
00031 #include <rpmio_internal.h>
00032 #include <rpmmacro.h>
00033 #include <rpmsq.h>
00034 
00035 #include "rpmdb.h"
00036 #include "fprint.h"
00037 #include "legacy.h"
00038 #include "header_internal.h"    /* XXX for HEADERFLAG_ALLOCATED */
00039 #include "debug.h"
00040 
00041 /*@access dbiIndexSet@*/
00042 /*@access dbiIndexItem@*/
00043 /*@access rpmts@*/              /* XXX compared with NULL */
00044 /*@access Header@*/             /* XXX compared with NULL */
00045 /*@access rpmdbMatchIterator@*/
00046 /*@access pgpDig@*/
00047 
00048 /*@unchecked@*/
00049 int _rpmdb_debug = 0;
00050 
00051 /*@unchecked@*/
00052 static int _rebuildinprogress = 0;
00053 /*@unchecked@*/
00054 static int _db_filter_dups = 0;
00055 
00056 #define _DBI_FLAGS      0
00057 #define _DBI_PERMS      0644
00058 #define _DBI_MAJOR      -1
00059 
00060 /*@unchecked@*/
00061 /*@globstate@*/ /*@null@*/ int * dbiTags = NULL;
00062 /*@unchecked@*/
00063 int dbiTagsMax = 0;
00064 
00065 /* We use this to comunicate back to the the rpm transaction
00066  *  what their install instance was on a rpmdbAdd().
00067  */ 
00068 /*@unchecked@*/
00069 unsigned int myinstall_instance = 0;
00070 
00071 /* Bit mask macros. */
00072 /*@-exporttype@*/
00073 typedef unsigned int __pbm_bits;
00074 /*@=exporttype@*/
00075 #define __PBM_NBITS             (8 * sizeof (__pbm_bits))
00076 #define __PBM_IX(d)             ((d) / __PBM_NBITS)
00077 #define __PBM_MASK(d)           ((__pbm_bits) 1 << (((unsigned)(d)) % __PBM_NBITS))
00078 /*@-exporttype@*/
00079 typedef struct {
00080     __pbm_bits bits[1];
00081 } pbm_set;
00082 /*@=exporttype@*/
00083 #define __PBM_BITS(set) ((set)->bits)
00084 
00085 #define PBM_FREE(s)     _free(s);
00086 #define PBM_SET(d, s)   (__PBM_BITS (s)[__PBM_IX (d)] |= __PBM_MASK (d))
00087 #define PBM_CLR(d, s)   (__PBM_BITS (s)[__PBM_IX (d)] &= ~__PBM_MASK (d))
00088 #define PBM_ISSET(d, s) ((__PBM_BITS (s)[__PBM_IX (d)] & __PBM_MASK (d)) != 0)
00089 
00090 #define PBM_ALLOC(d)    xcalloc(__PBM_IX (d) + 1, sizeof(__pbm_bits))
00091 
00098 /*@unused@*/
00099 static inline pbm_set * PBM_REALLOC(pbm_set ** sp, int * odp, int nd)
00100         /*@modifies *sp, *odp @*/
00101 {
00102     int i, nb;
00103 
00104 /*@-bounds -sizeoftype@*/
00105     if (nd > (*odp)) {
00106         nd *= 2;
00107         nb = __PBM_IX(nd) + 1;
00108 /*@-unqualifiedtrans@*/
00109         *sp = xrealloc(*sp, nb * sizeof(__pbm_bits));
00110 /*@=unqualifiedtrans@*/
00111         for (i = __PBM_IX(*odp) + 1; i < nb; i++)
00112             __PBM_BITS(*sp)[i] = 0;
00113         *odp = nd;
00114     }
00115 /*@=bounds =sizeoftype@*/
00116 /*@-compdef -retalias -usereleased@*/
00117     return *sp;
00118 /*@=compdef =retalias =usereleased@*/
00119 }
00120 
00126 static inline unsigned char nibble(char c)
00127         /*@*/
00128 {
00129     if (c >= '0' && c <= '9')
00130         return (c - '0');
00131     if (c >= 'A' && c <= 'F')
00132         return (c - 'A') + 10;
00133     if (c >= 'a' && c <= 'f')
00134         return (c - 'a') + 10;
00135     return 0;
00136 }
00137 
00138 #ifdef  DYING
00139 
00145 static int printable(const void * ptr, size_t len)      /*@*/
00146 {
00147     const char * s = ptr;
00148     int i;
00149     for (i = 0; i < len; i++, s++)
00150         if (!(*s >= ' ' && *s <= '~')) return 0;
00151     return 1;
00152 }
00153 #endif
00154 
00160 static int dbiTagToDbix(int rpmtag)
00161         /*@*/
00162 {
00163     int dbix;
00164 
00165     if (dbiTags != NULL)
00166     for (dbix = 0; dbix < dbiTagsMax; dbix++) {
00167 /*@-boundsread@*/
00168         if (rpmtag == dbiTags[dbix])
00169             return dbix;
00170 /*@=boundsread@*/
00171     }
00172     return -1;
00173 }
00174 
00178 static void dbiTagsInit(void)
00179         /*@globals dbiTags, dbiTagsMax, rpmGlobalMacroContext, h_errno @*/
00180         /*@modifies dbiTags, dbiTagsMax, rpmGlobalMacroContext @*/
00181 {
00182 /*@observer@*/
00183     static const char * const _dbiTagStr_default =
00184         "Packages:Name:Basenames:Group:Requirename:Providename:Conflictname:Triggername:Dirnames:Requireversion:Provideversion:Installtid:Sigmd5:Sha1header:Filemd5s:Depends:Pubkeys";
00185     char * dbiTagStr = NULL;
00186     char * o, * oe;
00187     int rpmtag;
00188 
00189     dbiTagStr = rpmExpand("%{?_dbi_tags}", NULL);
00190     if (!(dbiTagStr && *dbiTagStr)) {
00191         dbiTagStr = _free(dbiTagStr);
00192         dbiTagStr = xstrdup(_dbiTagStr_default);
00193     }
00194 
00195     /* Discard previous values. */
00196     dbiTags = _free(dbiTags);
00197     dbiTagsMax = 0;
00198 
00199     /* Always allocate package index */
00200     dbiTags = xcalloc(1, sizeof(*dbiTags));
00201     dbiTags[dbiTagsMax++] = RPMDBI_PACKAGES;
00202 
00203     for (o = dbiTagStr; o && *o; o = oe) {
00204         while (*o && xisspace(*o))
00205             o++;
00206         if (*o == '\0')
00207             break;
00208         for (oe = o; oe && *oe; oe++) {
00209             if (xisspace(*oe))
00210                 /*@innerbreak@*/ break;
00211             if (oe[0] == ':' && !(oe[1] == '/' && oe[2] == '/'))
00212                 /*@innerbreak@*/ break;
00213         }
00214         if (oe && *oe)
00215             *oe++ = '\0';
00216         rpmtag = tagValue(o);
00217         if (rpmtag < 0) {
00218             rpmMessage(RPMMESS_WARNING,
00219                 _("dbiTagsInit: unrecognized tag name: \"%s\" ignored\n"), o);
00220             continue;
00221         }
00222         if (dbiTagToDbix(rpmtag) >= 0)
00223             continue;
00224 
00225         dbiTags = xrealloc(dbiTags, (dbiTagsMax + 1) * sizeof(*dbiTags)); /* XXX memory leak */
00226         dbiTags[dbiTagsMax++] = rpmtag;
00227     }
00228 
00229     dbiTagStr = _free(dbiTagStr);
00230 }
00231 
00232 /*@-redecl@*/
00233 #define DB1vec          NULL
00234 #define DB2vec          NULL
00235 
00236 #ifdef HAVE_DB3_DB_H
00237 /*@-exportheadervar -declundef @*/
00238 /*@observer@*/ /*@unchecked@*/
00239 extern struct _dbiVec db3vec;
00240 /*@=exportheadervar =declundef @*/
00241 #define DB3vec          &db3vec
00242 /*@=redecl@*/
00243 #else
00244 #define DB3vec          NULL
00245 #endif
00246 
00247 #ifdef HAVE_SQLITE3_H
00248 /*@-exportheadervar -declundef @*/
00249 /*@observer@*/ /*@unchecked@*/
00250 extern struct _dbiVec sqlitevec;
00251 /*@=exportheadervar =declundef @*/
00252 #define SQLITEvec       &sqlitevec
00253 /*@=redecl@*/
00254 #else
00255 #define SQLITEvec       NULL
00256 #endif
00257 
00258 /*@-nullassign@*/
00259 /*@observer@*/ /*@unchecked@*/
00260 static struct _dbiVec *mydbvecs[] = {
00261     DB1vec, DB1vec, DB2vec, DB3vec, SQLITEvec, NULL
00262 };
00263 /*@=nullassign@*/
00264 
00265 dbiIndex dbiOpen(rpmdb db, rpmTag rpmtag, /*@unused@*/ unsigned int flags)
00266 {
00267     int dbix;
00268     dbiIndex dbi = NULL;
00269     int _dbapi, _dbapi_rebuild, _dbapi_wanted;
00270     int rc = 0;
00271 
00272     if (db == NULL)
00273         return NULL;
00274 
00275     dbix = dbiTagToDbix(rpmtag);
00276     if (dbix < 0 || dbix >= dbiTagsMax)
00277         return NULL;
00278 
00279     /* Is this index already open ? */
00280 /*@-compdef@*/ /* FIX: db->_dbi may be NULL */
00281     if ((dbi = db->_dbi[dbix]) != NULL)
00282         return dbi;
00283 /*@=compdef@*/
00284 
00285     _dbapi_rebuild = rpmExpandNumeric("%{_dbapi_rebuild}");
00286     if (_dbapi_rebuild < 1 || _dbapi_rebuild > 4)
00287         _dbapi_rebuild = 4;
00288 /*    _dbapi_wanted = (_rebuildinprogress ? -1 : db->db_api); */
00289     _dbapi_wanted = (_rebuildinprogress ? _dbapi_rebuild : db->db_api);
00290 
00291     switch (_dbapi_wanted) {
00292     default:
00293         _dbapi = _dbapi_wanted;
00294         if (_dbapi < 0 || _dbapi >= 5 || mydbvecs[_dbapi] == NULL) {
00295             rpmMessage(RPMMESS_DEBUG, "dbiOpen: _dbiapi failed\n");
00296             return NULL;
00297         }
00298         errno = 0;
00299         dbi = NULL;
00300         rc = (*mydbvecs[_dbapi]->open) (db, rpmtag, &dbi);
00301         if (rc) {
00302             static int _printed[32];
00303             if (!_printed[dbix & 0x1f]++)
00304                 rpmError(RPMERR_DBOPEN,
00305                         _("cannot open %s index using db%d - %s (%d)\n"),
00306                         tagName(rpmtag), _dbapi,
00307                         (rc > 0 ? strerror(rc) : ""), rc);
00308             _dbapi = -1;
00309         }
00310         break;
00311     case -1:
00312         _dbapi = 5;
00313         while (_dbapi-- > 1) {
00314             if (mydbvecs[_dbapi] == NULL)
00315                 continue;
00316             errno = 0;
00317             dbi = NULL;
00318             rc = (*mydbvecs[_dbapi]->open) (db, rpmtag, &dbi);
00319             if (rc == 0 && dbi)
00320                 /*@loopbreak@*/ break;
00321         }
00322         if (_dbapi <= 0) {
00323             static int _printed[32];
00324             if (!_printed[dbix & 0x1f]++)
00325                 rpmError(RPMERR_DBOPEN, _("cannot open %s index\n"),
00326                         tagName(rpmtag));
00327             rc = 1;
00328             goto exit;
00329         }
00330         if (db->db_api == -1 && _dbapi > 0)
00331             db->db_api = _dbapi;
00332         break;
00333     }
00334 
00335 /* We don't ever _REQUIRE_ conversion... */
00336 #define SQLITE_HACK
00337 #ifdef  SQLITE_HACK_XXX
00338     /* Require conversion. */
00339     if (rc && _dbapi_wanted >= 0 && _dbapi != _dbapi_wanted && _dbapi_wanted == _dbapi_rebuild) {
00340         rc = (_rebuildinprogress ? 0 : 1);
00341         goto exit;
00342     }
00343 
00344     /* Suggest possible configuration */
00345     if (_dbapi_wanted >= 0 && _dbapi != _dbapi_wanted) {
00346         rc = 1;
00347         goto exit;
00348     }
00349 
00350     /* Suggest possible configuration */
00351     if (_dbapi_wanted < 0 && _dbapi != _dbapi_rebuild) {
00352         rc = (_rebuildinprogress ? 0 : 1);
00353         goto exit;
00354     }
00355 #endif
00356 
00357 exit:
00358     if (dbi != NULL && rc == 0) {
00359         db->_dbi[dbix] = dbi;
00360 /*@-sizeoftype@*/
00361         if (rpmtag == RPMDBI_PACKAGES && db->db_bits == NULL) {
00362             db->db_nbits = 1024;
00363             if (!dbiStat(dbi, DB_FAST_STAT)) {
00364                 DB_HASH_STAT * hash = (DB_HASH_STAT *)dbi->dbi_stats;
00365                 if (hash)
00366                     db->db_nbits += hash->hash_nkeys;
00367             }
00368             db->db_bits = PBM_ALLOC(db->db_nbits);
00369         }
00370 /*@=sizeoftype@*/
00371     }
00372 #ifdef HAVE_DB3_DB_H
00373       else
00374         dbi = db3Free(dbi);
00375 #endif
00376 
00377 /*@-compdef -nullstate@*/ /* FIX: db->_dbi may be NULL */
00378     return dbi;
00379 /*@=compdef =nullstate@*/
00380 }
00381 
00388 static dbiIndexItem dbiIndexNewItem(unsigned int hdrNum, unsigned int tagNum)
00389         /*@*/
00390 {
00391     dbiIndexItem rec = xcalloc(1, sizeof(*rec));
00392     rec->hdrNum = hdrNum;
00393     rec->tagNum = tagNum;
00394     return rec;
00395 }
00396 
00397 union _dbswap {
00398     unsigned int ui;
00399     unsigned char uc[4];
00400 };
00401 
00402 #define _DBSWAP(_a) \
00403 /*@-bounds@*/ \
00404   { unsigned char _b, *_c = (_a).uc; \
00405     _b = _c[3]; _c[3] = _c[0]; _c[0] = _b; \
00406     _b = _c[2]; _c[2] = _c[1]; _c[1] = _b; \
00407 /*@=bounds@*/ \
00408   }
00409 
00417 static int dbt2set(dbiIndex dbi, DBT * data, /*@out@*/ dbiIndexSet * setp)
00418         /*@modifies dbi, *setp @*/
00419 {
00420     int _dbbyteswapped = dbiByteSwapped(dbi);
00421     const char * sdbir;
00422     dbiIndexSet set;
00423     int i;
00424 
00425     if (dbi == NULL || data == NULL || setp == NULL)
00426         return -1;
00427 
00428     if ((sdbir = data->data) == NULL) {
00429         *setp = NULL;
00430         return 0;
00431     }
00432 
00433     set = xmalloc(sizeof(*set));
00434     set->count = data->size / dbi->dbi_jlen;
00435     set->recs = xmalloc(set->count * sizeof(*(set->recs)));
00436 
00437 /*@-bounds -sizeoftype @*/
00438     switch (dbi->dbi_jlen) {
00439     default:
00440     case 2*sizeof(int_32):
00441         for (i = 0; i < set->count; i++) {
00442             union _dbswap hdrNum, tagNum;
00443 
00444             memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
00445             sdbir += sizeof(hdrNum.ui);
00446             memcpy(&tagNum.ui, sdbir, sizeof(tagNum.ui));
00447             sdbir += sizeof(tagNum.ui);
00448             if (_dbbyteswapped) {
00449                 _DBSWAP(hdrNum);
00450                 _DBSWAP(tagNum);
00451             }
00452             set->recs[i].hdrNum = hdrNum.ui;
00453             set->recs[i].tagNum = tagNum.ui;
00454             set->recs[i].fpNum = 0;
00455         }
00456         break;
00457     case 1*sizeof(int_32):
00458         for (i = 0; i < set->count; i++) {
00459             union _dbswap hdrNum;
00460 
00461             memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
00462             sdbir += sizeof(hdrNum.ui);
00463             if (_dbbyteswapped) {
00464                 _DBSWAP(hdrNum);
00465             }
00466             set->recs[i].hdrNum = hdrNum.ui;
00467             set->recs[i].tagNum = 0;
00468             set->recs[i].fpNum = 0;
00469         }
00470         break;
00471     }
00472     *setp = set;
00473 /*@=bounds =sizeoftype @*/
00474 /*@-compdef@*/
00475     return 0;
00476 /*@=compdef@*/
00477 }
00478 
00486 static int set2dbt(dbiIndex dbi, DBT * data, dbiIndexSet set)
00487         /*@modifies dbi, *data @*/
00488 {
00489     int _dbbyteswapped = dbiByteSwapped(dbi);
00490     char * tdbir;
00491     int i;
00492 
00493     if (dbi == NULL || data == NULL || set == NULL)
00494         return -1;
00495 
00496     data->size = set->count * (dbi->dbi_jlen);
00497     if (data->size == 0) {
00498         data->data = NULL;
00499         return 0;
00500     }
00501     tdbir = data->data = xmalloc(data->size);
00502 
00503 /*@-bounds -sizeoftype@*/
00504     switch (dbi->dbi_jlen) {
00505     default:
00506     case 2*sizeof(int_32):
00507         for (i = 0; i < set->count; i++) {
00508             union _dbswap hdrNum, tagNum;
00509 
00510             memset(&hdrNum, 0, sizeof(hdrNum));
00511             memset(&tagNum, 0, sizeof(tagNum));
00512             hdrNum.ui = set->recs[i].hdrNum;
00513             tagNum.ui = set->recs[i].tagNum;
00514             if (_dbbyteswapped) {
00515                 _DBSWAP(hdrNum);
00516                 _DBSWAP(tagNum);
00517             }
00518             memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
00519             tdbir += sizeof(hdrNum.ui);
00520             memcpy(tdbir, &tagNum.ui, sizeof(tagNum.ui));
00521             tdbir += sizeof(tagNum.ui);
00522         }
00523         break;
00524     case 1*sizeof(int_32):
00525         for (i = 0; i < set->count; i++) {
00526             union _dbswap hdrNum;
00527 
00528             memset(&hdrNum, 0, sizeof(hdrNum));
00529             hdrNum.ui = set->recs[i].hdrNum;
00530             if (_dbbyteswapped) {
00531                 _DBSWAP(hdrNum);
00532             }
00533             memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
00534             tdbir += sizeof(hdrNum.ui);
00535         }
00536         break;
00537     }
00538 /*@=bounds =sizeoftype@*/
00539 
00540 /*@-compdef@*/
00541     return 0;
00542 /*@=compdef@*/
00543 }
00544 
00545 /* XXX assumes hdrNum is first int in dbiIndexItem */
00546 static int hdrNumCmp(const void * one, const void * two)
00547         /*@*/
00548 {
00549     const int * a = one, * b = two;
00550     return (*a - *b);
00551 }
00552 
00562 static int dbiAppendSet(dbiIndexSet set, const void * recs,
00563         int nrecs, size_t recsize, int sortset)
00564         /*@modifies *set @*/
00565 {
00566     const char * rptr = recs;
00567     size_t rlen = (recsize < sizeof(*(set->recs)))
00568                 ? recsize : sizeof(*(set->recs));
00569 
00570     if (set == NULL || recs == NULL || nrecs <= 0 || recsize == 0)
00571         return 1;
00572 
00573     set->recs = xrealloc(set->recs,
00574                         (set->count + nrecs) * sizeof(*(set->recs)));
00575 
00576     memset(set->recs + set->count, 0, nrecs * sizeof(*(set->recs)));
00577 
00578     while (nrecs-- > 0) {
00579         /*@-mayaliasunique@*/
00580         memcpy(set->recs + set->count, rptr, rlen);
00581         /*@=mayaliasunique@*/
00582         rptr += recsize;
00583         set->count++;
00584     }
00585 
00586     if (sortset && set->count > 1)
00587         qsort(set->recs, set->count, sizeof(*(set->recs)), hdrNumCmp);
00588 
00589     return 0;
00590 }
00591 
00601 static int dbiPruneSet(dbiIndexSet set, void * recs, int nrecs,
00602                 size_t recsize, int sorted)
00603         /*@modifies set, recs @*/
00604 {
00605     int from;
00606     int to = 0;
00607     int num = set->count;
00608     int numCopied = 0;
00609 
00610 assert(set->count > 0);
00611     if (nrecs > 1 && !sorted)
00612         qsort(recs, nrecs, recsize, hdrNumCmp);
00613 
00614     for (from = 0; from < num; from++) {
00615         if (bsearch(&set->recs[from], recs, nrecs, recsize, hdrNumCmp)) {
00616             set->count--;
00617             continue;
00618         }
00619         if (from != to)
00620             set->recs[to] = set->recs[from]; /* structure assignment */
00621         to++;
00622         numCopied++;
00623     }
00624     return (numCopied == num);
00625 }
00626 
00627 /* XXX transaction.c */
00628 unsigned int dbiIndexSetCount(dbiIndexSet set) {
00629     return set->count;
00630 }
00631 
00632 /* XXX transaction.c */
00633 unsigned int dbiIndexRecordOffset(dbiIndexSet set, int recno) {
00634     return set->recs[recno].hdrNum;
00635 }
00636 
00637 /* XXX transaction.c */
00638 unsigned int dbiIndexRecordFileNumber(dbiIndexSet set, int recno) {
00639     return set->recs[recno].tagNum;
00640 }
00641 
00642 /* XXX transaction.c */
00643 dbiIndexSet dbiFreeIndexSet(dbiIndexSet set) {
00644     if (set) {
00645         set->recs = _free(set->recs);
00646         set = _free(set);
00647     }
00648     return set;
00649 }
00650 
00651 typedef struct miRE_s {
00652     rpmTag              tag;            
00653     rpmMireMode         mode;           
00654 /*@only@*/
00655     const char *        pattern;        
00656     int                 notmatch;       
00657 /*@only@*/
00658     regex_t *           preg;           
00659     int                 cflags;         
00660     int                 eflags;         
00661     int                 fnflags;        
00662 } * miRE;
00663 
00664 struct _rpmdbMatchIterator {
00665 /*@dependent@*/ /*@null@*/
00666     rpmdbMatchIterator  mi_next;
00667 /*@only@*/
00668     const void *        mi_keyp;
00669     size_t              mi_keylen;
00670 /*@refcounted@*/
00671     rpmdb               mi_db;
00672     rpmTag              mi_rpmtag;
00673     dbiIndexSet         mi_set;
00674     DBC *               mi_dbc;
00675     DBT                 mi_key;
00676     DBT                 mi_data;
00677     int                 mi_setx;
00678 /*@refcounted@*/ /*@null@*/
00679     Header              mi_h;
00680     int                 mi_sorted;
00681     int                 mi_cflags;
00682     int                 mi_modified;
00683     unsigned int        mi_prevoffset;  /* header instance (native endian) */
00684     unsigned int        mi_offset;      /* header instance (native endian) */
00685     unsigned int        mi_filenum;     /* tag element (native endian) */
00686     int                 mi_nre;
00687 /*@only@*/ /*@null@*/
00688     miRE                mi_re;
00689 /*@null@*/
00690     rpmts               mi_ts;
00691 /*@null@*/
00692     rpmRC (*mi_hdrchk) (rpmts ts, const void * uh, size_t uc, const char ** msg)
00693         /*@modifies ts, *msg @*/;
00694 
00695 };
00696 
00697 /*@unchecked@*/
00698 static rpmdb rpmdbRock;
00699 
00700 /*@unchecked@*/ /*@exposed@*/ /*@null@*/
00701 static rpmdbMatchIterator rpmmiRock;
00702 
00703 int rpmdbCheckTerminate(int terminate)
00704         /*@globals rpmdbRock, rpmmiRock @*/
00705         /*@modifies rpmdbRock, rpmmiRock @*/
00706 {
00707     sigset_t newMask, oldMask;
00708     static int terminating = 0;
00709 
00710     if (terminating) return 1;
00711 
00712     (void) sigfillset(&newMask);                /* block all signals */
00713     (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
00714 
00715     if (sigismember(&rpmsqCaught, SIGINT)
00716      || sigismember(&rpmsqCaught, SIGQUIT)
00717      || sigismember(&rpmsqCaught, SIGHUP)
00718      || sigismember(&rpmsqCaught, SIGTERM)
00719      || sigismember(&rpmsqCaught, SIGPIPE)
00720      || terminate)
00721         terminating = 1;
00722 
00723     if (terminating) {
00724         rpmdb db;
00725         rpmdbMatchIterator mi;
00726 
00727 /*@-branchstate@*/
00728         while ((mi = rpmmiRock) != NULL) {
00729 /*@i@*/     rpmmiRock = mi->mi_next;
00730             mi->mi_next = NULL;
00731 /*@i@*/     mi = rpmdbFreeIterator(mi);
00732         }
00733 /*@=branchstate@*/
00734 
00735 /*@-newreftrans@*/
00736         while ((db = rpmdbRock) != NULL) {
00737 /*@i@*/     rpmdbRock = db->db_next;
00738             db->db_next = NULL;
00739             (void) rpmdbClose(db);
00740         }
00741 /*@=newreftrans@*/
00742     }
00743     sigprocmask(SIG_SETMASK, &oldMask, NULL);
00744     return terminating;
00745 }
00746 
00747 int rpmdbCheckSignals(void)
00748 {
00749     if (rpmdbCheckTerminate(0)) {
00750 /*@-abstract@*/ /* sigset_t is abstract type */
00751         rpmMessage(RPMMESS_DEBUG, "Exiting on signal(0x%lx) ...\n", *((unsigned long *)&rpmsqCaught));
00752         exit(EXIT_FAILURE);
00753 /*@=abstract@*/
00754     }
00755     return 0;
00756 }
00757 
00761 static int blockSignals(/*@unused@*/ rpmdb db, /*@out@*/ sigset_t * oldMask)
00762         /*@globals fileSystem @*/
00763         /*@modifies *oldMask, fileSystem @*/
00764 {
00765     sigset_t newMask;
00766 
00767     (void) sigfillset(&newMask);                /* block all signals */
00768     (void) sigprocmask(SIG_BLOCK, &newMask, oldMask);
00769     (void) sigdelset(&newMask, SIGINT);
00770     (void) sigdelset(&newMask, SIGQUIT);
00771     (void) sigdelset(&newMask, SIGHUP);
00772     (void) sigdelset(&newMask, SIGTERM);
00773     (void) sigdelset(&newMask, SIGPIPE);
00774     return sigprocmask(SIG_BLOCK, &newMask, NULL);
00775 }
00776 
00780 /*@mayexit@*/
00781 static int unblockSignals(/*@unused@*/ rpmdb db, sigset_t * oldMask)
00782         /*@globals rpmdbRock, fileSystem, internalState @*/
00783         /*@modifies rpmdbRock, fileSystem, internalState @*/
00784 {
00785     (void) rpmdbCheckSignals();
00786     return sigprocmask(SIG_SETMASK, oldMask, NULL);
00787 }
00788 
00789 #define _DB_ROOT        "/"
00790 #define _DB_HOME        "%{_dbpath}"
00791 #define _DB_FLAGS       0
00792 #define _DB_MODE        0
00793 #define _DB_PERMS       0644
00794 
00795 #define _DB_MAJOR       -1
00796 #define _DB_ERRPFX      "rpmdb"
00797 
00798 /*@-fullinitblock@*/
00799 /*@observer@*/ /*@unchecked@*/
00800 static struct rpmdb_s dbTemplate = {
00801     _DB_ROOT,   _DB_HOME, _DB_FLAGS, _DB_MODE, _DB_PERMS,
00802     _DB_MAJOR,  _DB_ERRPFX
00803 };
00804 /*@=fullinitblock@*/
00805 
00806 static int isTemporaryDB(int rpmtag) 
00807 {
00808     int rc = 0;
00809     switch (rpmtag) {
00810     case RPMDBI_AVAILABLE:
00811     case RPMDBI_ADDED:
00812     case RPMDBI_REMOVED:
00813     case RPMDBI_DEPENDS:
00814         rc = 1;
00815         break;
00816     default:
00817         break;
00818     }
00819     return rc;
00820 }
00821 
00822 int rpmdbOpenAll(rpmdb db)
00823 {
00824     int dbix;
00825     int rc = 0;
00826 
00827     if (db == NULL) return -2;
00828 
00829     if (dbiTags != NULL)
00830     for (dbix = 0; dbix < dbiTagsMax; dbix++) {
00831         if (db->_dbi[dbix] != NULL)
00832             continue;
00833         /* Filter out temporary databases */
00834         if (isTemporaryDB(dbiTags[dbix])) 
00835             continue;
00836         (void) dbiOpen(db, dbiTags[dbix], db->db_flags);
00837     }
00838     return rc;
00839 }
00840 
00841 int rpmdbCloseDBI(rpmdb db, int rpmtag)
00842 {
00843     int dbix;
00844     int rc = 0;
00845 
00846     if (db == NULL || db->_dbi == NULL || dbiTags == NULL)
00847         return 0;
00848 
00849     for (dbix = 0; dbix < dbiTagsMax; dbix++) {
00850         if (dbiTags[dbix] != rpmtag)
00851             continue;
00852 /*@-boundswrite@*/
00853         if (db->_dbi[dbix] != NULL) {
00854             int xx;
00855             /*@-unqualifiedtrans@*/             /* FIX: double indirection. */
00856             xx = dbiClose(db->_dbi[dbix], 0);
00857             if (xx && rc == 0) rc = xx;
00858             db->_dbi[dbix] = NULL;
00859             /*@=unqualifiedtrans@*/
00860         }
00861 /*@=boundswrite@*/
00862         break;
00863     }
00864     return rc;
00865 }
00866 
00867 /* XXX query.c, rpminstall.c, verify.c */
00868 /*@-incondefs@*/
00869 int rpmdbClose(rpmdb db)
00870         /*@globals rpmdbRock @*/
00871         /*@modifies rpmdbRock @*/
00872 {
00873     rpmdb * prev, next;
00874     int dbix;
00875     int rc = 0;
00876 
00877     if (db == NULL)
00878         goto exit;
00879 
00880     (void) rpmdbUnlink(db, "rpmdbClose");
00881 
00882     /*@-usereleased@*/
00883     if (db->nrefs > 0)
00884         goto exit;
00885 
00886     if (db->_dbi)
00887     for (dbix = db->db_ndbi; --dbix >= 0; ) {
00888         int xx;
00889         if (db->_dbi[dbix] == NULL)
00890             continue;
00891         /*@-unqualifiedtrans@*/         /* FIX: double indirection. */
00892         xx = dbiClose(db->_dbi[dbix], 0);
00893         if (xx && rc == 0) rc = xx;
00894         db->_dbi[dbix] = NULL;
00895         /*@=unqualifiedtrans@*/
00896     }
00897     db->db_errpfx = _free(db->db_errpfx);
00898     db->db_root = _free(db->db_root);
00899     db->db_home = _free(db->db_home);
00900     db->db_bits = PBM_FREE(db->db_bits);
00901     db->_dbi = _free(db->_dbi);
00902 
00903 /*@-newreftrans@*/
00904     prev = &rpmdbRock;
00905     while ((next = *prev) != NULL && next != db)
00906         prev = &next->db_next;
00907     if (next) {
00908 /*@i@*/ *prev = next->db_next;
00909         next->db_next = NULL;
00910     }
00911 /*@=newreftrans@*/
00912 
00913     /*@-refcounttrans@*/ db = _free(db); /*@=refcounttrans@*/
00914     /*@=usereleased@*/
00915 
00916 exit:
00917     (void) rpmsqEnable(-SIGHUP, NULL);
00918     (void) rpmsqEnable(-SIGINT, NULL);
00919     (void) rpmsqEnable(-SIGTERM,NULL);
00920     (void) rpmsqEnable(-SIGQUIT,NULL);
00921     (void) rpmsqEnable(-SIGPIPE,NULL);
00922     return rc;
00923 }
00924 /*@=incondefs@*/
00925 
00926 int rpmdbSync(rpmdb db)
00927 {
00928     int dbix;
00929     int rc = 0;
00930 
00931     if (db == NULL) return 0;
00932     for (dbix = 0; dbix < db->db_ndbi; dbix++) {
00933         int xx;
00934         if (db->_dbi[dbix] == NULL)
00935             continue;
00936         if (db->_dbi[dbix]->dbi_no_dbsync)
00937             continue;
00938         xx = dbiSync(db->_dbi[dbix], 0);
00939         if (xx && rc == 0) rc = xx;
00940     }
00941     return rc;
00942 }
00943 
00944 /*@-mods@*/     /* FIX: dbTemplate structure assignment */
00945 static /*@only@*/ /*@null@*/
00946 rpmdb newRpmdb(/*@kept@*/ /*@null@*/ const char * root,
00947                 /*@kept@*/ /*@null@*/ const char * home,
00948                 int mode, int perms, int flags)
00949         /*@globals _db_filter_dups, rpmGlobalMacroContext, h_errno @*/
00950         /*@modifies _db_filter_dups, rpmGlobalMacroContext @*/
00951 {
00952     rpmdb db = xcalloc(sizeof(*db), 1);
00953     const char * epfx = _DB_ERRPFX;
00954     static int _initialized = 0;
00955 
00956     if (!_initialized) {
00957         _db_filter_dups = rpmExpandNumeric("%{_filterdbdups}");
00958         _initialized = 1;
00959     }
00960 
00961 /*@-boundswrite@*/
00962     /*@-assignexpose@*/
00963     *db = dbTemplate;   /* structure assignment */
00964     /*@=assignexpose@*/
00965 /*@=boundswrite@*/
00966 
00967     db->_dbi = NULL;
00968 
00969     if (!(perms & 0600)) perms = 0644;  /* XXX sanity */
00970 
00971     if (mode >= 0)      db->db_mode = mode;
00972     if (perms >= 0)     db->db_perms = perms;
00973     if (flags >= 0)     db->db_flags = flags;
00974 
00975 /*@-nullpass@*/
00976     /* HACK: no URL's for root prefixed dbpath yet. */
00977     if (root && *root) {
00978         const char * rootpath = NULL;
00979         urltype ut = urlPath(root, &rootpath);
00980         switch (ut) {
00981         case URL_IS_PATH:
00982         case URL_IS_UNKNOWN:
00983             db->db_root = rpmGetPath(root, NULL);
00984             break;
00985         case URL_IS_HTTPS:
00986         case URL_IS_HTTP:
00987         case URL_IS_FTP:
00988         case URL_IS_HKP:
00989         case URL_IS_DASH:
00990         default:
00991             db->db_root = rpmGetPath(_DB_ROOT, NULL);
00992             break;
00993         }
00994     } else
00995         db->db_root = rpmGetPath(_DB_ROOT, NULL);
00996     db->db_home = rpmGetPath( (home && *home ? home : _DB_HOME), NULL);
00997 /*@=nullpass@*/
00998     if (!(db->db_home && db->db_home[0] != '%')) {
00999         rpmError(RPMERR_DBOPEN, _("no dbpath has been set\n"));
01000         db->db_root = _free(db->db_root);
01001         db->db_home = _free(db->db_home);
01002         db = _free(db);
01003         /*@-globstate@*/ return NULL; /*@=globstate@*/
01004     }
01005     db->db_errpfx = rpmExpand( (epfx && *epfx ? epfx : _DB_ERRPFX), NULL);
01006     db->db_remove_env = 0;
01007     db->db_filter_dups = _db_filter_dups;
01008     db->db_ndbi = dbiTagsMax;
01009     db->_dbi = xcalloc(db->db_ndbi, sizeof(*db->_dbi));
01010     db->nrefs = 0;
01011     /*@-globstate@*/
01012     return rpmdbLink(db, "rpmdbCreate");
01013     /*@=globstate@*/
01014 }
01015 /*@=mods@*/
01016 
01017 static int openDatabase(/*@null@*/ const char * prefix,
01018                 /*@null@*/ const char * dbpath,
01019                 int _dbapi, /*@null@*/ /*@out@*/ rpmdb *dbp,
01020                 int mode, int perms, int flags)
01021         /*@globals rpmdbRock, rpmGlobalMacroContext, h_errno,
01022                 fileSystem, internalState @*/
01023         /*@modifies rpmdbRock, *dbp, rpmGlobalMacroContext,
01024                 fileSystem, internalState @*/
01025         /*@requires maxSet(dbp) >= 0 @*/
01026 {
01027     rpmdb db;
01028     int rc, xx;
01029     static int _tags_initialized = 0;
01030     int justCheck = flags & RPMDB_FLAG_JUSTCHECK;
01031     int minimal = flags & RPMDB_FLAG_MINIMAL;
01032 
01033     if (!_tags_initialized || dbiTagsMax == 0) {
01034         dbiTagsInit();
01035         _tags_initialized++;
01036     }
01037 
01038     /* Insure that _dbapi has one of -1, 1, 2, or 3 */
01039     if (_dbapi < -1 || _dbapi > 4)
01040         _dbapi = -1;
01041     if (_dbapi == 0)
01042         _dbapi = 1;
01043 
01044     if (dbp)
01045         *dbp = NULL;
01046     if (mode & O_WRONLY) 
01047         return 1;
01048 
01049     db = newRpmdb(prefix, dbpath, mode, perms, flags);
01050     if (db == NULL)
01051         return 1;
01052 
01053     (void) rpmsqEnable(SIGHUP,  NULL);
01054     (void) rpmsqEnable(SIGINT,  NULL);
01055     (void) rpmsqEnable(SIGTERM,NULL);
01056     (void) rpmsqEnable(SIGQUIT,NULL);
01057     (void) rpmsqEnable(SIGPIPE,NULL);
01058 
01059     db->db_api = _dbapi;
01060 
01061     {   int dbix;
01062 
01063         rc = 0;
01064         if (dbiTags != NULL)
01065         for (dbix = 0; rc == 0 && dbix < dbiTagsMax; dbix++) {
01066             dbiIndex dbi;
01067             int rpmtag;
01068 
01069             /* Filter out temporary databases */
01070             if (isTemporaryDB((rpmtag = dbiTags[dbix])))
01071                 continue;
01072 
01073             dbi = dbiOpen(db, rpmtag, 0);
01074             if (dbi == NULL) {
01075                 rc = -2;
01076                 break;
01077             }
01078 
01079             switch (rpmtag) {
01080             case RPMDBI_PACKAGES:
01081                 if (dbi == NULL) rc |= 1;
01082 #if 0
01083                 /* XXX open only Packages, indices created on the fly. */
01084                 if (db->db_api == 3)
01085 #endif
01086                     goto exit;
01087                 /*@notreached@*/ /*@switchbreak@*/ break;
01088             case RPMTAG_NAME:
01089                 if (dbi == NULL) rc |= 1;
01090                 if (minimal)
01091                     goto exit;
01092                 /*@switchbreak@*/ break;
01093             default:
01094                 /*@switchbreak@*/ break;
01095             }
01096         }
01097     }
01098 
01099 exit:
01100     if (rc || justCheck || dbp == NULL)
01101         xx = rpmdbClose(db);
01102     else {
01103 /*@-assignexpose -newreftrans@*/
01104 /*@i@*/ db->db_next = rpmdbRock;
01105         rpmdbRock = db;
01106 /*@i@*/ *dbp = db;
01107 /*@=assignexpose =newreftrans@*/
01108     }
01109 
01110     return rc;
01111 }
01112 
01113 rpmdb XrpmdbUnlink(rpmdb db, const char * msg, const char * fn, unsigned ln)
01114 {
01115 /*@-modfilesys@*/
01116 if (_rpmdb_debug)
01117 fprintf(stderr, "--> db %p -- %d %s at %s:%u\n", db, db->nrefs, msg, fn, ln);
01118 /*@=modfilesys@*/
01119     db->nrefs--;
01120     return NULL;
01121 }
01122 
01123 rpmdb XrpmdbLink(rpmdb db, const char * msg, const char * fn, unsigned ln)
01124 {
01125     db->nrefs++;
01126 /*@-modfilesys@*/
01127 if (_rpmdb_debug)
01128 fprintf(stderr, "--> db %p ++ %d %s at %s:%u\n", db, db->nrefs, msg, fn, ln);
01129 /*@=modfilesys@*/
01130     /*@-refcounttrans@*/ return db; /*@=refcounttrans@*/
01131 }
01132 
01133 /* XXX python/rpmmodule.c */
01134 int rpmdbOpen (const char * prefix, rpmdb *dbp, int mode, int perms)
01135 {
01136     int _dbapi = rpmExpandNumeric("%{_dbapi}");
01137 /*@-boundswrite@*/
01138     return openDatabase(prefix, NULL, _dbapi, dbp, mode, perms, 0);
01139 /*@=boundswrite@*/
01140 }
01141 
01142 int rpmdbInit (const char * prefix, int perms)
01143 {
01144     rpmdb db = NULL;
01145     int _dbapi = rpmExpandNumeric("%{_dbapi}");
01146     int rc;
01147 
01148 /*@-boundswrite@*/
01149     rc = openDatabase(prefix, NULL, _dbapi, &db, (O_CREAT | O_RDWR),
01150                 perms, RPMDB_FLAG_JUSTCHECK);
01151 /*@=boundswrite@*/
01152     if (db != NULL) {
01153         int xx;
01154         xx = rpmdbOpenAll(db);
01155         if (xx && rc == 0) rc = xx;
01156         xx = rpmdbClose(db);
01157         if (xx && rc == 0) rc = xx;
01158         db = NULL;
01159     }
01160     return rc;
01161 }
01162 
01163 int rpmdbVerify(const char * prefix)
01164 {
01165     rpmdb db = NULL;
01166     int _dbapi = rpmExpandNumeric("%{_dbapi}");
01167     int rc = 0;
01168 
01169 /*@-boundswrite@*/
01170     rc = openDatabase(prefix, NULL, _dbapi, &db, O_RDONLY, 0644, 0);
01171 /*@=boundswrite@*/
01172 
01173     if (db != NULL) {
01174         int dbix;
01175         int xx;
01176         rc = rpmdbOpenAll(db);
01177 
01178         for (dbix = db->db_ndbi; --dbix >= 0; ) {
01179             if (db->_dbi[dbix] == NULL)
01180                 continue;
01181             /*@-unqualifiedtrans@*/             /* FIX: double indirection. */
01182             xx = dbiVerify(db->_dbi[dbix], 0);
01183             if (xx && rc == 0) rc = xx;
01184             db->_dbi[dbix] = NULL;
01185             /*@=unqualifiedtrans@*/
01186         }
01187 
01188         /*@-nullstate@*/        /* FIX: db->_dbi[] may be NULL. */
01189         xx = rpmdbClose(db);
01190         /*@=nullstate@*/
01191         if (xx && rc == 0) rc = xx;
01192         db = NULL;
01193     }
01194     return rc;
01195 }
01196 
01206 static int rpmdbFindByFile(rpmdb db, /*@null@*/ const char * filespec,
01207                 DBT * key, DBT * data, /*@out@*/ dbiIndexSet * matches)
01208         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01209         /*@modifies db, *key, *data, *matches, rpmGlobalMacroContext,
01210                 fileSystem, internalState @*/
01211         /*@requires maxSet(matches) >= 0 @*/
01212 {
01213     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
01214     HFD_t hfd = headerFreeData;
01215     const char * dirName;
01216     const char * baseName;
01217     rpmTagType bnt, dnt;
01218     fingerPrintCache fpc;
01219     fingerPrint fp1;
01220     dbiIndex dbi = NULL;
01221     DBC * dbcursor;
01222     dbiIndexSet allMatches = NULL;
01223     dbiIndexItem rec = NULL;
01224     int i;
01225     int rc;
01226     int xx;
01227 
01228     *matches = NULL;
01229     if (filespec == NULL) return -2;
01230 
01231     /*@-branchstate@*/
01232     if ((baseName = strrchr(filespec, '/')) != NULL) {
01233         char * t;
01234         size_t len;
01235 
01236         len = baseName - filespec + 1;
01237 /*@-boundswrite@*/
01238         t = strncpy(alloca(len + 1), filespec, len);
01239         t[len] = '\0';
01240 /*@=boundswrite@*/
01241         dirName = t;
01242         baseName++;
01243     } else {
01244         dirName = "";
01245         baseName = filespec;
01246     }
01247     /*@=branchstate@*/
01248     if (baseName == NULL)
01249         return -2;
01250 
01251     fpc = fpCacheCreate(20);
01252     fp1 = fpLookup(fpc, dirName, baseName, 1);
01253 
01254     dbi = dbiOpen(db, RPMTAG_BASENAMES, 0);
01255 /*@-branchstate@*/
01256     if (dbi != NULL) {
01257         dbcursor = NULL;
01258         xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
01259 
01260 /*@-temptrans@*/
01261 key->data = (void *) baseName;
01262 /*@=temptrans@*/
01263 key->size = strlen(baseName);
01264 if (key->size == 0) key->size++;        /* XXX "/" fixup. */
01265 
01266         rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
01267         if (rc > 0) {
01268             rpmError(RPMERR_DBGETINDEX,
01269                 _("error(%d) getting \"%s\" records from %s index\n"),
01270                 rc, key->data, tagName(dbi->dbi_rpmtag));
01271         }
01272 
01273 if (rc == 0)
01274 (void) dbt2set(dbi, data, &allMatches);
01275 
01276         xx = dbiCclose(dbi, dbcursor, 0);
01277         dbcursor = NULL;
01278     } else
01279         rc = -2;
01280 /*@=branchstate@*/
01281 
01282     if (rc) {
01283         allMatches = dbiFreeIndexSet(allMatches);
01284         fpc = fpCacheFree(fpc);
01285         return rc;
01286     }
01287 
01288     *matches = xcalloc(1, sizeof(**matches));
01289     rec = dbiIndexNewItem(0, 0);
01290     i = 0;
01291     if (allMatches != NULL)
01292     while (i < allMatches->count) {
01293         const char ** baseNames, ** dirNames;
01294         int_32 * dirIndexes;
01295         unsigned int offset = dbiIndexRecordOffset(allMatches, i);
01296         unsigned int prevoff;
01297         Header h;
01298 
01299         {   rpmdbMatchIterator mi;
01300             mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &offset, sizeof(offset));
01301             h = rpmdbNextIterator(mi);
01302             if (h)
01303                 h = headerLink(h);
01304             mi = rpmdbFreeIterator(mi);
01305         }
01306 
01307         if (h == NULL) {
01308             i++;
01309             continue;
01310         }
01311 
01312         xx = hge(h, RPMTAG_BASENAMES, &bnt, (void **) &baseNames, NULL);
01313         xx = hge(h, RPMTAG_DIRNAMES, &dnt, (void **) &dirNames, NULL);
01314         xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
01315 
01316         do {
01317             fingerPrint fp2;
01318             int num = dbiIndexRecordFileNumber(allMatches, i);
01319 
01320             fp2 = fpLookup(fpc, dirNames[dirIndexes[num]], baseNames[num], 1);
01321             /*@-nullpass@*/
01322             if (FP_EQUAL(fp1, fp2)) {
01323             /*@=nullpass@*/
01324                 rec->hdrNum = dbiIndexRecordOffset(allMatches, i);
01325                 rec->tagNum = dbiIndexRecordFileNumber(allMatches, i);
01326                 xx = dbiAppendSet(*matches, rec, 1, sizeof(*rec), 0);
01327             }
01328 
01329             prevoff = offset;
01330             i++;
01331             if (i < allMatches->count)
01332                 offset = dbiIndexRecordOffset(allMatches, i);
01333         } while (i < allMatches->count && offset == prevoff);
01334 
01335         baseNames = hfd(baseNames, bnt);
01336         dirNames = hfd(dirNames, dnt);
01337         h = headerFree(h);
01338     }
01339 
01340     rec = _free(rec);
01341     allMatches = dbiFreeIndexSet(allMatches);
01342 
01343     fpc = fpCacheFree(fpc);
01344 
01345     if ((*matches)->count == 0) {
01346         *matches = dbiFreeIndexSet(*matches);
01347         return 1;
01348     }
01349 
01350     return 0;
01351 }
01352 
01353 /* XXX python/upgrade.c, install.c, uninstall.c */
01354 int rpmdbCountPackages(rpmdb db, const char * name)
01355 {
01356 DBC * dbcursor = NULL;
01357 DBT * key = alloca(sizeof(*key));
01358 DBT * data = alloca(sizeof(*data));
01359     dbiIndex dbi;
01360     int rc;
01361     int xx;
01362 
01363     if (db == NULL)
01364         return 0;
01365 
01366 memset(key, 0, sizeof(*key));
01367 memset(data, 0, sizeof(*data));
01368 
01369     dbi = dbiOpen(db, RPMTAG_NAME, 0);
01370     if (dbi == NULL)
01371         return 0;
01372 
01373 /*@-temptrans@*/
01374 key->data = (void *) name;
01375 /*@=temptrans@*/
01376 key->size = strlen(name);
01377 
01378     xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
01379     rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
01380 #ifndef SQLITE_HACK
01381     xx = dbiCclose(dbi, dbcursor, 0);
01382     dbcursor = NULL;
01383 #endif
01384 
01385     if (rc == 0) {              /* success */
01386         dbiIndexSet matches;
01387         /*@-nullpass@*/ /* FIX: matches might be NULL */
01388         matches = NULL;
01389         (void) dbt2set(dbi, data, &matches);
01390         if (matches) {
01391             rc = dbiIndexSetCount(matches);
01392             matches = dbiFreeIndexSet(matches);
01393         }
01394         /*@=nullpass@*/
01395     } else
01396     if (rc == DB_NOTFOUND) {    /* not found */
01397         rc = 0;
01398     } else {                    /* error */
01399         rpmError(RPMERR_DBGETINDEX,
01400                 _("error(%d) getting \"%s\" records from %s index\n"),
01401                 rc, key->data, tagName(dbi->dbi_rpmtag));
01402         rc = -1;
01403     }
01404 
01405 #ifdef  SQLITE_HACK
01406     xx = dbiCclose(dbi, dbcursor, 0);
01407     dbcursor = NULL;
01408 #endif
01409 
01410     return rc;
01411 }
01412 
01425 static rpmRC dbiFindMatches(dbiIndex dbi, DBC * dbcursor,
01426                 DBT * key, DBT * data,
01427                 const char * name,
01428                 /*@null@*/ const char * version,
01429                 /*@null@*/ const char * release,
01430                 /*@out@*/ dbiIndexSet * matches)
01431         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01432         /*@modifies dbi, *dbcursor, *key, *data, *matches,
01433                 rpmGlobalMacroContext, fileSystem, internalState @*/
01434         /*@requires maxSet(matches) >= 0 @*/
01435 {
01436     int gotMatches = 0;
01437     int rc;
01438     int i;
01439 
01440 /*@-temptrans@*/
01441 key->data = (void *) name;
01442 /*@=temptrans@*/
01443 key->size = strlen(name);
01444 
01445     rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
01446 
01447     if (rc == 0) {              /* success */
01448         (void) dbt2set(dbi, data, matches);
01449         if (version == NULL && release == NULL)
01450             return RPMRC_OK;
01451     } else
01452     if (rc == DB_NOTFOUND) {    /* not found */
01453         return RPMRC_NOTFOUND;
01454     } else {                    /* error */
01455         rpmError(RPMERR_DBGETINDEX,
01456                 _("error(%d) getting \"%s\" records from %s index\n"),
01457                 rc, key->data, tagName(dbi->dbi_rpmtag));
01458         return RPMRC_FAIL;
01459     }
01460 
01461     /* Make sure the version and release match. */
01462     /*@-branchstate@*/
01463     for (i = 0; i < dbiIndexSetCount(*matches); i++) {
01464         unsigned int recoff = dbiIndexRecordOffset(*matches, i);
01465         rpmdbMatchIterator mi;
01466         Header h;
01467 
01468         if (recoff == 0)
01469             continue;
01470 
01471         mi = rpmdbInitIterator(dbi->dbi_rpmdb,
01472                         RPMDBI_PACKAGES, &recoff, sizeof(recoff));
01473 
01474         /* Set iterator selectors for version/release if available. */
01475         if (version &&
01476             rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, version))
01477         {
01478             rc = RPMRC_FAIL;
01479             goto exit;
01480         }
01481         if (release &&
01482             rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, release))
01483         {
01484             rc = RPMRC_FAIL;
01485             goto exit;
01486         }
01487 
01488         h = rpmdbNextIterator(mi);
01489 /*@-boundswrite@*/
01490         if (h)
01491             (*matches)->recs[gotMatches++] = (*matches)->recs[i];
01492         else
01493             (*matches)->recs[i].hdrNum = 0;
01494 /*@=boundswrite@*/
01495         mi = rpmdbFreeIterator(mi);
01496     }
01497     /*@=branchstate@*/
01498 
01499     if (gotMatches) {
01500         (*matches)->count = gotMatches;
01501         rc = RPMRC_OK;
01502     } else
01503         rc = RPMRC_NOTFOUND;
01504 
01505 exit:
01506 /*@-unqualifiedtrans@*/ /* FIX: double indirection */
01507     if (rc && matches && *matches)
01508         *matches = dbiFreeIndexSet(*matches);
01509 /*@=unqualifiedtrans@*/
01510     return rc;
01511 }
01512 
01525 static rpmRC dbiFindByLabel(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
01526                 /*@null@*/ const char * arg, /*@out@*/ dbiIndexSet * matches)
01527         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01528         /*@modifies dbi, *dbcursor, *key, *data, *matches,
01529                 rpmGlobalMacroContext, fileSystem, internalState @*/
01530         /*@requires maxSet(matches) >= 0 @*/
01531 {
01532     const char * release;
01533     char * localarg;
01534     char * s;
01535     char c;
01536     int brackets;
01537     rpmRC rc;
01538  
01539     if (arg == NULL || strlen(arg) == 0) return RPMRC_NOTFOUND;
01540 
01541     /* did they give us just a name? */
01542     rc = dbiFindMatches(dbi, dbcursor, key, data, arg, NULL, NULL, matches);
01543     if (rc != RPMRC_NOTFOUND) return rc;
01544 
01545     /*@-unqualifiedtrans@*/ /* FIX: double indirection */
01546     *matches = dbiFreeIndexSet(*matches);
01547     /*@=unqualifiedtrans@*/
01548 
01549     /* maybe a name and a release */
01550     localarg = alloca(strlen(arg) + 1);
01551     s = stpcpy(localarg, arg);
01552 
01553     c = '\0';
01554     brackets = 0;
01555     for (s -= 1; s > localarg; s--) {
01556         switch (*s) {
01557         case '[':
01558             brackets = 1;
01559             /*@switchbreak@*/ break;
01560         case ']':
01561             if (c != '[') brackets = 0;
01562             /*@switchbreak@*/ break;
01563         }
01564         c = *s;
01565         if (!brackets && *s == '-')
01566             break;
01567     }
01568 
01569     /*@-nullstate@*/    /* FIX: *matches may be NULL. */
01570     if (s == localarg) return RPMRC_NOTFOUND;
01571 
01572 /*@-boundswrite@*/
01573     *s = '\0';
01574 /*@=boundswrite@*/
01575     rc = dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, NULL, matches);
01576     /*@=nullstate@*/
01577     if (rc != RPMRC_NOTFOUND) return rc;
01578 
01579     /*@-unqualifiedtrans@*/ /* FIX: double indirection */
01580     *matches = dbiFreeIndexSet(*matches);
01581     /*@=unqualifiedtrans@*/
01582     
01583     /* how about name-version-release? */
01584 
01585     release = s + 1;
01586 
01587     c = '\0';
01588     brackets = 0;
01589     for (; s > localarg; s--) {
01590         switch (*s) {
01591         case '[':
01592             brackets = 1;
01593             /*@switchbreak@*/ break;
01594         case ']':
01595             if (c != '[') brackets = 0;
01596             /*@switchbreak@*/ break;
01597         }
01598         c = *s;
01599         if (!brackets && *s == '-')
01600             break;
01601     }
01602 
01603     if (s == localarg) return RPMRC_NOTFOUND;
01604 
01605 /*@-boundswrite@*/
01606     *s = '\0';
01607 /*@=boundswrite@*/
01608     /*@-nullstate@*/    /* FIX: *matches may be NULL. */
01609     return dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, release, matches);
01610     /*@=nullstate@*/
01611 }
01612 
01621 static int miFreeHeader(rpmdbMatchIterator mi, dbiIndex dbi)
01622         /*@globals fileSystem, internalState @*/
01623         /*@modifies mi, dbi, fileSystem, internalState @*/
01624 {
01625     int rc = 0;
01626 
01627     if (mi == NULL || mi->mi_h == NULL)
01628         return 0;
01629 
01630     if (dbi && mi->mi_dbc && mi->mi_modified && mi->mi_prevoffset) {
01631         DBT * key = &mi->mi_key;
01632         DBT * data = &mi->mi_data;
01633         sigset_t signalMask;
01634         rpmRC rpmrc = RPMRC_NOTFOUND;
01635         int xx;
01636 
01637 /*@i@*/ key->data = (void *) &mi->mi_prevoffset;
01638         key->size = sizeof(mi->mi_prevoffset);
01639         data->data = headerUnload(mi->mi_h);
01640         data->size = headerSizeof(mi->mi_h, HEADER_MAGIC_NO);
01641 
01642         /* Check header digest/signature on blob export (if requested). */
01643         if (mi->mi_hdrchk && mi->mi_ts) {
01644             const char * msg = NULL;
01645             int lvl;
01646 
01647             rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, data->data, data->size, &msg);
01648             lvl = (rpmrc == RPMRC_FAIL ? RPMMESS_ERROR : RPMMESS_DEBUG);
01649             rpmMessage(lvl, "%s h#%8u %s",
01650                 (rpmrc == RPMRC_FAIL ? _("miFreeHeader: skipping") : "write"),
01651                         mi->mi_prevoffset, (msg ? msg : "\n"));
01652             msg = _free(msg);
01653         }
01654 
01655         if (data->data != NULL && rpmrc != RPMRC_FAIL) {
01656             (void) blockSignals(dbi->dbi_rpmdb, &signalMask);
01657             rc = dbiPut(dbi, mi->mi_dbc, key, data, DB_KEYLAST);
01658             if (rc) {
01659                 rpmError(RPMERR_DBPUTINDEX,
01660                         _("error(%d) storing record #%d into %s\n"),
01661                         rc, mi->mi_prevoffset, tagName(dbi->dbi_rpmtag));
01662             }
01663             xx = dbiSync(dbi, 0);
01664             (void) unblockSignals(dbi->dbi_rpmdb, &signalMask);
01665         }
01666         data->data = _free(data->data);
01667         data->size = 0;
01668     }
01669 
01670     mi->mi_h = headerFree(mi->mi_h);
01671 
01672 /*@-nullstate@*/
01673     return rc;
01674 /*@=nullstate@*/
01675 }
01676 
01677 rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
01678         /*@globals rpmmiRock @*/
01679         /*@modifies rpmmiRock @*/
01680 {
01681     rpmdbMatchIterator * prev, next;
01682     dbiIndex dbi;
01683     int xx;
01684     int i;
01685 
01686     if (mi == NULL)
01687         return NULL;
01688 
01689     prev = &rpmmiRock;
01690     while ((next = *prev) != NULL && next != mi)
01691         prev = &next->mi_next;
01692     if (next) {
01693 /*@i@*/ *prev = next->mi_next;
01694         next->mi_next = NULL;
01695     }
01696 
01697     dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
01698     if (dbi == NULL)    /* XXX can't happen */
01699         return NULL;
01700 
01701     xx = miFreeHeader(mi, dbi);
01702 
01703     if (mi->mi_dbc)
01704         xx = dbiCclose(dbi, mi->mi_dbc, 0);
01705     mi->mi_dbc = NULL;
01706 
01707     if (mi->mi_re != NULL)
01708     for (i = 0; i < mi->mi_nre; i++) {
01709         miRE mire = mi->mi_re + i;
01710         mire->pattern = _free(mire->pattern);
01711         if (mire->preg != NULL) {
01712             regfree(mire->preg);
01713             /*@+voidabstract -usereleased @*/ /* LCL: regfree has bogus only */
01714             mire->preg = _free(mire->preg);
01715             /*@=voidabstract =usereleased @*/
01716         }
01717     }
01718     mi->mi_re = _free(mi->mi_re);
01719 
01720     mi->mi_set = dbiFreeIndexSet(mi->mi_set);
01721     mi->mi_keyp = _free(mi->mi_keyp);
01722     mi->mi_db = rpmdbUnlink(mi->mi_db, "matchIterator");
01723 
01724     mi = _free(mi);
01725 
01726     (void) rpmdbCheckSignals();
01727 
01728     return mi;
01729 }
01730 
01731 unsigned int rpmdbGetIteratorOffset(rpmdbMatchIterator mi) {
01732     return (mi ? mi->mi_offset : 0);
01733 }
01734 
01735 unsigned int rpmdbGetIteratorFileNum(rpmdbMatchIterator mi) {
01736     return (mi ? mi->mi_filenum : 0);
01737 }
01738 
01739 int rpmdbGetIteratorCount(rpmdbMatchIterator mi) {
01740     return (mi && mi->mi_set ?  mi->mi_set->count : 0);
01741 }
01742 
01749 static int miregexec(miRE mire, const char * val)
01750         /*@*/
01751 {
01752     int rc = 0;
01753 
01754     switch (mire->mode) {
01755     case RPMMIRE_STRCMP:
01756         rc = strcmp(mire->pattern, val);
01757         if (rc) rc = 1;
01758         break;
01759     case RPMMIRE_DEFAULT:
01760     case RPMMIRE_REGEX:
01761 /*@-boundswrite@*/
01762         rc = regexec(mire->preg, val, 0, NULL, mire->eflags);
01763 /*@=boundswrite@*/
01764         if (rc && rc != REG_NOMATCH) {
01765             char msg[256];
01766             (void) regerror(rc, mire->preg, msg, sizeof(msg)-1);
01767             msg[sizeof(msg)-1] = '\0';
01768             rpmError(RPMERR_REGEXEC, "%s: regexec failed: %s\n",
01769                         mire->pattern, msg);
01770             rc = -1;
01771         }
01772         break;
01773     case RPMMIRE_GLOB:
01774         rc = fnmatch(mire->pattern, val, mire->fnflags);
01775         if (rc && rc != FNM_NOMATCH)
01776             rc = -1;
01777         break;
01778     default:
01779         rc = -1;
01780         break;
01781     }
01782 
01783     return rc;
01784 }
01785 
01792 static int mireCmp(const void * a, const void * b)
01793 {
01794     const miRE mireA = (const miRE) a;
01795     const miRE mireB = (const miRE) b;
01796     return (mireA->tag - mireB->tag);
01797 }
01798 
01806 static /*@only@*/ char * mireDup(rpmTag tag, rpmMireMode *modep,
01807                         const char * pattern)
01808         /*@modifies *modep @*/
01809         /*@requires maxSet(modep) >= 0 @*/
01810 {
01811     const char * s;
01812     char * pat;
01813     char * t;
01814     int brackets;
01815     size_t nb;
01816     int c;
01817 
01818 /*@-boundswrite@*/
01819     switch (*modep) {
01820     default:
01821     case RPMMIRE_DEFAULT:
01822         if (tag == RPMTAG_DIRNAMES || tag == RPMTAG_BASENAMES) {
01823             *modep = RPMMIRE_GLOB;
01824             pat = xstrdup(pattern);
01825             break;
01826         }
01827 
01828         nb = strlen(pattern) + sizeof("^$");
01829 
01830         /* Find no. of bytes needed for pattern. */
01831         /* periods and plusses are escaped, splats become '.*' */
01832         c = '\0';
01833         brackets = 0;
01834         for (s = pattern; *s != '\0'; s++) {
01835             switch (*s) {
01836             case '.':
01837             case '+':
01838             case '*':
01839                 if (!brackets) nb++;
01840                 /*@switchbreak@*/ break;
01841             case '\\':
01842                 s++;
01843                 /*@switchbreak@*/ break;
01844             case '[':
01845                 brackets = 1;
01846                 /*@switchbreak@*/ break;
01847             case ']':
01848                 if (c != '[') brackets = 0;
01849                 /*@switchbreak@*/ break;
01850             }
01851             c = *s;
01852         }
01853 
01854         pat = t = xmalloc(nb);
01855 
01856         if (pattern[0] != '^') *t++ = '^';
01857 
01858         /* Copy pattern, escaping periods, prefixing splats with period. */
01859         c = '\0';
01860         brackets = 0;
01861         for (s = pattern; *s != '\0'; s++, t++) {
01862             switch (*s) {
01863             case '.':
01864             case '+':
01865                 if (!brackets) *t++ = '\\';
01866                 /*@switchbreak@*/ break;
01867             case '*':
01868                 if (!brackets) *t++ = '.';
01869                 /*@switchbreak@*/ break;
01870             case '\\':
01871                 *t++ = *s++;
01872                 /*@switchbreak@*/ break;
01873             case '[':
01874                 brackets = 1;
01875                 /*@switchbreak@*/ break;
01876             case ']':
01877                 if (c != '[') brackets = 0;
01878                 /*@switchbreak@*/ break;
01879             }
01880             c = *t = *s;
01881         }
01882 
01883         if (s > pattern && s[-1] != '$') *t++ = '$';
01884         *t = '\0';
01885         *modep = RPMMIRE_REGEX;
01886         break;
01887     case RPMMIRE_STRCMP:
01888     case RPMMIRE_REGEX:
01889     case RPMMIRE_GLOB:
01890         pat = xstrdup(pattern);
01891         break;
01892     }
01893 /*@-boundswrite@*/
01894 
01895     return pat;
01896 }
01897 
01898 int rpmdbSetIteratorRE(rpmdbMatchIterator mi, rpmTag tag,
01899                 rpmMireMode mode, const char * pattern)
01900 {
01901     static rpmMireMode defmode = (rpmMireMode)-1;
01902     miRE mire = NULL;
01903     const char * allpat = NULL;
01904     int notmatch = 0;
01905     regex_t * preg = NULL;
01906     int cflags = 0;
01907     int eflags = 0;
01908     int fnflags = 0;
01909     int rc = 0;
01910 
01911 /*@-boundsread@*/
01912     if (defmode == (rpmMireMode)-1) {
01913         const char *t = rpmExpand("%{?_query_selector_match}", NULL);
01914 
01915         if (*t == '\0' || !strcmp(t, "default"))
01916             defmode = RPMMIRE_DEFAULT;
01917         else if (!strcmp(t, "strcmp"))
01918             defmode = RPMMIRE_STRCMP;
01919         else if (!strcmp(t, "regex"))
01920             defmode = RPMMIRE_REGEX;
01921         else if (!strcmp(t, "glob"))
01922             defmode = RPMMIRE_GLOB;
01923         else
01924             defmode = RPMMIRE_DEFAULT;
01925         t = _free(t);
01926      }
01927 
01928     if (mi == NULL || pattern == NULL)
01929         return rc;
01930 
01931     /* Leading '!' inverts pattern match sense, like "grep -v". */
01932     if (*pattern == '!') {
01933         notmatch = 1;
01934         pattern++;
01935     }
01936 /*@=boundsread@*/
01937 
01938 /*@-boundswrite@*/
01939     allpat = mireDup(tag, &mode, pattern);
01940 /*@=boundswrite@*/
01941 
01942     if (mode == RPMMIRE_DEFAULT)
01943         mode = defmode;
01944 
01945     /*@-branchstate@*/
01946     switch (mode) {
01947     case RPMMIRE_DEFAULT:
01948     case RPMMIRE_STRCMP:
01949         break;
01950     case RPMMIRE_REGEX:
01951         /*@-type@*/
01952         preg = xcalloc(1, sizeof(*preg));
01953         /*@=type@*/
01954         cflags = (REG_EXTENDED | REG_NOSUB);
01955         rc = regcomp(preg, allpat, cflags);
01956         if (rc) {
01957             char msg[256];
01958             (void) regerror(rc, preg, msg, sizeof(msg)-1);
01959             msg[sizeof(msg)-1] = '\0';
01960             rpmError(RPMERR_REGCOMP, "%s: regcomp failed: %s\n", allpat, msg);
01961         }
01962         break;
01963     case RPMMIRE_GLOB:
01964         fnflags = FNM_PATHNAME | FNM_PERIOD;
01965         break;
01966     default:
01967         rc = -1;
01968         break;
01969     }
01970     /*@=branchstate@*/
01971 
01972     if (rc) {
01973         /*@=kepttrans@*/        /* FIX: mire has kept values */
01974         allpat = _free(allpat);
01975         if (preg) {
01976             regfree(preg);
01977             /*@+voidabstract -usereleased @*/ /* LCL: regfree has bogus only */
01978             preg = _free(preg);
01979             /*@=voidabstract =usereleased @*/
01980         }
01981         /*@=kepttrans@*/
01982         return rc;
01983     }
01984 
01985     mi->mi_re = xrealloc(mi->mi_re, (mi->mi_nre + 1) * sizeof(*mi->mi_re));
01986     mire = mi->mi_re + mi->mi_nre;
01987     mi->mi_nre++;
01988     
01989     mire->tag = tag;
01990     mire->mode = mode;
01991     mire->pattern = allpat;
01992     mire->notmatch = notmatch;
01993     mire->preg = preg;
01994     mire->cflags = cflags;
01995     mire->eflags = eflags;
01996     mire->fnflags = fnflags;
01997 
01998 /*@-boundsread@*/
01999     if (mi->mi_nre > 1)
02000         qsort(mi->mi_re, mi->mi_nre, sizeof(*mi->mi_re), mireCmp);
02001 /*@=boundsread@*/
02002 
02003     return rc;
02004 }
02005 
02011 static int mireSkip (const rpmdbMatchIterator mi)
02012         /*@*/
02013 {
02014     HGE_t hge = (HGE_t) headerGetEntryMinMemory;
02015     HFD_t hfd = (HFD_t) headerFreeData;
02016     union {
02017         void * ptr;
02018         const char ** argv;
02019         const char * str;
02020         int_32 * i32p;
02021         int_16 * i16p;
02022         int_8 * i8p;
02023     } u;
02024     char numbuf[32];
02025     rpmTagType t;
02026     int_32 c;
02027     miRE mire;
02028     static int_32 zero = 0;
02029     int ntags = 0;
02030     int nmatches = 0;
02031     int i, j;
02032     int rc;
02033 
02034     if (mi->mi_h == NULL)       /* XXX can't happen */
02035         return 0;
02036 
02037     /*
02038      * Apply tag tests, implicitly "||" for multiple patterns/values of a
02039      * single tag, implicitly "&&" between multiple tag patterns.
02040      */
02041 /*@-boundsread@*/
02042     if ((mire = mi->mi_re) != NULL)
02043     for (i = 0; i < mi->mi_nre; i++, mire++) {
02044         int anymatch;
02045 
02046         if (!hge(mi->mi_h, mire->tag, &t, (void **)&u, &c)) {
02047             if (mire->tag != RPMTAG_EPOCH)
02048                 continue;
02049             t = RPM_INT32_TYPE;
02050 /*@-immediatetrans@*/
02051             u.i32p = &zero;
02052 /*@=immediatetrans@*/
02053             c = 1;
02054         }
02055 
02056         anymatch = 0;           /* no matches yet */
02057         while (1) {
02058             switch (t) {
02059             case RPM_CHAR_TYPE:
02060             case RPM_INT8_TYPE:
02061                 sprintf(numbuf, "%d", (int) *u.i8p);
02062                 rc = miregexec(mire, numbuf);
02063                 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
02064                     anymatch++;
02065                 /*@switchbreak@*/ break;
02066             case RPM_INT16_TYPE:
02067                 sprintf(numbuf, "%d", (int) *u.i16p);
02068                 rc = miregexec(mire, numbuf);
02069                 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
02070                     anymatch++;
02071                 /*@switchbreak@*/ break;
02072             case RPM_INT32_TYPE:
02073                 sprintf(numbuf, "%d", (int) *u.i32p);
02074                 rc = miregexec(mire, numbuf);
02075                 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
02076                     anymatch++;
02077                 /*@switchbreak@*/ break;
02078             case RPM_STRING_TYPE:
02079                 rc = miregexec(mire, u.str);
02080                 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
02081                     anymatch++;
02082                 /*@switchbreak@*/ break;
02083             case RPM_I18NSTRING_TYPE:
02084             case RPM_STRING_ARRAY_TYPE:
02085                 for (j = 0; j < c; j++) {
02086                     rc = miregexec(mire, u.argv[j]);
02087                     if ((!rc && !mire->notmatch) || (rc && mire->notmatch)) {
02088                         anymatch++;
02089                         /*@innerbreak@*/ break;
02090                     }
02091                 }
02092                 /*@switchbreak@*/ break;
02093             case RPM_NULL_TYPE:
02094             case RPM_BIN_TYPE:
02095             default:
02096                 /*@switchbreak@*/ break;
02097             }
02098             if ((i+1) < mi->mi_nre && mire[0].tag == mire[1].tag) {
02099                 i++;
02100                 mire++;
02101                 /*@innercontinue@*/ continue;
02102             }
02103             /*@innerbreak@*/ break;
02104         }
02105 /*@=boundsread@*/
02106 
02107         u.ptr = hfd(u.ptr, t);
02108 
02109         ntags++;
02110         if (anymatch)
02111             nmatches++;
02112     }
02113 
02114     return (ntags == nmatches ? 0 : 1);
02115 }
02116 
02117 int rpmdbSetIteratorRewrite(rpmdbMatchIterator mi, int rewrite)
02118 {
02119     int rc;
02120     if (mi == NULL)
02121         return 0;
02122     rc = (mi->mi_cflags & DB_WRITECURSOR) ? 1 : 0;
02123     if (rewrite)
02124         mi->mi_cflags |= DB_WRITECURSOR;
02125     else
02126         mi->mi_cflags &= ~DB_WRITECURSOR;
02127     return rc;
02128 }
02129 
02130 int rpmdbSetIteratorModified(rpmdbMatchIterator mi, int modified)
02131 {
02132     int rc;
02133     if (mi == NULL)
02134         return 0;
02135     rc = mi->mi_modified;
02136     mi->mi_modified = modified;
02137     return rc;
02138 }
02139 
02140 int rpmdbSetHdrChk(rpmdbMatchIterator mi, rpmts ts,
02141         rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
02142 {
02143     int rc = 0;
02144     if (mi == NULL)
02145         return 0;
02146 /*@-assignexpose -newreftrans @*/ /* XXX forward linkage prevents rpmtsLink */
02147 /*@i@*/ mi->mi_ts = ts;
02148     mi->mi_hdrchk = hdrchk;
02149 /*@=assignexpose =newreftrans @*/
02150     return rc;
02151 }
02152 
02153 
02154 /*@-nullstate@*/ /* FIX: mi->mi_key.data may be NULL */
02155 Header rpmdbNextIterator(rpmdbMatchIterator mi)
02156 {
02157     dbiIndex dbi;
02158     void * uh;
02159     size_t uhlen;
02160     DBT * key;
02161     DBT * data;
02162     void * keyp;
02163     size_t keylen;
02164     int rc;
02165     int xx;
02166 
02167     if (mi == NULL)
02168         return NULL;
02169 
02170     dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
02171     if (dbi == NULL)
02172         return NULL;
02173 
02174     /*
02175      * Cursors are per-iterator, not per-dbi, so get a cursor for the
02176      * iterator on 1st call. If the iteration is to rewrite headers, and the
02177      * CDB model is used for the database, then the cursor needs to
02178      * marked with DB_WRITECURSOR as well.
02179      */
02180     if (mi->mi_dbc == NULL)
02181         xx = dbiCopen(dbi, dbi->dbi_txnid, &mi->mi_dbc, mi->mi_cflags);
02182 
02183 /*@-boundswrite@*/
02184     key = &mi->mi_key;
02185     memset(key, 0, sizeof(*key));
02186     data = &mi->mi_data;
02187     memset(data, 0, sizeof(*data));
02188 /*@=boundswrite@*/
02189 
02190 top:
02191     uh = NULL;
02192     uhlen = 0;
02193 
02194     do {
02195 union _dbswap mi_offset;
02196 
02197         /*@-branchstate -compmempass @*/
02198         if (mi->mi_set) {
02199             if (!(mi->mi_setx < mi->mi_set->count))
02200                 return NULL;
02201             mi->mi_offset = dbiIndexRecordOffset(mi->mi_set, mi->mi_setx);
02202             mi->mi_filenum = dbiIndexRecordFileNumber(mi->mi_set, mi->mi_setx);
02203 mi_offset.ui = mi->mi_offset;
02204 if (dbiByteSwapped(dbi) == 1)
02205     _DBSWAP(mi_offset);
02206             keyp = &mi_offset;
02207             keylen = sizeof(mi_offset.ui);
02208         } else {
02209 
02210             key->data = keyp = (void *)mi->mi_keyp;
02211             key->size = keylen = mi->mi_keylen;
02212             data->data = uh;
02213             data->size = uhlen;
02214 #if !defined(_USE_COPY_LOAD)
02215             data->flags |= DB_DBT_MALLOC;
02216 #endif
02217             rc = dbiGet(dbi, mi->mi_dbc, key, data,
02218                         (key->data == NULL ? DB_NEXT : DB_SET));
02219             data->flags = 0;
02220             keyp = key->data;
02221             keylen = key->size;
02222             uh = data->data;
02223             uhlen = data->size;
02224 
02225             /*
02226              * If we got the next key, save the header instance number.
02227              *
02228              * For db3 Packages, instance 0 (i.e. mi->mi_setx == 0) is the
02229              * largest header instance in the database, and should be
02230              * skipped.
02231              */
02232 /*@-boundswrite@*/
02233             if (keyp && mi->mi_setx && rc == 0) {
02234                 memcpy(&mi_offset, keyp, sizeof(mi_offset.ui));
02235 if (dbiByteSwapped(dbi) == 1)
02236     _DBSWAP(mi_offset);
02237                 mi->mi_offset = mi_offset.ui;
02238             }
02239 /*@=boundswrite@*/
02240 
02241             /* Terminate on error or end of keys */
02242             if (rc || (mi->mi_setx && mi->mi_offset == 0))
02243                 return NULL;
02244         }
02245         /*@=branchstate =compmempass @*/
02246         mi->mi_setx++;
02247     } while (mi->mi_offset == 0);
02248 
02249     /* If next header is identical, return it now. */
02250 /*@-compdef -refcounttrans -retalias -retexpose -usereleased @*/
02251     if (mi->mi_prevoffset && mi->mi_offset == mi->mi_prevoffset)
02252         return mi->mi_h;
02253 /*@=compdef =refcounttrans =retalias =retexpose =usereleased @*/
02254 
02255     /* Retrieve next header blob for index iterator. */
02256     /*@-branchstate -compmempass -immediatetrans @*/
02257     if (uh == NULL) {
02258         key->data = keyp;
02259         key->size = keylen;
02260 #if !defined(_USE_COPY_LOAD)
02261         data->flags |= DB_DBT_MALLOC;
02262 #endif
02263         rc = dbiGet(dbi, mi->mi_dbc, key, data, DB_SET);
02264         data->flags = 0;
02265         keyp = key->data;
02266         keylen = key->size;
02267         uh = data->data;
02268         uhlen = data->size;
02269         if (rc)
02270             return NULL;
02271     }
02272     /*@=branchstate =compmempass =immediatetrans @*/
02273 
02274     /* Rewrite current header (if necessary) and unlink. */
02275     xx = miFreeHeader(mi, dbi);
02276 
02277     /* Is this the end of the iteration? */
02278     if (uh == NULL)
02279         return NULL;
02280 
02281     /* Check header digest/signature once (if requested). */
02282 /*@-boundsread -branchstate -sizeoftype @*/
02283     if (mi->mi_hdrchk && mi->mi_ts) {
02284         rpmRC rpmrc = RPMRC_NOTFOUND;
02285 
02286         /* Don't bother re-checking a previously read header. */
02287         if (mi->mi_db->db_bits) {
02288             pbm_set * set;
02289 
02290             set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
02291                         &mi->mi_db->db_nbits, mi->mi_offset);
02292             if (PBM_ISSET(mi->mi_offset, set))
02293                 rpmrc = RPMRC_OK;
02294         }
02295 
02296         /* If blob is unchecked, check blob import consistency now. */
02297         if (rpmrc != RPMRC_OK) {
02298             const char * msg = NULL;
02299             int lvl;
02300 
02301             rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, uh, uhlen, &msg);
02302             lvl = (rpmrc == RPMRC_FAIL ? RPMMESS_ERROR : RPMMESS_DEBUG);
02303             rpmMessage(lvl, "%s h#%8u %s",
02304                 (rpmrc == RPMRC_FAIL ? _("rpmdbNextIterator: skipping") : " read"),
02305                         mi->mi_offset, (msg ? msg : "\n"));
02306             msg = _free(msg);
02307 
02308             /* Mark header checked. */
02309             if (mi->mi_db && mi->mi_db->db_bits && rpmrc == RPMRC_OK) {
02310                 pbm_set * set;
02311 
02312                 set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
02313                         &mi->mi_db->db_nbits, mi->mi_offset);
02314                 PBM_SET(mi->mi_offset, set);
02315             }
02316 
02317             /* Skip damaged and inconsistent headers. */
02318             if (rpmrc == RPMRC_FAIL)
02319                 goto top;
02320         }
02321     }
02322 /*@=boundsread =branchstate =sizeoftype @*/
02323 
02324     /* Did the header blob load correctly? */
02325 #if !defined(_USE_COPY_LOAD)
02326 /*@-onlytrans@*/
02327     mi->mi_h = headerLoad(uh);
02328 /*@=onlytrans@*/
02329     if (mi->mi_h)
02330         mi->mi_h->flags |= HEADERFLAG_ALLOCATED;
02331 #else
02332     mi->mi_h = headerCopyLoad(uh);
02333 #endif
02334     if (mi->mi_h == NULL || !headerIsEntry(mi->mi_h, RPMTAG_NAME)) {
02335         rpmError(RPMERR_BADHEADER,
02336                 _("rpmdb: damaged header #%u retrieved -- skipping.\n"),
02337                 mi->mi_offset);
02338         goto top;
02339     }
02340 
02341     /*
02342      * Skip this header if iterator selector (if any) doesn't match.
02343      */
02344     if (mireSkip(mi)) {
02345         /* XXX hack, can't restart with Packages locked on single instance. */
02346         if (mi->mi_set || mi->mi_keyp == NULL)
02347             goto top;
02348         return NULL;
02349     }
02350 
02351     mi->mi_prevoffset = mi->mi_offset;
02352     mi->mi_modified = 0;
02353 
02354 /*@-compdef -retalias -retexpose -usereleased @*/
02355     return mi->mi_h;
02356 /*@=compdef =retalias =retexpose =usereleased @*/
02357 }
02358 /*@=nullstate@*/
02359 
02360 static void rpmdbSortIterator(/*@null@*/ rpmdbMatchIterator mi)
02361         /*@modifies mi @*/
02362 {
02363     if (mi && mi->mi_set && mi->mi_set->recs && mi->mi_set->count > 0) {
02364     /*
02365      * mergesort is much (~10x with lots of identical basenames) faster
02366      * than pure quicksort, but glibc uses msort_with_tmp() on stack.
02367      */
02368 #if defined(__GLIBC__)
02369 /*@-boundsread@*/
02370         qsort(mi->mi_set->recs, mi->mi_set->count,
02371                 sizeof(*mi->mi_set->recs), hdrNumCmp);
02372 /*@=boundsread@*/
02373 #else
02374         mergesort(mi->mi_set->recs, mi->mi_set->count,
02375                 sizeof(*mi->mi_set->recs), hdrNumCmp);
02376 #endif
02377         mi->mi_sorted = 1;
02378     }
02379 }
02380 
02381 /*@-bounds@*/ /* LCL: segfault */
02382 static int rpmdbGrowIterator(/*@null@*/ rpmdbMatchIterator mi, int fpNum)
02383         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
02384         /*@modifies mi, rpmGlobalMacroContext, fileSystem, internalState @*/
02385 {
02386     DBC * dbcursor;
02387     DBT * key;
02388     DBT * data;
02389     dbiIndex dbi = NULL;
02390     dbiIndexSet set;
02391     int rc;
02392     int xx;
02393     int i;
02394 
02395     if (mi == NULL)
02396         return 1;
02397 
02398     dbcursor = mi->mi_dbc;
02399     key = &mi->mi_key;
02400     data = &mi->mi_data;
02401     if (key->data == NULL)
02402         return 1;
02403 
02404     dbi = dbiOpen(mi->mi_db, mi->mi_rpmtag, 0);
02405     if (dbi == NULL)
02406         return 1;
02407 
02408     xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
02409     rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
02410 #ifndef SQLITE_HACK
02411     xx = dbiCclose(dbi, dbcursor, 0);
02412     dbcursor = NULL;
02413 #endif
02414 
02415     if (rc) {                   /* error/not found */
02416         if (rc != DB_NOTFOUND)
02417             rpmError(RPMERR_DBGETINDEX,
02418                 _("error(%d) getting \"%s\" records from %s index\n"),
02419                 rc, key->data, tagName(dbi->dbi_rpmtag));
02420 #ifdef  SQLITE_HACK
02421         xx = dbiCclose(dbi, dbcursor, 0);
02422         dbcursor = NULL;
02423 #endif
02424         return rc;
02425     }
02426 
02427     set = NULL;
02428     (void) dbt2set(dbi, data, &set);
02429     for (i = 0; i < set->count; i++)
02430         set->recs[i].fpNum = fpNum;
02431 
02432 #ifdef  SQLITE_HACK
02433     xx = dbiCclose(dbi, dbcursor, 0);
02434     dbcursor = NULL;
02435 #endif
02436 
02437 /*@-branchstate@*/
02438     if (mi->mi_set == NULL) {
02439         mi->mi_set = set;
02440     } else {
02441 #if 0
02442 fprintf(stderr, "+++ %d = %d + %d\t\"%s\"\n", (mi->mi_set->count + set->count), mi->mi_set->count, set->count, ((char *)key->data));
02443 #endif
02444         mi->mi_set->recs = xrealloc(mi->mi_set->recs,
02445                 (mi->mi_set->count + set->count) * sizeof(*(mi->mi_set->recs)));
02446         memcpy(mi->mi_set->recs + mi->mi_set->count, set->recs,
02447                 set->count * sizeof(*(mi->mi_set->recs)));
02448         mi->mi_set->count += set->count;
02449         set = dbiFreeIndexSet(set);
02450     }
02451 /*@=branchstate@*/
02452 
02453     return rc;
02454 }
02455 /*@=bounds@*/
02456 
02457 int rpmdbPruneIterator(rpmdbMatchIterator mi, int * hdrNums,
02458         int nHdrNums, int sorted)
02459 {
02460     if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
02461         return 1;
02462 
02463     if (mi->mi_set)
02464         (void) dbiPruneSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), sorted);
02465     return 0;
02466 }
02467 
02468 int rpmdbAppendIterator(rpmdbMatchIterator mi, const int * hdrNums, int nHdrNums)
02469 {
02470     if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
02471         return 1;
02472 
02473     if (mi->mi_set == NULL)
02474         mi->mi_set = xcalloc(1, sizeof(*mi->mi_set));
02475     (void) dbiAppendSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), 0);
02476     return 0;
02477 }
02478 
02479 rpmdbMatchIterator rpmdbInitIterator(rpmdb db, rpmTag rpmtag,
02480                 const void * keyp, size_t keylen)
02481         /*@globals rpmmiRock @*/
02482         /*@modifies rpmmiRock @*/
02483 {
02484     rpmdbMatchIterator mi;
02485     DBT * key;
02486     DBT * data;
02487     dbiIndexSet set = NULL;
02488     dbiIndex dbi;
02489     const void * mi_keyp = NULL;
02490     int isLabel = 0;
02491 
02492     if (db == NULL)
02493         return NULL;
02494 
02495     (void) rpmdbCheckSignals();
02496 
02497     /* XXX HACK to remove rpmdbFindByLabel/findMatches from the API */
02498     if (rpmtag == RPMDBI_LABEL) {
02499         rpmtag = RPMTAG_NAME;
02500         isLabel = 1;
02501     }
02502 
02503     dbi = dbiOpen(db, rpmtag, 0);
02504     if (dbi == NULL)
02505         return NULL;
02506 
02507     /* Chain cursors for teardown on abnormal exit. */
02508     mi = xcalloc(1, sizeof(*mi));
02509     mi->mi_next = rpmmiRock;
02510     rpmmiRock = mi;
02511 
02512     key = &mi->mi_key;
02513     data = &mi->mi_data;
02514 
02515     /*
02516      * Handle label and file name special cases.
02517      * Otherwise, retrieve join keys for secondary lookup.
02518      */
02519 /*@-branchstate@*/
02520     if (rpmtag != RPMDBI_PACKAGES && keyp) {
02521         DBC * dbcursor = NULL;
02522         int rc;
02523         int xx;
02524 
02525         if (isLabel) {
02526             xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
02527             rc = dbiFindByLabel(dbi, dbcursor, key, data, keyp, &set);
02528             xx = dbiCclose(dbi, dbcursor, 0);
02529             dbcursor = NULL;
02530         } else if (rpmtag == RPMTAG_BASENAMES) {
02531             rc = rpmdbFindByFile(db, keyp, key, data, &set);
02532         } else {
02533             xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
02534 
02535 /*@-temptrans@*/
02536 key->data = (void *) keyp;
02537 /*@=temptrans@*/
02538 key->size = keylen;
02539 if (key->data && key->size == 0) key->size = strlen((char *)key->data);
02540 if (key->data && key->size == 0) key->size++;   /* XXX "/" fixup. */
02541 
02542 /*@-nullstate@*/
02543             rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
02544 /*@=nullstate@*/
02545             if (rc > 0) {
02546                 rpmError(RPMERR_DBGETINDEX,
02547                         _("error(%d) getting \"%s\" records from %s index\n"),
02548                         rc, (key->data ? key->data : "???"), tagName(dbi->dbi_rpmtag));
02549             }
02550 
02551             /* Join keys need to be native endian internally. */
02552             if (rc == 0)
02553                 (void) dbt2set(dbi, data, &set);
02554 
02555             xx = dbiCclose(dbi, dbcursor, 0);
02556             dbcursor = NULL;
02557         }
02558         if (rc) {       /* error/not found */
02559             set = dbiFreeIndexSet(set);
02560             rpmmiRock = mi->mi_next;
02561             mi->mi_next = NULL;
02562             mi = _free(mi);
02563             return NULL;
02564         }
02565     }
02566 /*@=branchstate@*/
02567 
02568     /* Copy the retrieval key, byte swapping header instance if necessary. */
02569     if (keyp) {
02570         switch (rpmtag) {
02571         case RPMDBI_PACKAGES:
02572           { union _dbswap *k;
02573 
02574 assert(keylen == sizeof(k->ui));                /* xxx programmer error */
02575             k = xmalloc(sizeof(*k));
02576             memcpy(k, keyp, keylen);
02577             if (dbiByteSwapped(dbi) == 1)
02578                 _DBSWAP(*k);
02579             mi_keyp = k;
02580           } break;
02581         default:
02582           { char * k;
02583             if (keylen == 0)
02584                 keylen = strlen(keyp);
02585             k = xmalloc(keylen + 1);
02586 /*@-boundsread@*/
02587             memcpy(k, keyp, keylen);
02588 /*@=boundsread@*/
02589             k[keylen] = '\0';   /* XXX assumes strings */
02590             mi_keyp = k;
02591           } break;
02592         }
02593     }
02594 
02595     mi->mi_keyp = mi_keyp;
02596     mi->mi_keylen = keylen;
02597 
02598     mi->mi_db = rpmdbLink(db, "matchIterator");
02599     mi->mi_rpmtag = rpmtag;
02600 
02601     mi->mi_dbc = NULL;
02602     mi->mi_set = set;
02603     mi->mi_setx = 0;
02604     mi->mi_h = NULL;
02605     mi->mi_sorted = 0;
02606     mi->mi_cflags = 0;
02607     mi->mi_modified = 0;
02608     mi->mi_prevoffset = 0;
02609     mi->mi_offset = 0;
02610     mi->mi_filenum = 0;
02611     mi->mi_nre = 0;
02612     mi->mi_re = NULL;
02613 
02614     mi->mi_ts = NULL;
02615     mi->mi_hdrchk = NULL;
02616 
02617 /*@i@*/ return mi;
02618 }
02619 
02620 /* XXX psm.c */
02621 int rpmdbRemove(rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum,
02622                 /*@unused@*/ rpmts ts,
02623                 /*@unused@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
02624 {
02625 DBC * dbcursor = NULL;
02626 DBT * key = alloca(sizeof(*key));
02627 DBT * data = alloca(sizeof(*data));
02628 union _dbswap mi_offset;
02629     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
02630     HFD_t hfd = headerFreeData;
02631     Header h;
02632     sigset_t signalMask;
02633     int ret = 0;
02634     int rc = 0;
02635 
02636     if (db == NULL)
02637         return 0;
02638 
02639 memset(key, 0, sizeof(*key));
02640 memset(data, 0, sizeof(*data));
02641 
02642     {   rpmdbMatchIterator mi;
02643         mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &hdrNum, sizeof(hdrNum));
02644         h = rpmdbNextIterator(mi);
02645         if (h)
02646             h = headerLink(h);
02647         mi = rpmdbFreeIterator(mi);
02648     }
02649 
02650     if (h == NULL) {
02651         rpmError(RPMERR_DBCORRUPT, _("%s: cannot read header at 0x%x\n"),
02652               "rpmdbRemove", hdrNum);
02653         return 1;
02654     }
02655 
02656 #ifdef  DYING
02657     /* Add remove transaction id to header. */
02658     if (rid != 0 && rid != -1) {
02659         int_32 tid = rid;
02660         (void) headerAddEntry(h, RPMTAG_REMOVETID, RPM_INT32_TYPE, &tid, 1);
02661     }
02662 #endif
02663 
02664     {   const char *n, *v, *r;
02665         (void) headerNVR(h, &n, &v, &r);
02666         rpmMessage(RPMMESS_DEBUG, "  --- h#%8u %s-%s-%s\n", hdrNum, n, v, r);
02667     }
02668 
02669     (void) blockSignals(db, &signalMask);
02670 
02671         /*@-nullpass -nullptrarith -nullderef @*/ /* FIX: rpmvals heartburn */
02672     {   int dbix;
02673         dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
02674 
02675         if (dbiTags != NULL)
02676         for (dbix = 0; dbix < dbiTagsMax; dbix++) {
02677             dbiIndex dbi;
02678             const char *av[1];
02679             const char ** rpmvals = NULL;
02680             rpmTagType rpmtype = 0;
02681             int rpmcnt = 0;
02682             int rpmtag;
02683             int xx;
02684             int i, j;
02685 
02686             dbi = NULL;
02687 /*@-boundsread@*/
02688             rpmtag = dbiTags[dbix];
02689 /*@=boundsread@*/
02690 
02691             /*@-branchstate@*/
02692             /* Filter out temporary databases */
02693             if (isTemporaryDB(rpmtag)) 
02694                 continue;
02695 
02696             switch (rpmtag) {
02697             case RPMDBI_PACKAGES:
02698                 dbi = dbiOpen(db, rpmtag, 0);
02699                 if (dbi == NULL)        /* XXX shouldn't happen */
02700                     continue;
02701               
02702 /*@-immediatetrans@*/
02703 mi_offset.ui = hdrNum;
02704 if (dbiByteSwapped(dbi) == 1)
02705     _DBSWAP(mi_offset);
02706                 key->data = &mi_offset;
02707 /*@=immediatetrans@*/
02708                 key->size = sizeof(mi_offset.ui);
02709 
02710                 rc = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
02711                 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
02712                 if (rc) {
02713                     rpmError(RPMERR_DBGETINDEX,
02714                         _("error(%d) setting header #%d record for %s removal\n"),
02715                         rc, hdrNum, tagName(dbi->dbi_rpmtag));
02716                 } else
02717                     rc = dbiDel(dbi, dbcursor, key, data, 0);
02718                 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
02719                 dbcursor = NULL;
02720                 if (!dbi->dbi_no_dbsync)
02721                     xx = dbiSync(dbi, 0);
02722                 continue;
02723                 /*@notreached@*/ /*@switchbreak@*/ break;
02724             }
02725             /*@=branchstate@*/
02726         
02727             if (!hge(h, rpmtag, &rpmtype, (void **) &rpmvals, &rpmcnt))
02728                 continue;
02729 
02730           dbi = dbiOpen(db, rpmtag, 0);
02731           if (dbi != NULL) {
02732             int printed;
02733 
02734             if (rpmtype == RPM_STRING_TYPE) {
02735                 /* XXX force uniform headerGetEntry return */
02736                 av[0] = (const char *) rpmvals;
02737                 rpmvals = av;
02738                 rpmcnt = 1;
02739             }
02740 
02741             printed = 0;
02742             xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
02743 /*@-branchstate@*/
02744             for (i = 0; i < rpmcnt; i++) {
02745                 dbiIndexSet set;
02746                 int stringvalued;
02747                 byte bin[32];
02748 
02749                 switch (dbi->dbi_rpmtag) {
02750                 case RPMTAG_FILEMD5S:
02751                     /* Filter out empty MD5 strings. */
02752                     if (!(rpmvals[i] && *rpmvals[i] != '\0'))
02753                         /*@innercontinue@*/ continue;
02754                     /*@switchbreak@*/ break;
02755                 default:
02756                     /*@switchbreak@*/ break;
02757                 }
02758 
02759                 /* Identify value pointer and length. */
02760                 stringvalued = 0;
02761                 switch (rpmtype) {
02762 /*@-sizeoftype@*/
02763                 case RPM_CHAR_TYPE:
02764                 case RPM_INT8_TYPE:
02765                     key->size = sizeof(RPM_CHAR_TYPE);
02766                     key->data = rpmvals + i;
02767                     /*@switchbreak@*/ break;
02768                 case RPM_INT16_TYPE:
02769                     key->size = sizeof(int_16);
02770                     key->data = rpmvals + i;
02771                     /*@switchbreak@*/ break;
02772                 case RPM_INT32_TYPE:
02773                     key->size = sizeof(int_32);
02774                     key->data = rpmvals + i;
02775                     /*@switchbreak@*/ break;
02776 /*@=sizeoftype@*/
02777                 case RPM_BIN_TYPE:
02778                     key->size = rpmcnt;
02779                     key->data = rpmvals;
02780                     rpmcnt = 1;         /* XXX break out of loop. */
02781                     /*@switchbreak@*/ break;
02782                 case RPM_STRING_TYPE:
02783                 case RPM_I18NSTRING_TYPE:
02784                     rpmcnt = 1;         /* XXX break out of loop. */
02785                     /*@fallthrough@*/
02786                 case RPM_STRING_ARRAY_TYPE:
02787                     /* Convert from hex to binary. */
02788 /*@-boundsread@*/
02789                     if (dbi->dbi_rpmtag == RPMTAG_FILEMD5S) {
02790                         const char * s;
02791                         byte * t;
02792 
02793                         s = rpmvals[i];
02794                         t = bin;
02795                         for (j = 0; j < 16; j++, t++, s += 2)
02796                             *t = (nibble(s[0]) << 4) | nibble(s[1]);
02797                         key->data = bin;
02798                         key->size = 16;
02799                         /*@switchbreak@*/ break;
02800                     }
02801                     /* Extract the pubkey id from the base64 blob. */
02802                     if (dbi->dbi_rpmtag == RPMTAG_PUBKEYS) {
02803                         pgpDig dig = pgpNewDig();
02804                         const byte * pkt;
02805                         ssize_t pktlen;
02806 
02807                         if (b64decode(rpmvals[i], (void **)&pkt, &pktlen))
02808                             /*@innercontinue@*/ continue;
02809                         (void) pgpPrtPkts(pkt, pktlen, dig, 0);
02810                         memcpy(bin, dig->pubkey.signid, 8);
02811                         pkt = _free(pkt);
02812                         dig = _free(dig);
02813                         key->data = bin;
02814                         key->size = 8;
02815                         /*@switchbreak@*/ break;
02816                     }
02817 /*@=boundsread@*/
02818                     /*@fallthrough@*/
02819                 default:
02820 /*@i@*/             key->data = (void *) rpmvals[i];
02821                     key->size = strlen(rpmvals[i]);
02822                     stringvalued = 1;
02823                     /*@switchbreak@*/ break;
02824                 }
02825 
02826                 if (!printed) {
02827                     if (rpmcnt == 1 && stringvalued) {
02828                         rpmMessage(RPMMESS_DEBUG,
02829                                 _("removing \"%s\" from %s index.\n"),
02830                                 (char *)key->data, tagName(dbi->dbi_rpmtag));
02831                     } else {
02832                         rpmMessage(RPMMESS_DEBUG,
02833                                 _("removing %d entries from %s index.\n"),
02834                                 rpmcnt, tagName(dbi->dbi_rpmtag));
02835                     }
02836                     printed++;
02837                 }
02838 
02839                 /* XXX
02840                  * This is almost right, but, if there are duplicate tag
02841                  * values, there will be duplicate attempts to remove
02842                  * the header instance. It's faster to just ignore errors
02843                  * than to do things correctly.
02844                  */
02845 
02846 /* XXX with duplicates, an accurate data value and DB_GET_BOTH is needed. */
02847 
02848                 set = NULL;
02849 
02850 if (key->size == 0) key->size = strlen((char *)key->data);
02851 if (key->size == 0) key->size++;        /* XXX "/" fixup. */
02852  
02853 /*@-compmempass@*/
02854                 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
02855                 if (rc == 0) {                  /* success */
02856                     (void) dbt2set(dbi, data, &set);
02857                 } else if (rc == DB_NOTFOUND) { /* not found */
02858                     /*@innercontinue@*/ continue;
02859                 } else {                        /* error */
02860                     rpmError(RPMERR_DBGETINDEX,
02861                         _("error(%d) setting \"%s\" records from %s index\n"),
02862                         rc, key->data, tagName(dbi->dbi_rpmtag));
02863                     ret += 1;
02864                     /*@innercontinue@*/ continue;
02865                 }
02866 /*@=compmempass@*/
02867 
02868                 rc = dbiPruneSet(set, rec, 1, sizeof(*rec), 1);
02869 
02870                 /* If nothing was pruned, then don't bother updating. */
02871                 if (rc) {
02872                     set = dbiFreeIndexSet(set);
02873                     /*@innercontinue@*/ continue;
02874                 }
02875 
02876 /*@-compmempass@*/
02877                 if (set->count > 0) {
02878                     (void) set2dbt(dbi, data, set);
02879                     rc = dbiPut(dbi, dbcursor, key, data, DB_KEYLAST);
02880                     if (rc) {
02881                         rpmError(RPMERR_DBPUTINDEX,
02882                                 _("error(%d) storing record \"%s\" into %s\n"),
02883                                 rc, key->data, tagName(dbi->dbi_rpmtag));
02884                         ret += 1;
02885                     }
02886                     data->data = _free(data->data);
02887                     data->size = 0;
02888                 } else {
02889                     rc = dbiDel(dbi, dbcursor, key, data, 0);
02890                     if (rc) {
02891                         rpmError(RPMERR_DBPUTINDEX,
02892                                 _("error(%d) removing record \"%s\" from %s\n"),
02893                                 rc, key->data, tagName(dbi->dbi_rpmtag));
02894                         ret += 1;
02895                     }
02896                 }
02897 /*@=compmempass@*/
02898                 set = dbiFreeIndexSet(set);
02899             }
02900 /*@=branchstate@*/
02901 
02902             xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
02903             dbcursor = NULL;
02904 
02905             if (!dbi->dbi_no_dbsync)
02906                 xx = dbiSync(dbi, 0);
02907           }
02908 
02909             if (rpmtype != RPM_BIN_TYPE)        /* XXX WTFO? HACK ALERT */
02910                 rpmvals = hfd(rpmvals, rpmtype);
02911             rpmtype = 0;
02912             rpmcnt = 0;
02913         }
02914 
02915         rec = _free(rec);
02916     }
02917     /*@=nullpass =nullptrarith =nullderef @*/
02918 
02919     (void) unblockSignals(db, &signalMask);
02920 
02921     h = headerFree(h);
02922 
02923     /* XXX return ret; */
02924     return 0;
02925 }
02926 
02927 /* XXX install.c */
02928 int rpmdbAdd(rpmdb db, int iid, Header h,
02929                 /*@unused@*/ rpmts ts,
02930                 /*@unused@*/ rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
02931 {
02932 DBC * dbcursor = NULL;
02933 DBT * key = alloca(sizeof(*key));
02934 DBT * data = alloca(sizeof(*data));
02935     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
02936     HFD_t hfd = headerFreeData;
02937     sigset_t signalMask;
02938     const char ** baseNames;
02939     rpmTagType bnt;
02940     int count = 0;
02941     dbiIndex dbi;
02942     int dbix;
02943     union _dbswap mi_offset;
02944     unsigned int hdrNum = 0;
02945     int ret = 0;
02946     int rc;
02947     int xx;
02948 
02949     /* Reinitialize to zero, so in the event the add fails
02950      * we won't have bogus information (i.e. the last succesful
02951      * add).
02952      */
02953 /*@-mods@*/
02954     myinstall_instance = 0;
02955 /*@=mods@*/
02956 
02957     if (db == NULL)
02958         return 0;
02959 
02960 memset(key, 0, sizeof(*key));
02961 memset(data, 0, sizeof(*data));
02962 
02963 #ifdef  NOTYET  /* XXX headerRemoveEntry() broken on dribbles. */
02964     xx = headerRemoveEntry(h, RPMTAG_REMOVETID);
02965 #endif
02966     if (iid != 0 && iid != -1) {
02967         int_32 tid = iid;
02968         if (!headerIsEntry(h, RPMTAG_INSTALLTID))
02969            xx = headerAddEntry(h, RPMTAG_INSTALLTID, RPM_INT32_TYPE, &tid, 1);
02970     }
02971 
02972     /*
02973      * If old style filename tags is requested, the basenames need to be
02974      * retrieved early, and the header needs to be converted before
02975      * being written to the package header database.
02976      */
02977 
02978     xx = hge(h, RPMTAG_BASENAMES, &bnt, (void **) &baseNames, &count);
02979 
02980     if (_noDirTokens)
02981         expandFilelist(h);
02982 
02983     (void) blockSignals(db, &signalMask);
02984 
02985     {
02986         unsigned int firstkey = 0;
02987         void * keyp = &firstkey;
02988         size_t keylen = sizeof(firstkey);
02989         void * datap = NULL;
02990         size_t datalen = 0;
02991 
02992       dbi = dbiOpen(db, RPMDBI_PACKAGES, 0);
02993       /*@-branchstate@*/
02994       if (dbi != NULL) {
02995 
02996         /* XXX db0: hack to pass sizeof header to fadAlloc */
02997         datap = h;
02998         datalen = headerSizeof(h, HEADER_MAGIC_NO);
02999 
03000         xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
03001 
03002         /* Retrieve join key for next header instance. */
03003 
03004 /*@-compmempass@*/
03005         key->data = keyp;
03006         key->size = keylen;
03007 /*@i@*/ data->data = datap;
03008         data->size = datalen;
03009         ret = dbiGet(dbi, dbcursor, key, data, DB_SET);
03010         keyp = key->data;
03011         keylen = key->size;
03012         datap = data->data;
03013         datalen = data->size;
03014 /*@=compmempass@*/
03015 
03016 /*@-bounds@*/
03017         hdrNum = 0;
03018         if (ret == 0 && datap) {
03019             memcpy(&mi_offset, datap, sizeof(mi_offset.ui));
03020             if (dbiByteSwapped(dbi) == 1)
03021                 _DBSWAP(mi_offset);
03022             hdrNum = mi_offset.ui;
03023         }
03024         ++hdrNum;
03025         mi_offset.ui = hdrNum;
03026         if (dbiByteSwapped(dbi) == 1)
03027             _DBSWAP(mi_offset);
03028         if (ret == 0 && datap) {
03029             memcpy(datap, &mi_offset, sizeof(mi_offset.ui));
03030         } else {
03031             datap = &mi_offset;
03032             datalen = sizeof(mi_offset.ui);
03033         }
03034 /*@=bounds@*/
03035 
03036         key->data = keyp;
03037         key->size = keylen;
03038 /*@-kepttrans@*/
03039         data->data = datap;
03040 /*@=kepttrans@*/
03041         data->size = datalen;
03042 
03043 /*@-compmempass@*/
03044         ret = dbiPut(dbi, dbcursor, key, data, DB_KEYLAST);
03045 /*@=compmempass@*/
03046         xx = dbiSync(dbi, 0);
03047 
03048         xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
03049         dbcursor = NULL;
03050       }
03051       /*@=branchstate@*/
03052 
03053     }
03054 
03055     if (ret) {
03056         rpmError(RPMERR_DBCORRUPT,
03057                 _("error(%d) allocating new package instance\n"), ret);
03058         goto exit;
03059     }
03060 
03061     /* Now update the indexes */
03062 
03063     if (hdrNum)
03064     {   
03065         dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
03066 
03067         /* Save the header number for the current transaction. */
03068 /*@-mods@*/
03069         myinstall_instance = hdrNum;
03070 /*@=mods@*/
03071         
03072         if (dbiTags != NULL)
03073         for (dbix = 0; dbix < dbiTagsMax; dbix++) {
03074             const char *av[1];
03075             const char **rpmvals = NULL;
03076             rpmTagType rpmtype = 0;
03077             int rpmcnt = 0;
03078             int rpmtag;
03079             int_32 * requireFlags;
03080             rpmRC rpmrc;
03081             int i, j;
03082 
03083             rpmrc = RPMRC_NOTFOUND;
03084             dbi = NULL;
03085             requireFlags = NULL;
03086 /*@-boundsread@*/
03087             rpmtag = dbiTags[dbix];
03088 /*@=boundsread@*/
03089 
03090             /* Filter out temporary databases */
03091             if (isTemporaryDB(rpmtag)) 
03092                 continue;
03093 
03094             switch (rpmtag) {
03095             case RPMDBI_PACKAGES:
03096                 dbi = dbiOpen(db, rpmtag, 0);
03097                 if (dbi == NULL)        /* XXX shouldn't happen */
03098                     continue;
03099                 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
03100 
03101 mi_offset.ui = hdrNum;
03102 if (dbiByteSwapped(dbi) == 1)
03103     _DBSWAP(mi_offset);
03104 /*@-immediatetrans@*/
03105 key->data = (void *) &mi_offset;
03106 /*@=immediatetrans@*/
03107 key->size = sizeof(mi_offset.ui);
03108 data->data = headerUnload(h);
03109 data->size = headerSizeof(h, HEADER_MAGIC_NO);
03110 
03111                 /* Check header digest/signature on blob export. */
03112                 if (hdrchk && ts) {
03113                     const char * msg = NULL;
03114                     int lvl;
03115 
03116                     rpmrc = (*hdrchk) (ts, data->data, data->size, &msg);
03117                     lvl = (rpmrc == RPMRC_FAIL ? RPMMESS_ERROR : RPMMESS_DEBUG);
03118                     rpmMessage(lvl, "%s h#%8u %s",
03119                         (rpmrc == RPMRC_FAIL ? _("rpmdbAdd: skipping") : "  +++"),
03120                                 hdrNum, (msg ? msg : "\n"));
03121                     msg = _free(msg);
03122                 }
03123 
03124                 if (data->data != NULL && rpmrc != RPMRC_FAIL) {
03125 /*@-compmempass@*/
03126                     xx = dbiPut(dbi, dbcursor, key, data, DB_KEYLAST);
03127 /*@=compmempass@*/
03128                     xx = dbiSync(dbi, 0);
03129                 }
03130 data->data = _free(data->data);
03131 data->size = 0;
03132                 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
03133                 dbcursor = NULL;
03134                 if (!dbi->dbi_no_dbsync)
03135                     xx = dbiSync(dbi, 0);
03136                 continue;
03137                 /*@notreached@*/ /*@switchbreak@*/ break;
03138             case RPMTAG_BASENAMES:      /* XXX preserve legacy behavior */
03139                 rpmtype = bnt;
03140                 rpmvals = baseNames;
03141                 rpmcnt = count;
03142                 /*@switchbreak@*/ break;
03143             case RPMTAG_REQUIRENAME:
03144                 xx = hge(h, rpmtag, &rpmtype, (void **)&rpmvals, &rpmcnt);
03145                 xx = hge(h, RPMTAG_REQUIREFLAGS, NULL, (void **)&requireFlags, NULL);
03146                 /*@switchbreak@*/ break;
03147             default:
03148                 xx = hge(h, rpmtag, &rpmtype, (void **)&rpmvals, &rpmcnt);
03149                 /*@switchbreak@*/ break;
03150             }
03151 
03152             /*@-branchstate@*/
03153             if (rpmcnt <= 0) {
03154                 if (rpmtag != RPMTAG_GROUP)
03155                     continue;
03156 
03157                 /* XXX preserve legacy behavior */
03158                 rpmtype = RPM_STRING_TYPE;
03159                 rpmvals = (const char **) "Unknown";
03160                 rpmcnt = 1;
03161             }
03162             /*@=branchstate@*/
03163 
03164           dbi = dbiOpen(db, rpmtag, 0);
03165           if (dbi != NULL) {
03166             int printed;
03167 
03168             if (rpmtype == RPM_STRING_TYPE) {
03169                 /* XXX force uniform headerGetEntry return */
03170                 /*@-observertrans@*/
03171                 av[0] = (const char *) rpmvals;
03172                 /*@=observertrans@*/
03173                 rpmvals = av;
03174                 rpmcnt = 1;
03175             }
03176 
03177             printed = 0;
03178             xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
03179 
03180             for (i = 0; i < rpmcnt; i++) {
03181                 dbiIndexSet set;
03182                 int stringvalued;
03183                 byte bin[32];
03184                 byte * t;
03185 
03186                 /*
03187                  * Include the tagNum in all indices. rpm-3.0.4 and earlier
03188                  * included the tagNum only for files.
03189                  */
03190                 rec->tagNum = i;
03191                 switch (dbi->dbi_rpmtag) {
03192                 case RPMTAG_PUBKEYS:
03193                     /*@switchbreak@*/ break;
03194                 case RPMTAG_FILEMD5S:
03195                     /* Filter out empty MD5 strings. */
03196                     if (!(rpmvals[i] && *rpmvals[i] != '\0'))
03197                         /*@innercontinue@*/ continue;
03198                     /*@switchbreak@*/ break;
03199                 case RPMTAG_REQUIRENAME:
03200                     /* Filter out install prerequisites. */
03201                     if (requireFlags && isInstallPreReq(requireFlags[i]))
03202                         /*@innercontinue@*/ continue;
03203                     /*@switchbreak@*/ break;
03204                 case RPMTAG_TRIGGERNAME:
03205                     if (i) {    /* don't add duplicates */
03206 /*@-boundsread@*/
03207                         for (j = 0; j < i; j++) {
03208                             if (!strcmp(rpmvals[i], rpmvals[j]))
03209                                 /*@innerbreak@*/ break;
03210                         }
03211 /*@=boundsread@*/
03212                         if (j < i)
03213                             /*@innercontinue@*/ continue;
03214                     }
03215                     /*@switchbreak@*/ break;
03216                 default:
03217                     /*@switchbreak@*/ break;
03218                 }
03219 
03220                 /* Identify value pointer and length. */
03221                 stringvalued = 0;
03222 /*@-branchstate@*/
03223                 switch (rpmtype) {
03224 /*@-sizeoftype@*/
03225                 case RPM_CHAR_TYPE:
03226                 case RPM_INT8_TYPE:
03227                     key->size = sizeof(int_8);
03228 /*@i@*/             key->data = rpmvals + i;
03229                     /*@switchbreak@*/ break;
03230                 case RPM_INT16_TYPE:
03231                     key->size = sizeof(int_16);
03232 /*@i@*/             key->data = rpmvals + i;
03233                     /*@switchbreak@*/ break;
03234                 case RPM_INT32_TYPE:
03235                     key->size = sizeof(int_32);
03236 /*@i@*/             key->data = rpmvals + i;
03237                     /*@switchbreak@*/ break;
03238 /*@=sizeoftype@*/
03239                 case RPM_BIN_TYPE:
03240                     key->size = rpmcnt;
03241 /*@i@*/             key->data = rpmvals;
03242                     rpmcnt = 1;         /* XXX break out of loop. */
03243                     /*@switchbreak@*/ break;
03244                 case RPM_STRING_TYPE:
03245                 case RPM_I18NSTRING_TYPE:
03246                     rpmcnt = 1;         /* XXX break out of loop. */
03247                     /*@fallthrough@*/
03248                 case RPM_STRING_ARRAY_TYPE:
03249                     /* Convert from hex to binary. */
03250 /*@-boundsread@*/
03251                     if (dbi->dbi_rpmtag == RPMTAG_FILEMD5S) {
03252                         const char * s;
03253 
03254                         s = rpmvals[i];
03255                         t = bin;
03256                         for (j = 0; j < 16; j++, t++, s += 2)
03257                             *t = (nibble(s[0]) << 4) | nibble(s[1]);
03258                         key->data = bin;
03259                         key->size = 16;
03260                         /*@switchbreak@*/ break;
03261                     }
03262                     /* Extract the pubkey id from the base64 blob. */
03263                     if (dbi->dbi_rpmtag == RPMTAG_PUBKEYS) {
03264                         pgpDig dig = pgpNewDig();
03265                         const byte * pkt;
03266                         ssize_t pktlen;
03267 
03268                         if (b64decode(rpmvals[i], (void **)&pkt, &pktlen))
03269                             /*@innercontinue@*/ continue;
03270                         (void) pgpPrtPkts(pkt, pktlen, dig, 0);
03271                         memcpy(bin, dig->pubkey.signid, 8);
03272                         pkt = _free(pkt);
03273                         dig = _free(dig);
03274                         key->data = bin;
03275                         key->size = 8;
03276                         /*@switchbreak@*/ break;
03277                     }
03278 /*@=boundsread@*/
03279                     /*@fallthrough@*/
03280                 default:
03281 /*@i@*/             key->data = (void *) rpmvals[i];
03282                     key->size = strlen(rpmvals[i]);
03283                     stringvalued = 1;
03284                     /*@switchbreak@*/ break;
03285                 }
03286 /*@=branchstate@*/
03287 
03288                 if (!printed) {
03289                     if (rpmcnt == 1 && stringvalued) {
03290                         rpmMessage(RPMMESS_DEBUG,
03291                                 _("adding \"%s\" to %s index.\n"),
03292                                 (char *)key->data, tagName(dbi->dbi_rpmtag));
03293                     } else {
03294                         rpmMessage(RPMMESS_DEBUG,
03295                                 _("adding %d entries to %s index.\n"),
03296                                 rpmcnt, tagName(dbi->dbi_rpmtag));
03297                     }
03298                     printed++;
03299                 }
03300 
03301 /* XXX with duplicates, an accurate data value and DB_GET_BOTH is needed. */
03302 
03303                 set = NULL;
03304 
03305 if (key->size == 0) key->size = strlen((char *)key->data);
03306 if (key->size == 0) key->size++;        /* XXX "/" fixup. */
03307 
03308 /*@-compmempass@*/
03309                 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
03310                 if (rc == 0) {                  /* success */
03311                 /* With duplicates, cursor is positioned, discard the record. */
03312                     if (!dbi->dbi_permit_dups)
03313                         (void) dbt2set(dbi, data, &set);
03314                 } else if (rc != DB_NOTFOUND) { /* error */
03315                     rpmError(RPMERR_DBGETINDEX,
03316                         _("error(%d) getting \"%s\" records from %s index\n"),
03317                         rc, key->data, tagName(dbi->dbi_rpmtag));
03318                     ret += 1;
03319                     /*@innercontinue@*/ continue;
03320                 }
03321 /*@=compmempass@*/
03322 
03323                 if (set == NULL)                /* not found or duplicate */
03324                     set = xcalloc(1, sizeof(*set));
03325 
03326                 (void) dbiAppendSet(set, rec, 1, sizeof(*rec), 0);
03327 
03328 /*@-compmempass@*/
03329                 (void) set2dbt(dbi, data, set);
03330                 rc = dbiPut(dbi, dbcursor, key, data, DB_KEYLAST);
03331 /*@=compmempass@*/
03332 
03333                 if (rc) {
03334                     rpmError(RPMERR_DBPUTINDEX,
03335                                 _("error(%d) storing record %s into %s\n"),
03336                                 rc, key->data, tagName(dbi->dbi_rpmtag));
03337                     ret += 1;
03338                 }
03339 /*@-unqualifiedtrans@*/
03340                 data->data = _free(data->data);
03341 /*@=unqualifiedtrans@*/
03342                 data->size = 0;
03343                 set = dbiFreeIndexSet(set);
03344             }
03345 
03346             xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
03347             dbcursor = NULL;
03348 
03349             if (!dbi->dbi_no_dbsync)
03350                 xx = dbiSync(dbi, 0);
03351           }
03352 
03353         /*@-observertrans@*/
03354             if (rpmtype != RPM_BIN_TYPE)        /* XXX WTFO? HACK ALERT */
03355                 rpmvals = hfd(rpmvals, rpmtype);
03356         /*@=observertrans@*/
03357             rpmtype = 0;
03358             rpmcnt = 0;
03359         }
03360         /*@=nullpass =nullptrarith =nullderef @*/
03361 
03362         rec = _free(rec);
03363     }
03364 
03365 exit:
03366     (void) unblockSignals(db, &signalMask);
03367 
03368     return ret;
03369 }
03370 
03371 #define _skip(_dn)      { sizeof(_dn)-1, (_dn) }
03372 
03373 /*@unchecked@*/ /*@observer@*/
03374 static struct skipDir_s {
03375     int dnlen;
03376 /*@observer@*/ /*@null@*/
03377     const char * dn;
03378 } skipDirs[] = {
03379     { 0, NULL }
03380 };
03381 
03382 static int skipDir(const char * dn)
03383         /*@*/
03384 {
03385     struct skipDir_s * sd = skipDirs;
03386     int dnlen;
03387 
03388     dnlen = strlen(dn);
03389     for (sd = skipDirs; sd->dn != NULL; sd++) {
03390         if (dnlen < sd->dnlen)
03391             continue;
03392         if (strncmp(dn, sd->dn, sd->dnlen))
03393             continue;
03394         return 1;
03395     }
03396     return 0;
03397 }
03398 
03399 /* XXX transaction.c */
03400 /*@-compmempass@*/
03401 int rpmdbFindFpList(rpmdb db, fingerPrint * fpList, dbiIndexSet * matchList, 
03402                     int numItems)
03403 {
03404 DBT * key;
03405 DBT * data;
03406     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
03407     HFD_t hfd = headerFreeData;
03408     rpmdbMatchIterator mi;
03409     fingerPrintCache fpc;
03410     Header h;
03411     int i, xx;
03412 
03413     if (db == NULL) return 1;
03414 
03415     mi = rpmdbInitIterator(db, RPMTAG_BASENAMES, NULL, 0);
03416     if (mi == NULL)     /* XXX should  never happen */
03417         return 1;
03418 
03419 key = &mi->mi_key;
03420 data = &mi->mi_data;
03421 
03422     /* Gather all installed headers with matching basename's. */
03423     for (i = 0; i < numItems; i++) {
03424 
03425 /*@-boundswrite@*/
03426         matchList[i] = xcalloc(1, sizeof(*(matchList[i])));
03427 /*@=boundswrite@*/
03428 
03429 /*@-boundsread -dependenttrans@*/
03430 key->data = (void *) fpList[i].baseName;
03431 /*@=boundsread =dependenttrans@*/
03432 key->size = strlen((char *)key->data);
03433 if (key->size == 0) key->size++;        /* XXX "/" fixup. */
03434 
03435         if (skipDir(fpList[i].entry->dirName))
03436             continue;
03437 
03438         xx = rpmdbGrowIterator(mi, i);
03439 
03440     }
03441 
03442     if ((i = rpmdbGetIteratorCount(mi)) == 0) {
03443         mi = rpmdbFreeIterator(mi);
03444         return 0;
03445     }
03446     fpc = fpCacheCreate(i);
03447 
03448     rpmdbSortIterator(mi);
03449     /* iterator is now sorted by (recnum, filenum) */
03450 
03451     /* For all installed headers with matching basename's ... */
03452     if (mi != NULL)
03453     while ((h = rpmdbNextIterator(mi)) != NULL) {
03454         const char ** dirNames;
03455         const char ** baseNames;
03456         const char ** fullBaseNames;
03457         rpmTagType bnt, dnt;
03458         int_32 * dirIndexes;
03459         int_32 * fullDirIndexes;
03460         fingerPrint * fps;
03461         dbiIndexItem im;
03462         int start;
03463         int num;
03464         int end;
03465 
03466         start = mi->mi_setx - 1;
03467         im = mi->mi_set->recs + start;
03468 
03469         /* Find the end of the set of matched basename's in this package. */
03470 /*@-boundsread@*/
03471         for (end = start + 1; end < mi->mi_set->count; end++) {
03472             if (im->hdrNum != mi->mi_set->recs[end].hdrNum)
03473                 /*@innerbreak@*/ break;
03474         }
03475 /*@=boundsread@*/
03476         num = end - start;
03477 
03478         /* Compute fingerprints for this installed header's matches */
03479         xx = hge(h, RPMTAG_BASENAMES, &bnt, (void **) &fullBaseNames, NULL);
03480         xx = hge(h, RPMTAG_DIRNAMES, &dnt, (void **) &dirNames, NULL);
03481         xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fullDirIndexes, NULL);
03482 
03483         baseNames = xcalloc(num, sizeof(*baseNames));
03484         dirIndexes = xcalloc(num, sizeof(*dirIndexes));
03485 /*@-bounds@*/
03486         for (i = 0; i < num; i++) {
03487             baseNames[i] = fullBaseNames[im[i].tagNum];
03488             dirIndexes[i] = fullDirIndexes[im[i].tagNum];
03489         }
03490 /*@=bounds@*/
03491 
03492         fps = xcalloc(num, sizeof(*fps));
03493         fpLookupList(fpc, dirNames, baseNames, dirIndexes, num, fps);
03494 
03495         /* Add db (recnum,filenum) to list for fingerprint matches. */
03496 /*@-boundsread@*/
03497         for (i = 0; i < num; i++, im++) {
03498             /*@-nullpass@*/ /* FIX: fpList[].subDir may be NULL */
03499             if (!FP_EQUAL(fps[i], fpList[im->fpNum]))
03500                 /*@innercontinue@*/ continue;
03501             /*@=nullpass@*/
03502             xx = dbiAppendSet(matchList[im->fpNum], im, 1, sizeof(*im), 0);
03503         }
03504 /*@=boundsread@*/
03505 
03506         fps = _free(fps);
03507         dirNames = hfd(dirNames, dnt);
03508         fullBaseNames = hfd(fullBaseNames, bnt);
03509         baseNames = _free(baseNames);
03510         dirIndexes = _free(dirIndexes);
03511 
03512         mi->mi_setx = end;
03513     }
03514 
03515     mi = rpmdbFreeIterator(mi);
03516 
03517     fpc = fpCacheFree(fpc);
03518 
03519     return 0;
03520 
03521 }
03522 /*@=compmempass@*/
03523 
03529 static int rpmioFileExists(const char * urlfn)
03530         /*@globals h_errno, fileSystem, internalState @*/
03531         /*@modifies fileSystem, internalState @*/
03532 {
03533     const char *fn;
03534     int urltype = urlPath(urlfn, &fn);
03535     struct stat buf;
03536 
03537     /*@-branchstate@*/
03538     if (*fn == '\0') fn = "/";
03539     /*@=branchstate@*/
03540     switch (urltype) {
03541     case URL_IS_HTTPS:  /* XXX WRONG WRONG WRONG */
03542     case URL_IS_HTTP:   /* XXX WRONG WRONG WRONG */
03543     case URL_IS_FTP:    /* XXX WRONG WRONG WRONG */
03544     case URL_IS_HKP:    /* XXX WRONG WRONG WRONG */
03545     case URL_IS_PATH:
03546     case URL_IS_UNKNOWN:
03547         if (Stat(fn, &buf)) {
03548             switch(errno) {
03549             case ENOENT:
03550             case EINVAL:
03551                 return 0;
03552             }
03553         }
03554         break;
03555     case URL_IS_DASH:
03556     default:
03557         return 0;
03558         /*@notreached@*/ break;
03559     }
03560 
03561     return 1;
03562 }
03563 
03564 static int rpmdbRemoveDatabase(const char * prefix,
03565                 const char * dbpath, int _dbapi)
03566         /*@globals h_errno, fileSystem, internalState @*/
03567         /*@modifies fileSystem, internalState @*/
03568 { 
03569     int i;
03570     char * filename;
03571     int xx;
03572 
03573     i = strlen(dbpath);
03574     /*@-bounds -branchstate@*/
03575     if (dbpath[i - 1] != '/') {
03576         filename = alloca(i);
03577         strcpy(filename, dbpath);
03578         filename[i] = '/';
03579         filename[i + 1] = '\0';
03580         dbpath = filename;
03581     }
03582     /*@=bounds =branchstate@*/
03583     
03584     filename = alloca(strlen(prefix) + strlen(dbpath) + 40);
03585 
03586     switch (_dbapi) {
03587     case 4:
03588         /*@fallthrough@*/
03589     case 3:
03590         if (dbiTags != NULL)
03591         for (i = 0; i < dbiTagsMax; i++) {
03592 /*@-boundsread@*/
03593             const char * base = tagName(dbiTags[i]);
03594 /*@=boundsread@*/
03595             sprintf(filename, "%s/%s/%s", prefix, dbpath, base);
03596             (void)rpmCleanPath(filename);
03597             if (!rpmioFileExists(filename))
03598                 continue;
03599             xx = unlink(filename);
03600         }
03601         for (i = 0; i < 16; i++) {
03602             sprintf(filename, "%s/%s/__db.%03d", prefix, dbpath, i);
03603             (void)rpmCleanPath(filename);
03604             if (!rpmioFileExists(filename))
03605                 continue;
03606             xx = unlink(filename);
03607         }
03608         break;
03609     case 2:
03610     case 1:
03611     case 0:
03612         break;
03613     }
03614 
03615     sprintf(filename, "%s/%s", prefix, dbpath);
03616     (void)rpmCleanPath(filename);
03617     xx = rmdir(filename);
03618 
03619     return 0;
03620 }
03621 
03622 static int rpmdbMoveDatabase(const char * prefix,
03623                 const char * olddbpath, int _olddbapi,
03624                 const char * newdbpath, /*@unused@*/ int _newdbapi)
03625         /*@globals h_errno, fileSystem, internalState @*/
03626         /*@modifies fileSystem, internalState @*/
03627 {
03628     int i;
03629     char * ofilename, * nfilename;
03630     struct stat * nst = alloca(sizeof(*nst));
03631     int rc = 0;
03632     int xx;
03633  
03634     i = strlen(olddbpath);
03635     /*@-branchstate@*/
03636     if (olddbpath[i - 1] != '/') {
03637         ofilename = alloca(i + 2);
03638         strcpy(ofilename, olddbpath);
03639         ofilename[i] = '/';
03640         ofilename[i + 1] = '\0';
03641         olddbpath = ofilename;
03642     }
03643     /*@=branchstate@*/
03644     
03645     i = strlen(newdbpath);
03646     /*@-branchstate@*/
03647     if (newdbpath[i - 1] != '/') {
03648         nfilename = alloca(i + 2);
03649         strcpy(nfilename, newdbpath);
03650         nfilename[i] = '/';
03651         nfilename[i + 1] = '\0';
03652         newdbpath = nfilename;
03653     }
03654     /*@=branchstate@*/
03655     
03656     ofilename = alloca(strlen(prefix) + strlen(olddbpath) + 40);
03657     nfilename = alloca(strlen(prefix) + strlen(newdbpath) + 40);
03658 
03659     switch (_olddbapi) {
03660     case 4:
03661         /* Fall through */
03662     case 3:
03663         if (dbiTags != NULL)
03664         for (i = 0; i < dbiTagsMax; i++) {
03665             const char * base;
03666             int rpmtag;
03667 
03668             /* Filter out temporary databases */
03669             if (isTemporaryDB((rpmtag = dbiTags[i])))
03670                 continue;
03671 
03672             base = tagName(rpmtag);
03673             sprintf(ofilename, "%s/%s/%s", prefix, olddbpath, base);
03674             (void)rpmCleanPath(ofilename);
03675             if (!rpmioFileExists(ofilename))
03676                 continue;
03677             sprintf(nfilename, "%s/%s/%s", prefix, newdbpath, base);
03678             (void)rpmCleanPath(nfilename);
03679 
03680             /*
03681              * Get uid/gid/mode/mtime. If old doesn't exist, use new.
03682              * XXX Yes, the variable names are backwards.
03683              */
03684             if (stat(nfilename, nst) < 0)
03685                 if (stat(ofilename, nst) < 0)
03686                     continue;
03687 
03688             if ((xx = rename(ofilename, nfilename)) != 0) {
03689                 rc = 1;
03690                 continue;
03691             }
03692             xx = chown(nfilename, nst->st_uid, nst->st_gid);
03693             xx = chmod(nfilename, (nst->st_mode & 07777));
03694             {   struct utimbuf stamp;
03695                 stamp.actime = nst->st_atime;
03696                 stamp.modtime = nst->st_mtime;
03697                 xx = utime(nfilename, &stamp);
03698             }
03699         }
03700         for (i = 0; i < 16; i++) {
03701             sprintf(ofilename, "%s/%s/__db.%03d", prefix, olddbpath, i);
03702             (void)rpmCleanPath(ofilename);
03703             if (rpmioFileExists(ofilename))
03704                 xx = unlink(ofilename);
03705             sprintf(nfilename, "%s/%s/__db.%03d", prefix, newdbpath, i);
03706             (void)rpmCleanPath(nfilename);
03707             if (rpmioFileExists(nfilename))
03708                 xx = unlink(nfilename);
03709         }
03710         break;
03711     case 2:
03712     case 1:
03713     case 0:
03714         break;
03715     }
03716 #ifdef  SQLITE_HACK_XXX
03717     if (rc || _olddbapi == _newdbapi)
03718         return rc;
03719 
03720     rc = rpmdbRemoveDatabase(prefix, newdbpath, _newdbapi);
03721 
03722 
03723     /* Remove /etc/rpm/macros.db1 configuration file if db3 rebuilt. */
03724     if (rc == 0 && _newdbapi == 1 && _olddbapi == 3) {
03725         const char * mdb1 = "/etc/rpm/macros.db1";
03726         struct stat st;
03727         if (!stat(mdb1, &st) && S_ISREG(st.st_mode) && !unlink(mdb1))
03728             rpmMessage(RPMMESS_DEBUG,
03729                 _("removing %s after successful db3 rebuild.\n"), mdb1);
03730     }
03731 #endif
03732     return rc;
03733 }
03734 
03735 int rpmdbRebuild(const char * prefix, rpmts ts,
03736                 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, const char ** msg))
03737         /*@globals _rebuildinprogress @*/
03738         /*@modifies _rebuildinprogress @*/
03739 {
03740     rpmdb olddb;
03741     const char * dbpath = NULL;
03742     const char * rootdbpath = NULL;
03743     rpmdb newdb;
03744     const char * newdbpath = NULL;
03745     const char * newrootdbpath = NULL;
03746     const char * tfn;
03747     int nocleanup = 1;
03748     int failed = 0;
03749     int removedir = 0;
03750     int rc = 0, xx;
03751     int _dbapi;
03752     int _dbapi_rebuild;
03753 
03754     /*@-branchstate@*/
03755     if (prefix == NULL) prefix = "/";
03756     /*@=branchstate@*/
03757 
03758     _dbapi = rpmExpandNumeric("%{_dbapi}");
03759     _dbapi_rebuild = rpmExpandNumeric("%{_dbapi_rebuild}");
03760 
03761     /*@-nullpass@*/
03762     tfn = rpmGetPath("%{?_dbpath}", NULL);
03763     /*@=nullpass@*/
03764 /*@-boundsread@*/
03765     if (!(tfn && tfn[0] != '\0'))
03766 /*@=boundsread@*/
03767     {
03768         rpmMessage(RPMMESS_DEBUG, _("no dbpath has been set"));
03769         rc = 1;
03770         goto exit;
03771     }
03772     dbpath = rootdbpath = rpmGetPath(prefix, tfn, NULL);
03773     if (!(prefix[0] == '/' && prefix[1] == '\0'))
03774         dbpath += strlen(prefix) - 1;
03775     tfn = _free(tfn);
03776 
03777     /*@-nullpass@*/
03778     tfn = rpmGetPath("%{?_dbpath_rebuild}", NULL);
03779     /*@=nullpass@*/
03780 /*@-boundsread@*/
03781     if (!(tfn && tfn[0] != '\0' && strcmp(tfn, dbpath)))
03782 /*@=boundsread@*/
03783     {
03784         char pidbuf[20];
03785         char *t;
03786         sprintf(pidbuf, "rebuilddb.%d", (int) getpid());
03787         t = xmalloc(strlen(dbpath) + strlen(pidbuf) + 1);
03788 /*@-boundswrite@*/
03789         (void)stpcpy(stpcpy(t, dbpath), pidbuf);
03790 /*@=boundswrite@*/
03791         tfn = _free(tfn);
03792         tfn = t;
03793         nocleanup = 0;
03794     }
03795     newdbpath = newrootdbpath = rpmGetPath(prefix, tfn, NULL);
03796     if (!(prefix[0] == '/' && prefix[1] == '\0'))
03797         newdbpath += strlen(prefix) - 1;
03798     tfn = _free(tfn);
03799 
03800     rpmMessage(RPMMESS_DEBUG, _("rebuilding database %s into %s\n"),
03801         rootdbpath, newrootdbpath);
03802 
03803     if (!access(newrootdbpath, F_OK)) {
03804         rpmError(RPMERR_MKDIR, _("temporary database %s already exists\n"),
03805               newrootdbpath);
03806         rc = 1;
03807         goto exit;
03808     }
03809 
03810     rpmMessage(RPMMESS_DEBUG, _("creating directory %s\n"), newrootdbpath);
03811     if (Mkdir(newrootdbpath, 0755)) {
03812         rpmError(RPMERR_MKDIR, _("creating directory %s: %s\n"),
03813               newrootdbpath, strerror(errno));
03814         rc = 1;
03815         goto exit;
03816     }
03817     removedir = 1;
03818 
03819     _rebuildinprogress = 0;
03820 
03821     rpmMessage(RPMMESS_DEBUG, _("opening old database with dbapi %d\n"),
03822                 _dbapi);
03823 /*@-boundswrite@*/
03824     if (openDatabase(prefix, dbpath, _dbapi, &olddb, O_RDONLY, 0644, 
03825                      RPMDB_FLAG_MINIMAL)) {
03826         rc = 1;
03827         goto exit;
03828     }
03829 /*@=boundswrite@*/
03830     _dbapi = olddb->db_api;
03831     _rebuildinprogress = 1;
03832     rpmMessage(RPMMESS_DEBUG, _("opening new database with dbapi %d\n"),
03833                 _dbapi_rebuild);
03834     (void) rpmDefineMacro(NULL, "_rpmdb_rebuild %{nil}", -1);
03835 /*@-boundswrite@*/
03836     if (openDatabase(prefix, newdbpath, _dbapi_rebuild, &newdb, O_RDWR | O_CREAT, 0644, 0)) {
03837         rc = 1;
03838         goto exit;
03839     }
03840 /*@=boundswrite@*/
03841 
03842     _rebuildinprogress = 0;
03843 
03844     _dbapi_rebuild = newdb->db_api;
03845     
03846     {   Header h = NULL;
03847         rpmdbMatchIterator mi;
03848 #define _RECNUM rpmdbGetIteratorOffset(mi)
03849 
03850         mi = rpmdbInitIterator(olddb, RPMDBI_PACKAGES, NULL, 0);
03851         if (ts && hdrchk)
03852             (void) rpmdbSetHdrChk(mi, ts, hdrchk);
03853 
03854         while ((h = rpmdbNextIterator(mi)) != NULL) {
03855 
03856             /* let's sanity check this record a bit, otherwise just skip it */
03857             if (!(headerIsEntry(h, RPMTAG_NAME) &&
03858                 headerIsEntry(h, RPMTAG_VERSION) &&
03859                 headerIsEntry(h, RPMTAG_RELEASE) &&
03860                 headerIsEntry(h, RPMTAG_BUILDTIME)))
03861             {
03862                 rpmError(RPMERR_INTERNAL,
03863                         _("header #%u in the database is bad -- skipping.\n"),
03864                         _RECNUM);
03865                 continue;
03866             }
03867 
03868             /* Filter duplicate entries ? (bug in pre rpm-3.0.4) */
03869             if (_db_filter_dups || newdb->db_filter_dups) {
03870                 const char * name, * version, * release;
03871                 int skip = 0;
03872 
03873                 (void) headerNVR(h, &name, &version, &release);
03874 
03875                 /*@-shadow@*/
03876                 {   rpmdbMatchIterator mi;
03877                     mi = rpmdbInitIterator(newdb, RPMTAG_NAME, name, 0);
03878                     (void) rpmdbSetIteratorRE(mi, RPMTAG_VERSION,
03879                                 RPMMIRE_DEFAULT, version);
03880                     (void) rpmdbSetIteratorRE(mi, RPMTAG_RELEASE,
03881                                 RPMMIRE_DEFAULT, release);
03882                     while (rpmdbNextIterator(mi)) {
03883                         skip = 1;
03884                         /*@innerbreak@*/ break;
03885                     }
03886                     mi = rpmdbFreeIterator(mi);
03887                 }
03888                 /*@=shadow@*/
03889 
03890                 if (skip)
03891                     continue;
03892             }
03893 
03894             /* Deleted entries are eliminated in legacy headers by copy. */
03895             {   Header nh = (headerIsEntry(h, RPMTAG_HEADERIMAGE)
03896                                 ? headerCopy(h) : NULL);
03897                 rc = rpmdbAdd(newdb, -1, (nh ? nh : h), ts, hdrchk);
03898                 nh = headerFree(nh);
03899             }
03900 
03901             if (rc) {
03902                 rpmError(RPMERR_INTERNAL,
03903                         _("cannot add record originally at %u\n"), _RECNUM);
03904                 failed = 1;
03905                 break;
03906             }
03907         }
03908 
03909         mi = rpmdbFreeIterator(mi);
03910 
03911     }
03912 
03913     xx = rpmdbClose(olddb);
03914     xx = rpmdbClose(newdb);
03915 
03916     if (failed) {
03917         rpmMessage(RPMMESS_NORMAL, _("failed to rebuild database: original database "
03918                 "remains in place\n"));
03919 
03920         xx = rpmdbRemoveDatabase(prefix, newdbpath, _dbapi_rebuild);
03921         rc = 1;
03922         goto exit;
03923     } else if (!nocleanup) {
03924         if (rpmdbMoveDatabase(prefix, newdbpath, _dbapi_rebuild, dbpath, _dbapi)) {
03925             rpmMessage(RPMMESS_ERROR, _("failed to replace old database with new "
03926                         "database!\n"));
03927             rpmMessage(RPMMESS_ERROR, _("replace files in %s with files from %s "
03928                         "to recover"), dbpath, newdbpath);
03929             rc = 1;
03930             goto exit;
03931         }
03932     }
03933     rc = 0;
03934 
03935 exit:
03936     if (removedir && !(rc == 0 && nocleanup)) {
03937         rpmMessage(RPMMESS_DEBUG, _("removing directory %s\n"), newrootdbpath);
03938         if (Rmdir(newrootdbpath))
03939             rpmMessage(RPMMESS_ERROR, _("failed to remove directory %s: %s\n"),
03940                         newrootdbpath, strerror(errno));
03941     }
03942     newrootdbpath = _free(newrootdbpath);
03943     rootdbpath = _free(rootdbpath);
03944 
03945     return rc;
03946 }

Generated on Thu Oct 25 09:23:16 2007 for rpm by  doxygen 1.5.1