[pacman-dev] [PATCH 6/9] Add a 'valid' flag to the database object

Dan McGee dan at archlinux.org
Wed Jun 8 03:51:49 EDT 2011


This will let us keep track if the database itself has been marked valid
in whatever fashion. For local databases at the moment we ensure there
are no depends files; for sync databases we ensure the PGP signature is
valid if required/requested. The loading of the pkgcache is prohibited
if the database is invalid.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/alpm.c     |    2 +-
 lib/libalpm/be_local.c |   25 +++++++++++++++++--------
 lib/libalpm/be_sync.c  |   20 +++++++++++++-------
 lib/libalpm/db.c       |   18 +++++-------------
 lib/libalpm/db.h       |    4 ++--
 lib/libalpm/trans.c    |   12 ------------
 src/pacman/conf.c      |    3 +++
 src/pacman/util.c      |    4 ----
 8 files changed, 41 insertions(+), 47 deletions(-)

diff --git a/lib/libalpm/alpm.c b/lib/libalpm/alpm.c
index 5d475ce..f3f83c6 100644
--- a/lib/libalpm/alpm.c
+++ b/lib/libalpm/alpm.c
@@ -70,7 +70,7 @@ pmhandle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
 	snprintf(myhandle->lockfile, lockfilelen, "%s%s", myhandle->dbpath, lf);
 
 	if(_alpm_db_register_local(myhandle) == NULL) {
-		myerr = PM_ERR_DB_CREATE;
+		myerr = myhandle->pm_errno;
 		goto cleanup;
 	}
 
diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c
index d9a76cc..a37357a 100644
--- a/lib/libalpm/be_local.c
+++ b/lib/libalpm/be_local.c
@@ -863,12 +863,15 @@ int _alpm_local_db_remove(pmdb_t *db, pmpkg_t *info)
 	return ret;
 }
 
