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 HEADERFLAG_ALLOCATED */
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:Triggername:Dirnames:Requireversion:Provideversion:Installtid:Sigmd5:Sha1header:Filemd5s: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 */
311 * Create and initialize item for index database set.
312 * @param hdrNum header instance in db
313 * @param tagNum tag index in header
316 static dbiIndexItem dbiIndexNewItem(unsigned int hdrNum, unsigned int tagNum)
318 dbiIndexItem rec = xcalloc(1, sizeof(*rec));
319 rec->hdrNum = hdrNum;
320 rec->tagNum = tagNum;
329 #define _DBSWAP(_a) \
331 { unsigned char _b, *_c = (_a).uc; \
332 _b = _c[3]; _c[3] = _c[0]; _c[0] = _b; \
333 _b = _c[2]; _c[2] = _c[1]; _c[1] = _b; \
338 * Convert retrieved data to index set.
339 * @param dbi index database handle
340 * @param data retrieved data
341 * @retval setp (malloc'ed) index set
342 * @return 0 on success
344 static int dbt2set(dbiIndex dbi, DBT * data, dbiIndexSet * setp)
346 int _dbbyteswapped = dbiByteSwapped(dbi);
351 if (dbi == NULL || data == NULL || setp == NULL)
354 if ((sdbir = data->data) == NULL) {
359 set = xmalloc(sizeof(*set));
360 set->count = data->size / dbi->dbi_jlen;
361 set->recs = xmalloc(set->count * sizeof(*(set->recs)));
363 switch (dbi->dbi_jlen) {
365 case 2*sizeof(int32_t):
366 for (i = 0; i < set->count; i++) {
367 union _dbswap hdrNum, tagNum;
369 memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
370 sdbir += sizeof(hdrNum.ui);
371 memcpy(&tagNum.ui, sdbir, sizeof(tagNum.ui));
372 sdbir += sizeof(tagNum.ui);
373 if (_dbbyteswapped) {
377 set->recs[i].hdrNum = hdrNum.ui;
378 set->recs[i].tagNum = tagNum.ui;
379 set->recs[i].fpNum = 0;
382 case 1*sizeof(int32_t):
383 for (i = 0; i < set->count; i++) {
384 union _dbswap hdrNum;
386 memcpy(&hdrNum.ui, sdbir, sizeof(hdrNum.ui));
387 sdbir += sizeof(hdrNum.ui);
388 if (_dbbyteswapped) {
391 set->recs[i].hdrNum = hdrNum.ui;
392 set->recs[i].tagNum = 0;
393 set->recs[i].fpNum = 0;
402 * Convert index set to database representation.
403 * @param dbi index database handle
404 * @param data retrieved data
405 * @param set index set
406 * @return 0 on success
408 static int set2dbt(dbiIndex dbi, DBT * data, dbiIndexSet set)
410 int _dbbyteswapped = dbiByteSwapped(dbi);
414 if (dbi == NULL || data == NULL || set == NULL)
417 data->size = set->count * (dbi->dbi_jlen);
418 if (data->size == 0) {
422 tdbir = data->data = xmalloc(data->size);
424 switch (dbi->dbi_jlen) {
426 case 2*sizeof(int32_t):
427 for (i = 0; i < set->count; i++) {
428 union _dbswap hdrNum, tagNum;
430 memset(&hdrNum, 0, sizeof(hdrNum));
431 memset(&tagNum, 0, sizeof(tagNum));
432 hdrNum.ui = set->recs[i].hdrNum;
433 tagNum.ui = set->recs[i].tagNum;
434 if (_dbbyteswapped) {
438 memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
439 tdbir += sizeof(hdrNum.ui);
440 memcpy(tdbir, &tagNum.ui, sizeof(tagNum.ui));
441 tdbir += sizeof(tagNum.ui);
444 case 1*sizeof(int32_t):
445 for (i = 0; i < set->count; i++) {
446 union _dbswap hdrNum;
448 memset(&hdrNum, 0, sizeof(hdrNum));
449 hdrNum.ui = set->recs[i].hdrNum;
450 if (_dbbyteswapped) {
453 memcpy(tdbir, &hdrNum.ui, sizeof(hdrNum.ui));
454 tdbir += sizeof(hdrNum.ui);
462 /* XXX assumes hdrNum is first int in dbiIndexItem */
463 static int hdrNumCmp(const void * one, const void * two)
465 const int * a = one, * b = two;
470 * Append element(s) to set of index database items.
471 * @param set set of index database items
472 * @param recs array of items to append to set
473 * @param nrecs number of items
474 * @param recsize size of an array item
475 * @param sortset should resulting set be sorted?
476 * @return 0 success, 1 failure (bad args)
478 static int dbiAppendSet(dbiIndexSet set, const void * recs,
479 int nrecs, size_t recsize, int sortset)
481 const char * rptr = recs;
482 size_t rlen = (recsize < sizeof(*(set->recs)))
483 ? recsize : sizeof(*(set->recs));
485 if (set == NULL || recs == NULL || nrecs <= 0 || recsize == 0)
488 set->recs = xrealloc(set->recs,
489 (set->count + nrecs) * sizeof(*(set->recs)));
491 memset(set->recs + set->count, 0, nrecs * sizeof(*(set->recs)));
493 while (nrecs-- > 0) {
494 memcpy(set->recs + set->count, rptr, rlen);
499 if (sortset && set->count > 1)
500 qsort(set->recs, set->count, sizeof(*(set->recs)), hdrNumCmp);
506 * Remove element(s) from set of index database items.
507 * @param set set of index database items
508 * @param recs array of items to remove from set
509 * @param nrecs number of items
510 * @param recsize size of an array item
511 * @param sorted array is already sorted?
512 * @return 0 success, 1 failure (no items found)
514 static int dbiPruneSet(dbiIndexSet set, void * recs, int nrecs,
515 size_t recsize, int sorted)
519 int num = set->count;
522 assert(set->count > 0);
523 if (nrecs > 1 && !sorted)
524 qsort(recs, nrecs, recsize, hdrNumCmp);
526 for (from = 0; from < num; from++) {
527 if (bsearch(&set->recs[from], recs, nrecs, recsize, hdrNumCmp)) {
532 set->recs[to] = set->recs[from]; /* structure assignment */
536 return (numCopied == num);
539 /* XXX transaction.c */
540 unsigned int dbiIndexSetCount(dbiIndexSet set) {
544 /* XXX transaction.c */
545 unsigned int dbiIndexRecordOffset(dbiIndexSet set, int recno) {
546 return set->recs[recno].hdrNum;
549 /* XXX transaction.c */
550 unsigned int dbiIndexRecordFileNumber(dbiIndexSet set, int recno) {
551 return set->recs[recno].tagNum;
554 /* XXX transaction.c */
555 dbiIndexSet dbiFreeIndexSet(dbiIndexSet set) {
557 set->recs = _free(set->recs);
563 typedef struct miRE_s {
564 rpmTag tag; /*!< header tag */
565 rpmMireMode mode; /*!< pattern match mode */
566 char * pattern; /*!< pattern string */
567 int notmatch; /*!< like "grep -v" */
568 regex_t * preg; /*!< regex compiled pattern buffer */
569 int cflags; /*!< regcomp(3) flags */
570 int eflags; /*!< regexec(3) flags */
571 int fnflags; /*!< fnmatch(3) flags */
574 struct rpmdbMatchIterator_s {
575 rpmdbMatchIterator mi_next;
589 unsigned int mi_prevoffset; /* header instance (native endian) */
590 unsigned int mi_offset; /* header instance (native endian) */
591 unsigned int mi_filenum; /* tag element (native endian) */
595 rpmRC (*mi_hdrchk) (rpmts ts, const void * uh, size_t uc, char ** msg);
599 static rpmdb rpmdbRock;
601 static rpmdbMatchIterator rpmmiRock;
603 int rpmdbCheckTerminate(int terminate)
605 sigset_t newMask, oldMask;
606 static int terminating = 0;
608 if (terminating) return 0;
610 (void) sigfillset(&newMask); /* block all signals */
611 (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
613 if (rpmsqIsCaught(SIGINT) > 0
614 || rpmsqIsCaught(SIGQUIT) > 0
615 || rpmsqIsCaught(SIGHUP) > 0
616 || rpmsqIsCaught(SIGTERM) > 0
617 || rpmsqIsCaught(SIGPIPE) > 0
623 rpmdbMatchIterator mi;
625 while ((mi = rpmmiRock) != NULL) {
626 rpmmiRock = mi->mi_next;
628 mi = rpmdbFreeIterator(mi);
631 while ((db = rpmdbRock) != NULL) {
632 rpmdbRock = db->db_next;
634 (void) rpmdbClose(db);
637 sigprocmask(SIG_SETMASK, &oldMask, NULL);
641 int rpmdbCheckSignals(void)
643 if (rpmdbCheckTerminate(0)) {
644 rpmlog(RPMLOG_DEBUG, "Exiting on signal...\n");
651 * Block all signals, returning previous signal mask.
653 static int blockSignals(sigset_t * oldMask)
657 (void) sigfillset(&newMask); /* block all signals */
658 (void) sigprocmask(SIG_BLOCK, &newMask, oldMask);
659 (void) sigdelset(&newMask, SIGINT);
660 (void) sigdelset(&newMask, SIGQUIT);
661 (void) sigdelset(&newMask, SIGHUP);
662 (void) sigdelset(&newMask, SIGTERM);
663 (void) sigdelset(&newMask, SIGPIPE);
664 return sigprocmask(SIG_BLOCK, &newMask, NULL);
668 * Restore signal mask.
670 static int unblockSignals(sigset_t * oldMask)
672 (void) rpmdbCheckSignals();
673 return sigprocmask(SIG_SETMASK, oldMask, NULL);
677 #define _DB_HOME "%{_dbpath}"
680 #define _DB_PERMS 0644
683 #define _DB_ERRPFX "rpmdb"
685 static struct rpmdb_s const dbTemplate = {
686 _DB_ROOT, _DB_HOME, _DB_FLAGS, _DB_MODE, _DB_PERMS,
687 _DB_MAJOR, _DB_ERRPFX
690 static int isTemporaryDB(rpmTag rpmtag)
694 case RPMDBI_AVAILABLE:
706 rpmop rpmdbOp(rpmdb rpmdb, rpmdbOpX opx)
711 op = &rpmdb->db_getops;
714 op = &rpmdb->db_putops;
717 op = &rpmdb->db_delops;
725 int rpmdbSetChrootDone(rpmdb db, int chrootDone)
729 ochrootDone = db->db_chrootDone;
730 db->db_chrootDone = chrootDone;
735 int rpmdbOpenAll(rpmdb db)
740 if (db == NULL) return -2;
742 if (dbiTags.tags != NULL)
743 for (dbix = 0; dbix < dbiTags.max; dbix++) {
744 if (db->_dbi[dbix] != NULL)
746 /* Filter out temporary databases */
747 if (isTemporaryDB(dbiTags.tags[dbix]))
749 (void) dbiOpen(db, dbiTags.tags[dbix], db->db_flags);
754 int rpmdbCloseDBI(rpmdb db, rpmTag rpmtag)
759 if (db == NULL || db->_dbi == NULL || dbiTags.tags == NULL)
762 for (dbix = 0; dbix < dbiTags.max; dbix++) {
763 if (dbiTags.tags[dbix] != rpmtag)
765 if (db->_dbi[dbix] != NULL) {
767 /* FIX: double indirection. */
768 xx = dbiClose(db->_dbi[dbix], 0);
769 if (xx && rc == 0) rc = xx;
770 db->_dbi[dbix] = NULL;
777 /* XXX query.c, rpminstall.c, verify.c */
778 int rpmdbClose(rpmdb db)
787 (void) rpmdbUnlink(db, RPMDBG_M("rpmdbClose"));
793 for (dbix = db->db_ndbi; --dbix >= 0; ) {
795 if (db->_dbi[dbix] == NULL)
797 xx = dbiClose(db->_dbi[dbix], 0);
798 if (xx && rc == 0) rc = xx;
799 db->_dbi[dbix] = NULL;
801 db->db_errpfx = _free(db->db_errpfx);
802 db->db_root = _free(db->db_root);
803 db->db_home = _free(db->db_home);
804 db->db_bits = PBM_FREE(db->db_bits);
805 db->_dbi = _free(db->_dbi);
808 while ((next = *prev) != NULL && next != db)
809 prev = &next->db_next;
811 *prev = next->db_next;
812 next->db_next = NULL;
820 (void) rpmsqEnable(-SIGHUP, NULL);
821 (void) rpmsqEnable(-SIGINT, NULL);
822 (void) rpmsqEnable(-SIGTERM,NULL);
823 (void) rpmsqEnable(-SIGQUIT,NULL);
824 (void) rpmsqEnable(-SIGPIPE,NULL);
828 int rpmdbSync(rpmdb db)
833 if (db == NULL) return 0;
834 for (dbix = 0; dbix < db->db_ndbi; dbix++) {
836 if (db->_dbi[dbix] == NULL)
838 if (db->_dbi[dbix]->dbi_no_dbsync)
840 xx = dbiSync(db->_dbi[dbix], 0);
841 if (xx && rc == 0) rc = xx;
846 /* FIX: dbTemplate structure assignment */
848 rpmdb newRpmdb(const char * root,
850 int mode, int perms, int flags)
852 rpmdb db = xcalloc(sizeof(*db), 1);
853 const char * epfx = _DB_ERRPFX;
854 static int _initialized = 0;
857 _db_filter_dups = rpmExpandNumeric("%{_filterdbdups}");
861 *db = dbTemplate; /* structure assignment */
865 if (!(perms & 0600)) perms = 0644; /* XXX sanity */
867 if (mode >= 0) db->db_mode = mode;
868 if (perms >= 0) db->db_perms = perms;
869 if (flags >= 0) db->db_flags = flags;
872 db->db_root = rpmGetPath(root, NULL);
874 db->db_root = rpmGetPath(_DB_ROOT, NULL);
876 db->db_home = rpmGetPath( (home && *home ? home : _DB_HOME), NULL);
877 if (!(db->db_home && db->db_home[0] != '%')) {
878 rpmlog(RPMLOG_ERR, _("no dbpath has been set\n"));
879 db->db_root = _free(db->db_root);
880 db->db_home = _free(db->db_home);
884 db->db_errpfx = rpmExpand( (epfx && *epfx ? epfx : _DB_ERRPFX), NULL);
885 db->db_remove_env = 0;
886 db->db_filter_dups = _db_filter_dups;
887 db->db_ndbi = dbiTags.max;
888 db->_dbi = xcalloc(db->db_ndbi, sizeof(*db->_dbi));
890 return rpmdbLink(db, RPMDBG_M("rpmdbCreate"));
893 static int openDatabase(const char * prefix,
895 int _dbapi, rpmdb *dbp,
896 int mode, int perms, int flags)
900 int justCheck = flags & RPMDB_FLAG_JUSTCHECK;
901 int minimal = flags & RPMDB_FLAG_MINIMAL;
905 /* Insure that _dbapi has one of -1, 1, 2, or 3 */
906 if (_dbapi < -1 || _dbapi > 4)
913 if ((mode & O_ACCMODE) == O_WRONLY)
916 db = newRpmdb(prefix, dbpath, mode, perms, flags);
920 (void) rpmsqEnable(SIGHUP, NULL);
921 (void) rpmsqEnable(SIGINT, NULL);
922 (void) rpmsqEnable(SIGTERM,NULL);
923 (void) rpmsqEnable(SIGQUIT,NULL);
924 (void) rpmsqEnable(SIGPIPE,NULL);
931 if (dbiTags.tags != NULL)
932 for (dbix = 0; rc == 0 && dbix < dbiTags.max; dbix++) {
936 /* Filter out temporary databases */
937 if (isTemporaryDB((rpmtag = dbiTags.tags[dbix])))
940 dbi = dbiOpen(db, rpmtag, 0);
947 case RPMDBI_PACKAGES:
948 if (dbi == NULL) rc |= 1;
950 /* XXX open only Packages, indices created on the fly. */
956 if (dbi == NULL) rc |= 1;
967 if (rc || justCheck || dbp == NULL)
970 db->db_next = rpmdbRock;
978 rpmdb rpmdbUnlink(rpmdb db, const char * msg)
981 fprintf(stderr, "--> db %p -- %d %s\n", db, db->nrefs, msg);
986 rpmdb rpmdbLink(rpmdb db, const char * msg)
990 fprintf(stderr, "--> db %p ++ %d %s\n", db, db->nrefs, msg);
994 /* XXX python/rpmmodule.c */
995 int rpmdbOpen (const char * prefix, rpmdb *dbp, int mode, int perms)
997 int _dbapi = rpmExpandNumeric("%{_dbapi}");
998 return openDatabase(prefix, NULL, _dbapi, dbp, mode, perms, 0);
1001 int rpmdbInit (const char * prefix, int perms)
1004 int _dbapi = rpmExpandNumeric("%{_dbapi}");
1007 rc = openDatabase(prefix, NULL, _dbapi, &db, (O_CREAT | O_RDWR),
1008 perms, RPMDB_FLAG_JUSTCHECK);
1011 xx = rpmdbOpenAll(db);
1012 if (xx && rc == 0) rc = xx;
1013 xx = rpmdbClose(db);
1014 if (xx && rc == 0) rc = xx;
1020 int rpmdbVerify(const char * prefix)
1023 int _dbapi = rpmExpandNumeric("%{_dbapi}");
1026 rc = openDatabase(prefix, NULL, _dbapi, &db, O_RDONLY, 0644, 0);
1031 rc = rpmdbOpenAll(db);
1033 for (dbix = db->db_ndbi; --dbix >= 0; ) {
1034 if (db->_dbi[dbix] == NULL)
1036 /* FIX: double indirection. */
1037 xx = dbiVerify(db->_dbi[dbix], 0);
1038 if (xx && rc == 0) rc = xx;
1039 db->_dbi[dbix] = NULL;
1042 /* FIX: db->_dbi[] may be NULL. */
1043 xx = rpmdbClose(db);
1044 if (xx && rc == 0) rc = xx;
1051 * Find file matches in database.
1052 * @param db rpm database
1057 * @return 0 on success, 1 on not found, -2 on error
1059 static int rpmdbFindByFile(rpmdb db, const char * filespec,
1060 DBT * key, DBT * data, dbiIndexSet * matches)
1063 const char * baseName;
1064 fingerPrintCache fpc;
1066 dbiIndex dbi = NULL;
1068 dbiIndexSet allMatches = NULL;
1069 dbiIndexItem rec = NULL;
1075 if (filespec == NULL) return -2;
1077 if ((baseName = strrchr(filespec, '/')) != NULL) {
1078 size_t len = baseName - filespec + 1;
1079 dirName = strncpy(xmalloc(len + 1), filespec, len);
1080 dirName[len] = '\0';
1083 dirName = xstrdup("");
1084 baseName = filespec;
1086 if (baseName == NULL)
1089 fpc = fpCacheCreate(20);
1090 fp1 = fpLookup(fpc, dirName, baseName, 1);
1093 dbi = dbiOpen(db, RPMTAG_BASENAMES, 0);
1096 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
1098 key->data = (void *) baseName;
1099 key->size = strlen(baseName);
1101 key->size++; /* XXX "/" fixup. */
1103 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
1106 _("error(%d) getting \"%s\" records from %s index\n"),
1107 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
1111 (void) dbt2set(dbi, data, &allMatches);
1113 xx = dbiCclose(dbi, dbcursor, 0);
1119 allMatches = dbiFreeIndexSet(allMatches);
1120 fpc = fpCacheFree(fpc);
1124 *matches = xcalloc(1, sizeof(**matches));
1125 rec = dbiIndexNewItem(0, 0);
1127 if (allMatches != NULL)
1128 while (i < allMatches->count) {
1129 struct rpmtd_s bn, dn, di;
1130 const char ** baseNames, ** dirNames;
1131 uint32_t * dirIndexes;
1132 unsigned int offset = dbiIndexRecordOffset(allMatches, i);
1133 unsigned int prevoff;
1136 { rpmdbMatchIterator mi;
1137 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &offset, sizeof(offset));
1138 h = rpmdbNextIterator(mi);
1141 mi = rpmdbFreeIterator(mi);
1149 headerGet(h, RPMTAG_BASENAMES, &bn, HEADERGET_MINMEM);
1150 headerGet(h, RPMTAG_DIRNAMES, &dn, HEADERGET_MINMEM);
1151 headerGet(h, RPMTAG_DIRINDEXES, &di, HEADERGET_MINMEM);
1152 baseNames = bn.data;
1154 dirIndexes = di.data;
1158 int num = dbiIndexRecordFileNumber(allMatches, i);
1160 fp2 = fpLookup(fpc, dirNames[dirIndexes[num]], baseNames[num], 1);
1161 if (FP_EQUAL(fp1, fp2)) {
1162 rec->hdrNum = dbiIndexRecordOffset(allMatches, i);
1163 rec->tagNum = dbiIndexRecordFileNumber(allMatches, i);
1164 xx = dbiAppendSet(*matches, rec, 1, sizeof(*rec), 0);
1169 if (i < allMatches->count)
1170 offset = dbiIndexRecordOffset(allMatches, i);
1171 } while (i < allMatches->count && offset == prevoff);
1180 allMatches = dbiFreeIndexSet(allMatches);
1182 fpc = fpCacheFree(fpc);
1184 if ((*matches)->count == 0) {
1185 *matches = dbiFreeIndexSet(*matches);
1192 /* XXX python/upgrade.c, install.c, uninstall.c */
1193 int rpmdbCountPackages(rpmdb db, const char * name)
1195 DBC * dbcursor = NULL;
1205 memset(&key, 0, sizeof(key));
1206 memset(&data, 0, sizeof(data));
1208 dbi = dbiOpen(db, RPMTAG_NAME, 0);
1212 key.data = (void *) name;
1213 key.size = strlen(name);
1215 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
1216 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
1218 xx = dbiCclose(dbi, dbcursor, 0);
1222 if (rc == 0) { /* success */
1223 dbiIndexSet matches;
1224 /* FIX: matches might be NULL */
1226 (void) dbt2set(dbi, &data, &matches);
1228 rc = dbiIndexSetCount(matches);
1229 matches = dbiFreeIndexSet(matches);
1232 if (rc == DB_NOTFOUND) { /* not found */
1234 } else { /* error */
1236 _("error(%d) getting \"%s\" records from %s index\n"),
1237 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
1242 xx = dbiCclose(dbi, dbcursor, 0);
1250 * Attempt partial matches on name[-version[-release]] strings.
1251 * @param dbi index database handle (always RPMTAG_NAME)
1252 * @param dbcursor index database cursor
1253 * @param key search key/length/flags
1254 * @param data search data/length/flags
1255 * @param name package name
1256 * @param version package version (can be a pattern)
1257 * @param release package release (can be a pattern)
1258 * @retval matches set of header instances that match
1259 * @return RPMRC_OK on match, RPMRC_NOMATCH or RPMRC_FAIL
1261 static rpmRC dbiFindMatches(dbiIndex dbi, DBC * dbcursor,
1262 DBT * key, DBT * data,
1264 const char * version,
1265 const char * release,
1266 dbiIndexSet * matches)
1272 key->data = (void *) name;
1273 key->size = strlen(name);
1275 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
1277 if (rc == 0) { /* success */
1278 (void) dbt2set(dbi, data, matches);
1279 if (version == NULL && release == NULL)
1282 if (rc == DB_NOTFOUND) { /* not found */
1283 return RPMRC_NOTFOUND;
1284 } else { /* error */
1286 _("error(%d) getting \"%s\" records from %s index\n"),
1287 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
1291 /* Make sure the version and release match. */
1292 for (i = 0; i < dbiIndexSetCount(*matches); i++) {
1293 unsigned int recoff = dbiIndexRecordOffset(*matches, i);
1294 rpmdbMatchIterator mi;
1300 mi = rpmdbInitIterator(dbi->dbi_rpmdb,
1301 RPMDBI_PACKAGES, &recoff, sizeof(recoff));
1303 /* Set iterator selectors for version/release if available. */
1305 rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, version))
1311 rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, release))
1317 h = rpmdbNextIterator(mi);
1319 (*matches)->recs[gotMatches++] = (*matches)->recs[i];
1321 (*matches)->recs[i].hdrNum = 0;
1322 mi = rpmdbFreeIterator(mi);
1326 (*matches)->count = gotMatches;
1329 rc = RPMRC_NOTFOUND;
1332 /* FIX: double indirection */
1333 if (rc && matches && *matches)
1334 *matches = dbiFreeIndexSet(*matches);
1339 * Lookup by name, name-version, and finally by name-version-release.
1340 * Both version and release can be patterns.
1341 * @todo Name must be an exact match, as name is a db key.
1342 * @param dbi index database handle (always RPMTAG_NAME)
1343 * @param dbcursor index database cursor
1344 * @param key search key/length/flags
1345 * @param data search data/length/flags
1346 * @param arg name[-version[-release]] string
1347 * @retval matches set of header instances that match
1348 * @return RPMRC_OK on match, RPMRC_NOMATCH or RPMRC_FAIL
1350 static rpmRC dbiFindByLabel(dbiIndex dbi, DBC * dbcursor, DBT * key, DBT * data,
1351 const char * arg, dbiIndexSet * matches)
1353 const char * release;
1360 if (arg == NULL || strlen(arg) == 0) return RPMRC_NOTFOUND;
1362 /* did they give us just a name? */
1363 rc = dbiFindMatches(dbi, dbcursor, key, data, arg, NULL, NULL, matches);
1364 if (rc != RPMRC_NOTFOUND) return rc;
1366 /* FIX: double indirection */
1367 *matches = dbiFreeIndexSet(*matches);
1369 /* maybe a name and a release */
1370 localarg = xmalloc(strlen(arg) + 1);
1371 s = stpcpy(localarg, arg);
1375 for (s -= 1; s > localarg; s--) {
1381 if (c != '[') brackets = 0;
1385 if (!brackets && *s == '-')
1389 /* FIX: *matches may be NULL. */
1390 if (s == localarg) {
1391 rc = RPMRC_NOTFOUND;
1396 rc = dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, NULL, matches);
1397 if (rc != RPMRC_NOTFOUND) goto exit;
1399 /* FIX: double indirection */
1400 *matches = dbiFreeIndexSet(*matches);
1402 /* how about name-version-release? */
1408 for (; s > localarg; s--) {
1414 if (c != '[') brackets = 0;
1418 if (!brackets && *s == '-')
1422 if (s == localarg) {
1423 rc = RPMRC_NOTFOUND;
1428 /* FIX: *matches may be NULL. */
1429 rc = dbiFindMatches(dbi, dbcursor, key, data, localarg, s + 1, release, matches);
1436 * Rewrite a header into packages (if necessary) and free the header.
1437 * Note: this is called from a markReplacedFiles iteration, and *must*
1438 * preserve the "join key" (i.e. offset) for the header.
1439 * @param mi database iterator
1440 * @param dbi index database handle
1441 * @return 0 on success
1443 static int miFreeHeader(rpmdbMatchIterator mi, dbiIndex dbi)
1447 if (mi == NULL || mi->mi_h == NULL)
1450 if (dbi && mi->mi_dbc && mi->mi_modified && mi->mi_prevoffset) {
1451 DBT * key = &mi->mi_key;
1452 DBT * data = &mi->mi_data;
1453 sigset_t signalMask;
1454 rpmRC rpmrc = RPMRC_NOTFOUND;
1457 key->data = (void *) &mi->mi_prevoffset;
1458 key->size = sizeof(mi->mi_prevoffset);
1459 data->data = headerUnload(mi->mi_h);
1460 data->size = headerSizeof(mi->mi_h, HEADER_MAGIC_NO);
1462 /* Check header digest/signature on blob export (if requested). */
1463 if (mi->mi_hdrchk && mi->mi_ts) {
1467 rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, data->data, data->size, &msg);
1468 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
1469 rpmlog(lvl, "%s h#%8u %s",
1470 (rpmrc == RPMRC_FAIL ? _("miFreeHeader: skipping") : "write"),
1471 mi->mi_prevoffset, (msg ? msg : "\n"));
1475 if (data->data != NULL && rpmrc != RPMRC_FAIL) {
1476 (void) blockSignals(&signalMask);
1477 rc = dbiPut(dbi, mi->mi_dbc, key, data, DB_KEYLAST);
1480 _("error(%d) storing record #%d into %s\n"),
1481 rc, mi->mi_prevoffset, rpmTagGetName(dbi->dbi_rpmtag));
1483 xx = dbiSync(dbi, 0);
1484 (void) unblockSignals(&signalMask);
1486 data->data = _free(data->data);
1490 mi->mi_h = headerFree(mi->mi_h);
1495 rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
1497 rpmdbMatchIterator * prev, next;
1506 while ((next = *prev) != NULL && next != mi)
1507 prev = &next->mi_next;
1509 *prev = next->mi_next;
1510 next->mi_next = NULL;
1513 dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
1514 if (dbi == NULL) /* XXX can't happen */
1517 xx = miFreeHeader(mi, dbi);
1520 xx = dbiCclose(dbi, mi->mi_dbc, 0);
1523 if (mi->mi_re != NULL)
1524 for (i = 0; i < mi->mi_nre; i++) {
1525 miRE mire = mi->mi_re + i;
1526 mire->pattern = _free(mire->pattern);
1527 if (mire->preg != NULL) {
1528 regfree(mire->preg);
1529 /* LCL: regfree has bogus only */
1530 mire->preg = _free(mire->preg);
1533 mi->mi_re = _free(mi->mi_re);
1535 mi->mi_set = dbiFreeIndexSet(mi->mi_set);
1536 mi->mi_keyp = _free(mi->mi_keyp);
1537 mi->mi_db = rpmdbUnlink(mi->mi_db, RPMDBG_M("matchIterator"));
1541 (void) rpmdbCheckSignals();
1546 unsigned int rpmdbGetIteratorOffset(rpmdbMatchIterator mi) {
1547 return (mi ? mi->mi_offset : 0);
1550 unsigned int rpmdbGetIteratorFileNum(rpmdbMatchIterator mi) {
1551 return (mi ? mi->mi_filenum : 0);
1554 int rpmdbGetIteratorCount(rpmdbMatchIterator mi) {
1555 return (mi && mi->mi_set ? mi->mi_set->count : 0);
1559 * Return pattern match.
1560 * @param mire match iterator regex
1561 * @param val value to match
1562 * @return 0 if pattern matches, >0 on nomatch, <0 on error
1564 static int miregexec(miRE mire, const char * val)
1568 switch (mire->mode) {
1569 case RPMMIRE_STRCMP:
1570 rc = strcmp(mire->pattern, val);
1573 case RPMMIRE_DEFAULT:
1575 rc = regexec(mire->preg, val, 0, NULL, mire->eflags);
1576 if (rc && rc != REG_NOMATCH) {
1578 (void) regerror(rc, mire->preg, msg, sizeof(msg)-1);
1579 msg[sizeof(msg)-1] = '\0';
1580 rpmlog(RPMLOG_ERR, _("%s: regexec failed: %s\n"),
1581 mire->pattern, msg);
1586 rc = fnmatch(mire->pattern, val, mire->fnflags);
1587 if (rc && rc != FNM_NOMATCH)
1599 * Compare iterator selectors by rpm tag (qsort/bsearch).
1600 * @param a 1st iterator selector
1601 * @param b 2nd iterator selector
1602 * @return result of comparison
1604 static int mireCmp(const void * a, const void * b)
1606 const miRE mireA = (const miRE) a;
1607 const miRE mireB = (const miRE) b;
1608 return (mireA->tag - mireB->tag);
1612 * Copy pattern, escaping for appropriate mode.
1613 * @param tag rpm tag
1614 * @retval modep type of pattern match
1615 * @param pattern pattern to duplicate
1616 * @return duplicated pattern
1618 static char * mireDup(rpmTag tag, rpmMireMode *modep,
1619 const char * pattern)
1630 case RPMMIRE_DEFAULT:
1631 if (tag == RPMTAG_DIRNAMES || tag == RPMTAG_BASENAMES) {
1632 *modep = RPMMIRE_GLOB;
1633 pat = xstrdup(pattern);
1637 nb = strlen(pattern) + sizeof("^$");
1639 /* Find no. of bytes needed for pattern. */
1640 /* periods and plusses are escaped, splats become '.*' */
1643 for (s = pattern; *s != '\0'; s++) {
1648 if (!brackets) nb++;
1657 if (c != '[') brackets = 0;
1663 pat = t = xmalloc(nb);
1665 if (pattern[0] != '^') *t++ = '^';
1667 /* Copy pattern, escaping periods, prefixing splats with period. */
1670 for (s = pattern; *s != '\0'; s++, t++) {
1674 if (!brackets) *t++ = '\\';
1677 if (!brackets) *t++ = '.';
1686 if (c != '[') brackets = 0;
1692 if (s > pattern && s[-1] != '$') *t++ = '$';
1694 *modep = RPMMIRE_REGEX;
1696 case RPMMIRE_STRCMP:
1699 pat = xstrdup(pattern);
1706 int rpmdbSetIteratorRE(rpmdbMatchIterator mi, rpmTag tag,
1707 rpmMireMode mode, const char * pattern)
1709 static rpmMireMode defmode = (rpmMireMode)-1;
1711 char * allpat = NULL;
1713 regex_t * preg = NULL;
1719 if (defmode == (rpmMireMode)-1) {
1720 char *t = rpmExpand("%{?_query_selector_match}", NULL);
1722 if (*t == '\0' || !strcmp(t, "default"))
1723 defmode = RPMMIRE_DEFAULT;
1724 else if (!strcmp(t, "strcmp"))
1725 defmode = RPMMIRE_STRCMP;
1726 else if (!strcmp(t, "regex"))
1727 defmode = RPMMIRE_REGEX;
1728 else if (!strcmp(t, "glob"))
1729 defmode = RPMMIRE_GLOB;
1731 defmode = RPMMIRE_DEFAULT;
1735 if (mi == NULL || pattern == NULL)
1738 /* Leading '!' inverts pattern match sense, like "grep -v". */
1739 if (*pattern == '!') {
1744 allpat = mireDup(tag, &mode, pattern);
1746 if (mode == RPMMIRE_DEFAULT)
1750 case RPMMIRE_DEFAULT:
1751 case RPMMIRE_STRCMP:
1754 preg = xcalloc(1, sizeof(*preg));
1755 cflags = (REG_EXTENDED | REG_NOSUB);
1756 rc = regcomp(preg, allpat, cflags);
1759 (void) regerror(rc, preg, msg, sizeof(msg)-1);
1760 msg[sizeof(msg)-1] = '\0';
1761 rpmlog(RPMLOG_ERR, _("%s: regcomp failed: %s\n"), allpat, msg);
1765 fnflags = FNM_PATHNAME | FNM_PERIOD;
1773 /* FIX: mire has kept values */
1774 allpat = _free(allpat);
1777 /* LCL: regfree has bogus only */
1783 mi->mi_re = xrealloc(mi->mi_re, (mi->mi_nre + 1) * sizeof(*mi->mi_re));
1784 mire = mi->mi_re + mi->mi_nre;
1789 mire->pattern = allpat;
1790 mire->notmatch = notmatch;
1792 mire->cflags = cflags;
1793 mire->eflags = eflags;
1794 mire->fnflags = fnflags;
1797 qsort(mi->mi_re, mi->mi_nre, sizeof(*mi->mi_re), mireCmp);
1803 * Return iterator selector match.
1804 * @param mi rpm database iterator
1805 * @return 1 if header should be skipped
1807 static int mireSkip (const rpmdbMatchIterator mi)
1815 if (mi->mi_h == NULL) /* XXX can't happen */
1819 * Apply tag tests, implicitly "||" for multiple patterns/values of a
1820 * single tag, implicitly "&&" between multiple tag patterns.
1822 if ((mire = mi->mi_re) != NULL)
1823 for (int i = 0; i < mi->mi_nre; i++, mire++) {
1827 if (!headerGet(mi->mi_h, mire->tag, &td, HEADERGET_MINMEM)) {
1828 if (mire->tag != RPMTAG_EPOCH) {
1832 /* "is package already installed" checks rely on this behavior */
1834 td.type = RPM_INT32_TYPE;
1838 anymatch = 0; /* no matches yet */
1841 while (rpmtdNext(&td) >= 0) {
1842 char *str = rpmtdFormat(&td, RPMTD_FORMAT_STRING, NULL);
1844 rc = miregexec(mire, str);
1845 if ((!rc && !mire->notmatch) || (rc && mire->notmatch))
1850 if ((i+1) < mi->mi_nre && mire[0].tag == mire[1].tag) {
1864 return (ntags == nmatches ? 0 : 1);
1867 int rpmdbSetIteratorRewrite(rpmdbMatchIterator mi, int rewrite)
1872 rc = (mi->mi_cflags & DB_WRITECURSOR) ? 1 : 0;
1874 mi->mi_cflags |= DB_WRITECURSOR;
1876 mi->mi_cflags &= ~DB_WRITECURSOR;
1880 int rpmdbSetIteratorModified(rpmdbMatchIterator mi, int modified)
1885 rc = mi->mi_modified;
1886 mi->mi_modified = modified;
1890 int rpmdbSetHdrChk(rpmdbMatchIterator mi, rpmts ts,
1891 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
1896 /* XXX forward linkage prevents rpmtsLink */
1898 mi->mi_hdrchk = hdrchk;
1903 /* FIX: mi->mi_key.data may be NULL */
1904 Header rpmdbNextIterator(rpmdbMatchIterator mi)
1919 dbi = dbiOpen(mi->mi_db, RPMDBI_PACKAGES, 0);
1924 * Cursors are per-iterator, not per-dbi, so get a cursor for the
1925 * iterator on 1st call. If the iteration is to rewrite headers, and the
1926 * CDB model is used for the database, then the cursor needs to
1927 * marked with DB_WRITECURSOR as well.
1929 if (mi->mi_dbc == NULL)
1930 xx = dbiCopen(dbi, dbi->dbi_txnid, &mi->mi_dbc, mi->mi_cflags);
1933 memset(key, 0, sizeof(*key));
1934 data = &mi->mi_data;
1935 memset(data, 0, sizeof(*data));
1942 union _dbswap mi_offset;
1945 if (!(mi->mi_setx < mi->mi_set->count))
1947 mi->mi_offset = dbiIndexRecordOffset(mi->mi_set, mi->mi_setx);
1948 mi->mi_filenum = dbiIndexRecordFileNumber(mi->mi_set, mi->mi_setx);
1949 mi_offset.ui = mi->mi_offset;
1950 if (dbiByteSwapped(dbi) == 1)
1953 keylen = sizeof(mi_offset.ui);
1956 key->data = keyp = (void *)mi->mi_keyp;
1957 key->size = keylen = mi->mi_keylen;
1960 #if !defined(_USE_COPY_LOAD)
1961 data->flags |= DB_DBT_MALLOC;
1963 rc = dbiGet(dbi, mi->mi_dbc, key, data,
1964 (key->data == NULL ? DB_NEXT : DB_SET));
1972 * If we got the next key, save the header instance number.
1974 * For db3 Packages, instance 0 (i.e. mi->mi_setx == 0) is the
1975 * largest header instance in the database, and should be
1978 if (keyp && mi->mi_setx && rc == 0) {
1979 memcpy(&mi_offset, keyp, sizeof(mi_offset.ui));
1980 if (dbiByteSwapped(dbi) == 1)
1982 mi->mi_offset = mi_offset.ui;
1985 /* Terminate on error or end of keys */
1986 if (rc || (mi->mi_setx && mi->mi_offset == 0))
1990 } while (mi->mi_offset == 0);
1992 /* If next header is identical, return it now. */
1993 if (mi->mi_prevoffset && mi->mi_offset == mi->mi_prevoffset)
1996 /* Retrieve next header blob for index iterator. */
2000 #if !defined(_USE_COPY_LOAD)
2001 data->flags |= DB_DBT_MALLOC;
2003 rc = dbiGet(dbi, mi->mi_dbc, key, data, DB_SET);
2013 /* Rewrite current header (if necessary) and unlink. */
2014 xx = miFreeHeader(mi, dbi);
2016 /* Is this the end of the iteration? */
2020 /* Check header digest/signature once (if requested). */
2021 if (mi->mi_hdrchk && mi->mi_ts) {
2022 rpmRC rpmrc = RPMRC_NOTFOUND;
2024 /* Don't bother re-checking a previously read header. */
2025 if (mi->mi_db->db_bits) {
2028 set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
2029 &mi->mi_db->db_nbits, mi->mi_offset);
2030 if (PBM_ISSET(mi->mi_offset, set))
2034 /* If blob is unchecked, check blob import consistency now. */
2035 if (rpmrc != RPMRC_OK) {
2039 rpmrc = (*mi->mi_hdrchk) (mi->mi_ts, uh, uhlen, &msg);
2040 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
2041 rpmlog(lvl, "%s h#%8u %s",
2042 (rpmrc == RPMRC_FAIL ? _("rpmdbNextIterator: skipping") : " read"),
2043 mi->mi_offset, (msg ? msg : "\n"));
2046 /* Mark header checked. */
2047 if (mi->mi_db && mi->mi_db->db_bits && rpmrc == RPMRC_OK) {
2050 set = PBM_REALLOC((pbm_set **)&mi->mi_db->db_bits,
2051 &mi->mi_db->db_nbits, mi->mi_offset);
2052 PBM_SET(mi->mi_offset, set);
2055 /* Skip damaged and inconsistent headers. */
2056 if (rpmrc == RPMRC_FAIL)
2061 /* Did the header blob load correctly? */
2062 #if !defined(_USE_COPY_LOAD)
2063 mi->mi_h = headerLoad(uh);
2065 mi->mi_h->flags |= HEADERFLAG_ALLOCATED;
2067 mi->mi_h = headerCopyLoad(uh);
2069 if (mi->mi_h == NULL || !headerIsEntry(mi->mi_h, RPMTAG_NAME)) {
2071 _("rpmdb: damaged header #%u retrieved -- skipping.\n"),
2077 * Skip this header if iterator selector (if any) doesn't match.
2080 /* XXX hack, can't restart with Packages locked on single instance. */
2081 if (mi->mi_set || mi->mi_keyp == NULL)
2085 mi->mi_h->instance = mi->mi_offset;
2087 mi->mi_prevoffset = mi->mi_offset;
2088 mi->mi_modified = 0;
2093 static void rpmdbSortIterator(rpmdbMatchIterator mi)
2095 if (mi && mi->mi_set && mi->mi_set->recs && mi->mi_set->count > 0) {
2097 * mergesort is much (~10x with lots of identical basenames) faster
2098 * than pure quicksort, but glibc uses msort_with_tmp() on stack.
2100 #if defined(__GLIBC__)
2101 qsort(mi->mi_set->recs, mi->mi_set->count,
2102 sizeof(*mi->mi_set->recs), hdrNumCmp);
2104 mergesort(mi->mi_set->recs, mi->mi_set->count,
2105 sizeof(*mi->mi_set->recs), hdrNumCmp);
2112 static int rpmdbGrowIterator(rpmdbMatchIterator mi, int fpNum)
2117 dbiIndex dbi = NULL;
2126 dbcursor = mi->mi_dbc;
2128 data = &mi->mi_data;
2129 if (key->data == NULL)
2132 dbi = dbiOpen(mi->mi_db, mi->mi_rpmtag, 0);
2136 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2137 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
2139 xx = dbiCclose(dbi, dbcursor, 0);
2143 if (rc) { /* error/not found */
2144 if (rc != DB_NOTFOUND)
2146 _("error(%d) getting \"%s\" records from %s index\n"),
2147 rc, (char*)key->data, rpmTagGetName(dbi->dbi_rpmtag));
2149 xx = dbiCclose(dbi, dbcursor, 0);
2156 (void) dbt2set(dbi, data, &set);
2157 for (i = 0; i < set->count; i++)
2158 set->recs[i].fpNum = fpNum;
2161 xx = dbiCclose(dbi, dbcursor, 0);
2165 if (mi->mi_set == NULL) {
2169 fprintf(stderr, "+++ %d = %d + %d\t\"%s\"\n", (mi->mi_set->count + set->count), mi->mi_set->count, set->count, ((char *)key->data));
2171 mi->mi_set->recs = xrealloc(mi->mi_set->recs,
2172 (mi->mi_set->count + set->count) * sizeof(*(mi->mi_set->recs)));
2173 memcpy(mi->mi_set->recs + mi->mi_set->count, set->recs,
2174 set->count * sizeof(*(mi->mi_set->recs)));
2175 mi->mi_set->count += set->count;
2176 set = dbiFreeIndexSet(set);
2182 int rpmdbPruneIterator(rpmdbMatchIterator mi, int * hdrNums,
2183 int nHdrNums, int sorted)
2185 if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
2189 (void) dbiPruneSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), sorted);
2193 int rpmdbAppendIterator(rpmdbMatchIterator mi, const int * hdrNums, int nHdrNums)
2195 if (mi == NULL || hdrNums == NULL || nHdrNums <= 0)
2198 if (mi->mi_set == NULL)
2199 mi->mi_set = xcalloc(1, sizeof(*mi->mi_set));
2200 (void) dbiAppendSet(mi->mi_set, hdrNums, nHdrNums, sizeof(*hdrNums), 0);
2204 rpmdbMatchIterator rpmdbInitIterator(rpmdb db, rpmTag rpmtag,
2205 const void * keyp, size_t keylen)
2207 rpmdbMatchIterator mi;
2210 dbiIndexSet set = NULL;
2212 void * mi_keyp = NULL;
2218 (void) rpmdbCheckSignals();
2220 /* XXX HACK to remove rpmdbFindByLabel/findMatches from the API */
2221 if (rpmtag == RPMDBI_LABEL) {
2222 rpmtag = RPMTAG_NAME;
2226 dbi = dbiOpen(db, rpmtag, 0);
2230 /* Chain cursors for teardown on abnormal exit. */
2231 mi = xcalloc(1, sizeof(*mi));
2232 mi->mi_next = rpmmiRock;
2236 data = &mi->mi_data;
2239 * Handle label and file name special cases.
2240 * Otherwise, retrieve join keys for secondary lookup.
2242 if (rpmtag != RPMDBI_PACKAGES && keyp) {
2243 DBC * dbcursor = NULL;
2248 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2249 rc = dbiFindByLabel(dbi, dbcursor, key, data, keyp, &set);
2250 xx = dbiCclose(dbi, dbcursor, 0);
2252 } else if (rpmtag == RPMTAG_BASENAMES) {
2253 rc = rpmdbFindByFile(db, keyp, key, data, &set);
2255 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0);
2257 key->data = (void *) keyp;
2259 if (key->data && key->size == 0)
2260 key->size = strlen((char *)key->data);
2261 if (key->data && key->size == 0)
2262 key->size++; /* XXX "/" fixup. */
2264 rc = dbiGet(dbi, dbcursor, key, data, DB_SET);
2267 _("error(%d) getting \"%s\" records from %s index\n"),
2268 rc, (key->data ? (char *)key->data : "???"),
2269 rpmTagGetName(dbi->dbi_rpmtag));
2272 /* Join keys need to be native endian internally. */
2274 (void) dbt2set(dbi, data, &set);
2276 xx = dbiCclose(dbi, dbcursor, 0);
2279 if (rc) { /* error/not found */
2280 set = dbiFreeIndexSet(set);
2281 rpmmiRock = mi->mi_next;
2288 /* Copy the retrieval key, byte swapping header instance if necessary. */
2291 case RPMDBI_PACKAGES:
2294 assert(keylen == sizeof(k->ui)); /* xxx programmer error */
2295 k = xmalloc(sizeof(*k));
2296 memcpy(k, keyp, keylen);
2297 if (dbiByteSwapped(dbi) == 1)
2304 keylen = strlen(keyp);
2305 k = xmalloc(keylen + 1);
2306 memcpy(k, keyp, keylen);
2307 k[keylen] = '\0'; /* XXX assumes strings */
2313 mi->mi_keyp = mi_keyp;
2314 mi->mi_keylen = keylen;
2316 mi->mi_db = rpmdbLink(db, RPMDBG_M("matchIterator"));
2317 mi->mi_rpmtag = rpmtag;
2325 mi->mi_modified = 0;
2326 mi->mi_prevoffset = 0;
2333 mi->mi_hdrchk = NULL;
2339 * Convert current tag data to db key
2340 * @param tagdata Tag data container
2341 * @retval key DB key struct
2342 * @retval freedata Should key.data be freed afterwards
2343 * Return 0 to signal this item should be discarded (ie continue)
2345 static int td2key(rpmtd tagdata, DBT *key, int *freedata)
2347 const char *str = NULL;
2348 uint8_t *bin = NULL;
2351 switch (rpmtdType(tagdata)) {
2354 key->size = sizeof(uint8_t);
2355 key->data = rpmtdGetChar(tagdata);
2357 case RPM_INT16_TYPE:
2358 key->size = sizeof(uint16_t);
2359 key->data = rpmtdGetUint16(tagdata);
2361 case RPM_INT32_TYPE:
2362 key->size = sizeof(uint32_t);
2363 key->data = rpmtdGetUint32(tagdata);
2365 case RPM_INT64_TYPE:
2366 key->size = sizeof(uint64_t);
2367 key->data = rpmtdGetUint64(tagdata);
2370 key->size = tagdata->count;
2371 key->data = tagdata->data;
2373 case RPM_STRING_TYPE:
2374 case RPM_I18NSTRING_TYPE:
2375 case RPM_STRING_ARRAY_TYPE:
2376 str = rpmtdGetString(tagdata);
2377 if (rpmtdTag(tagdata) == RPMTAG_FILEDIGESTS) {
2381 /* Filter out empty MD5 strings. */
2382 if (!(str && *str != '\0'))
2385 binlen = strlen(str) / 2;
2386 bin = xmalloc(binlen);
2387 /* Convert from hex to binary. */
2389 for (int j = 0; j < binlen; j++, t++, str += 2)
2390 *t = (rnibble(str[0]) << 4) | rnibble(str[1]);
2395 } else if (rpmtdTag(tagdata) == RPMTAG_PUBKEYS) {
2396 /* Extract the pubkey id from the base64 blob. */
2397 bin = xmalloc(sizeof(pgpKeyID_t));
2398 int nbin = pgpExtractPubkeyFingerprint(str, bin);
2411 str = rpmtdGetString(tagdata);
2412 key->data = (char *) str; /* XXX discards const */
2413 key->size = strlen(str);
2418 key->size = strlen((char *)key->data);
2420 key->size++; /* XXX "/" fixup. */
2425 static void logAddRemove(int removing, rpmtd tagdata)
2427 rpm_count_t c = rpmtdCount(tagdata);
2428 if (c == 1 && rpmtdType(tagdata) == RPM_STRING_TYPE) {
2429 rpmlog(RPMLOG_DEBUG, "%s \"%s\" %s %s index.\n",
2430 removing ? "removing" : "adding", rpmtdGetString(tagdata),
2431 removing ? "from" : "to",
2432 rpmTagGetName(rpmtdTag(tagdata)));
2434 rpmlog(RPMLOG_DEBUG, "%s %d entries %s %s index.\n",
2435 removing ? "removing" : "adding", c,
2436 removing ? "from" : "to",
2437 rpmTagGetName(rpmtdTag(tagdata)));
2442 int rpmdbRemove(rpmdb db, int rid, unsigned int hdrNum,
2444 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
2446 DBC * dbcursor = NULL;
2449 union _dbswap mi_offset;
2451 sigset_t signalMask;
2458 memset(&key, 0, sizeof(key));
2459 memset(&data, 0, sizeof(data));
2461 { rpmdbMatchIterator mi;
2462 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &hdrNum, sizeof(hdrNum));
2463 h = rpmdbNextIterator(mi);
2466 mi = rpmdbFreeIterator(mi);
2470 rpmlog(RPMLOG_ERR, _("%s: cannot read header at 0x%x\n"),
2471 "rpmdbRemove", hdrNum);
2476 char *nevra = headerGetNEVRA(h, NULL);
2477 rpmlog(RPMLOG_DEBUG, " --- h#%8u %s\n", hdrNum, nevra);
2481 (void) blockSignals(&signalMask);
2483 /* FIX: rpmvals heartburn */
2485 dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
2487 if (dbiTags.tags != NULL)
2488 for (dbix = 0; dbix < dbiTags.max; dbix++) {
2492 struct rpmtd_s tagdata;
2495 rpmtag = dbiTags.tags[dbix];
2497 /* Filter out temporary databases */
2498 if (isTemporaryDB(rpmtag))
2501 if (rpmtag == RPMDBI_PACKAGES) {
2502 dbi = dbiOpen(db, rpmtag, 0);
2503 if (dbi == NULL) /* XXX shouldn't happen */
2506 mi_offset.ui = hdrNum;
2507 if (dbiByteSwapped(dbi) == 1)
2509 key.data = &mi_offset;
2510 key.size = sizeof(mi_offset.ui);
2512 rc = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2513 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2516 _("error(%d) setting header #%d record for %s removal\n"),
2517 rc, hdrNum, rpmTagGetName(dbi->dbi_rpmtag));
2519 rc = dbiDel(dbi, dbcursor, &key, &data, 0);
2520 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2522 if (!dbi->dbi_no_dbsync)
2523 xx = dbiSync(dbi, 0);
2527 if (!headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM))
2530 if (!(dbi = dbiOpen(db, rpmtag, 0))) {
2531 rpmtdFreeData(&tagdata);
2534 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2536 logAddRemove(1, &tagdata);
2537 while (rpmtdNext(&tagdata) >= 0) {
2541 if (!td2key(&tagdata, &key, &freedata)) {
2546 * This is almost right, but, if there are duplicate tag
2547 * values, there will be duplicate attempts to remove
2548 * the header instance. It's faster to just ignore errors
2549 * than to do things correctly.
2553 * XXX with duplicates, an accurate data value and
2554 * DB_GET_BOTH is needed.
2558 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2559 if (rc == 0) { /* success */
2560 (void) dbt2set(dbi, &data, &set);
2561 } else if (rc == DB_NOTFOUND) { /* not found */
2563 } else { /* error */
2565 _("error(%d) setting \"%s\" records from %s index\n"),
2566 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2571 rc = dbiPruneSet(set, rec, 1, sizeof(*rec), 1);
2573 /* If nothing was pruned, then don't bother updating. */
2575 set = dbiFreeIndexSet(set);
2579 if (set->count > 0) {
2580 (void) set2dbt(dbi, &data, set);
2581 rc = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2584 _("error(%d) storing record \"%s\" into %s\n"),
2585 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2588 data.data = _free(data.data);
2591 rc = dbiDel(dbi, dbcursor, &key, &data, 0);
2594 _("error(%d) removing record \"%s\" from %s\n"),
2595 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2599 set = dbiFreeIndexSet(set);
2606 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2609 if (!dbi->dbi_no_dbsync)
2610 xx = dbiSync(dbi, 0);
2612 rpmtdFreeData(&tagdata);
2618 (void) unblockSignals(&signalMask);
2622 /* XXX return ret; */
2627 int rpmdbAdd(rpmdb db, int iid, Header h,
2629 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
2631 DBC * dbcursor = NULL;
2634 sigset_t signalMask;
2637 union _dbswap mi_offset;
2638 unsigned int hdrNum = 0;
2646 memset(&key, 0, sizeof(key));
2647 memset(&data, 0, sizeof(data));
2649 #ifdef NOTYET /* XXX headerDel() broken on dribbles. */
2650 xx = headerDel(h, RPMTAG_REMOVETID);
2652 if (iid != 0 && iid != -1) {
2653 rpm_tid_t tid = iid;
2654 if (!headerIsEntry(h, RPMTAG_INSTALLTID))
2655 headerPutUint32(h, RPMTAG_INSTALLTID, &tid, 1);
2658 (void) blockSignals(&signalMask);
2661 unsigned int firstkey = 0;
2662 void * keyp = &firstkey;
2663 size_t keylen = sizeof(firstkey);
2664 void * datap = NULL;
2667 dbi = dbiOpen(db, RPMDBI_PACKAGES, 0);
2670 /* XXX db0: hack to pass sizeof header to fadAlloc */
2672 datalen = headerSizeof(h, HEADER_MAGIC_NO);
2674 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2676 /* Retrieve join key for next header instance. */
2681 data.size = datalen;
2682 ret = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2686 datalen = data.size;
2689 if (ret == 0 && datap) {
2690 memcpy(&mi_offset, datap, sizeof(mi_offset.ui));
2691 if (dbiByteSwapped(dbi) == 1)
2693 hdrNum = mi_offset.ui;
2696 mi_offset.ui = hdrNum;
2697 if (dbiByteSwapped(dbi) == 1)
2699 if (ret == 0 && datap) {
2700 memcpy(datap, &mi_offset, sizeof(mi_offset.ui));
2703 datalen = sizeof(mi_offset.ui);
2709 data.size = datalen;
2711 ret = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2712 xx = dbiSync(dbi, 0);
2714 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2722 _("error(%d) allocating new package instance\n"), ret);
2726 /* Now update the indexes */
2730 dbiIndexItem rec = dbiIndexNewItem(hdrNum, 0);
2732 if (dbiTags.tags != NULL)
2733 for (dbix = 0; dbix < dbiTags.max; dbix++) {
2737 struct rpmtd_s tagdata, reqflags;
2739 rpmrc = RPMRC_NOTFOUND;
2741 rpmtag = dbiTags.tags[dbix];
2743 /* Filter out temporary databases */
2744 if (isTemporaryDB(rpmtag))
2748 case RPMDBI_PACKAGES:
2749 dbi = dbiOpen(db, rpmtag, 0);
2750 if (dbi == NULL) /* XXX shouldn't happen */
2752 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2754 mi_offset.ui = hdrNum;
2755 if (dbiByteSwapped(dbi) == 1)
2757 key.data = (void *) &mi_offset;
2758 key.size = sizeof(mi_offset.ui);
2759 data.data = headerUnload(h);
2760 data.size = headerSizeof(h, HEADER_MAGIC_NO);
2762 /* Check header digest/signature on blob export. */
2767 rpmrc = (*hdrchk) (ts, data.data, data.size, &msg);
2768 lvl = (rpmrc == RPMRC_FAIL ? RPMLOG_ERR : RPMLOG_DEBUG);
2769 rpmlog(lvl, "%s h#%8u %s",
2770 (rpmrc == RPMRC_FAIL ? _("rpmdbAdd: skipping") : " +++"),
2771 hdrNum, (msg ? msg : "\n"));
2775 if (data.data != NULL && rpmrc != RPMRC_FAIL) {
2776 xx = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2777 xx = dbiSync(dbi, 0);
2779 data.data = _free(data.data);
2781 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2783 if (!dbi->dbi_no_dbsync)
2784 xx = dbiSync(dbi, 0);
2787 case RPMTAG_REQUIRENAME:
2788 headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM);
2789 headerGet(h, RPMTAG_REQUIREFLAGS, &reqflags, HEADERGET_MINMEM);
2792 headerGet(h, rpmtag, &tagdata, HEADERGET_MINMEM);
2796 if (rpmtdCount(&tagdata) == 0) {
2797 if (rpmtag != RPMTAG_GROUP)
2800 /* XXX preserve legacy behavior */
2801 tagdata.type = RPM_STRING_TYPE;
2802 tagdata.data = (const char **) "Unknown";
2806 if (!(dbi = dbiOpen(db, rpmtag, 0))) {
2807 rpmtdFreeData(&tagdata);
2810 xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR);
2812 logAddRemove(0, &tagdata);
2813 while (rpmtdNext(&tagdata) >= 0) {
2815 int i, freedata = 0;
2818 * Include the tagNum in all indices. rpm-3.0.4 and earlier
2819 * included the tagNum only for files.
2821 i = rec->tagNum = rpmtdGetIndex(&tagdata);
2823 case RPMTAG_REQUIRENAME: {
2824 /* Filter out install prerequisites. */
2825 rpm_flag_t *rflag = rpmtdNextUint32(&reqflags);
2826 if (rflag && isInstallPreReq(*rflag))
2830 case RPMTAG_TRIGGERNAME:
2831 if (i > 0) { /* don't add duplicates */
2832 const char **tnames = tagdata.data;
2833 const char *str = rpmtdGetString(&tagdata);
2834 for (j = 0; j < i; j++) {
2835 if (!strcmp(str, tnames[j]))
2846 if (!td2key(&tagdata, &key, &freedata)) {
2851 * XXX with duplicates, an accurate data value and
2852 * DB_GET_BOTH is needed.
2857 rc = dbiGet(dbi, dbcursor, &key, &data, DB_SET);
2858 if (rc == 0) { /* success */
2859 /* With duplicates, cursor is positioned, discard the record. */
2860 if (!dbi->dbi_permit_dups)
2861 (void) dbt2set(dbi, &data, &set);
2862 } else if (rc != DB_NOTFOUND) { /* error */
2864 _("error(%d) getting \"%s\" records from %s index\n"),
2865 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2870 if (set == NULL) /* not found or duplicate */
2871 set = xcalloc(1, sizeof(*set));
2873 (void) dbiAppendSet(set, rec, 1, sizeof(*rec), 0);
2875 (void) set2dbt(dbi, &data, set);
2876 rc = dbiPut(dbi, dbcursor, &key, &data, DB_KEYLAST);
2880 _("error(%d) storing record %s into %s\n"),
2881 rc, (char*)key.data, rpmTagGetName(dbi->dbi_rpmtag));
2884 data.data = _free(data.data);
2886 set = dbiFreeIndexSet(set);
2893 xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR);
2896 if (!dbi->dbi_no_dbsync)
2897 xx = dbiSync(dbi, 0);
2899 rpmtdFreeData(&tagdata);
2906 (void) unblockSignals(&signalMask);
2911 #define _skip(_dn) { sizeof(_dn)-1, (_dn) }
2913 static struct skipDir_s {
2920 static int skipDir(const char * dn)
2922 struct skipDir_s * sd = skipDirs;
2926 for (sd = skipDirs; sd->dn != NULL; sd++) {
2927 if (dnlen < sd->dnlen)
2929 if (strncmp(dn, sd->dn, sd->dnlen))
2936 /* XXX transaction.c */
2937 int rpmdbFindFpList(rpmdb db, fingerPrint * fpList, dbiIndexSet * matchList,
2938 int numItems, fingerPrintCache fpc)
2942 rpmdbMatchIterator mi;
2946 if (db == NULL) return 1;
2948 mi = rpmdbInitIterator(db, RPMTAG_BASENAMES, NULL, 0);
2949 if (mi == NULL) /* XXX should never happen */
2953 data = &mi->mi_data;
2955 /* Gather all installed headers with matching basename's. */
2956 for (i = 0; i < numItems; i++) {
2958 matchList[i] = xcalloc(1, sizeof(*(matchList[i])));
2960 key->data = (void *) fpList[i].baseName;
2961 key->size = strlen((char *)key->data);
2963 key->size++; /* XXX "/" fixup. */
2965 if (skipDir(fpList[i].entry->dirName))
2968 xx = rpmdbGrowIterator(mi, i);
2972 if ((i = rpmdbGetIteratorCount(mi)) == 0) {
2973 mi = rpmdbFreeIterator(mi);
2977 rpmdbSortIterator(mi);
2978 /* iterator is now sorted by (recnum, filenum) */
2980 /* For all installed headers with matching basename's ... */
2982 while ((h = rpmdbNextIterator(mi)) != NULL) {
2983 headerGetFlags hgflags = HEADERGET_MINMEM;
2984 struct rpmtd_s bnames, dnames, dindexes;
2985 const char ** dirNames;
2986 const char ** baseNames;
2987 const char ** fullBaseNames;
2988 uint32_t * dirIndexes;
2989 uint32_t * fullDirIndexes;
2996 start = mi->mi_setx - 1;
2997 im = mi->mi_set->recs + start;
2999 /* Find the end of the set of matched basename's in this package. */
3000 for (end = start + 1; end < mi->mi_set->count; end++) {
3001 if (im->hdrNum != mi->mi_set->recs[end].hdrNum)
3006 /* Compute fingerprints for this installed header's matches */
3007 headerGet(h, RPMTAG_BASENAMES, &bnames, hgflags);
3008 headerGet(h, RPMTAG_DIRNAMES, &dnames, hgflags);
3009 headerGet(h, RPMTAG_DIRINDEXES, &dindexes, hgflags);
3010 fullBaseNames = bnames.data;
3011 dirNames = dnames.data;
3012 fullDirIndexes = dindexes.data;
3014 baseNames = xcalloc(num, sizeof(*baseNames));
3015 dirIndexes = xcalloc(num, sizeof(*dirIndexes));
3016 for (i = 0; i < num; i++) {
3017 baseNames[i] = fullBaseNames[im[i].tagNum];
3018 dirIndexes[i] = fullDirIndexes[im[i].tagNum];
3021 fps = xcalloc(num, sizeof(*fps));
3022 fpLookupList(fpc, dirNames, baseNames, dirIndexes, num, fps);
3024 /* Add db (recnum,filenum) to list for fingerprint matches. */
3025 for (i = 0; i < num; i++, im++) {
3026 /* FIX: fpList[].subDir may be NULL */
3027 if (!FP_EQUAL(fps[i], fpList[im->fpNum]))
3029 xx = dbiAppendSet(matchList[im->fpNum], im, 1, sizeof(*im), 0);
3033 rpmtdFreeData(&bnames);
3034 rpmtdFreeData(&dnames);
3035 rpmtdFreeData(&dindexes);
3036 baseNames = _free(baseNames);
3037 dirIndexes = _free(dirIndexes);
3042 mi = rpmdbFreeIterator(mi);
3049 * Remove DB4 environment (and lock), ie the equivalent of
3050 * rm -f <prefix>/<dbpath>/__db.???
3051 * Environment files not existing is not an error, failure to unlink is,
3052 * return zero on success.
3053 * Only useful for BDB, dbapi 3 and 4.
3054 * TODO/FIX: push this down to db3.c where it belongs
3056 static int cleanDbenv(const char *prefix, const char *dbpath)
3058 ARGV_t paths = NULL, p;
3060 char *pattern = rpmGetPath(prefix, "/", dbpath, "/__db.???", NULL);
3062 if (rpmGlob(pattern, NULL, &paths) == 0) {
3063 for (p = paths; *p; p++) {
3072 static int rpmdbRemoveDatabase(const char * prefix,
3073 const char * dbpath, int _dbapi)
3083 if (dbiTags.tags != NULL)
3084 for (i = 0; i < dbiTags.max; i++) {
3085 const char * base = rpmTagGetName(dbiTags.tags[i]);
3086 path = rpmGetPath(prefix, "/", dbpath, "/", base, NULL);
3087 if (access(path, F_OK) == 0)
3091 cleanDbenv(prefix, dbpath);
3100 path = rpmGetPath(prefix, "/", dbpath, NULL);
3107 static int rpmdbMoveDatabase(const char * prefix,
3108 const char * olddbpath, int _olddbapi,
3109 const char * newdbpath, int _newdbapi)
3115 int selinux = is_selinux_enabled() && (matchpathcon_init(NULL) != -1);
3119 blockSignals(&sigMask);
3120 switch (_olddbapi) {
3124 if (dbiTags.tags != NULL)
3125 for (i = 0; i < dbiTags.max; i++) {
3130 /* Filter out temporary databases */
3131 if (isTemporaryDB((rpmtag = dbiTags.tags[i])))
3134 base = rpmTagGetName(rpmtag);
3135 src = rpmGetPath(prefix, "/", olddbpath, "/", base, NULL);
3136 dest = rpmGetPath(prefix, "/", newdbpath, "/", base, NULL);
3138 if (access(src, F_OK) != 0)
3142 * Restore uid/gid/mode/mtime/security context if possible.
3144 if (stat(dest, &st) < 0)
3145 if (stat(src, &st) < 0)
3148 if ((xx = rename(src, dest)) != 0) {
3152 xx = chown(dest, st.st_uid, st.st_gid);
3153 xx = chmod(dest, (st.st_mode & 07777));
3154 { struct utimbuf stamp;
3155 stamp.actime = st.st_atime;
3156 stamp.modtime = st.st_mtime;
3157 xx = utime(dest, &stamp);
3161 security_context_t scon = NULL;
3162 if (matchpathcon(dest, st.st_mode, &scon) != -1) {
3163 (void) setfilecon(dest, scon);
3173 cleanDbenv(prefix, olddbpath);
3174 cleanDbenv(prefix, newdbpath);
3181 unblockSignals(&sigMask);
3183 #ifdef SQLITE_HACK_XXX
3184 if (rc || _olddbapi == _newdbapi)
3187 rc = rpmdbRemoveDatabase(prefix, newdbpath, _newdbapi);
3191 (void) matchpathcon_fini();
3197 int rpmdbRebuild(const char * prefix, rpmts ts,
3198 rpmRC (*hdrchk) (rpmts ts, const void *uh, size_t uc, char ** msg))
3201 char * dbpath = NULL;
3202 char * rootdbpath = NULL;
3204 char * newdbpath = NULL;
3205 char * newrootdbpath = NULL;
3214 if (prefix == NULL) prefix = "/";
3216 _dbapi = rpmExpandNumeric("%{_dbapi}");
3217 _dbapi_rebuild = rpmExpandNumeric("%{_dbapi_rebuild}");
3219 tfn = rpmGetPath("%{?_dbpath}", NULL);
3220 if (!(tfn && tfn[0] != '\0'))
3222 rpmlog(RPMLOG_ERR, _("no dbpath has been set"));
3226 dbpath = rootdbpath = rpmGetPath(prefix, tfn, NULL);
3227 if (!(prefix[0] == '/' && prefix[1] == '\0'))
3228 dbpath += strlen(prefix) - 1;
3231 tfn = rpmGetPath("%{?_dbpath_rebuild}", NULL);
3232 if (!(tfn && tfn[0] != '\0' && strcmp(tfn, dbpath)))
3235 rasprintf(&tfn, "%srebuilddb.%d", dbpath, (int) getpid());
3238 newdbpath = newrootdbpath = rpmGetPath(prefix, tfn, NULL);
3239 if (!(prefix[0] == '/' && prefix[1] == '\0'))
3240 newdbpath += strlen(prefix) - 1;
3243 rpmlog(RPMLOG_DEBUG, "rebuilding database %s into %s\n",
3244 rootdbpath, newrootdbpath);
3246 if (!access(newrootdbpath, F_OK)) {
3247 rpmlog(RPMLOG_ERR, _("temporary database %s already exists\n"),
3253 rpmlog(RPMLOG_DEBUG, "creating directory %s\n", newrootdbpath);
3254 if (mkdir(newrootdbpath, 0755)) {
3255 rpmlog(RPMLOG_ERR, _("failed to create directory %s: %s\n"),
3256 newrootdbpath, strerror(errno));
3262 _rebuildinprogress = 0;
3264 rpmlog(RPMLOG_DEBUG, "opening old database with dbapi %d\n",
3266 if (openDatabase(prefix, dbpath, _dbapi, &olddb, O_RDONLY, 0644,
3267 RPMDB_FLAG_MINIMAL)) {
3271 _dbapi = olddb->db_api;
3272 _rebuildinprogress = 1;
3273 rpmlog(RPMLOG_DEBUG, "opening new database with dbapi %d\n",
3275 (void) rpmDefineMacro(NULL, "_rpmdb_rebuild %{nil}", -1);
3276 if (openDatabase(prefix, newdbpath, _dbapi_rebuild, &newdb, O_RDWR | O_CREAT, 0644, 0)) {
3281 _rebuildinprogress = 0;
3283 _dbapi_rebuild = newdb->db_api;
3286 rpmdbMatchIterator mi;
3287 #define _RECNUM rpmdbGetIteratorOffset(mi)
3289 mi = rpmdbInitIterator(olddb, RPMDBI_PACKAGES, NULL, 0);
3291 (void) rpmdbSetHdrChk(mi, ts, hdrchk);
3293 while ((h = rpmdbNextIterator(mi)) != NULL) {
3295 /* let's sanity check this record a bit, otherwise just skip it */
3296 if (!(headerIsEntry(h, RPMTAG_NAME) &&
3297 headerIsEntry(h, RPMTAG_VERSION) &&
3298 headerIsEntry(h, RPMTAG_RELEASE) &&
3299 headerIsEntry(h, RPMTAG_BUILDTIME)))
3302 _("header #%u in the database is bad -- skipping.\n"),
3307 /* Filter duplicate entries ? (bug in pre rpm-3.0.4) */
3308 if (_db_filter_dups || newdb->db_filter_dups) {
3309 const char * name, * version, * release;
3312 (void) headerNVR(h, &name, &version, &release);
3314 { rpmdbMatchIterator mi;
3315 mi = rpmdbInitIterator(newdb, RPMTAG_NAME, name, 0);
3316 (void) rpmdbSetIteratorRE(mi, RPMTAG_VERSION,
3317 RPMMIRE_DEFAULT, version);
3318 (void) rpmdbSetIteratorRE(mi, RPMTAG_RELEASE,
3319 RPMMIRE_DEFAULT, release);
3320 while (rpmdbNextIterator(mi)) {
3324 mi = rpmdbFreeIterator(mi);
3331 /* Deleted entries are eliminated in legacy headers by copy. */
3332 { Header nh = (headerIsEntry(h, RPMTAG_HEADERIMAGE)
3333 ? headerCopy(h) : NULL);
3334 rc = rpmdbAdd(newdb, -1, (nh ? nh : h), ts, hdrchk);
3335 nh = headerFree(nh);
3340 _("cannot add record originally at %u\n"), _RECNUM);
3346 mi = rpmdbFreeIterator(mi);
3350 xx = rpmdbClose(olddb);
3351 xx = rpmdbClose(newdb);
3354 rpmlog(RPMLOG_WARNING,
3355 _("failed to rebuild database: original database "
3356 "remains in place\n"));
3358 xx = rpmdbRemoveDatabase(prefix, newdbpath, _dbapi_rebuild);
3361 } else if (!nocleanup) {
3362 if (rpmdbMoveDatabase(prefix, newdbpath, _dbapi_rebuild, dbpath, _dbapi)) {
3363 rpmlog(RPMLOG_ERR, _("failed to replace old database with new "
3365 rpmlog(RPMLOG_ERR, _("replace files in %s with files from %s "
3366 "to recover"), dbpath, newdbpath);
3374 if (removedir && !(rc == 0 && nocleanup)) {
3375 rpmlog(RPMLOG_DEBUG, "removing directory %s\n", newrootdbpath);
3376 if (rmdir(newrootdbpath))
3377 rpmlog(RPMLOG_ERR, _("failed to remove directory %s: %s\n"),
3378 newrootdbpath, strerror(errno));
3380 newrootdbpath = _free(newrootdbpath);
3381 rootdbpath = _free(rootdbpath);