[pacman-dev] [PATCH] Clean way to separate local from sync dbs.

Travis Willard travis at archlinux.org
Sun Aug 26 12:02:57 EDT 2007


On Fri, 24 Aug 2007 22:40:41 -0400
Travis Willard <travis at archlinux.org> wrote:

> Here's the quick-and-dirty version of moving repos to
> $DBPATH/sync/$REPO.
> 
> I don't much like it, to be honest - I'd prefer to separate the
> concept of the local repo from the sync repos entirely

And here's my patch to do this.  Read the comment for details - I think
I was specific enough there.

From 7d9c73eda546195a0a207be8b264219184929712 Mon Sep 17 00:00:00 2001
From: Travis Willard <travis at archlinux.org>
Date: Sun, 26 Aug 2007 12:00:03 -0400
Subject: [PATCH] Clean way to separate local from sync dbs.

Introduce two new methods into the API - alpm_syncdb_register and
alpm_localdb_register, which replace the functionality of
alpm_db_register. which has now been flagged as deprecated.
localdb_register always returns the local DB, and syncdb_register will
always try to register a sync DB.  This removes the silly limitation
that a sync DB couldn't be named 'local', along with structurally
separating sync DBs and the local DB in the filesystem. Also updated
the pacman frontend to use the new functions.

Signed-off-by: Travis Willard <travis at archlinux.org>
---
 lib/libalpm/alpm.h  |    8 ++++++-
 lib/libalpm/db.c    |   56
+++++++++++++++++++++++++++++++++++++++++++------- lib/libalpm/db.h
|    9 +++++++- src/pacman/pacman.c |   10 +-------
 4 files changed, 65 insertions(+), 18 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 844d9bf..74d6102 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -31,6 +31,8 @@ extern "C" {
 #include <time.h> /* for time_t */
 #include <stdarg.h> /* for va_list */
 
+#define DEPRECATED __attribute__((deprecated))
+
 /*
  * Arch Linux Package Management library
  */
@@ -143,7 +145,11 @@ alpm_list_t *alpm_option_get_syncdbs();
  * Databases
  */
 
-pmdb_t *alpm_db_register(const char *treename);
+/* Deprecated interface alpm_db_register */
+pmdb_t DEPRECATED *alpm_db_register(const char *treename);
+/* Preferred interfaces localdb_register and syncdb_register */
+pmdb_t *alpm_localdb_register();
+pmdb_t *alpm_syncdb_register(const char *treename);
 int alpm_db_unregister(pmdb_t *db);
 int alpm_db_unregister_all(void);
 
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 6bc85b7..dac1959 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -53,8 +53,8 @@
  * @{
  */
 
-/** Register a package database
- * @param treename the name of the repository
+/** Register a package database (deprecated)
+ * @param treename the name of the sync repository or "local" for the
local DB
  * @return a pmdb_t* on success (the value), NULL on error
  */
 pmdb_t SYMEXPORT *alpm_db_register(const char *treename)
@@ -67,9 +67,42 @@ pmdb_t SYMEXPORT *alpm_db_register(const char
*treename) /* Do not register a database if a transaction is on-going */
 	ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL,
NULL)); 
-	return(_alpm_db_register(treename));
+	return(_alpm_db_register(treename, DB_REGISTER_OLD_BEHAVIOUR));
 }
 
+/** Register a sync-database of packages.
+ * @param treename the name of the sync repository
+ * @return a pmdb_t* on success (the value), NULL on error
+ */
+pmdb_t SYMEXPORT *alpm_syncdb_register(const char *treename)
+{
+	ALPM_LOG_FUNC;
+
+	/* Sanity checks */
+	ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
+	ASSERT(treename != NULL && strlen(treename) != 0,
RET_ERR(PM_ERR_WRONG_ARGS, NULL));
+	/* Do not register a database if a transaction is on-going */
+	ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL,
NULL)); +
+	return(_alpm_db_register(treename, DB_REGISTER_SYNC_DB));
+}
+
+/** Register the local package database
+ * @return a pmdb_t* representing the local database, or NULL on error
+ */
+pmdb_t SYMEXPORT *alpm_localdb_register()
+{
+	ALPM_LOG_FUNC;
+
+	/* Sanity checks */
+	ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, NULL));
+	/* Do not register a database if a transaction is on-going */
+	ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL,
NULL)); +
+	return(_alpm_db_register("local", DB_REGISTER_LOCAL_DB));
+}
+
+
 /* Helper function for alpm_db_unregister{_all} */
 static void _alpm_db_unregister(pmdb_t *db)
 {
@@ -690,7 +723,7 @@ alpm_list_t *_alpm_db_search(pmdb_t *db, const
alpm_list_t *needles) return(ret);
 }
 
