From: ewt Date: Thu, 27 Jun 1996 17:17:32 +0000 (+0000) Subject: added rpmdbInit() call X-Git-Tag: tznext/4.11.0.1.tizen20130304~11286 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3b2aada6e223c60301bddd6c8156064e1e137809;p=tools%2Flibrpm-tizen.git added rpmdbInit() call CVS patchset: 674 CVS date: 1996/06/27 17:17:32 --- diff --git a/lib/rpmdb.c b/lib/rpmdb.c index ee308d4..0588fc5 100644 --- a/lib/rpmdb.c +++ b/lib/rpmdb.c @@ -10,6 +10,7 @@ #include "dbindex.h" #include "falloc.h" #include "header.h" +#include "misc.h" #include "rpmerr.h" #include "rpmlib.h" @@ -36,10 +37,23 @@ static int addIndexEntry(dbIndex * idx, char * index, unsigned int offset, unsigned int fileNumber); static void blockSignals(void); static void unblockSignals(void); +static int doopen (char * prefix, rpmdb *rpmdbp, int mode, int perms, + int justcheck); static sigset_t signalMask; int rpmdbOpen (char * prefix, rpmdb *rpmdbp, int mode, int perms) { + return doopen(prefix, rpmdbp, mode, perms, 0); +} + +int rpmdbInit (char * prefix, int perms) { + rpmdb db; + + return doopen(prefix, &db, O_CREAT | O_RDWR, perms, 1); +} + +static int doopen (char * prefix, rpmdb *rpmdbp, int mode, int perms, + int justcheck) { char * filename; struct rpmdb db; @@ -50,92 +64,114 @@ int rpmdbOpen (char * prefix, rpmdb *rpmdbp, int mode, int perms) { strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/packages.rpm"); - db.pkgs = faOpen(filename, mode, 0644); - if (!db.pkgs) { - error(RPMERR_DBOPEN, "failed to open %s\n", filename); - return 1; - } - /* try and get a lock - this is released by the kernel when we close - the file */ - if (mode & O_RDWR) { - if (flock(db.pkgs->fd, LOCK_EX | LOCK_NB)) { - error(RPMERR_FLOCK, "cannot get %s lock on database", "exclusive"); - return 1; - } - } else { - if (flock(db.pkgs->fd, LOCK_SH | LOCK_NB)) { - error(RPMERR_FLOCK, "cannot get %s lock on database", "shared"); + memset(&db, 0, sizeof(db)); + + if (!justcheck || !exists(filename)) { + db.pkgs = faOpen(filename, mode, 0644); + if (!db.pkgs) { + error(RPMERR_DBOPEN, "failed to open %s\n", filename); return 1; - } + } + + /* try and get a lock - this is released by the kernel when we close + the file */ + if (mode & O_RDWR) { + if (flock(db.pkgs->fd, LOCK_EX | LOCK_NB)) { + error(RPMERR_FLOCK, "cannot get %s lock on database", + "exclusive"); + return 1; + } + } else { + if (flock(db.pkgs->fd, LOCK_SH | LOCK_NB)) { + error(RPMERR_FLOCK, "cannot get %s lock on database", "shared"); + return 1; + } + } } strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/nameindex.rpm"); - db.nameIndex = openDBIndex(filename, mode, 0644); - if (!db.nameIndex) { - faClose(db.pkgs); - return 1; + + if (!justcheck || !exists(filename)) { + db.nameIndex = openDBIndex(filename, mode, 0644); + if (!db.nameIndex) { + faClose(db.pkgs); + return 1; + } } - + strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/fileindex.rpm"); - db.fileIndex = openDBIndex(filename, mode, 0644); - if (!db.fileIndex) { - faClose(db.pkgs); - closeDBIndex(db.nameIndex); - return 1; + + if (!justcheck || !exists(filename)) { + db.fileIndex = openDBIndex(filename, mode, 0644); + if (!db.fileIndex) { + faClose(db.pkgs); + closeDBIndex(db.nameIndex); + return 1; + } } strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/groupindex.rpm"); - db.groupIndex = openDBIndex(filename, mode, 0644); - if (!db.groupIndex) { - faClose(db.pkgs); - closeDBIndex(db.nameIndex); - closeDBIndex(db.fileIndex); - return 1; + + if (!justcheck || !exists(filename)) { + db.groupIndex = openDBIndex(filename, mode, 0644); + if (!db.groupIndex) { + faClose(db.pkgs); + closeDBIndex(db.nameIndex); + closeDBIndex(db.fileIndex); + return 1; + } } strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/providesindex.rpm"); - db.providesIndex = openDBIndex(filename, mode, 0644); - if (!db.providesIndex) { - faClose(db.pkgs); - closeDBIndex(db.fileIndex); - closeDBIndex(db.nameIndex); - closeDBIndex(db.groupIndex); - return 1; + + if (!justcheck || !exists(filename)) { + db.providesIndex = openDBIndex(filename, mode, 0644); + if (!db.providesIndex) { + faClose(db.pkgs); + closeDBIndex(db.fileIndex); + closeDBIndex(db.nameIndex); + closeDBIndex(db.groupIndex); + return 1; + } } strcpy(filename, prefix); strcat(filename, "/var/lib/rpm/requiredby.rpm"); - db.requiredbyIndex = openDBIndex(filename, mode, 0644); - if (!db.requiredbyIndex) { - faClose(db.pkgs); - closeDBIndex(db.fileIndex); - closeDBIndex(db.nameIndex); - closeDBIndex(db.groupIndex); - closeDBIndex(db.providesIndex); - return 1; + + if (!justcheck || !exists(filename)) { + db.requiredbyIndex = openDBIndex(filename, mode, 0644); + if (!db.requiredbyIndex) { + faClose(db.pkgs); + closeDBIndex(db.fileIndex); + closeDBIndex(db.nameIndex); + closeDBIndex(db.groupIndex); + closeDBIndex(db.providesIndex); + return 1; + } } *rpmdbp = malloc(sizeof(struct rpmdb)); **rpmdbp = db; + if (justcheck) { + rpmdbClose(*rpmdbp); + } + return 0; } -int rpmdbCreate (rpmdb db, int mode, int perms); - /* this fails if any part of the db already exists */ - void rpmdbClose (rpmdb db) { - faClose(db->pkgs); - closeDBIndex(db->fileIndex); - closeDBIndex(db->groupIndex); - closeDBIndex(db->nameIndex); - closeDBIndex(db->providesIndex); - closeDBIndex(db->requiredbyIndex); + if (db->pkgs) faClose(db->pkgs); + if (db->fileIndex) closeDBIndex(db->fileIndex); + if (db->groupIndex) closeDBIndex(db->groupIndex); + if (db->nameIndex) closeDBIndex(db->nameIndex); + if (db->providesIndex) closeDBIndex(db->providesIndex); + if (db->requiredbyIndex) closeDBIndex(db->requiredbyIndex); free(db); } diff --git a/lib/rpmlib.h b/lib/rpmlib.h index 2793bc7..d7168e4 100644 --- a/lib/rpmlib.h +++ b/lib/rpmlib.h @@ -158,8 +158,8 @@ typedef void (*notifyFunction)(const unsigned long amount, int rpmdbOpen (char * prefix, rpmdb * dbp, int mode, int perms); /* 0 on error */ -int rpmdbCreate (rpmdb db, int mode, int perms); - /* this fails if any part of the db already exists */ +int rpmdbInit(char * prefix, int perms); + /* nonzero on error */ void rpmdbClose (rpmdb db); unsigned int rpmdbFirstRecNum(rpmdb db);