7 #define _USE_COPY_LOAD /* XXX don't use DB_DBT_MALLOC (yet) */
11 #ifndef DYING /* XXX already in "system.h" */
17 #include <rpm/rpmtypes.h>
18 #include <rpm/rpmurl.h>
19 #include <rpm/rpmpgp.h>
20 #include <rpm/rpmpgp.h>
21 #include <rpm/rpmmacro.h>
22 #include <rpm/rpmsq.h>
23 #include <rpm/rpmstring.h>
24 #include <rpm/rpmfileutil.h>
25 #include <rpm/rpmds.h> /* XXX isInstallPreReq macro only */
26 #include <rpm/rpmlog.h>
27 #include <rpm/rpmdb.h>
30 #include "lib/rpmdb_internal.h"
31 #include "lib/fprint.h"
32 #include "lib/header_internal.h" /* XXX for headerSetInstance() */
37 static int _rebuildinprogress = 0;
38 static int _db_filter_dups = 0;
41 #define _DBI_PERMS 0644
50 /* XXX should dbitags be per-db instead? */
51 static struct dbiTags_s dbiTags = { NULL, 0, 0 };
53 /* Bit mask macros. */
54 typedef unsigned int __pbm_bits;
55 #define __PBM_NBITS (8 * sizeof (__pbm_bits))
56 #define __PBM_IX(d) ((d) / __PBM_NBITS)
57 #define __PBM_MASK(d) ((__pbm_bits) 1 << (((unsigned)(d)) % __PBM_NBITS))
61 #define __PBM_BITS(set) ((set)->bits)
63 #define PBM_FREE(s) _free(s);
64 #define PBM_SET(d, s) (__PBM_BITS (s)[__PBM_IX (d)] |= __PBM_MASK (d))
65 #define PBM_CLR(d, s) (__PBM_BITS (s)[__PBM_IX (d)] &= ~__PBM_MASK (d))
66 #define PBM_ISSET(d, s) ((__PBM_BITS (s)[__PBM_IX (d)] & __PBM_MASK (d)) != 0)
68 #define PBM_ALLOC(d) xcalloc(__PBM_IX (d) + 1, sizeof(__pbm_bits))
71 * Reallocate a bit map.
72 * @retval sp address of bit map pointer
73 * @retval odp no. of bits in map
74 * @param nd desired no. of bits
76 static inline pbm_set * PBM_REALLOC(pbm_set ** sp, int * odp, int nd)
82 nb = __PBM_IX(nd) + 1;
83 *sp = xrealloc(*sp, nb * sizeof(__pbm_bits));
84 for (i = __PBM_IX(*odp) + 1; i < nb; i++)
85 __PBM_BITS(*sp)[i] = 0;
92 * Return dbi index used for rpm tag.
93 * @param rpmtag rpm header tag
94 * @return dbi index, -1 on error
96 static int dbiTagToDbix(rpmTag rpmtag)
100 if (dbiTags.tags != NULL)
101 for (dbix = 0; dbix < dbiTags.max; dbix++) {
102 if (rpmtag == dbiTags.tags[dbix])
109 * Initialize database (index, tag) tuple from configuration.
111 static void dbiTagsInit(void)
113 static const char * const _dbiTagStr_default =
114 "Packages:Name:Basenames:Group:Requirename:Providename:Conflictname:ObsoleteName:Triggername:Dirnames:Requireversion:Provideversion:Installtid:Sigmd5:Sha1header:Filedigests:Depends:Pubkeys";
115 char * dbiTagStr = NULL;
120 if (dbiTags.tags != NULL && dbiTags.max > 0) {
124 dbiTagStr = rpmExpand("%{?_dbi_tags}", NULL);
125 if (!(dbiTagStr && *dbiTagStr)) {
126 dbiTagStr = _free(dbiTagStr);
127 dbiTagStr = xstrdup(_dbiTagStr_default);
130 /* Discard previous values. */
131 dbiTags.tags = _free(dbiTags.tags);
134 /* Always allocate package index */
135 dbiTags.tags = xcalloc(1, sizeof(*dbiTags.tags));
136 dbiTags.tags[dbiTags.max++] = RPMDBI_PACKAGES;
138 for (o = dbiTagStr; o && *o; o = oe) {
139 while (*o && risspace(*o))
143 for (oe = o; oe && *oe; oe++) {
146 if (oe[0] == ':' && !(oe[1] == '/' && oe[2] == '/'))
151 rpmtag = rpmTagGetValue(o);
152 if (rpmtag == RPMTAG_NOT_FOUND) {
153 rpmlog(RPMLOG_WARNING,
154 _("dbiTagsInit: unrecognized tag name: \"%s\" ignored\n"), o);
157 if (dbiTagToDbix(rpmtag) >= 0)
160 dbiTags.tags = xrealloc(dbiTags.tags, (dbiTags.max + 1) * sizeof(*dbiTags.tags)); /* XXX memory leak */
161 dbiTags.tags[dbiTags.max++] = rpmtag;
164 dbiTagStr = _free(dbiTagStr);
167 static void dbiTagsFree(void)
169 if (--dbiTags.nlink > 0) {
172 dbiTags.tags = _free(dbiTags.tags);
180 extern struct _dbiVec db3vec;
181 #define DB3vec &db3vec
186 #ifdef HAVE_SQLITE3_H
187 extern struct _dbiVec sqlitevec;
188 #define SQLITEvec &sqlitevec
190 #define SQLITEvec NULL
193 static struct _dbiVec * const mydbvecs[] = {
194 DB1vec, DB1vec, DB2vec, DB3vec, SQLITEvec, NULL
197 dbiIndex dbiOpen(rpmdb db, rpmTag rpmtag, unsigned int flags)
201 int _dbapi, _dbapi_rebuild, _dbapi_wanted;
207 dbix = dbiTagToDbix(rpmtag);
208 if (dbix < 0 || dbix >= dbiTags.max)
211 /* Is this index already open ? */
212 /* FIX: db->_dbi may be NULL */
213 if ((dbi = db->_dbi[dbix]) != NULL)
216 _dbapi_rebuild = rpmExpandNumeric("%{_dbapi_rebuild}");
217 if (_dbapi_rebuild < 1 || _dbapi_rebuild > 4)
219 /* _dbapi_wanted = (_rebuildinprogress ? -1 : db->db_api); */
220 _dbapi_wanted = (_rebuildinprogress ? _dbapi_rebuild : db->db_api);
222 switch (_dbapi_wanted) {
224 _dbapi = _dbapi_wanted;
225 if (_dbapi < 0 || _dbapi >= 5 || mydbvecs[_dbapi] == NULL) {
226 rpmlog(RPMLOG_ERR, _("dbiOpen: dbapi %d not available\n"), _dbapi);
231 rc = (*mydbvecs[_dbapi]->open) (db, rpmtag, &dbi);
233 static int _printed[32];
234 if (!_printed[dbix & 0x1f]++)
236 _("cannot open %s index using db%d - %s (%d)\n"),
237 rpmTagGetName(rpmtag), _dbapi,
238 (rc > 0 ? strerror(rc) : ""), rc);
244 while (_dbapi-- > 1) {
245 if (mydbvecs[_dbapi] == NULL)
249 rc = (*mydbvecs[_dbapi]->open) (db, rpmtag, &dbi);
254 static int _printed[32];
255 if (!_printed[dbix & 0x1f]++)
256 rpmlog(RPMLOG_ERR, _("cannot open %s index\n"),
257 rpmTagGetName(rpmtag));
261 if (db->db_api == -1 && _dbapi > 0)
266 /* We don't ever _REQUIRE_ conversion... */
268 #ifdef SQLITE_HACK_XXX
269 /* Require conversion. */
270 if (rc && _dbapi_wanted >= 0 && _dbapi != _dbapi_wanted && _dbapi_wanted == _dbapi_rebuild) {
271 rc = (_rebuildinprogress ? 0 : 1);
275 /* Suggest possible configuration */
276 if (_dbapi_wanted >= 0 && _dbapi != _dbapi_wanted) {
281 /* Suggest possible configuration */
282 if (_dbapi_wanted < 0 && _dbapi != _dbapi_rebuild) {
283 rc = (_rebuildinprogress ? 0 : 1);
289 if (dbi != NULL && rc == 0) {
290 db->_dbi[dbix] = dbi;
291 if (rpmtag == RPMDBI_PACKAGES && db->db_bits == NULL) {
293 if (!dbiStat(dbi, DB_FAST_STAT)) {
294 DB_HASH_STAT * hash = (DB_HASH_STAT *)dbi->dbi_stats;
296 db->db_nbits += hash->hash_nkeys;
298 db->db_bits = PBM_ALLOC(db->db_nbits);
306 /* FIX: db->_dbi may be NULL */
310 /* Retrieve (key,data) pair from index database. */
311 int dbiGet(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
315 assert((flags == DB_NEXT) || (key->data != NULL && key->size > 0));
316 (void) rpmswEnter(&dbi->dbi_rpmdb->db_getops, 0);
317 rc = (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags);
318 (void) rpmswExit(&dbi->dbi_rpmdb->db_getops, data->size);
322 /* Store (key,data) pair in index database. */
323 int dbiPut(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
327 assert(key->data != NULL && key->size > 0 && data->data != NULL && data->size > 0);
328 (void) rpmswEnter(&dbi->dbi_rpmdb->db_putops, (ssize_t) 0);
329 rc = (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags);
330 (void) rpmswExit(&dbi->dbi_rpmdb->db_putops, (ssize_t) data->size);
335 * Create and initialize item for index database set.
336 * @param hdrNum header instance in db
337 * @param tagNum tag index in header
340 static dbiIndexItem dbiIndexNewItem(unsigned int hdrNum, unsigned int tagNum)
342 dbiIndexItem rec = xcalloc(1, sizeof(*rec));
343 rec->hdrNum = hdrNum;
344 rec->tagNum = tagNum;
353 #define _DBSWAP(_a) \
355 { unsigned char _b, *_c = (_a).uc; \
356 _b = _c[3]; _c[3] = _c[0]; _c[0] = _b; \
357 _b = _c[2]; _c[2] = _c[1]; _c[1] = _b; \
362 * Ensure sufficient memory for nrecs of new records in dbiIndexSet.
363 * Allocate in power of two sizes to avoid memory fragmentation, so
364 * realloc is not always needed.
366 static inline void dbiGrowSet(dbiIndexSet set, unsigned int nrecs)
368 size_t need = (set->count + nrecs) * sizeof(*(set->recs));
369 size_t alloced = set->alloced ? set->alloced : 1 << 4;
371 while (alloced < need)
374 if (alloced != set->alloced) {
375 set->recs = xrealloc(set->recs, alloced);
376 set->alloced = alloced;
381 * Convert retrieved data to index set.
382 * @param dbi index database handle
383 * @param data retrieved data
384 * @retval setp (malloc'ed) index set
385 * @return 0 on success
387 static int dbt2set(dbiIndex dbi, DBT * data, dbiIndexSet * setp)
389 int _dbbyteswapped = dbiByteSwapped(dbi);
394 if (dbi == NULL || data == NULL || setp == NULL)
397 if ((sdbir = data->data) == NULL) {
402 set = xcalloc(1, sizeof(*set));
403 dbiGrowSet(set, data->size / dbi->dbi_jlen);
404 set->count = data->size / dbi->dbi_jlen;
406 switch (dbi->dbi_jlen) {
408 case 2*sizeof(int32_t):
409 for (i = 0; i < set->count; i++) {
410 union _dbswap hdrNum, tagNum;
412 memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
413 sdbir += sizeof(hdrNum.ui);
414 memcpy(&tagNum.ui, sdbir, sizeof(tagNum.ui));
415 sdbir += sizeof(tagNum.ui);
416 if (_dbbyteswapped) {
420 set->recs[i].hdrNum = hdrNum.ui;
421 set->recs[i].tagNum = tagNum.ui;
424 case 1*sizeof(int32_t):
425 for (i = 0; i < set->count; i++) {
426 union _dbswap hdrNum;
428 memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
429 sdbir += sizeof(hdrNum.ui);
430 if (_dbbyteswapped) {
433 set->recs[i].hdrNum = hdrNum.ui;
434 set->recs[i].tagNum = 0;
443 * Convert index set to database representation.
444 * @param dbi index database handle
445 * @param data retrieved data
446 * @param set index set
447 * @return 0 on success
449 static int set2dbt(dbiIndex dbi, DBT * data, dbiIndexSet set)
451 int _dbbyteswapped = dbiByteSwapped(dbi);
455 if (dbi == NULL || data == NULL || set == NULL)
458 data->size = set->count * (dbi->dbi_jlen);
459 if (data->size == 0) {
463 tdbir = data->data = xmalloc(data->size);
465 switch (dbi->dbi_jlen) {
467 case 2*sizeof(int32_t):
468 for (i = 0; i < set->count; i++) {
469 union _dbswap hdrNum, tagNum;
471 memset(&hdrNum, 0, sizeof(hdrNum));
472 memset(&tagNum, 0, sizeof(tagNum));
473 hdrNum.ui = set->recs[i].hdrNum;
474 tagNum.ui = set->recs[i].tagNum;
475 if (_dbbyteswapped) {
479 memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
480 tdbir += sizeof(hdrNum.ui);
481 memcpy(tdbir, &tagNum.ui, sizeof(tagNum.ui));
482 tdbir += sizeof(tagNum.ui);
485 case 1*sizeof(int32_t):
486 for (i = 0; i < set->count; i++) {
487 union _dbswap hdrNum;
489 memset(&hdrNum, 0, sizeof(hdrNum));
490 hdrNum.ui = set->recs[i].hdrNum;
491 if (_dbbyteswapped) {
494 memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
495 tdbir += sizeof(hdrNum.ui);
503 /* XXX assumes hdrNum is first int in dbiIndexItem */
504 static int hdrNumCmp(const void * one, const void * two)
506 const unsigned int * a = one, * b = two;
511 * Append element(s) to set of index database items.
512 * @param set set of index database items
513 * @param recs array of items to append to set
514 * @param nrecs number of items
515 * @param recsize size of an array item
516 * @param sortset should resulting set be sorted?
517 * @return 0 success, 1 failure (bad args)
519 static int dbiAppendSet(dbiIndexSet set, const void * recs,
520 int nrecs, size_t recsize, int sortset)
522 const char * rptr = recs;
523 size_t rlen = (recsize < sizeof(*(set->recs)))
524 ? recsize : sizeof(*(set->recs));
526 if (set == NULL || recs == NULL || nrecs <= 0 || recsize == 0)
529 dbiGrowSet(set, nrecs);
530 memset(set->recs + set->count, 0, nrecs * sizeof(*(set->recs)));
532 while (nrecs-- > 0) {
533 memcpy(set->recs + set->count, rptr, rlen);
538 if (sortset && set->count > 1)
539 qsort(set->recs, set->count, sizeof(*(set->recs)), hdrNumCmp);
545 * Remove element(s) from set of index database items.
546 * @param set set of index database items
547 * @param recs array of items to remove from set
548 * @param nrecs number of items
549 * @param recsize size of an array item
550 * @param sorted array is already sorted?
551 * @return 0 success, 1 failure (no items found)
553 static int dbiPruneSet(dbiIndexSet set, void * recs, int nrecs,
554 size_t recsize, int sorted)
558 unsigned int num = set->count;
559 unsigned int numCopied = 0;
561 assert(set->count > 0);
562 if (nrecs > 1 && !sorted)
563 qsort(recs, nrecs, recsize, hdrNumCmp);
565 for (from = 0; from < num; from++) {
566 if (bsearch(&set->recs[from], recs, nrecs, recsize, hdrNumCmp)) {
571 set->recs[to] = set->recs[from]; /* structure assignment */
575 return (numCopied == num);
578 /* XXX transaction.c */
579 unsigned int dbiIndexSetCount(dbiIndexSet set) {
583 /* XXX transaction.c */
584 unsigned int dbiIndexRecordOffset(dbiIndexSet set, int recno) {
585 return set->recs[recno].hdrNum;
588 /* XXX transaction.c */
589 unsigned int dbiIndexRecordFileNumber(dbiIndexSet set, int recno) {
590 return set->recs[recno].tagNum;
593 /* XXX transaction.c */
594 dbiIndexSet dbiFreeIndexSet(dbiIndexSet set) {
596 set->recs = _free(set->recs);
602 typedef struct miRE_s {
603 rpmTag tag; /*!< header tag */
604 rpmMireMode mode; /*!< pattern match mode */
605 char * pattern; /*!< pattern string */
606 int notmatch; /*!< like "grep -v" */
607 regex_t * preg; /*!< regex compiled pattern buffer */
608 int cflags; /*!< regcomp(3) flags */
609 int eflags; /*!< regexec(3) flags */
610 int fnflags; /*!< fnmatch(3) flags */
613 struct rpmdbMatchIterator_s {
614 rpmdbMatchIterator mi_next;
628 unsigned int mi_prevoffset; /* header instance (native endian) */
629 unsigned int mi_offset; /* header instance (native endian) */
630 unsigned int mi_filenum; /* tag element (native endian) */
634 rpmRC (*mi_hdrchk) (rpmts ts, const void * uh, size_t uc, char ** msg);
638 static rpmdb rpmdbRock;
640 static rpmdbMatchIterator rpmmiRock;
642 int rpmdbCheckTerminate(int terminate)
644 sigset_t newMask, oldMask;
645 static int terminating = 0;
647 if (terminating) return 0;
649 (void) sigfillset(&newMask); /* block all signals */
650 (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
652 if (rpmsqIsCaught(SIGINT) > 0
653 || rpmsqIsCaught(SIGQUIT) > 0
654 || rpmsqIsCaught(SIGHUP) > 0
655 || rpmsqIsCaught(SIGTERM) > 0
656 || rpmsqIsCaught(SIGPIPE) > 0
662 rpmdbMatchIterator mi;
664 while ((mi = rpmmiRock) != NULL) {
665 rpmmiRock = mi->mi_next;
667 mi = rpmdbFreeIterator(mi);
670 while ((db = rpmdbRock) != NULL) {
671 rpmdbRock = db->db_next;
673 (void) rpmdbClose(db);
676 sigprocmask(SIG_SETMASK, &oldMask, NULL);
680 int rpmdbCheckSignals(void)
682 if (rpmdbCheckTerminate(0)) {
683 rpmlog(RPMLOG_DEBUG, "Exiting on signal...\n");
690 * Block all signals, returning previous signal mask.
692 static int blockSignals(sigset_t * oldMask)
696 (void) sigfillset(&newMask); /* block all signals */
697 (void) sigprocmask(SIG_BLOCK, &newMask, oldMask);
698 (void) sigdelset(&newMask, SIGINT);
699 (void) sigdelset(&newMask, SIGQUIT);
700 (void) sigdelset(&newMask, SIGHUP);
701 (void) sigdelset(&newMask, SIGTERM);
702 (void) sigdelset(&newMask, SIGPIPE);
703 return sigprocmask(SIG_BLOCK, &newMask, NULL);
707 * Restore signal mask.
709 static int unblockSignals(sigset_t * oldMask)
711 (void) rpmdbCheckSignals();
712 return sigprocmask(SIG_SETMASK, oldMask, NULL);
716 #define _DB_HOME "%{_dbpath}"
719 #define _DB_PERMS 0644
722 #define _DB_ERRPFX "rpmdb"
724 static struct rpmdb_s const dbTemplate = {
725 _DB_ROOT, _DB_HOME, _DB_FLAGS, _DB_MODE, _DB_PERMS,
726 _DB_MAJOR, _DB_ERRPFX
729 static int isTemporaryDB(rpmTag rpmtag)
733 case RPMDBI_AVAILABLE:
745 rpmop rpmdbOp(rpmdb rpmdb, rpmdbOpX opx)
750 op = &rpmdb->db_getops;
753 op = &rpmdb->db_putops;
756 op = &rpmdb->db_delops;
764 int rpmdbSetChrootDone(rpmdb db, int chrootDone)
768 ochrootDone = db->db_chrootDone;
769 db->db_chrootDone = chrootDone;
774 int rpmdbOpenAll(rpmdb db)
779 if (db == NULL) return -2;
781 if (dbiTags.tags != NULL)
782 for (dbix = 0; dbix < dbiTags.max; dbix++) {
783 if (db->_dbi[dbix] != NULL)
785 /* Filter out temporary databases */
786 if (isTemporaryDB(dbiTags.tags[dbix]))
788 (void) dbiOpen(db, dbiTags.tags[dbix], db->db_flags);
793 int rpmdbCloseDBI(rpmdb db, rpmTag rpmtag)
798 if (db == NULL || db->_dbi == NULL || dbiTags.tags == NULL)
801 for (dbix = 0; dbix < dbiTags.max; dbix++) {
802 if (dbiTags.tags[dbix] != rpmtag)
804 if (db->_dbi[dbix] != NULL) {
806 /* FIX: double indirection. */
807 xx = dbiClose(db->_dbi[dbix], 0);
808 if (xx && rc == 0) rc = xx;
809 db->_dbi[dbix] = NULL;
816 /* XXX query.c, rpminstall.c, verify.c */
817 int rpmdbClose(rpmdb db)
826 (void) rpmdbUnlink(db, RPMDBG_M("rpmdbClose"));
832 for (dbix = db->db_ndbi; --dbix >= 0; ) {
834 if (db->_dbi[dbix] == NULL)
836 xx = dbiClose(db->_dbi[dbix], 0);
837 if (xx && rc == 0) rc = xx;
838 db->_dbi[dbix] = NULL;
840 db->db_errpfx = _free(db->db_errpfx);
841 db->db_root = _free(db->db_root);
842 db->db_home = _free(db->db_home);
843 db->db_bits = PBM_FREE(db->db_bits);
844 db->_dbi = _free(db->_dbi);
847 while ((next = *prev) != NULL && next != db)
848 prev = &next->db_next;
850 *prev = next->db_next;
851 next->db_next = NULL;
859 (void) rpmsqEnable(-SIGHUP, NULL);
860 (void) rpmsqEnable(-SIGINT, NULL);
861 (void) rpmsqEnable(-SIGTERM,NULL);
862 (void) rpmsqEnable(-SIGQUIT,NULL);
863 (void) rpmsqEnable(-SIGPIPE,NULL);
867 int rpmdbSync(rpmdb db)
872 if (db == NULL) return 0;
873 for (dbix = 0; dbix < db->db_ndbi; dbix++) {
875 if (db->_dbi[dbix] == NULL)
877 if (db->_dbi[dbix]->dbi_no_dbsync)
879 xx = dbiSync(db->_dbi[dbix], 0);
880 if (xx && rc == 0) rc = xx;
885 /* FIX: dbTemplate structure assignment */
887 rpmdb newRpmdb(const char * root,
889 int mode, int perms, int flags)
891 rpmdb db = xcalloc(sizeof(*db), 1);
892 const char * epfx = _DB_ERRPFX;
893 static int _initialized = 0;
896 _db_filter_dups = rpmExpandNumeric("%{_filterdbdups}");
900 *db = dbTemplate; /* structure assignment */
904 if (!(perms & 0600)) perms = 0644; /* XXX sanity */
906 if (mode >= 0) db->db_mode = mode;
907 if (perms >= 0) db->db_perms = perms;
908 if (flags >= 0) db->db_flags = flags;
911 db->db_root = rpmGetPath(root, NULL);
913 db->db_root = rpmGetPath(_DB_ROOT, NULL);
915 db->db_home = rpmGetPath( (home && *home ? home : _DB_HOME), NULL);
916 if (!(db->db_home && db->db_home[0] != '%')) {
917 rpmlog(RPMLOG_ERR, _("no dbpath has been set\n"));
918 db->db_root = _free(db->db_root);
919 db->db_home = _free(db->db_home);
923 db->db_errpfx = rpmExpand( (epfx && *epfx ? epfx : _DB_ERRPFX), NULL);
924 db->db_remove_env = 0;
925 db->db_filter_dups = _db_filter_dups;
926 db->db_ndbi = dbiTags.max;
927 db->_dbi = xcalloc(db->db_ndbi, sizeof(*db->_dbi));
929 return rpmdbLink(db, RPMDBG_M("rpmdbCreate"));
932 static int openDatabase(const char * prefix,
934 int _dbapi, rpmdb *dbp,
935 int mode, int perms, int flags)
939 int justCheck = flags & RPMDB_FLAG_JUSTCHECK;
940 int minimal = flags & RPMDB_FLAG_MINIMAL;
944 /* Insure that _dbapi has one of -1, 1, 2, or 3 */
945 if (_dbapi < -1 || _dbapi > 4)
952 if ((mode & O_ACCMODE) == O_WRONLY)
955 db = newRpmdb(prefix, dbpath, mode, perms, flags);
959 (void) rpmsqEnable(SIGHUP, NULL);
960 (void) rpmsqEnable(SIGINT, NULL);
961 (void) rpmsqEnable(SIGTERM,NULL);
962 (void) rpmsqEnable(SIGQUIT,NULL);
963 (void) rpmsqEnable(SIGPIPE,NULL);
970 if (dbiTags.tags != NULL)
971 for (dbix = 0; rc == 0 && dbix < dbiTags.max; dbix++) {
975 /* Filter out temporary databases */
976 if (isTemporaryDB((rpmtag = dbiTags.tags[dbix])))
979 dbi = dbiOpen(db, rpmtag, 0);
986 case RPMDBI_PACKAGES:
987 if (dbi == NULL) rc |= 1;
989 /* XXX open only Packages, indices created on the fly. */
995 if (dbi == NULL) rc |= 1;
1006 if (rc || justCheck || dbp == NULL)
1007 xx = rpmdbClose(db);
1009 db->db_next = rpmdbRock;
1017 rpmdb rpmdbUnlink(rpmdb db, const char * msg)
1020 fprintf(stderr, "--> db %p -- %d %s\n", db, db->nrefs, msg);
1025 rpmdb rpmdbLink(rpmdb db, const char * msg)
1029 fprintf(stderr, "--> db %p ++ %d %s\n", db, db->nrefs, msg);
1033 /* XXX python/rpmmodule.c */
1034 int rpmdbOpen (const char * prefix, rpmdb *dbp, int mode, int perms)
1036 int _dbapi = rpmExpandNumeric("%{_dbapi}");
1037 return openDatabase(prefix, NULL, _dbapi, dbp, mode, perms, 0);
1040 int rpmdbInit (const char * prefix, int perms)
1043 int _dbapi = rpmExpandNumeric("%{_dbapi}");
1046 rc = openDatabase(prefix, NULL, _dbapi, &db, (O_CREAT | O_RDWR),
1047 perms, RPMDB_FLAG_JUSTCHECK);
1050 xx = rpmdbOpenAll(db);
1051 if (xx && rc == 0) rc = xx;
1052 xx = rpmdbClose(db);
1053 if (xx && rc == 0) rc = xx;
1059 int rpmdbVerify(const char * prefix)
1062 int _dbapi = rpmExpandNumeric("%{_dbapi}");
1065 rc = openDatabase(prefix, NULL, _dbapi, &db, O_RDONLY, 0644, 0);
1070 rc = rpmdbOpenAll(db);
1072 for (dbix = db->db_ndbi; --dbix >= 0; ) {
1073 if (db->_dbi[dbix] == NULL)
1075 /* FIX: double indirection. */
1076 xx = dbiVerify(db->_dbi[dbix], 0);
1077 if (xx && rc == 0) rc = xx;
1078 db->_dbi[dbix] = NULL;
1081 /* FIX: db->_dbi[] may be NULL. */
1082 xx = rpmdbClose(db);
1083 if (xx && rc == 0) rc = xx;
1090 * Find file matches in database.
1091 * @param db rpm database
1096 * @return 0 on success, 1 on not found, -2 on error
1098 static int rpmdbFindByFile(rpmdb db, const char * filespec,
1099 DBT * key, DBT * data, dbiIndexSet * matches)
1101 char * dirName = NULL;
1102 const char * baseName;
1103 fingerPrintCache fpc = NULL;
1105 dbiIndex dbi = NULL;
1107 dbiIndexSet allMatches = NULL;
1108 dbiIndexItem rec = NULL;
1110 int rc = -2; /* assume error */
1114 if (filespec == NULL) return rc; /* nothing alloced yet */
1116 if ((baseName = strrchr(filespec, '/')) != NULL) {
1117 size_t len = baseName - filespec + 1;
1118 dirName = strncpy(xmalloc(len + 1), filespec, len);
1119 dirName[len] = '\0';
1122 dirName = xstrdup("");
1123 baseName = filespec;
1125 if (baseName == NULL)
1128 dbi = dbiOpen(db, RPMTAG_BASENAMES, 0);
1131 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
1133 key->data = (void *) baseName;
1134 key->size = strlen(baseName);
1136 key->size++; /* XXX "/" fixup. */
1138 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
1141 _("error(%d) getting \"%s\" records from %s index\n"),
1142 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
1146 (void) dbt2set(dbi, data, &allMatches);
1148 xx = dbiCclose(dbi, dbcursor, 0);
1153 if (rc || allMatches == NULL) goto exit;
1155 *matches = xcalloc(1, sizeof(**matches));
1156 rec = dbiIndexNewItem(0, 0);
1157 fpc = fpCacheCreate(allMatches->count);
1158 fp1 = fpLookup(fpc, dirName, baseName, 1);
1161 while (i < allMatches->count) {
1162 struct rpmtd_s bn, dn, di;
1163 const char ** baseNames, ** dirNames;
1164 uint32_t * dirIndexes;
1165 unsigned int offset = dbiIndexRecordOffset(allMatches, i);
1166 unsigned int prevoff;
1169 { rpmdbMatchIterator mi;
1170 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &offset, sizeof(offset));
1171 h = rpmdbNextIterator(mi);
1174 mi = rpmdbFreeIterator(mi);
1182 headerGet(h, RPMTAG_BASENAMES, &bn, HEADERGET_MINMEM);
1183 headerGet(h, RPMTAG_DIRNAMES, &dn, HEADERGET_MINMEM);
1184 headerGet(h, RPMTAG_DIRINDEXES, &di, HEADERGET_MINMEM);
1185 baseNames = bn.data;
1187 dirIndexes = di.data;
1191 int num = dbiIndexRecordFileNumber(allMatches, i);
1193 fp2 = fpLookup(fpc, dirNames[dirIndexes[num]], baseNames[num], 1);
1194 if (FP_EQUAL(fp1, fp2)) {
1195 rec->hdrNum = dbiIndexRecordOffset(allMatches, i);
1196 rec->tagNum = dbiIndexRecordFileNumber(allMatches, i);
1197 xx = dbiAppendSet(*matches, rec, 1, sizeof(*rec), 0);
1202 if (i < allMatches->count)
1203 offset = dbiIndexRecordOffset(allMatches, i);
1204 } while (i < allMatches->count && offset == prevoff);
1215 if ((*matches)->count == 0) {
1216 *matches = dbiFreeIndexSet(*matches);
1223 dbiFreeIndexSet(allMatches);
1228 /* XXX python/upgrade.c, install.c, uninstall.c */
1229 int rpmdbCountPackages(rpmdb db, const char * name)
1231 DBC * dbcursor = NULL;
1241 memset(&key, 0, sizeof(key));
1242 memset(&data, 0, sizeof(data));
1244 dbi = dbiOpen(db, RPMTAG_NAME, 0);
1248 key.data = (void *) name;
1249 key.size = strlen(name);
1251 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
1252 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
1254 xx = dbiCclose(dbi, dbcursor, 0);
1258 if (rc == 0) { /* success */
1259 dbiIndexSet matches;
1260 /* FIX: matches might be NULL */
1262 (void) dbt2set(dbi, &data, &matches);
1264 rc = dbiIndexSetCount(matches);
1265 matches = dbiFreeIndexSet(matches);
1268 if (rc == DB_NOTFOUND) { /* not found */
1270 } else { /* error */
1272 _("error(%d) getting \"%s\" records from %s index\n"),
1273 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
1278 xx = dbiCclose(dbi, dbcursor, 0);
1286 * Attempt partial matches on name[-version[-release]] strings.
1287 * @param dbi index database handle (always RPMTAG_NAME)
1288 * @param dbcursor index database cursor
1289 * @param key search key/length/flags
1290 * @param data search data/length/flags
1291 * @param name package name
1292 * @param version package version (can be a pattern)
1293 * @param release package release (can be a pattern)
1294 * @retval matches set of header instances that match
1295 * @return RPMRC_OK on match, RPMRC_NOMATCH or RPMRC_FAIL
1297 static rpmRC dbiFindMatches(dbiIndex dbi, DBC * dbcursor,
1298 DBT * key, DBT * data,
1300 const char * version,
1301 const char * release,
1302 dbiIndexSet * matches)
1304 unsigned int gotMatches = 0;
1308 key->data = (void *) name;
1309 key->size = strlen(name);
1311 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
1313 if (rc == 0) { /* success */
1314 (void) dbt2set(dbi, data, matches);
1315 if (version == NULL && release == NULL)
1318 if (rc == DB_NOTFOUND) { /* not found */
1319 return RPMRC_NOTFOUND;
1320 } else { /* error */
1322 _("error(%d) getting \"%s\" records from %s index\n"),
1323 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
1327 /* Make sure the version and release match. */
1328 for (i = 0; i < dbiIndexSetCount(*matches); i++) {
1329 unsigned int recoff = dbiIndexRecordOffset(*matches, i);
1330 rpmdbMatchIterator mi;
1336 mi = rpmdbInitIterator(dbi->dbi_rpmdb,
1337 RPMDBI_PACKAGES, &recoff, sizeof(recoff));
1339 /* Set iterator selectors for version/release if available. */
1341 rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, version))
1347 rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, release))
1353 h = rpmdbNextIterator(mi);
1355 (*matches)->recs[gotMatches++] = (*matches)->recs[i];
1357 (*matches)->recs[i].hdrNum = 0;
1358 mi = rpmdbFreeIterator(mi);
1362 (*matches)->count = gotMatches;
1365 rc = RPMRC_NOTFOUND;
1368 /* FIX: double indirection */
1369 if (rc && matches && *matches)
1370 *matches = dbiFreeIndexSet(*matches);
1375 * Lookup by name, name-version, and finally by name-version-release.
1376 * Both version and release can be patterns.
1377 * @todo Name must be an exact match, as name is a db key.
1378 * @param dbi index database handle (always RPMTAG_NAME)
1379 * @param dbcursor index database cursor
1380 * @param key search key/length/flags
1381 * @param data search data/length/flags
1382 * @param arg name[-version[-release]] string
1383 * @retval matches set of header instances that match
1384 * @return RPMRC_OK on match, RPMRC_NOMATCH or RPMRC_FAIL
1386 static rpmRC dbiFindByLabel(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
1387 const char * arg, dbiIndexSet * matches)
1389 const char * release;
1396 if (arg == NULL || strlen(arg) == 0) return RPMRC_NOTFOUND;
1398 /* did they give us just a name? */
1399 rc = dbiFindMatches(dbi, dbcursor, key, data, arg, NULL, NULL, matches);
1400 if (rc != RPMRC_NOTFOUND) return rc;
1402 /* FIX: double indirection */
1403 *matches = dbiFreeIndexSet(*matches);
1405 /* maybe a name and a release */
1406 localarg = xmalloc(strlen(arg) + 1);
1407 s = stpcpy(localarg, arg);
1411 for (s -= 1; s > localarg; s--) {
1417 if (c != '[') brackets = 0;
1421 if (!brackets && *s == '-')
1425 /* FIX: *matches may be NULL. */
1426 if (s == localarg) {
1427 rc = RPMRC_NOTFOUND;
1432 rc = dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, NULL, matches);
1433 if (rc != RPMRC_NOTFOUND) goto exit;
1435 /* FIX: double indirection */
1436 *matches = dbiFreeIndexSet(*matches);
1438 /* how about name-version-release? */
1444 for (; s > localarg; s--) {
1450 if (c != '[') brackets = 0;
1454 if (!brackets && *s == '-')
1458 if (s == localarg) {
1459 rc = RPMRC_NOTFOUND;
1464 /* FIX: *matches may be NULL. */
1465 rc = dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, release, matches);
1472 * Rewrite a header into packages (if necessary) and free the header.
1473 * Note: this is called from a markReplacedFiles iteration, and *must*
1474 * preserve the "join key" (i.e. offset) for the header.
1475 * @param mi database iterator
1476 * @param dbi index database handle
1477 * @return 0 on success
1479 static int miFreeHeader(rpmdbMatchIterator mi, dbiIndex dbi)
1483 if (mi == NULL || mi->mi_h == NULL)
1486 if (dbi && mi->mi_dbc && mi->mi_modified && mi->mi_prevoffset) {
1487 DBT * key = &mi->mi_key;
1488 DBT * data = &mi->mi_data;
1489 sigset_t signalMask;
1490 rpmRC rpmrc = RPMRC_NOTFOUND;
1493 key->data = (void *) &mi->mi_prevoffset;
1494 key->size = sizeof(mi->mi_prevoffset);
1495 data->data = headerUnload(mi->mi_h);
1496 data->size = headerSizeof(mi->mi_h, HEADER_MAGIC_NO);
1498 /* Check header digest/signature on blob export (if requested). */
1499 if (mi->mi_hdrchk && mi->mi_ts) {
1503 rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, data->data, data->size, &msg);
1504 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
1505 rpmlog(lvl, "%s h#%8u %s",
1506 (rpmrc == RPMRC_FAIL ? _("miFreeHeader: skipping") : "write"),
1507 mi->mi_prevoffset, (msg ? msg : "\n"));
1511 if (data->data != NULL && rpmrc != RPMRC_FAIL) {
1512 (void) blockSignals(&signalMask);
1513 rc = dbiPut(dbi, mi->mi_dbc, key, data, DB_KEYLAST);
1516 _("error(%d) storing record #%d into %s\n"),
1517 rc, mi->mi_prevoffset, rpmTagGetName(dbi->dbi_rpmtag));
1519 xx = dbiSync(dbi, 0);
1520 (void) unblockSignals(&signalMask);
1522 data->data = _free(data->data);
1526 mi->mi_h = headerFree(mi->mi_h);
1531 rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
1533 rpmdbMatchIterator * prev, next;
1542 while ((next = *prev) != NULL && next != mi)
1543 prev = &next->mi_next;
1545 *prev = next->mi_next;
1546 next->mi_next = NULL;
1549 dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
1550 if (dbi == NULL) /* XXX can't happen */
1553 xx = miFreeHeader(mi, dbi);
1556 xx = dbiCclose(dbi, mi->mi_dbc, 0);
1559 if (mi->mi_re != NULL)
1560 for (i = 0; i < mi->mi_nre; i++) {
1561 miRE mire = mi->mi_re + i;
1562 mire->pattern = _free(mire->pattern);
1563 if (mire->preg != NULL) {
1564 regfree(mire->preg);
1565 mire->preg = _free(mire->preg);
1568 mi->mi_re = _free(mi->mi_re);
1570 mi->mi_set = dbiFreeIndexSet(mi->mi_set);
1571 mi->mi_keyp = _free(mi->mi_keyp);
1572 mi->mi_db = rpmdbUnlink(mi->mi_db, RPMDBG_M("matchIterator"));
1576 (void) rpmdbCheckSignals();
1581 unsigned int rpmdbGetIteratorOffset(rpmdbMatchIterator mi) {
1582 return (mi ? mi->mi_offset : 0);
1585 unsigned int rpmdbGetIteratorFileNum(rpmdbMatchIterator mi) {
1586 return (mi ? mi->mi_filenum : 0);
1589 int rpmdbGetIteratorCount(rpmdbMatchIterator mi) {
1590 return (mi && mi->mi_set ? mi->mi_set->count : 0);
1594 * Return pattern match.
1595 * @param mire match iterator regex
1596 * @param val value to match
1597 * @return 0 if pattern matches, >0 on nomatch, <0 on error
1599 static int miregexec(miRE mire, const char * val)
1603 switch (mire->mode) {
1604 case RPMMIRE_STRCMP:
1605 rc = (!rstreq(mire->pattern, val));
1607 case RPMMIRE_DEFAULT:
1609 rc = regexec(mire->preg, val, 0, NULL, mire->eflags);
1610 if (rc && rc != REG_NOMATCH) {
1612 (void) regerror(rc, mire->preg, msg, sizeof(msg)-1);
1613 msg[sizeof(msg)-1] = '\0';
1614 rpmlog(RPMLOG_ERR, _("%s: regexec failed: %s\n"),
1615 mire->pattern, msg);
1620 rc = fnmatch(mire->pattern, val, mire->fnflags);
1621 if (rc && rc != FNM_NOMATCH)
1633 * Compare iterator selectors by rpm tag (qsort/bsearch).
1634 * @param a 1st iterator selector
1635 * @param b 2nd iterator selector
1636 * @return result of comparison
1638 static int mireCmp(const void * a, const void * b)
1640 const miRE mireA = (const miRE) a;
1641 const miRE mireB = (const miRE) b;
1642 return (mireA->tag - mireB->tag);
1646 * Copy pattern, escaping for appropriate mode.
1647 * @param tag rpm tag
1648 * @retval modep type of pattern match
1649 * @param pattern pattern to duplicate
1650 * @return duplicated pattern
1652 static char * mireDup(rpmTag tag, rpmMireMode *modep,
1653 const char * pattern)
1664 case RPMMIRE_DEFAULT:
1665 if (tag == RPMTAG_DIRNAMES || tag == RPMTAG_BASENAMES) {
1666 *modep = RPMMIRE_GLOB;
1667 pat = xstrdup(pattern);
1671 nb = strlen(pattern) + sizeof("^$");
1673 /* Find no. of bytes needed for pattern. */
1674 /* periods and plusses are escaped, splats become '.*' */
1677 for (s = pattern; *s != '\0'; s++) {
1682 if (!brackets) nb++;
1691 if (c != '[') brackets = 0;
1697 pat = t = xmalloc(nb);
1699 if (pattern[0] != '^') *t++ = '^';
1701 /* Copy pattern, escaping periods, prefixing splats with period. */
1704 for (s = pattern; *s != '\0'; s++, t++) {
1708 if (!brackets) *t++ = '\\';
1711 if (!brackets) *t++ = '.';
1720 if (c != '[') brackets = 0;
1726 if (s > pattern && s[-1] != '$') *t++ = '$';
1728 *modep = RPMMIRE_REGEX;
1730 case RPMMIRE_STRCMP:
1733 pat = xstrdup(pattern);
1740 int rpmdbSetIteratorRE(rpmdbMatchIterator mi, rpmTag tag,
1741 rpmMireMode mode, const char * pattern)
1743 static rpmMireMode defmode = (rpmMireMode)-1;
1745 char * allpat = NULL;
1747 regex_t * preg = NULL;
1753 if (defmode == (rpmMireMode)-1) {
1754 char *t = rpmExpand("%{?_query_selector_match}", NULL);
1756 if (*t == '\0' || rstreq(t, "default"))
1757 defmode = RPMMIRE_DEFAULT;
1758 else if (rstreq(t, "strcmp"))
1759 defmode = RPMMIRE_STRCMP;
1760 else if (rstreq(t, "regex"))
1761 defmode = RPMMIRE_REGEX;
1762 else if (rstreq(t, "glob"))
1763 defmode = RPMMIRE_GLOB;
1765 defmode = RPMMIRE_DEFAULT;
1769 if (mi == NULL || pattern == NULL)
1772 /* Leading '!' inverts pattern match sense, like "grep -v". */
1773 if (*pattern == '!') {
1778 allpat = mireDup(tag, &mode, pattern);
1780 if (mode == RPMMIRE_DEFAULT)
1784 case RPMMIRE_DEFAULT:
1785 case RPMMIRE_STRCMP:
1788 preg = xcalloc(1, sizeof(*preg));
1789 cflags = (REG_EXTENDED | REG_NOSUB);
1790 rc = regcomp(preg, allpat, cflags);
1793 (void) regerror(rc, preg, msg, sizeof(msg)-1);
1794 msg[sizeof(msg)-1] = '\0';
1795 rpmlog(RPMLOG_ERR, _("%s: regcomp failed: %s\n"), allpat, msg);
1799 fnflags = FNM_PATHNAME | FNM_PERIOD;
1807 /* FIX: mire has kept values */
1808 allpat = _free(allpat);
1816 mi->mi_re = xrealloc(mi->mi_re, (mi->mi_nre + 1) * sizeof(*mi->mi_re));
1817 mire = mi->mi_re + mi->mi_nre;
1822 mire->pattern = allpat;
1823 mire->notmatch = notmatch;
1825 mire->cflags = cflags;
1826 mire->eflags = eflags;
1827 mire->fnflags = fnflags;
1830 qsort(mi->mi_re, mi->mi_nre, sizeof(*mi->mi_re), mireCmp);
1836 * Return iterator selector match.
1837 * @param mi rpm database iterator
1838 * @return 1 if header should be skipped
1840 static int mireSkip (const rpmdbMatchIterator mi)
1848 if (mi->mi_h == NULL) /* XXX can't happen */
1852 * Apply tag tests, implicitly "||" for multiple patterns/values of a
1853 * single tag, implicitly "&&" between multiple tag patterns.
1855 if ((mire = mi->mi_re) != NULL)
1856 for (int i = 0; i < mi->mi_nre; i++, mire++) {
1860 if (!headerGet(mi->mi_h, mire->tag, &td, HEADERGET_MINMEM)) {
1861 if (mire->tag != RPMTAG_EPOCH) {
1865 /* "is package already installed" checks rely on this behavior */
1867 td.type = RPM_INT32_TYPE;
1871 anymatch = 0; /* no matches yet */
1874 while (rpmtdNext(&td) >= 0) {
1875 char *str = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1877 rc = miregexec(mire, str);
1878 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
1883 if ((i+1) < mi->mi_nre && mire[0].tag == mire[1].tag) {
1897 return (ntags == nmatches ? 0 : 1);
1900 int rpmdbSetIteratorRewrite(rpmdbMatchIterator mi, int rewrite)
1905 rc = (mi->mi_cflags & DB_WRITECURSOR) ? 1 : 0;
1907 mi->mi_cflags |= DB_WRITECURSOR;
1909 mi->mi_cflags &= ~DB_WRITECURSOR;
1913 int rpmdbSetIteratorModified(rpmdbMatchIterator mi, int modified)
1918 rc = mi->mi_modified;
1919 mi->mi_modified = modified;
1923 int rpmdbSetHdrChk(rpmdbMatchIterator mi, rpmts ts,
1924 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
1929 /* XXX forward linkage prevents rpmtsLink */
1931 mi->mi_hdrchk = hdrchk;
1936 /* FIX: mi->mi_key.data may be NULL */
1937 Header rpmdbNextIterator(rpmdbMatchIterator mi)
1952 dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
1957 * Cursors are per-iterator, not per-dbi, so get a cursor for the
1958 * iterator on 1st call. If the iteration is to rewrite headers, and the
1959 * CDB model is used for the database, then the cursor needs to
1960 * marked with DB_WRITECURSOR as well.
1962 if (mi->mi_dbc == NULL)
1963 xx = dbiCopen(dbi, dbi->dbi_txnid, &mi->mi_dbc, mi->mi_cflags);
1966 memset(key, 0, sizeof(*key));
1967 data = &mi->mi_data;
1968 memset(data, 0, sizeof(*data));
1975 union _dbswap mi_offset;
1978 if (!(mi->mi_setx < mi->mi_set->count))
1980 mi->mi_offset = dbiIndexRecordOffset(mi->mi_set, mi->mi_setx);
1981 mi->mi_filenum = dbiIndexRecordFileNumber(mi->mi_set, mi->mi_setx);
1982 mi_offset.ui = mi->mi_offset;
1983 if (dbiByteSwapped(dbi) == 1)
1986 keylen = sizeof(mi_offset.ui);
1989 key->data = keyp = (void *)mi->mi_keyp;
1990 key->size = keylen = mi->mi_keylen;
1993 #if !defined(_USE_COPY_LOAD)
1994 data->flags |= DB_DBT_MALLOC;
1996 rc = dbiGet(dbi, mi->mi_dbc, key, data,
1997 (key->data == NULL ? DB_NEXT : DB_SET));
2005 * If we got the next key, save the header instance number.
2007 * For db3 Packages, instance 0 (i.e. mi->mi_setx == 0) is the
2008 * largest header instance in the database, and should be
2011 if (keyp && mi->mi_setx && rc == 0) {
2012 memcpy(&mi_offset, keyp, sizeof(mi_offset.ui));
2013 if (dbiByteSwapped(dbi) == 1)
2015 mi->mi_offset = mi_offset.ui;
2018 /* Terminate on error or end of keys */
2019 if (rc || (mi->mi_setx && mi->mi_offset == 0))
2023 } while (mi->mi_offset == 0);
2025 /* If next header is identical, return it now. */
2026 if (mi->mi_prevoffset && mi->mi_offset == mi->mi_prevoffset)
2029 /* Retrieve next header blob for index iterator. */
2033 #if !defined(_USE_COPY_LOAD)
2034 data->flags |= DB_DBT_MALLOC;
2036 rc = dbiGet(dbi, mi->mi_dbc, key, data, DB_SET);
2046 /* Rewrite current header (if necessary) and unlink. */
2047 xx = miFreeHeader(mi, dbi);
2049 /* Is this the end of the iteration? */
2053 /* Check header digest/signature once (if requested). */
2054 if (mi->mi_hdrchk && mi->mi_ts) {
2055 rpmRC rpmrc = RPMRC_NOTFOUND;
2057 /* Don't bother re-checking a previously read header. */
2058 if (mi->mi_db->db_bits) {
2061 set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
2062 &mi->mi_db->db_nbits, mi->mi_offset);
2063 if (PBM_ISSET(mi->mi_offset, set))
2067 /* If blob is unchecked, check blob import consistency now. */
2068 if (rpmrc != RPMRC_OK) {
2072 rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, uh, uhlen, &msg);
2073 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
2074 rpmlog(lvl, "%s h#%8u %s",
2075 (rpmrc == RPMRC_FAIL ? _("rpmdbNextIterator: skipping") : " read"),
2076 mi->mi_offset, (msg ? msg : "\n"));
2079 /* Mark header checked. */
2080 if (mi->mi_db && mi->mi_db->db_bits && rpmrc == RPMRC_OK) {
2083 set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
2084 &mi->mi_db->db_nbits, mi->mi_offset);
2085 PBM_SET(mi->mi_offset, set);
2088 /* Skip damaged and inconsistent headers. */
2089 if (rpmrc == RPMRC_FAIL)
2094 /* Did the header blob load correctly? */
2095 #if !defined(_USE_COPY_LOAD)
2096 mi->mi_h = headerLoad(uh);
2098 mi->mi_h = headerCopyLoad(uh);
2100 if (mi->mi_h == NULL || !headerIsEntry(mi->mi_h, RPMTAG_NAME)) {
2102 _("rpmdb: damaged header #%u retrieved -- skipping.\n"),
2108 * Skip this header if iterator selector (if any) doesn't match.
2111 /* XXX hack, can't restart with Packages locked on single instance. */
2112 if (mi->mi_set || mi->mi_keyp == NULL)
2116 headerSetInstance(mi->mi_h, mi->mi_offset);
2118 mi->mi_prevoffset = mi->mi_offset;
2119 mi->mi_modified = 0;
2125 * sort the iterator by (recnum, filenum)
2126 * Return database iterator.
2127 * @param mi rpm database iterator
2129 void rpmdbSortIterator(rpmdbMatchIterator mi)
2131 if (mi && mi->mi_set && mi->mi_set->recs && mi->mi_set->count > 0) {
2133 * mergesort is much (~10x with lots of identical basenames) faster
2134 * than pure quicksort, but glibc uses msort_with_tmp() on stack.
2136 #if defined(__GLIBC__)
2137 qsort(mi->mi_set->recs, mi->mi_set->count,
2138 sizeof(*mi->mi_set->recs), hdrNumCmp);
2140 mergesort(mi->mi_set->recs, mi->mi_set->count,
2141 sizeof(*mi->mi_set->recs), hdrNumCmp);
2147 static int rpmdbGrowIterator(rpmdbMatchIterator mi)
2152 dbiIndex dbi = NULL;
2160 dbcursor = mi->mi_dbc;
2162 data = &mi->mi_data;
2163 if (key->data == NULL)
2166 dbi = dbiOpen(mi->mi_db, mi->mi_rpmtag, 0);
2170 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2171 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
2173 xx = dbiCclose(dbi, dbcursor, 0);
2177 if (rc) { /* error/not found */
2178 if (rc != DB_NOTFOUND)
2180 _("error(%d) getting \"%s\" records from %s index\n"),
2181 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
2183 xx = dbiCclose(dbi, dbcursor, 0);
2190 (void) dbt2set(dbi, data, &set);
2193 xx = dbiCclose(dbi, dbcursor, 0);
2197 if (mi->mi_set == NULL) {
2200 dbiGrowSet(mi->mi_set, set->count);
2201 memcpy(mi->mi_set->recs + mi->mi_set->count, set->recs,
2202 set->count * sizeof(*(mi->mi_set->recs)));
2203 mi->mi_set->count += set->count;
2204 set = dbiFreeIndexSet(set);
2210 int rpmdbPruneIterator(rpmdbMatchIterator mi, int * hdrNums,
2211 int nHdrNums, int sorted)
2213 if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
2217 (void) dbiPruneSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), sorted);
2221 int rpmdbAppendIterator(rpmdbMatchIterator mi, const int * hdrNums, int nHdrNums)
2223 if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
2226 if (mi->mi_set == NULL)
2227 mi->mi_set = xcalloc(1, sizeof(*mi->mi_set));
2228 (void) dbiAppendSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), 0);
2232 rpmdbMatchIterator rpmdbInitIterator(rpmdb db, rpmTag rpmtag,
2233 const void * keyp, size_t keylen)
2235 rpmdbMatchIterator mi;
2238 dbiIndexSet set = NULL;
2240 void * mi_keyp = NULL;
2246 (void) rpmdbCheckSignals();
2248 /* XXX HACK to remove rpmdbFindByLabel/findMatches from the API */
2249 if (rpmtag == RPMDBI_LABEL) {
2250 rpmtag = RPMTAG_NAME;
2254 dbi = dbiOpen(db, rpmtag, 0);
2258 /* Chain cursors for teardown on abnormal exit. */
2259 mi = xcalloc(1, sizeof(*mi));
2260 mi->mi_next = rpmmiRock;
2264 data = &mi->mi_data;
2267 * Handle label and file name special cases.
2268 * Otherwise, retrieve join keys for secondary lookup.
2270 if (rpmtag != RPMDBI_PACKAGES && keyp) {
2271 DBC * dbcursor = NULL;
2276 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2277 rc = dbiFindByLabel(dbi, dbcursor, key, data, keyp, &set);
2278 xx = dbiCclose(dbi, dbcursor, 0);
2280 } else if (rpmtag == RPMTAG_BASENAMES) {
2281 rc = rpmdbFindByFile(db, keyp, key, data, &set);
2283 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2285 key->data = (void *) keyp;
2287 if (key->data && key->size == 0)
2288 key->size = strlen((char *)key->data);
2289 if (key->data && key->size == 0)
2290 key->size++; /* XXX "/" fixup. */
2292 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
2295 _("error(%d) getting \"%s\" records from %s index\n"),
2296 rc, (key->data ? (char *)key->data : "???"),
2297 rpmTagGetName(dbi->dbi_rpmtag));
2300 /* Join keys need to be native endian internally. */
2302 (void) dbt2set(dbi, data, &set);
2304 xx = dbiCclose(dbi, dbcursor, 0);
2307 if (rc) { /* error/not found */
2308 set = dbiFreeIndexSet(set);
2309 rpmmiRock = mi->mi_next;
2316 /* Copy the retrieval key, byte swapping header instance if necessary. */
2319 case RPMDBI_PACKAGES:
2322 assert(keylen == sizeof(k->ui)); /* xxx programmer error */
2323 k = xmalloc(sizeof(*k));
2324 memcpy(k, keyp, keylen);
2325 if (dbiByteSwapped(dbi) == 1)
2332 keylen = strlen(keyp);
2333 k = xmalloc(keylen + 1);
2334 memcpy(k, keyp, keylen);
2335 k[keylen] = '\0'; /* XXX assumes strings */
2341 mi->mi_keyp = mi_keyp;
2342 mi->mi_keylen = keylen;
2344 mi->mi_db = rpmdbLink(db, RPMDBG_M("matchIterator"));
2345 mi->mi_rpmtag = rpmtag;
2353 mi->mi_modified = 0;
2354 mi->mi_prevoffset = 0;
2361 mi->mi_hdrchk = NULL;
2367 * Return database iterator.
2368 * @param mi rpm database iterator
2369 * @param keyp key data (NULL for sequential access)
2370 * @param keylen key data length (0 will use strlen(keyp))
2371 * @return 0 on success
2373 int rpmdbExtendIterator(rpmdbMatchIterator mi,
2374 const void * keyp, size_t keylen)
2376 mi->mi_key.data = (void *) keyp;
2377 mi->mi_key.size = keylen ? keylen : strlen(keyp);
2378 return rpmdbGrowIterator(mi);
2382 * Convert current tag data to db key
2383 * @param tagdata Tag data container
2384 * @retval key DB key struct
2385 * @retval freedata Should key.data be freed afterwards
2386 * Return 0 to signal this item should be discarded (ie continue)
2388 static int td2key(rpmtd tagdata, DBT *key, int *freedata)
2390 const char *str = NULL;
2391 uint8_t *bin = NULL;
2394 switch (rpmtdType(tagdata)) {
2397 key->size = sizeof(uint8_t);
2398 key->data = rpmtdGetChar(tagdata);
2400 case RPM_INT16_TYPE:
2401 key->size = sizeof(uint16_t);
2402 key->data = rpmtdGetUint16(tagdata);
2404 case RPM_INT32_TYPE:
2405 key->size = sizeof(uint32_t);
2406 key->data = rpmtdGetUint32(tagdata);
2408 case RPM_INT64_TYPE:
2409 key->size = sizeof(uint64_t);
2410 key->data = rpmtdGetUint64(tagdata);
2413 key->size = tagdata->count;
2414 key->data = tagdata->data;
2416 case RPM_STRING_TYPE:
2417 case RPM_I18NSTRING_TYPE:
2418 case RPM_STRING_ARRAY_TYPE:
2419 str = rpmtdGetString(tagdata);
2420 if (rpmtdTag(tagdata) == RPMTAG_FILEDIGESTS) {
2424 /* Filter out empty MD5 strings. */
2425 if (!(str && *str != '\0'))
2428 binlen = strlen(str) / 2;
2429 bin = xmalloc(binlen);
2430 /* Convert from hex to binary. */
2432 for (int j = 0; j < binlen; j++, t++, str += 2)
2433 *t = (rnibble(str[0]) << 4) | rnibble(str[1]);
2438 } else if (rpmtdTag(tagdata) == RPMTAG_PUBKEYS) {
2439 /* Extract the pubkey id from the base64 blob. */
2440 bin = xmalloc(sizeof(pgpKeyID_t));
2441 int nbin = pgpExtractPubkeyFingerprint(str, bin);
2454 str = rpmtdGetString(tagdata);
2455 key->data = (char *) str; /* XXX discards const */
2456 key->size = strlen(str);
2461 key->size = strlen((char *)key->data);
2463 key->size++; /* XXX "/" fixup. */
2468 static void logAddRemove(int removing, rpmtd tagdata)
2470 rpm_count_t c = rpmtdCount(tagdata);
2471 if (c == 1 && rpmtdType(tagdata) == RPM_STRING_TYPE) {
2472 rpmlog(RPMLOG_DEBUG, "%s \"%s\" %s %s index.\n",
2473 removing ? "removing" : "adding", rpmtdGetString(tagdata),
2474 removing ? "from" : "to",
2475 rpmTagGetName(rpmtdTag(tagdata)));
2477 rpmlog(RPMLOG_DEBUG, "%s %d entries %s %s index.\n",
2478 removing ? "removing" : "adding", c,
2479 removing ? "from" : "to",
2480 rpmTagGetName(rpmtdTag(tagdata)));
2485 int rpmdbRemove(rpmdb db, int rid, unsigned int hdrNum,
2487 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
2489 DBC * dbcursor = NULL;
2492 union _dbswap mi_offset;
2494 sigset_t signalMask;
2501 memset(&key, 0, sizeof(key));
2502 memset(&data, 0, sizeof(data));
2504 { rpmdbMatchIterator mi;
2505 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &hdrNum, sizeof(hdrNum));
2506 h = rpmdbNextIterator(mi);
2509 mi = rpmdbFreeIterator(mi);
2513 rpmlog(RPMLOG_ERR, _("%s: cannot read header at 0x%x\n"),
2514 "rpmdbRemove", hdrNum);
2519 char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
2520 rpmlog(RPMLOG_DEBUG, " --- h#%8u %s\n", hdrNum, nevra);
2524 (void) blockSignals(&signalMask);
2526 /* FIX: rpmvals heartburn */
2528 dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
2530 if (dbiTags.tags != NULL)
2531 for (dbix = 0; dbix < dbiTags.max; dbix++) {
2535 struct rpmtd_s tagdata;
2538 rpmtag = dbiTags.tags[dbix];
2540 /* Filter out temporary databases */
2541 if (isTemporaryDB(rpmtag))
2544 if (rpmtag == RPMDBI_PACKAGES) {
2545 dbi = dbiOpen(db, rpmtag, 0);
2546 if (dbi == NULL) /* XXX shouldn't happen */
2549 mi_offset.ui = hdrNum;
2550 if (dbiByteSwapped(dbi) == 1)
2552 key.data = &mi_offset;
2553 key.size = sizeof(mi_offset.ui);
2555 rc = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2556 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2559 _("error(%d) setting header #%d record for %s removal\n"),
2560 rc, hdrNum, rpmTagGetName(dbi->dbi_rpmtag));
2562 rc = dbiDel(dbi, dbcursor, &key, &data, 0);
2563 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2565 if (!dbi->dbi_no_dbsync)
2566 xx = dbiSync(dbi, 0);
2570 if (!headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM))
2573 if (!(dbi = dbiOpen(db, rpmtag, 0))) {
2574 rpmtdFreeData(&tagdata);
2577 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2579 logAddRemove(1, &tagdata);
2580 while (rpmtdNext(&tagdata) >= 0) {
2584 if (!td2key(&tagdata, &key, &freedata)) {
2589 * This is almost right, but, if there are duplicate tag
2590 * values, there will be duplicate attempts to remove
2591 * the header instance. It's faster to just ignore errors
2592 * than to do things correctly.
2596 * XXX with duplicates, an accurate data value and
2597 * DB_GET_BOTH is needed.
2601 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2602 if (rc == 0) { /* success */
2603 (void) dbt2set(dbi, &data, &set);
2604 } else if (rc == DB_NOTFOUND) { /* not found */
2606 } else { /* error */
2608 _("error(%d) setting \"%s\" records from %s index\n"),
2609 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2614 rc = dbiPruneSet(set, rec, 1, sizeof(*rec), 1);
2616 /* If nothing was pruned, then don't bother updating. */
2618 set = dbiFreeIndexSet(set);
2622 if (set->count > 0) {
2623 (void) set2dbt(dbi, &data, set);
2624 rc = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2627 _("error(%d) storing record \"%s\" into %s\n"),
2628 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2631 data.data = _free(data.data);
2634 rc = dbiDel(dbi, dbcursor, &key, &data, 0);
2637 _("error(%d) removing record \"%s\" from %s\n"),
2638 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2642 set = dbiFreeIndexSet(set);
2649 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2652 if (!dbi->dbi_no_dbsync)
2653 xx = dbiSync(dbi, 0);
2655 rpmtdFreeData(&tagdata);
2661 (void) unblockSignals(&signalMask);
2665 /* XXX return ret; */
2670 int rpmdbAdd(rpmdb db, int iid, Header h,
2672 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
2674 DBC * dbcursor = NULL;
2677 sigset_t signalMask;
2680 union _dbswap mi_offset;
2681 unsigned int hdrNum = 0;
2689 memset(&key, 0, sizeof(key));
2690 memset(&data, 0, sizeof(data));
2692 #ifdef NOTYET /* XXX headerDel() broken on dribbles. */
2693 xx = headerDel(h, RPMTAG_REMOVETID);
2695 if (iid != 0 && iid != -1) {
2696 rpm_tid_t tid = iid;
2697 if (!headerIsEntry(h, RPMTAG_INSTALLTID))
2698 headerPutUint32(h, RPMTAG_INSTALLTID, &tid, 1);
2701 (void) blockSignals(&signalMask);
2704 unsigned int firstkey = 0;
2705 void * keyp = &firstkey;
2706 size_t keylen = sizeof(firstkey);
2707 void * datap = NULL;
2710 dbi = dbiOpen(db, RPMDBI_PACKAGES, 0);
2713 /* XXX db0: hack to pass sizeof header to fadAlloc */
2715 datalen = headerSizeof(h, HEADER_MAGIC_NO);
2717 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2719 /* Retrieve join key for next header instance. */
2724 data.size = datalen;
2725 ret = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2729 datalen = data.size;
2732 if (ret == 0 && datap) {
2733 memcpy(&mi_offset, datap, sizeof(mi_offset.ui));
2734 if (dbiByteSwapped(dbi) == 1)
2736 hdrNum = mi_offset.ui;
2739 mi_offset.ui = hdrNum;
2740 if (dbiByteSwapped(dbi) == 1)
2742 if (ret == 0 && datap) {
2743 memcpy(datap, &mi_offset, sizeof(mi_offset.ui));
2746 datalen = sizeof(mi_offset.ui);
2752 data.size = datalen;
2754 ret = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2755 xx = dbiSync(dbi, 0);
2757 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2765 _("error(%d) allocating new package instance\n"), ret);
2769 /* Now update the indexes */
2773 dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
2775 if (dbiTags.tags != NULL)
2776 for (dbix = 0; dbix < dbiTags.max; dbix++) {
2780 struct rpmtd_s tagdata, reqflags;
2782 rpmrc = RPMRC_NOTFOUND;
2784 rpmtag = dbiTags.tags[dbix];
2786 /* Filter out temporary databases */
2787 if (isTemporaryDB(rpmtag))
2791 case RPMDBI_PACKAGES:
2792 dbi = dbiOpen(db, rpmtag, 0);
2793 if (dbi == NULL) /* XXX shouldn't happen */
2795 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2797 mi_offset.ui = hdrNum;
2798 if (dbiByteSwapped(dbi) == 1)
2800 key.data = (void *) &mi_offset;
2801 key.size = sizeof(mi_offset.ui);
2802 data.data = headerUnload(h);
2803 data.size = headerSizeof(h, HEADER_MAGIC_NO);
2805 /* Check header digest/signature on blob export. */
2810 rpmrc = (*hdrchk) (ts, data.data, data.size, &msg);
2811 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
2812 rpmlog(lvl, "%s h#%8u %s",
2813 (rpmrc == RPMRC_FAIL ? _("rpmdbAdd: skipping") : " +++"),
2814 hdrNum, (msg ? msg : "\n"));
2818 if (data.data != NULL && rpmrc != RPMRC_FAIL) {
2819 xx = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2820 xx = dbiSync(dbi, 0);
2822 data.data = _free(data.data);
2824 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2826 if (!dbi->dbi_no_dbsync)
2827 xx = dbiSync(dbi, 0);
2830 case RPMTAG_REQUIRENAME:
2831 headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM);
2832 headerGet(h, RPMTAG_REQUIREFLAGS, &reqflags, HEADERGET_MINMEM);
2835 headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM);
2839 if (rpmtdCount(&tagdata) == 0) {
2840 if (rpmtag != RPMTAG_GROUP)
2843 /* XXX preserve legacy behavior */
2844 tagdata.type = RPM_STRING_TYPE;
2845 tagdata.data = (const char **) "Unknown";
2849 if (!(dbi = dbiOpen(db, rpmtag, 0))) {
2850 rpmtdFreeData(&tagdata);
2853 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2855 logAddRemove(0, &tagdata);
2856 while (rpmtdNext(&tagdata) >= 0) {
2858 int i, freedata = 0;
2861 * Include the tagNum in all indices. rpm-3.0.4 and earlier
2862 * included the tagNum only for files.
2864 i = rec->tagNum = rpmtdGetIndex(&tagdata);
2866 case RPMTAG_REQUIRENAME: {
2867 /* Filter out install prerequisites. */
2868 rpm_flag_t *rflag = rpmtdNextUint32(&reqflags);
2869 if (rflag && isInstallPreReq(*rflag) &&
2870 !isErasePreReq(*rflag))
2874 case RPMTAG_TRIGGERNAME:
2875 if (i > 0) { /* don't add duplicates */
2876 const char **tnames = tagdata.data;
2877 const char *str = rpmtdGetString(&tagdata);
2878 for (j = 0; j < i; j++) {
2879 if (rstreq(str, tnames[j]))
2890 if (!td2key(&tagdata, &key, &freedata)) {
2895 * XXX with duplicates, an accurate data value and
2896 * DB_GET_BOTH is needed.
2901 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2902 if (rc == 0) { /* success */
2903 /* With duplicates, cursor is positioned, discard the record. */
2904 if (!dbi->dbi_permit_dups)
2905 (void) dbt2set(dbi, &data, &set);
2906 } else if (rc != DB_NOTFOUND) { /* error */
2908 _("error(%d) getting \"%s\" records from %s index\n"),
2909 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2914 if (set == NULL) /* not found or duplicate */
2915 set = xcalloc(1, sizeof(*set));
2917 (void) dbiAppendSet(set, rec, 1, sizeof(*rec), 0);
2919 (void) set2dbt(dbi, &data, set);
2920 rc = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2924 _("error(%d) storing record %s into %s\n"),
2925 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2928 data.data = _free(data.data);
2930 set = dbiFreeIndexSet(set);
2937 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2940 if (!dbi->dbi_no_dbsync)
2941 xx = dbiSync(dbi, 0);
2943 rpmtdFreeData(&tagdata);
2947 /* If everthing ok, mark header as installed now */
2949 headerSetInstance(h, hdrNum);
2954 (void) unblockSignals(&signalMask);
2960 * Remove DB4 environment (and lock), ie the equivalent of
2961 * rm -f <prefix>/<dbpath>/__db.???
2962 * Environment files not existing is not an error, failure to unlink is,
2963 * return zero on success.
2964 * Only useful for BDB, dbapi 3 and 4.
2965 * TODO/FIX: push this down to db3.c where it belongs
2967 static int cleanDbenv(const char *prefix, const char *dbpath)
2969 ARGV_t paths = NULL, p;
2971 char *pattern = rpmGetPath(prefix, "/", dbpath, "/__db.???", NULL);
2973 if (rpmGlob(pattern, NULL, &paths) == 0) {
2974 for (p = paths; *p; p++) {
2983 static int rpmdbRemoveDatabase(const char * prefix,
2984 const char * dbpath, int _dbapi)
2994 if (dbiTags.tags != NULL)
2995 for (i = 0; i < dbiTags.max; i++) {
2996 const char * base = rpmTagGetName(dbiTags.tags[i]);
2997 path = rpmGetPath(prefix, "/", dbpath, "/", base, NULL);
2998 if (access(path, F_OK) == 0)
3002 cleanDbenv(prefix, dbpath);
3011 path = rpmGetPath(prefix, "/", dbpath, NULL);
3018 static int rpmdbMoveDatabase(const char * prefix,
3019 const char * olddbpath, int _olddbapi,
3020 const char * newdbpath, int _newdbapi)
3026 int selinux = is_selinux_enabled() && (matchpathcon_init(NULL) != -1);
3030 blockSignals(&sigMask);
3031 switch (_olddbapi) {
3035 if (dbiTags.tags != NULL)
3036 for (i = 0; i < dbiTags.max; i++) {
3041 /* Filter out temporary databases */
3042 if (isTemporaryDB((rpmtag = dbiTags.tags[i])))
3045 base = rpmTagGetName(rpmtag);
3046 src = rpmGetPath(prefix, "/", olddbpath, "/", base, NULL);
3047 dest = rpmGetPath(prefix, "/", newdbpath, "/", base, NULL);
3049 if (access(src, F_OK) != 0)
3053 * Restore uid/gid/mode/mtime/security context if possible.
3055 if (stat(dest, &st) < 0)
3056 if (stat(src, &st) < 0)
3059 if ((xx = rename(src, dest)) != 0) {
3063 xx = chown(dest, st.st_uid, st.st_gid);
3064 xx = chmod(dest, (st.st_mode & 07777));
3065 { struct utimbuf stamp;
3066 stamp.actime = st.st_atime;
3067 stamp.modtime = st.st_mtime;
3068 xx = utime(dest, &stamp);
3072 security_context_t scon = NULL;
3073 if (matchpathcon(dest, st.st_mode, &scon) != -1) {
3074 (void) setfilecon(dest, scon);
3084 cleanDbenv(prefix, olddbpath);
3085 cleanDbenv(prefix, newdbpath);
3092 unblockSignals(&sigMask);
3094 #ifdef SQLITE_HACK_XXX
3095 if (rc || _olddbapi == _newdbapi)
3098 rc = rpmdbRemoveDatabase(prefix, newdbpath, _newdbapi);
3102 (void) matchpathcon_fini();
3108 int rpmdbRebuild(const char * prefix, rpmts ts,
3109 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
3112 char * dbpath = NULL;
3113 char * rootdbpath = NULL;
3115 char * newdbpath = NULL;
3116 char * newrootdbpath = NULL;
3125 if (prefix == NULL) prefix = "/";
3127 _dbapi = rpmExpandNumeric("%{_dbapi}");
3128 _dbapi_rebuild = rpmExpandNumeric("%{_dbapi_rebuild}");
3130 tfn = rpmGetPath("%{?_dbpath}", NULL);
3131 if (!(tfn && tfn[0] != '\0'))
3133 rpmlog(RPMLOG_ERR, _("no dbpath has been set"));
3137 dbpath = rootdbpath = rpmGetPath(prefix, tfn, NULL);
3138 if (!(prefix[0] == '/' && prefix[1] == '\0'))
3139 dbpath += strlen(prefix) - 1;
3142 tfn = rpmGetPath("%{?_dbpath_rebuild}", NULL);
3143 if (!(tfn && tfn[0] != '\0' && !rstreq(tfn, dbpath)))
3146 rasprintf(&tfn, "%srebuilddb.%d", dbpath, (int) getpid());
3149 newdbpath = newrootdbpath = rpmGetPath(prefix, tfn, NULL);
3150 if (!(prefix[0] == '/' && prefix[1] == '\0'))
3151 newdbpath += strlen(prefix) - 1;
3154 rpmlog(RPMLOG_DEBUG, "rebuilding database %s into %s\n",
3155 rootdbpath, newrootdbpath);
3157 if (!access(newrootdbpath, F_OK)) {
3158 rpmlog(RPMLOG_ERR, _("temporary database %s already exists\n"),
3164 rpmlog(RPMLOG_DEBUG, "creating directory %s\n", newrootdbpath);
3165 if (mkdir(newrootdbpath, 0755)) {
3166 rpmlog(RPMLOG_ERR, _("failed to create directory %s: %s\n"),
3167 newrootdbpath, strerror(errno));
3173 _rebuildinprogress = 0;
3175 rpmlog(RPMLOG_DEBUG, "opening old database with dbapi %d\n",
3177 if (openDatabase(prefix, dbpath, _dbapi, &olddb, O_RDONLY, 0644,
3178 RPMDB_FLAG_MINIMAL)) {
3182 _dbapi = olddb->db_api;
3183 _rebuildinprogress = 1;
3184 rpmlog(RPMLOG_DEBUG, "opening new database with dbapi %d\n",
3186 (void) rpmDefineMacro(NULL, "_rpmdb_rebuild %{nil}", -1);
3187 if (openDatabase(prefix, newdbpath, _dbapi_rebuild, &newdb, O_RDWR | O_CREAT, 0644, 0)) {
3192 _rebuildinprogress = 0;
3194 _dbapi_rebuild = newdb->db_api;
3197 rpmdbMatchIterator mi;
3198 #define _RECNUM rpmdbGetIteratorOffset(mi)
3200 mi = rpmdbInitIterator(olddb, RPMDBI_PACKAGES, NULL, 0);
3202 (void) rpmdbSetHdrChk(mi, ts, hdrchk);
3204 while ((h = rpmdbNextIterator(mi)) != NULL) {
3206 /* let's sanity check this record a bit, otherwise just skip it */
3207 if (!(headerIsEntry(h, RPMTAG_NAME) &&
3208 headerIsEntry(h, RPMTAG_VERSION) &&
3209 headerIsEntry(h, RPMTAG_RELEASE) &&
3210 headerIsEntry(h, RPMTAG_BUILDTIME)))
3213 _("header #%u in the database is bad -- skipping.\n"),
3218 /* Filter duplicate entries ? (bug in pre rpm-3.0.4) */
3219 if (_db_filter_dups || newdb->db_filter_dups) {
3220 const char *name = headerGetString(h, RPMTAG_NAME);
3221 const char *version = headerGetString(h, RPMTAG_VERSION);
3222 const char *release = headerGetString(h, RPMTAG_RELEASE);
3225 { rpmdbMatchIterator mi;
3226 mi = rpmdbInitIterator(newdb, RPMTAG_NAME, name, 0);
3227 (void) rpmdbSetIteratorRE(mi, RPMTAG_VERSION,
3228 RPMMIRE_DEFAULT, version);
3229 (void) rpmdbSetIteratorRE(mi, RPMTAG_RELEASE,
3230 RPMMIRE_DEFAULT, release);
3231 while (rpmdbNextIterator(mi)) {
3235 mi = rpmdbFreeIterator(mi);
3242 /* Deleted entries are eliminated in legacy headers by copy. */
3243 { Header nh = (headerIsEntry(h, RPMTAG_HEADERIMAGE)
3244 ? headerCopy(h) : NULL);
3245 rc = rpmdbAdd(newdb, -1, (nh ? nh : h), ts, hdrchk);
3246 nh = headerFree(nh);
3251 _("cannot add record originally at %u\n"), _RECNUM);
3257 mi = rpmdbFreeIterator(mi);
3261 xx = rpmdbClose(olddb);
3262 xx = rpmdbClose(newdb);
3265 rpmlog(RPMLOG_WARNING,
3266 _("failed to rebuild database: original database "
3267 "remains in place\n"));
3269 xx = rpmdbRemoveDatabase(prefix, newdbpath, _dbapi_rebuild);
3272 } else if (!nocleanup) {
3273 if (rpmdbMoveDatabase(prefix, newdbpath, _dbapi_rebuild, dbpath, _dbapi)) {
3274 rpmlog(RPMLOG_ERR, _("failed to replace old database with new "
3276 rpmlog(RPMLOG_ERR, _("replace files in %s with files from %s "
3277 "to recover"), dbpath, newdbpath);
3285 if (removedir && !(rc == 0 && nocleanup)) {
3286 rpmlog(RPMLOG_DEBUG, "removing directory %s\n", newrootdbpath);
3287 if (rmdir(newrootdbpath))
3288 rpmlog(RPMLOG_ERR, _("failed to remove directory %s: %s\n"),
3289 newrootdbpath, strerror(errno));
3291 newrootdbpath = _free(newrootdbpath);
3292 rootdbpath = _free(rootdbpath);