-pmdb_t *_alpm_db_register(const char *treename)
+pmdb_t *_alpm_db_register(const char *treename, pmdbregtype_t type)
 {
 	struct stat buf;
 	pmdb_t *db;
@@ -701,10 +734,16 @@ pmdb_t *_alpm_db_register(const char *treename)
 
 	ALPM_LOG_FUNC;
 
-	if(strcmp(treename, "local") == 0) {
+	if(type == DB_REGISTER_LOCAL_DB ||
+		 (type == DB_REGISTER_OLD_BEHAVIOUR &&
strcmp(treename, "local") == 0)) { +
 		if(handle->db_local != NULL) {
-			_alpm_log(PM_LOG_WARNING, _("attempt to
re-register the 'local' DB"));
-			RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
+			if (type == DB_REGISTER_OLD_BEHAVIOUR) {
+				_alpm_log(PM_LOG_WARNING, _("attempt
to re-register the 'local' DB"));
+				RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
+			} else {
+				return handle->db_local;
+			}
 		}
 		local = 1;
 	} else {
@@ -717,9 +756,10 @@ pmdb_t *_alpm_db_register(const char *treename)
 			}
 		}
 		local = 0;
+
 	}
 	
-	_alpm_log(PM_LOG_DEBUG, "registering database '%s'", treename);
+	_alpm_log(PM_LOG_DEBUG, "registering %s database '%s'",
local?"local":"sync", treename); 
 	/* make sure the database directory exists */
 	dbpath = alpm_option_get_dbpath();
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index 2597c8c..be895f2 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -37,6 +37,13 @@ typedef enum _pmdbinfrq_t {
 	INFRQ_ALL = 0x1F
 } pmdbinfrq_t;
 
+/* Register functionality. */
+typedef enum _pmdbregtype_t {
+	DB_REGISTER_OLD_BEHAVIOUR = 0x1,
+	DB_REGISTER_SYNC_DB = 0x2,
+	DB_REGISTER_LOCAL_DB = 0x3
+} pmdbregtype_t;
+
 /* Database */
 struct __pmdb_t {
 	char *path;
@@ -52,7 +59,7 @@ pmdb_t *_alpm_db_new(const char *dbpath, const char
*treename); void _alpm_db_free(pmdb_t *db);
 int _alpm_db_cmp(const void *db1, const void *db2);
 alpm_list_t *_alpm_db_search(pmdb_t *db, const alpm_list_t *needles);
-pmdb_t *_alpm_db_register(const char *treename);
+pmdb_t *_alpm_db_register(const char *treename, pmdbregtype_t type);
 
 /* be.c, backend specific calls */
 int _alpm_db_install(pmdb_t *db, const char *dbfile);
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 31302ab..ac4f6c5 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -495,15 +495,9 @@ static int _parseconfig(const char *file, const
char *givensection, file, linenum);
 				return(1);
 			}
-			/* a section/database named local is not
allowed */
-			if(!strcmp(section, "local")) {
-				pm_printf(PM_LOG_ERROR, _("config file
%s, line %d: 'local' cannot be used as section name.\n"),
-						file, linenum);
-				return(1);
-			}
 			/* if we are not looking at the options
section, register a db */ if(strcmp(section, "options") != 0) {
-				db = alpm_db_register(section);
+				db = alpm_syncdb_register(section);
 			}
 		} else {
 			/* directive */
@@ -811,7 +805,7 @@ int main(int argc, char *argv[])
 	}
 
 	/* Opening local database */
-	db_local = alpm_db_register("local");
+	db_local = alpm_localdb_register();
 	if(db_local == NULL) {
 		pm_printf(PM_LOG_ERROR, _("could not register 'local'
database (%s)\n"), alpm_strerror(pm_errno));
-- 
1.5.2.5





More information about the pacman-dev mailing list