-static int local_db_version(pmdb_t *db)
+static int local_db_validate(pmdb_t *db)
 {
 	struct dirent *ent = NULL;
 	const char *dbpath;
 	DIR *dbdir;
-	int version;
+
+	if(db->valid) {
+		return 0;
+	}
 
 	dbpath = _alpm_db_path(db);
 	if(dbpath == NULL) {
@@ -878,7 +881,7 @@ static int local_db_version(pmdb_t *db)
 	if(dbdir == NULL) {
 		if(errno == ENOENT) {
 			/* database dir doesn't exist yet */
-			version = 2;
+			db->valid = 1;
 			goto done;
 		} else {
 			RET_ERR(db->handle, PM_ERR_DB_OPEN, -1);
@@ -899,26 +902,26 @@ static int local_db_version(pmdb_t *db)
 		snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
 		if(access(path, F_OK) == 0) {
 			/* we found a depends file- bail */
-			version = 1;
+			db->handle->pm_errno = PM_ERR_DB_VERSION;
+			db->valid = 0;
 			goto done;
 		}
 	}
 	/* we found no depends file after full scan */
-	version = 2;
+	db->valid = 1;
 
 done:
 	if(dbdir) {
 		closedir(dbdir);
 	}
 
-	_alpm_log(db->handle, PM_LOG_DEBUG, "local database version %d\n", version);
-	return version;
+	return !db->valid;
 }
 
 struct db_operations local_db_ops = {
 	.populate         = local_db_populate,
 	.unregister       = _alpm_db_unregister,
-	.version          = local_db_version,
+	.validate         = local_db_validate,
 };
 
 pmdb_t *_alpm_db_register_local(pmhandle_t *handle)
@@ -929,11 +932,17 @@ pmdb_t *_alpm_db_register_local(pmhandle_t *handle)
 
 	db = _alpm_db_new("local", 1);
 	if(db == NULL) {
+		handle->pm_errno = PM_ERR_DB_CREATE;
 		return NULL;
 	}
 	db->ops = &local_db_ops;
 	db->handle = handle;
 
+	if(local_db_validate(db)) {
+		_alpm_db_free(db);
+		return NULL;
+	}
+
 	handle->db_local = db;
 	return db;
 }
diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c
index c6fee84..6f4c36c 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -69,20 +69,28 @@ static char *get_sync_dir(pmhandle_t *handle)
 
 static int sync_db_validate(pmdb_t *db)
 {
+	pgp_verify_t check_sig;
+
+	if(db->valid) {
+		return 0;
+	}
+
 	/* this takes into account the default verification level if UNKNOWN
 	 * was assigned to this db */
-	pgp_verify_t check_sig = _alpm_db_get_sigverify_level(db);
+	check_sig = _alpm_db_get_sigverify_level(db);
 
 	if(check_sig != PM_PGP_VERIFY_NEVER) {
 		int ret;
 		const char *dbpath = _alpm_db_path(db);
 		if(!dbpath) {
 			/* pm_errno set in _alpm_db_path() */
+			db->valid = 0;
 			return -1;
 		}
 
 		/* we can skip any validation if the database doesn't exist */
 		if(access(dbpath, R_OK) != 0 && errno == ENOENT) {
+			db->valid = 1;
 			return 0;
 		}
 
@@ -91,10 +99,12 @@ static int sync_db_validate(pmdb_t *db)
 		ret = _alpm_gpgme_checksig(db->handle, dbpath, NULL);
 		if((check_sig == PM_PGP_VERIFY_ALWAYS && ret != 0) ||
 				(check_sig == PM_PGP_VERIFY_OPTIONAL && ret == 1)) {
+			db->valid = 0;
 			RET_ERR(db->handle, PM_ERR_SIG_INVALID, -1);
 		}
 	}
 
+	db->valid = 1;
 	return 0;
 }
 
@@ -214,6 +224,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
 	/* Cache needs to be rebuilt */
 	_alpm_db_free_pkgcache(db);
 
+	db->valid = 0;
 	if(sync_db_validate(db)) {
 		/* pm_errno should be set */
 		ret = -1;
@@ -560,15 +571,10 @@ error:
 	return -1;
 }
 
-static int sync_db_version(pmdb_t UNUSED *db)
-{
-	return 2;
-}
-
 struct db_operations sync_db_ops = {
 	.populate         = sync_db_populate,
 	.unregister       = _alpm_db_unregister,
-	.version          = sync_db_version,
+	.validate         = sync_db_validate,
 };
 
 pmdb_t *_alpm_db_register_sync(pmhandle_t *handle, const char *treename,
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 9c974f5..508870f 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -371,14 +371,6 @@ const char *_alpm_db_path(pmdb_t *db)
 	return db->_path;
 }
 
-int _alpm_db_version(pmdb_t *db)
-{
-	if(!db) {
-		return -1;
-	}
-	return db->ops->version(db);
-}
-
 char *_alpm_db_sig_path(pmdb_t *db)
 {
 	char *sigpath;
@@ -477,11 +469,8 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles)
 /* Returns a new package cache from db.
  * It frees the cache if it already exists.
  */
-int _alpm_db_load_pkgcache(pmdb_t *db)
+static int load_pkgcache(pmdb_t *db)
 {
-	if(db == NULL) {
-		return -1;
-	}
 	_alpm_db_free_pkgcache(db);
 
 	_alpm_log(db->handle, PM_LOG_DEBUG, "loading package cache for repository '%s'\n",
@@ -520,7 +509,10 @@ pmpkghash_t *_alpm_db_get_pkgcache_hash(pmdb_t *db)
 	}
 
 	if(!db->pkgcache_loaded) {
-		_alpm_db_load_pkgcache(db);
+		if(db->ops->validate(db)) {
+			return NULL;
+		}
+		load_pkgcache(db);
 	}
 
 	/* hmmm, still NULL ?*/
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index c5fcd5f..5e9c9c1 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -46,7 +46,7 @@ typedef enum _pmdbinfrq_t {
 struct db_operations {
 	int (*populate) (pmdb_t *);
 	void (*unregister) (pmdb_t *);
-	int (*version) (pmdb_t *);
+	int (*validate) (pmdb_t *);
 };
 
 /* Database */
@@ -55,6 +55,7 @@ struct __pmdb_t {
 	char *treename;
 	/* do not access directly, use _alpm_db_path(db) for lazy access */
 	char *_path;
+	int valid;
 	int pkgcache_loaded;
 	int grpcache_loaded;
 	/* also indicates whether we are RO or RW */
@@ -73,7 +74,6 @@ pmdb_t *_alpm_db_new(const char *treename, int is_local);
 void _alpm_db_free(pmdb_t *db);
 const char *_alpm_db_path(pmdb_t *db);
 char *_alpm_db_sig_path(pmdb_t *db);
-int _alpm_db_version(pmdb_t *db);
 int _alpm_db_cmp(const void *d1, const void *d2);
 alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles);
 pmdb_t *_alpm_db_register_local(pmhandle_t *handle);
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index a95280c..7e1c092 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -101,8 +101,6 @@ int SYMEXPORT alpm_trans_init(pmhandle_t *handle, pmtransflag_t flags,
 		alpm_trans_cb_progress progress)
 {
 	pmtrans_t *trans;
-	const int required_db_version = 2;
-	int db_version;
 
 	/* Sanity checks */
 	ASSERT(handle != NULL, return -1);
@@ -122,16 +120,6 @@ int SYMEXPORT alpm_trans_init(pmhandle_t *handle, pmtransflag_t flags,
 	trans->cb_progress = progress;
 	trans->state = STATE_INITIALIZED;
 
-	/* check database version */
-	db_version = _alpm_db_version(handle->db_local);
-	if(db_version < required_db_version) {
-		_alpm_log(handle, PM_LOG_ERROR,
-				_("%s database version is too old\n"), handle->db_local->treename);
-		remove_lock(handle);
-		_alpm_trans_free(trans);
-		RET_ERR(handle, PM_ERR_DB_VERSION, -1);
-	}
-
 	handle->trans = trans;
 
 	return 0;
diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 33ac0b8..1c2f91c 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -449,6 +449,9 @@ static int setup_libalpm(void)
 	if(!handle) {
 		pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"),
 		        alpm_strerror(err));
+		if(err == PM_ERR_DB_VERSION) {
+			pm_printf(PM_LOG_ERROR, _("  try running pacman-db-upgrade\n"));
+		}
 		return -1;
 	}
 	config->handle = handle;
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 71acd6f..2388e5c 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -68,10 +68,6 @@ int trans_init(pmtransflag_t flags)
 						"  running, you can remove %s\n"),
 					alpm_option_get_lockfile(config->handle));
 		}
-		else if(err == PM_ERR_DB_VERSION) {
-			fprintf(stderr, _("  try running pacman-db-upgrade\n"));
-		}
-
 		return -1;
 	}
 	return 0;
-- 
1.7.5.2



More information about the pacman-dev mailing list