diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2010-10-18 13:18:19 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2010-10-18 13:18:19 +0300 |
commit | c1620e511d0a8613b01bd3ba5b487c027572ce44 (patch) | |
tree | 550d7c6ed77924be30977a72de6d8dcc07e66a87 /lib/backend | |
parent | 7a9da58298ad4b1c1e21b01de97f6d7aba2ca543 (diff) | |
download | librpm-tizen-c1620e511d0a8613b01bd3ba5b487c027572ce44.tar.gz librpm-tizen-c1620e511d0a8613b01bd3ba5b487c027572ce44.tar.bz2 librpm-tizen-c1620e511d0a8613b01bd3ba5b487c027572ce44.zip |
Remember if we created the db backing file on open
- Instead of trying to figure out suitable flags beforehand, try to
open the db a couple of times with different flags. This way we know
whether the backing file was created on open or not. Also simplifies
the flag shuffling somewhat.
- We also do this when O_CREAT db_mode, for the same reason: rpmdbInit()
can be called on existing database too, and we want to know which
of the files actually got created.
Diffstat (limited to 'lib/backend')
-rw-r--r-- | lib/backend/db3.c | 62 | ||||
-rw-r--r-- | lib/backend/dbconfig.c | 1 |
2 files changed, 26 insertions, 37 deletions
diff --git a/lib/backend/db3.c b/lib/backend/db3.c index b050f089c..66ba16064 100644 --- a/lib/backend/db3.c +++ b/lib/backend/db3.c @@ -507,6 +507,7 @@ int dbiOpen(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) const char *dbhome = rpmdbHome(rpmdb); dbiIndex dbi = NULL; int rc = 0; + int retry_open; DB * db = NULL; uint32_t oflags; @@ -527,56 +528,33 @@ int dbiOpen(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) * Map open mode flags onto configured database/environment flags. */ if ((rpmdb->db_mode & O_ACCMODE) == O_RDONLY) oflags |= DB_RDONLY; - if (rpmdb->db_mode & O_CREAT) { - oflags |= DB_CREATE; - dbi->dbi_oflags |= DB_CREATE; - } - - /* - * Avoid incompatible DB_CREATE/DB_RDONLY flags on DB->open. - */ - if ((oflags & DB_CREATE) && (oflags & DB_RDONLY)) { - /* dbhome is writable, and DB->open flags may conflict. */ - char * dbf = rpmGetPath(dbhome, "/", dbi->dbi_file, NULL); - - if (access(dbf, F_OK) == -1) { - /* File does not exist, DB->open might create ... */ - oflags &= ~DB_RDONLY; - } else { - /* File exists, DB->open need not create ... */ - oflags &= ~DB_CREATE; - } - - /* Only writers need DB_WRITECURSOR ... */ - if (!(oflags & DB_RDONLY) && access(dbf, W_OK) == 0) { - dbi->dbi_oflags &= ~DB_RDONLY; - } else { - dbi->dbi_oflags |= DB_RDONLY; - } - dbf = _free(dbf); - } - - /* - * Turn off verify-on-close if opening read-only. - */ - if (oflags & DB_RDONLY) - dbi->dbi_verify_on_close = 0; rc = db_init(dbi, dbhome); - if (rc == 0) { + retry_open = (rc == 0) ? 2 : 0; + + while (retry_open) { rc = db_create(&db, rpmdb->db_dbenv, 0); rc = cvtdberr(dbi, "db_create", rc, _debug); if (rc == 0 && db != NULL) { int _printit, xx; char *dbfs = prDbiOpenFlags(oflags, 0); rpmlog(RPMLOG_DEBUG, "opening db index %s/%s %s mode=0x%x\n", - dbhome, dbi->dbi_file, dbfs, rpmdb->db_mode); + dbhome, dbi->dbi_file, dbfs, rpmdb->db_mode); free(dbfs); rc = (db->open)(db, NULL, dbi->dbi_file, NULL, dbi->dbi_dbtype, oflags, rpmdb->db_perms); + /* Attempt to create if missing, discarding DB_RDONLY (!) */ + if (rc == ENOENT) { + oflags |= DB_CREATE; + oflags &= ~DB_RDONLY; + retry_open--; + } else { + retry_open = 0; + } + if (rc == 0 && dbi->dbi_dbtype == DB_UNKNOWN) { DBTYPE dbi_dbtype = DB_UNKNOWN; xx = db->get_type(db, &dbi_dbtype); @@ -587,10 +565,22 @@ int dbiOpen(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) /* XXX return rc == errno without printing */ _printit = (rc > 0 ? 0 : _debug); xx = cvtdberr(dbi, "db->open", rc, _printit); + + if (rc != 0) { + db->close(db, 0); + db = NULL; + } } } + /* + * Turn off verify-on-close if opening read-only. + */ + if (oflags & DB_RDONLY) + dbi->dbi_verify_on_close = 0; + dbi->dbi_db = db; + dbi->dbi_oflags = oflags; if (rc == 0 && dbi->dbi_lockdbfd && _lockdbfd++ == 0) { rc = dbiFlock(dbi, rpmdb->db_mode); diff --git a/lib/backend/dbconfig.c b/lib/backend/dbconfig.c index 1fb6224ae..2ee9922e8 100644 --- a/lib/backend/dbconfig.c +++ b/lib/backend/dbconfig.c @@ -232,7 +232,6 @@ dbiIndex dbiNew(rpmdb rpmdb, rpmTag rpmtag) dbi->dbi_file = rpmTagGetName(rpmtag); dbi->dbi_type = (rpmtag == RPMDBI_PACKAGES) ? DBI_PRIMARY : DBI_SECONDARY; dbi->dbi_byteswapped = -1; /* -1 unknown, 0 native order, 1 alien order */ - dbi->dbi_oflags |= DB_CREATE; /* XXX FIXME: These all are environment, not per-dbi configuration */ dbi->dbi_eflags |= (DB_INIT_MPOOL|DB_CREATE); |