From c748eadc80593c3941b55b1d4ec6e46899abd295 Mon Sep 17 00:00:00 2001
From: Dan McGee <dan@archlinux.org>
Date: Tue, 7 Jun 2011 23:08:06 -0500
Subject: Allow invalid sync DBs to be returned by the library

They are placeholders, but important for things like trying to re-sync a
database missing a signature. By using the alpm_db_validity() method at
the right time, a client can take the appropriate action with these
invalid databases as necessary.

In pacman's case, we disallow just about anything that involves looking
at a sync database outside of an '-Sy' operation (although we do check
the validity immediately after). A few operations are still permitted-
'-Q' ops that don't touch sync databases as well as '-R'.

Signed-off-by: Dan McGee <dan@archlinux.org>
---
 src/pacman/database.c |  2 +-
 src/pacman/query.c    |  7 ++-----
 src/pacman/remove.c   |  2 +-
 src/pacman/sync.c     | 15 +++++++++------
 src/pacman/upgrade.c  |  2 +-
 src/pacman/util.c     | 30 +++++++++++++++++++++++++++++-
 src/pacman/util.h     |  3 ++-
 7 files changed, 45 insertions(+), 16 deletions(-)

(limited to 'src/pacman')

diff --git a/src/pacman/database.c b/src/pacman/database.c
index 5a1aa52b..41efdd3d 100644
--- a/src/pacman/database.c
+++ b/src/pacman/database.c
@@ -59,7 +59,7 @@ int pacman_database(alpm_list_t *targets)
 	}
 
 	/* Lock database */
-	if(trans_init(0) == -1) {
+	if(trans_init(0, 0) == -1) {
 		return 1;
 	}
 
diff --git a/src/pacman/query.c b/src/pacman/query.c
index 5dff03ff..90329b33 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -501,11 +501,8 @@ int pacman_query(alpm_list_t *targets)
 		return ret;
 	}
 
-	if(config->op_q_foreign) {
-		/* ensure we have at least one valid sync db set up */
-		alpm_list_t *sync_dbs = alpm_option_get_syncdbs(config->handle);
-		if(sync_dbs == NULL) {
-			pm_printf(ALPM_LOG_ERROR, _("no usable package repositories configured.\n"));
+	if(config->op_q_foreign || config->op_q_upgrade) {
+		if(check_syncdbs(1, 1)) {
 			return 1;
 		}
 	}
diff --git a/src/pacman/remove.c b/src/pacman/remove.c
index 3cedb892..f0ac04e2 100644
--- a/src/pacman/remove.c
+++ b/src/pacman/remove.c
@@ -81,7 +81,7 @@ int pacman_remove(alpm_list_t *targets)
 	}
 
 	/* Step 0: create a new transaction */
-	if(trans_init(config->flags) == -1) {
+	if(trans_init(config->flags, 0) == -1) {
 		return 1;
 	}
 
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index 5f67236d..26c9ccca 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -738,7 +738,7 @@ static int sync_trans(alpm_list_t *targets)
 	alpm_list_t *i;
 
 	/* Step 1: create a new transaction... */
-	if(trans_init(config->flags) == -1) {
+	if(trans_init(config->flags, 1) == -1) {
 		return 1;
 	}
 
@@ -885,7 +885,7 @@ int pacman_sync(alpm_list_t *targets)
 	if(config->op_s_clean) {
 		int ret = 0;
 
-		if(trans_init(0) == -1) {
+		if(trans_init(0, 0) == -1) {
 			return 1;
 		}
 
@@ -900,13 +900,12 @@ int pacman_sync(alpm_list_t *targets)
 		return ret;
 	}
 
-	/* ensure we have at least one valid sync db set up */
-	sync_dbs = alpm_option_get_syncdbs(config->handle);
-	if(sync_dbs == NULL) {
-		pm_printf(ALPM_LOG_ERROR, _("no usable package repositories configured.\n"));
+	if(check_syncdbs(1, 0)) {
 		return 1;
 	}
 
+	sync_dbs = alpm_option_get_syncdbs(config->handle);
+
 	if(config->op_s_sync) {
 		/* grab a fresh package list */
 		printf(_(":: Synchronizing package databases...\n"));
@@ -916,6 +915,10 @@ int pacman_sync(alpm_list_t *targets)
 		}
 	}
 
+	if(check_syncdbs(1, 1)) {
+		return 1;
+	}
+
 	/* search for a package */
 	if(config->op_s_search) {
 		return sync_search(sync_dbs, targets);
diff --git a/src/pacman/upgrade.c b/src/pacman/upgrade.c
index 11d00e73..a99b1370 100644
--- a/src/pacman/upgrade.c
+++ b/src/pacman/upgrade.c
@@ -67,7 +67,7 @@ int pacman_upgrade(alpm_list_t *targets)
 	}
 
 	/* Step 1: create a new transaction */
-	if(trans_init(config->flags) == -1) {
+	if(trans_init(config->flags, 1) == -1) {
 		return 1;
 	}
 
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 28beaca3..c367d36e 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -49,9 +49,12 @@
 #include "callback.h"
 
 
-int trans_init(alpm_transflag_t flags)
+int trans_init(alpm_transflag_t flags, int check_valid)
 {
 	int ret;
+
+	check_syncdbs(0, check_valid);
+
 	if(config->print) {
 		ret = alpm_trans_init(config->handle, flags, NULL, NULL, NULL);
 	} else {
@@ -101,6 +104,31 @@ int needs_root(void)
 	}
 }
 
+int check_syncdbs(size_t need_repos, int check_valid)
+{
+	int ret = 0;
+	alpm_list_t *i;
+	alpm_list_t *sync_dbs = alpm_option_get_syncdbs(config->handle);
+
+	if(need_repos && sync_dbs == NULL) {
+		pm_printf(ALPM_LOG_ERROR, _("no usable package repositories configured.\n"));
+		return 1;
+	}
+
+	if(check_valid) {
+		/* ensure all known dbs are valid */
+		for(i = sync_dbs; i; i = alpm_list_next(i)) {
+			alpm_db_t *db = i->data;
+			if(alpm_db_get_valid(db)) {
+				pm_printf(ALPM_LOG_ERROR, _("database '%s' is not valid (%s)\n"),
+						alpm_db_get_name(db), alpm_strerror(alpm_errno(config->handle)));
+				ret = 1;
+			}
+		}
+	}
+	return ret;
+}
+
 /* discard unhandled input on the terminal's input buffer */
 static int flush_term_input(void) {
 #ifdef HAVE_TCFLUSH
diff --git a/src/pacman/util.h b/src/pacman/util.h
index a914d0c6..ee3dbd11 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -39,9 +39,10 @@
 /* update speed for the fill_progress based functions */
 #define UPDATE_SPEED_SEC 0.2f
 
-int trans_init(alpm_transflag_t flags);
+int trans_init(alpm_transflag_t flags, int check_valid);
 int trans_release(void);
 int needs_root(void);
+int check_syncdbs(size_t need_repos, int check_valid);
 int getcols(void);
 int rmrf(const char *path);
 const char *mbasename(const char *path);
-- 
cgit v1.2.3-70-g09d2