diff options
-rw-r--r-- | rpmdb/db1.c | 621 | ||||
-rw-r--r-- | rpmdb/rpmdb.c | 217 | ||||
-rw-r--r-- | rpmdb/rpmdb.h | 38 |
3 files changed, 72 insertions, 804 deletions
diff --git a/rpmdb/db1.c b/rpmdb/db1.c deleted file mode 100644 index 9e32798a5..000000000 --- a/rpmdb/db1.c +++ /dev/null @@ -1,621 +0,0 @@ -/*@-type@*/ /* FIX: shrug */ -/** \ingroup db1 - * \file rpmdb/db1.c - */ - -#include "system.h" - -#define _mymemset(_a, _b, _c) - -#include <rpmio_internal.h> -#include <rpmlib.h> -#include <rpmmacro.h> /* XXX rpmGenPath */ -#include <rpmurl.h> /* XXX urlGetPath */ - -#include "falloc.h" -#include "legacy.h" /* XXX providePackageNVR() and compressFileList() */ -#include "rpmdb.h" - -/* XXX must follow rpmdb.h */ -#define DB_VERSION_MAJOR 1 -#define DB_VERSION_MINOR 85 -#define DB_VERSION_PATCH 0 - -struct _DBT1 { - void * data; /* data */ - size_t size; /* data length */ -}; - -#undef DBT -#define DBT struct _DBT1 - -#include "debug.h" - -/*@access Header@*/ /* XXX compared with NULL */ -/*@access rpmdb@*/ -/*@access dbiIndex@*/ -/*@access dbiIndexSet@*/ -/*@-onlytrans@*/ - -#ifdef DYING -/* XXX remap DB3 types back into DB1 types */ -static inline DBTYPE db3_to_dbtype(int dbitype) -{ - switch(dbitype) { - case 1: return DB_BTREE; - case 2: return DB_HASH; - case 3: return DB_RECNO; - case 4: return DB_HASH; /* XXX W2DO? */ - case 5: return DB_HASH; /* XXX W2DO? */ - } - /*@notreached@*/ return DB_HASH; -} - -/*@-shadow@*/ -static /*@observer@*/ char * db_strerror(int error) -/*@=shadow@*/ -{ - if (error == 0) - return ("Successful return: 0"); - if (error > 0) - return (strerror(error)); - - switch (error) { - default: - { - /* - * !!! - * Room for a 64-bit number + slop. This buffer is only used - * if we're given an unknown error, which should never happen. - * Note, however, we're no longer thread-safe if it does. - */ - static char ebuf[40]; - char * t = ebuf; - - *t = '\0'; - t = stpcpy(t, "Unknown error: "); - sprintf(t, "%d", error); - return(ebuf); - } - } - /*@notreached@*/ -} - -static int cvtdberr(dbiIndex dbi, const char * msg, int error, int printit) - /*@*/ -{ - int rc = 0; - - if (error == 0) - rc = 0; - else if (error < 0) - rc = errno; - else if (error > 0) - rc = -1; - - if (printit && rc) { - if (msg) - rpmError(RPMERR_DBERR, _("db%d error(%d) from %s: %s\n"), - dbi->dbi_api, rc, msg, db_strerror(error)); - else - rpmError(RPMERR_DBERR, _("db%d error(%d): %s\n"), - dbi->dbi_api, rc, db_strerror(error)); - } - - return rc; -} -#endif /* DYING */ - -static int db1sync(dbiIndex dbi, /*@unused@*/ unsigned int flags) - /*@globals fileSystem @*/ - /*@modifies fileSystem @*/ -{ - int rc = 0; - - if (dbi->dbi_db) { - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs = dbi->dbi_db; - int fdno = Fileno(pkgs); - if (fdno >= 0 && (rc = fsync(fdno)) != 0) - rc = errno; - } -#ifdef DYING - else { - DB * db = dbi->dbi_db; - rc = db->sync(db, flags); - rc = cvtdberr(dbi, "db->sync", rc, _debug); - } -#endif - } - - return rc; -} - -/*@null@*/ static void * doGetRecord(dbiIndex dbi, unsigned int offset) - /*@globals fileSystem @*/ - /*@modifies dbi, fileSystem @*/ -{ - FD_t pkgs = dbi->dbi_db; - void * uh = NULL; - Header h = NULL; - const char ** fileNames; - int fileCount = 0; - int lasto = 0; - int i; - -retry: - if (offset >= fadGetFileSize(pkgs)) - goto exit; - - (void)Fseek(pkgs, offset, SEEK_SET); - - h = headerRead(pkgs, HEADER_MAGIC_NO); - - /* let's sanity check this record a bit, otherwise just skip it */ - if (h != NULL && - !( headerIsEntry(h, RPMTAG_NAME) && - headerIsEntry(h, RPMTAG_VERSION) && - headerIsEntry(h, RPMTAG_RELEASE) && - headerIsEntry(h, RPMTAG_BUILDTIME))) - { - h = headerFree(h, "doGetRecord"); - } - - if (h == NULL) { - /* XXX HACK: try to reconnect broken chain. */ - if (lasto == 0) { - rpmMessage(RPMMESS_WARNING, - _("Broken package chain at offset %d(0x%08x), attempting to reconnect ...\n"), - (int) offset, offset); - lasto = (offset ? offset : -1); - offset = fadNextOffset(pkgs, offset); - if (offset > 0) - goto retry; - } - goto exit; - } - - if (lasto) { - rpmMessage(RPMMESS_WARNING, - _("Reconnecting broken chain at offset %d(0x%08x).\n"), - (int) offset, offset); - dbi->dbi_lastoffset = offset; - } - - /* Retrofit "Provide: name = EVR" for binary packages. */ - providePackageNVR(h); - - /* - * The RPM used to build much of RH 5.1 could produce packages whose - * file lists did not have leading /'s. Now is a good time to fix that. - */ - - /* - * If this tag isn't present, either no files are in the package or - * we're dealing with a package that has just the compressed file name - * list. - */ - if (!headerGetEntryMinMemory(h, RPMTAG_OLDFILENAMES, NULL, - (const void **) &fileNames, &fileCount)) - goto exit; - - for (i = 0; i < fileCount; i++) - if (*fileNames[i] != '/') break; - - if (i == fileCount) { - free(fileNames); - } else { /* bad header -- let's clean it up */ - const char ** newFileNames = alloca(sizeof(*newFileNames) * fileCount); - for (i = 0; i < fileCount; i++) { - char * newFileName = alloca(strlen(fileNames[i]) + 2); - if (*fileNames[i] != '/') { - newFileName[0] = '/'; - newFileName[1] = '\0'; - } else - newFileName[0] = '\0'; - strcat(newFileName, fileNames[i]); - newFileNames[i] = newFileName; - } - - free(fileNames); - - (void) headerModifyEntry(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE, - newFileNames, fileCount); - } - - /* - * The file list was moved to a more compressed format which not - * only saves memory (nice), but gives fingerprinting a nice, fat - * speed boost (very nice). Go ahead and convert old headers to - * the new style (this is a noop for new headers). - */ - compressFilelist(h); - -exit: - /*@-branchstate@*/ - if (h != NULL) { - uh = headerUnload(h); - h = headerFree(h, "doGetRecord exit"); - } - /*@=branchstate@*/ - return uh; -} - -static int db1copen(/*@unused@*/ dbiIndex dbi, - /*@unused@*/ DBC ** dbcp, unsigned int flags) - /*@modifies *dbcp @*/ -{ - /* XXX per-iterator cursors need to be set to non-NULL. */ - if (flags) - *dbcp = (DBC *)-1; - return 0; -} - -static int db1cclose(dbiIndex dbi, - /*@unused@*/ DBC * dbcursor, /*@unused@*/ unsigned int flags) - /*@modifies dbi @*/ -{ - dbi->dbi_lastoffset = 0; - return 0; -} - -/*@-compmempass@*/ -static int db1cget(dbiIndex dbi, /*@unused@*/ DBC * dbcursor, - /*@null@*/ void ** keyp, - /*@null@*/ size_t * keylen, - /*@null@*/ void ** datap, - /*@null@*/ size_t * datalen, - /*@unused@*/ unsigned int flags) - /*@globals fileSystem @*/ - /*@modifies dbi, *keyp, *keylen, *datap, *datalen, fileSystem @*/ -{ - DBT key, data; - int rc = 0; - - if (dbi == NULL) - return EFAULT; - - memset(&key, 0, sizeof(key)); - memset(&data, 0, sizeof(data)); - /*@-unqualifiedtrans@*/ - if (keyp) key.data = *keyp; - if (keylen) key.size = *keylen; - if (datap) data.data = *datap; - if (datalen) data.size = *datalen; - /*@=unqualifiedtrans@*/ - - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs = dbi->dbi_db; - unsigned int offset; - unsigned int newSize; - - if (key.data == NULL) { /* XXX simulated DB_NEXT */ - if (dbi->dbi_lastoffset == 0) { - dbi->dbi_lastoffset = fadFirstOffset(pkgs); - } else { - dbi->dbi_lastoffset = fadNextOffset(pkgs, dbi->dbi_lastoffset); - } - /*@-immediatetrans@*/ - key.data = &dbi->dbi_lastoffset; - /*@=immediatetrans@*/ - key.size = sizeof(dbi->dbi_lastoffset); - - /* Catch end-of-chain conditions. */ - if (dbi->dbi_lastoffset == 0) - goto bail; - } - - memcpy(&offset, key.data, sizeof(offset)); - /* XXX hack to pass sizeof header to fadAlloc */ - newSize = data.size; - - if (offset == 0) { /* XXX simulated offset 0 record */ - offset = fadAlloc(pkgs, newSize); - if (offset == 0) - return ENOMEM; - offset--; /* XXX hack: caller will increment */ - /* XXX hack: return offset as data, free in db1cput */ - data.data = xmalloc(sizeof(offset)); - memcpy(data.data, &offset, sizeof(offset)); - data.size = sizeof(offset); - } else { /* XXX simulated retrieval */ - data.data = doGetRecord(dbi, offset); - data.size = 0; /* XXX WRONG */ - } - } -#ifdef DYING - else { - DB * db; - int _printit; - - if ((db = dbi->dbi_db) == NULL) - return EFAULT; - - if (key.data == NULL) { - rc = db->seq(db, &key, &data, (dbi->dbi_lastoffset++ ? R_NEXT : R_FIRST)); - _printit = (rc == 1 ? 0 : _debug); - rc = cvtdberr(dbi, "db->seq", rc, _printit); - } else { - rc = db->get(db, &key, &data, 0); - _printit = (rc == 1 ? 0 : _debug); - rc = cvtdberr(dbi, "db1cget", rc, _printit); - } - } -#else - else - rc = EINVAL; -#endif - -bail: - if (rc == 0) { - if (keyp) *keyp = key.data; - if (keylen) *keylen = key.size; - if (datap) *datap = data.data; - if (datalen) *datalen = data.size; - } - - /*@-nullstate@*/ - return rc; - /*@=nullstate@*/ -} -/*@=compmempass@*/ - -static int db1cdel(dbiIndex dbi, /*@unused@*/ DBC * dbcursor, const void * keyp, - size_t keylen, /*@unused@*/ unsigned int flags) - /*@globals fileSystem @*/ - /*@modifies dbi, fileSystem @*/ -{ - DBT key; - int rc = 0; - - memset(&key, 0, sizeof(key)); - key.data = (void *)keyp; - key.size = keylen; - - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs = dbi->dbi_db; - unsigned int offset; - memcpy(&offset, keyp, sizeof(offset)); - fadFree(pkgs, offset); - } -#ifdef DYING - else { - DB * db = dbi->dbi_db; - - if (db) - rc = db->del(db, &key, 0); - rc = cvtdberr(dbi, "db->del", rc, _debug); - } -#else - else - rc = EINVAL; -#endif - - return rc; -} - -static int db1cput(dbiIndex dbi, /*@unused@*/ DBC * dbcursor, - const void * keyp, size_t keylen, - const void * datap, size_t datalen, - /*@unused@*/ unsigned int flags) - /*@globals fileSystem @*/ - /*@modifies dbi, datap, fileSystem @*/ -{ - DBT key, data; - int rc = 0; - - memset(&key, 0, sizeof(key)); - memset(&data, 0, sizeof(data)); - key.data = (void *)keyp; - key.size = keylen; - data.data = (void *)datap; - data.size = datalen; - - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs = dbi->dbi_db; - unsigned int offset; - - memcpy(&offset, key.data, sizeof(offset)); - - if (offset == 0) { /* XXX simulated offset 0 record */ - /* XXX hack: return offset as data, free in db1cput */ - if (data.size == sizeof(offset)) - /*@-unqualifiedtrans@*/ free(data.data); /*@=unqualifiedtrans@*/ - } else { /* XXX simulated DB_KEYLAST */ - Header h = headerLoad(data.data); - int newSize = headerSizeof(h, HEADER_MAGIC_NO); - - (void)Fseek(pkgs, offset, SEEK_SET); - fdSetContentLength(pkgs, newSize); - rc = headerWrite(pkgs, h, HEADER_MAGIC_NO); - fdSetContentLength(pkgs, -1); - if (rc) - rc = EIO; - h = headerFree(h, "db1cput"); - } - } -#ifdef DYING - else { - DB * db = dbi->dbi_db; - - if (db) - rc = db->put(db, &key, &data, 0); - rc = cvtdberr(dbi, "db->put", rc, _debug); - } -#else - else - rc = EINVAL; -#endif - - return rc; -} - -static int db1ccount(/*@unused@*/ dbiIndex dbi, /*@unused@*/ DBC * dbcursor, - /*@unused@*/ /*@out@*/ unsigned int * countp, - /*@unused@*/ unsigned int flags) - /*@*/ -{ - return EINVAL; -} - -static int db1byteswapped(/*@unused@*/dbiIndex dbi) - /*@*/ -{ - return 0; -} - -static int db1stat(/*@unused@*/ dbiIndex dbi, /*@unused@*/ unsigned int flags) - /*@*/ -{ - return EINVAL; -} - -static int db1close(/*@only@*/ dbiIndex dbi, /*@unused@*/ unsigned int flags) - /*@globals rpmGlobalMacroContext, - fileSystem @*/ - /*@modifies dbi, rpmGlobalMacroContext, fileSystem @*/ -{ - rpmdb rpmdb = dbi->dbi_rpmdb; - const char * base = db1basename(dbi->dbi_rpmtag); - const char * urlfn = rpmGenPath(rpmdb->db_root, rpmdb->db_home, base); - const char * fn; - int rc = 0; - - (void) urlPath(urlfn, &fn); - - /*@-branchstate@*/ - if (dbi->dbi_db) { - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs = dbi->dbi_db; - rc = Fclose(pkgs); - } -#ifdef DYING - else { - DB * db = dbi->dbi_db; - rc = db->close(db); - rc = cvtdberr(dbi, "db->close", rc, _debug); - } -#else - else - rc = EINVAL; -#endif - dbi->dbi_db = NULL; - } - /*@=branchstate@*/ - - rpmMessage(RPMMESS_DEBUG, _("closed db file %s\n"), urlfn); - /* Remove temporary databases */ - if (dbi->dbi_temporary) { - rpmMessage(RPMMESS_DEBUG, _("removed db file %s\n"), urlfn); - (void) unlink(fn); - } - - dbi = db3Free(dbi); - base = _free(base); - urlfn = _free(urlfn); - return rc; -} - -static int db1open(rpmdb rpmdb, int rpmtag, - /*@out@*/ dbiIndex * dbip) - /*@globals rpmGlobalMacroContext, - fileSystem @*/ - /*@modifies *dbip, rpmGlobalMacroContext, fileSystem @*/ -{ - /*@-nestedextern@*/ - extern struct _dbiVec db1vec; - /*@=nestedextern@*/ - const char * base = NULL; - const char * urlfn = NULL; - const char * fn = NULL; - dbiIndex dbi = NULL; - int rc = 0; - - if (dbip) - *dbip = NULL; - if ((dbi = db3New(rpmdb, rpmtag)) == NULL) - return EFAULT; - dbi->dbi_api = DB_VERSION_MAJOR; - - base = db1basename(rpmtag); - urlfn = rpmGenPath(rpmdb->db_root, rpmdb->db_home, base); - (void) urlPath(urlfn, &fn); - if (!(fn && *fn != '\0')) { - rpmError(RPMERR_DBOPEN, _("bad db file %s\n"), urlfn); - rc = EFAULT; - goto exit; - } - - rpmMessage(RPMMESS_DEBUG, _("opening db file %s mode 0x%x\n"), - urlfn, dbi->dbi_mode); - - if (dbi->dbi_rpmtag == RPMDBI_PACKAGES) { - FD_t pkgs; - - pkgs = fadOpen(fn, dbi->dbi_mode, dbi->dbi_perms); - if (Ferror(pkgs)) { - rc = errno; /* XXX check errno validity */ - goto exit; - } - - /* XXX HACK: fcntl lock if db3 (DB_INIT_CDB | DB_INIT_LOCK) specified */ - if (dbi->dbi_lockdbfd || (dbi->dbi_eflags & 0x30)) { - struct flock l; - - l.l_whence = 0; - l.l_start = 0; - l.l_len = 0; - l.l_type = (dbi->dbi_mode & O_RDWR) ? F_WRLCK : F_RDLCK; - - if (Fcntl(pkgs, F_SETLK, (void *) &l)) { - rc = errno; /* XXX check errno validity */ - rpmError(RPMERR_FLOCK, _("cannot get %s lock on database\n"), - ((dbi->dbi_mode & O_RDWR) ? _("exclusive") : _("shared"))); - goto exit; - } - } - - dbi->dbi_db = pkgs; - } -#ifdef DYING - else { - void * dbopeninfo = NULL; - int dbimode = dbi->dbi_mode; - - if (dbi->dbi_temporary) - dbimode |= (O_CREAT | O_RDWR); - - dbi->dbi_db = dbopen(fn, dbimode, dbi->dbi_perms, - db3_to_dbtype(dbi->dbi_type), dbopeninfo); - if (dbi->dbi_db == NULL) rc = errno; - } -#else - else - rc = EINVAL; -#endif - -exit: - if (rc == 0 && dbi->dbi_db != NULL && dbip) { - dbi->dbi_vec = &db1vec; - if (dbip) *dbip = dbi; - } else - (void) db1close(dbi, 0); - - base = _free(base); - urlfn = _free(urlfn); - - return rc; -} -/*@=onlytrans@*/ - -/** \ingroup db1 - */ -/*@-exportheadervar@*/ -/*@observer@*/ /*@unchecked@*/ -struct _dbiVec db1vec = { - DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH, - db1open, db1close, db1sync, db1copen, db1cclose, db1cdel, db1cget, db1cput, - db1ccount, db1byteswapped, db1stat -}; -/*@=exportheadervar@*/ -/*@=type@*/ diff --git a/rpmdb/rpmdb.c b/rpmdb/rpmdb.c index 4e5f3bbee..ae5fe1e0b 100644 --- a/rpmdb/rpmdb.c +++ b/rpmdb/rpmdb.c @@ -1046,9 +1046,12 @@ DBT * key = alloca(sizeof(*key)); if (dbi != NULL) { dbcursor = NULL; xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0); + memset(key, 0, sizeof(*key)); key->data = (void *) baseName; key->size = strlen(baseName); +if (key->size == 0) key->size++; /* XXX "/" fixup. */ + rc = dbiSearch(dbi, dbcursor, key, &allMatches); xx = dbiCclose(dbi, dbcursor, 0); dbcursor = NULL; @@ -2088,32 +2091,25 @@ static void rpmdbSortIterator(/*@null@*/ rpmdbMatchIterator mi) } } -static int rpmdbGrowIterator(/*@null@*/ rpmdbMatchIterator mi, - const void * keyp, size_t keylen, int fpNum) +static int rpmdbGrowIterator(/*@null@*/ rpmdbMatchIterator mi, DBT * key, + int fpNum) /*@globals fileSystem @*/ /*@modifies mi, fileSystem @*/ { -DBT * key = alloca(sizeof(*key)); dbiIndex dbi = NULL; DBC * dbcursor = NULL; dbiIndexSet set = NULL; int rc; int xx; - if (!(mi && keyp)) + if (!(mi && key->data)) return 1; dbi = dbiOpen(mi->mi_db, mi->mi_rpmtag, 0); if (dbi == NULL) return 1; - if (keylen == 0) - keylen = strlen(keyp); - xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0); -memset(key, 0, sizeof(*key)); -key->data = (void *) keyp; -key->size = keylen; rc = dbiSearch(dbi, dbcursor, key, &set); xx = dbiCclose(dbi, dbcursor, 0); dbcursor = NULL; @@ -2187,8 +2183,6 @@ DBT * key = alloca(sizeof(*key)); dbi->dbi_lastoffset = 0; /* db0: rewind to beginning */ -if (keyp && keylen == 0) keylen = strlen((char *)keyp); - if (rpmtag != RPMDBI_PACKAGES && keyp) { DBC * dbcursor = NULL; int rc; @@ -2204,10 +2198,14 @@ if (keyp && keylen == 0) keylen = strlen((char *)keyp); rc = rpmdbFindByFile(db, keyp, &set); } else { xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, 0); + memset(key, 0, sizeof(*key)); key->data = (void *) keyp; key->size = keylen; +if (key->size == 0) key->size = strlen((char *)key->data); +if (key->size == 0) key->size++; /* XXX "/" fixup. */ rc = dbiSearch(dbi, dbcursor, key, &set); + xx = dbiCclose(dbi, dbcursor, 0); dbcursor = NULL; } @@ -2259,41 +2257,6 @@ key->size = keylen; /*@=nullret@*/ } -/** - * Remove entry from database index. - * @param dbi index database handle - * @param dbcursor index database cursor - * @param key search key/length - * @param rec record to remove - * @return 0 on success - */ -static int removeIndexEntry(dbiIndex dbi, DBC * dbcursor, DBT * key, - dbiIndexItem rec) - /*@globals fileSystem @*/ - /*@modifies *dbcursor, fileSystem @*/ -{ - dbiIndexSet set = NULL; - int rc; - - rc = dbiSearch(dbi, dbcursor, key, &set); - - if (rc < 0) /* not found */ - rc = 0; - else if (rc > 0) /* error */ - rc = 1; /* error message already generated from dbindex.c */ - else { /* success */ - /*@-mods@*/ /* a single rec is not modified */ - rc = dbiPruneSet(set, rec, 1, sizeof(*rec), 1); - /*@=mods@*/ - if (rc == 0 && dbiUpdateIndex(dbi, dbcursor, key, set)) - rc = 1; - } - - set = dbiFreeIndexSet(set); - - return rc; -} - /*@-mods@*/ /* XXX install.c uninstall.c */ int rpmdbRemove(rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum) @@ -2405,6 +2368,7 @@ int rpmdbRemove(rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum) printed = 0; xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR); for (i = 0; i < rpmcnt; i++) { + dbiIndexSet set; const void * valp; size_t vallen; int stringvalued; @@ -2502,10 +2466,29 @@ int rpmdbRemove(rpmdb db, /*@unused@*/ int rid, unsigned int hdrNum) * the header instance. It's easier to just ignore errors * than to do things correctly. */ + memset(key, 0, sizeof(*key)); key->data = (void *) valp; key->size = vallen; - xx = removeIndexEntry(dbi, dbcursor, key, rec); +if (key->size == 0) key->size = strlen((char *)key->data); +if (key->size == 0) key->size++; /* XXX "/" fixup. */ + + set = NULL; + xx = dbiSearch(dbi, dbcursor, key, &set); + + if (xx < 0) /* not found */ + xx = 0; + else if (xx > 0) /* error */ + xx = 1; /* error message already generated from dbindex.c */ + else { /* success */ + /*@-mods@*/ /* a single rec is not modified */ + xx = dbiPruneSet(set, rec, 1, sizeof(*rec), 1); + /*@=mods@*/ + if (xx == 0 && dbiUpdateIndex(dbi, dbcursor, key, set)) + xx = 1; + } + set = dbiFreeIndexSet(set); + } xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR); @@ -2532,47 +2515,6 @@ key->size = vallen; return 0; } -/** - * Add entry to database index. - * @param dbi index database handle - * @param dbcursor index database cursor - * @param key search key/length - * @param rec record to add - * @return 0 on success - */ -static int addIndexEntry(dbiIndex dbi, DBC * dbcursor, DBT * key, - dbiIndexItem rec) - /*@globals fileSystem @*/ - /*@modifies *dbcursor, fileSystem @*/ -{ - dbiIndexSet set = NULL; - int rc; - - rc = dbiSearch(dbi, dbcursor, key, &set); - - if (rc > 0) { /* error */ - rc = 1; - } else { - - /* With duplicates, cursor is positioned, discard the record. */ - /*@-branchstate@*/ - if (rc == 0 && dbi->dbi_permit_dups) - set = dbiFreeIndexSet(set); - /*@=branchstate@*/ - - if (set == NULL || rc < 0) { /* not found */ - rc = 0; - set = xcalloc(1, sizeof(*set)); - } - (void) dbiAppendSet(set, rec, 1, sizeof(*rec), 0); - if (dbiUpdateIndex(dbi, dbcursor, key, set)) - rc = 1; - } - set = dbiFreeIndexSet(set); - - return 0; -} - /* XXX install.c */ int rpmdbAdd(rpmdb db, int iid, Header h) { @@ -2777,6 +2719,7 @@ DBT * key = alloca(sizeof(*key)); printed = 0; xx = dbiCopen(dbi, dbi->dbi_txnid, &dbcursor, DB_WRITECURSOR); for (i = 0; i < rpmcnt; i++) { + dbiIndexSet set; const void * valp; size_t vallen; int stringvalued; @@ -2892,7 +2835,33 @@ DBT * key = alloca(sizeof(*key)); memset(key, 0, sizeof(*key)); key->data = (void *) valp; key->size = vallen; - rc += addIndexEntry(dbi, dbcursor, key, rec); +if (key->size == 0) key->size = strlen((char *)key->data); +if (key->size == 0) key->size++; /* XXX "/" fixup. */ + + set = NULL; + xx = dbiSearch(dbi, dbcursor, key, &set); + + if (xx > 0) { /* error */ + xx = 1; + } else { + + /* With duplicates, cursor is positioned, discard the record. */ + /*@-branchstate@*/ + if (xx == 0 && dbi->dbi_permit_dups) + set = dbiFreeIndexSet(set); + /*@=branchstate@*/ + + if (set == NULL || xx < 0) { /* not found */ + xx = 0; + set = xcalloc(1, sizeof(*set)); + } + (void) dbiAppendSet(set, rec, 1, sizeof(*rec), 0); + if (dbiUpdateIndex(dbi, dbcursor, key, set)) + xx = 1; + } + set = dbiFreeIndexSet(set); + rc += xx; + } xx = dbiCclose(dbi, dbcursor, DB_WRITECURSOR); dbcursor = NULL; @@ -2923,6 +2892,7 @@ exit: int rpmdbFindFpList(rpmdb db, fingerPrint * fpList, dbiIndexSet * matchList, int numItems) { +DBT * key = alloca(sizeof(*key)); HGE_t hge = (HGE_t)headerGetEntryMinMemory; HFD_t hfd = headerFreeData; rpmdbMatchIterator mi; @@ -2936,7 +2906,11 @@ int rpmdbFindFpList(rpmdb db, fingerPrint * fpList, dbiIndexSet * matchList, /* Gather all matches from the database */ for (i = 0; i < numItems; i++) { - xx = rpmdbGrowIterator(mi, fpList[i].baseName, 0, i); +memset(key, 0, sizeof(*key)); +key->data = (void *) fpList[i].baseName; +key->size = strlen((char *)key->data); +if (key->size == 0) key->size++; /* XXX "/" fixup. */ + xx = rpmdbGrowIterator(mi, key, i); matchList[i] = xcalloc(1, sizeof(*(matchList[i]))); } @@ -3017,29 +2991,6 @@ int rpmdbFindFpList(rpmdb db, fingerPrint * fpList, dbiIndexSet * matchList, } -char * db1basename (int rpmtag) -{ - char * base = NULL; - /*@-branchstate@*/ - switch (rpmtag) { - case RPMDBI_PACKAGES: base = "packages.rpm"; break; - case RPMTAG_NAME: base = "nameindex.rpm"; break; - case RPMTAG_BASENAMES: base = "fileindex.rpm"; break; - case RPMTAG_GROUP: base = "groupindex.rpm"; break; - case RPMTAG_REQUIRENAME: base = "requiredby.rpm"; break; - case RPMTAG_PROVIDENAME: base = "providesindex.rpm"; break; - case RPMTAG_CONFLICTNAME: base = "conflictsindex.rpm"; break; - case RPMTAG_TRIGGERNAME: base = "triggerindex.rpm"; break; - default: - { const char * tn = tagName(rpmtag); - base = alloca( strlen(tn) + sizeof(".idx") + 1 ); - (void) stpcpy( stpcpy(base, tn), ".idx"); - } break; - } - /*@=branchstate@*/ - return xstrdup(base); -} - /** * Check if file esists using stat(2). * @param urlfn file name (may be URL) @@ -3122,16 +3073,6 @@ static int rpmdbRemoveDatabase(const char * prefix, case 2: case 1: case 0: - if (dbiTags != NULL) - for (i = 0; i < dbiTagsMax; i++) { - const char * base = db1basename(dbiTags[i]); - sprintf(filename, "%s/%s/%s", prefix, dbpath, base); - (void)rpmCleanPath(filename); - if (!rpmioFileExists(filename)) - continue; - xx = unlink(filename); - base = _free(base); - } break; } @@ -3226,34 +3167,6 @@ static int rpmdbMoveDatabase(const char * prefix, case 2: case 1: case 0: - if (dbiTags != NULL) - for (i = 0; i < dbiTagsMax; i++) { - const char * base; - int rpmtag; - - /* Filter out temporary databases */ - switch ((rpmtag = dbiTags[i])) { - case RPMDBI_AVAILABLE: - case RPMDBI_ADDED: - case RPMDBI_REMOVED: - case RPMDBI_DEPENDS: - continue; - /*@notreached@*/ /*@switchbreak@*/ break; - default: - /*@switchbreak@*/ break; - } - - base = db1basename(rpmtag); - sprintf(ofilename, "%s/%s/%s", prefix, olddbpath, base); - (void)rpmCleanPath(ofilename); - if (!rpmioFileExists(ofilename)) - continue; - sprintf(nfilename, "%s/%s/%s", prefix, newdbpath, base); - (void)rpmCleanPath(nfilename); - if ((xx = Rename(ofilename, nfilename)) != 0) - rc = 1; - base = _free(base); - } break; } if (rc || _olddbapi == _newdbapi) diff --git a/rpmdb/rpmdb.h b/rpmdb/rpmdb.h index f3b14e386..0dc0ad610 100644 --- a/rpmdb/rpmdb.h +++ b/rpmdb/rpmdb.h @@ -6,6 +6,7 @@ * Access RPM indices using Berkeley DB interface(s). */ +#include <assert.h> #include <rpmlib.h> #include <db.h> @@ -444,14 +445,8 @@ int dbiDel(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data, /*@globals fileSystem@*/ /*@modifies *dbcursor, fileSystem @*/ { - int NULkey = - (key && key->data && *((char *)key->data) == '\0' && key->size == 0); - int rc; - - if (NULkey) key->size++; - rc = (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags); - if (NULkey) key->size--; - return rc; + assert(key->size > 0); + return (dbi->dbi_vec->cdel) (dbi, dbcursor, key, data, flags); } /** \ingroup dbi @@ -469,14 +464,8 @@ int dbiGet(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data, /*@globals fileSystem@*/ /*@modifies *dbcursor, *key, *data, fileSystem @*/ { - int NULkey = - (key && key->data && *((char *)key->data) == '\0' && key->size == 0); - int rc; - - if (NULkey) key->size++; - rc = (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags); - if (NULkey) key->size--; - return rc; + assert(key->size > 0); + return (dbi->dbi_vec->cget) (dbi, dbcursor, key, data, flags); } /** \ingroup dbi @@ -494,14 +483,8 @@ int dbiPut(dbiIndex dbi, /*@null@*/ DBC * dbcursor, DBT * key, DBT * data, /*@globals fileSystem@*/ /*@modifies *dbcursor, *key, fileSystem @*/ { - int NULkey = - (key && key->data && *((char *)key->data) == '\0' && key->size == 0); - int rc; - - if (NULkey) key->size++; - rc = (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags); - if (NULkey) key->size--; - return rc; + assert(key->size > 0); + return (dbi->dbi_vec->cput) (dbi, dbcursor, key, data, flags); } /** \ingroup dbi @@ -581,13 +564,6 @@ int dbiByteSwapped(dbiIndex dbi) } /*@=globuse =mods =mustmod @*/ -/** \ingroup db1 - * Return base file name for db1 database (legacy). - * @param rpmtag rpm tag - * @return base file name of db1 database - */ -char * db1basename(int rpmtag) - /*@*/; /*@=exportlocal@*/ /** \ingroup rpmdb |