Remove need to explicitly register the local DB

Perform the cheap struct and string setup of the local DB at handle
initialization time to match the teardown we do when releasing the handle.
If the local DB is not needed, all real initialization is done lazily after
DB paths and other things have been configured anyway.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-01-28 17:41:15 -06:00
parent 9b876fff09
commit ef86da97f5
11 changed files with 24 additions and 45 deletions

View File

@ -54,6 +54,10 @@ int SYMEXPORT alpm_initialize(void)
if(handle == NULL) {
RET_ERR(PM_ERR_MEMORY, -1);
}
if(_alpm_db_register_local() == NULL) {
/* error code should be set */
return(-1);
}
#ifdef ENABLE_NLS
bindtextdomain("libalpm", LOCALEDIR);

View File

@ -174,8 +174,6 @@ typedef enum _pmpkgreason_t {
* Databases
*/
/* Preferred interfaces db_register_local and db_register_sync */
pmdb_t *alpm_db_register_local(void);
pmdb_t *alpm_db_register_sync(const char *treename);
int alpm_db_unregister(pmdb_t *db);
int alpm_db_unregister_all(void);

View File

@ -400,7 +400,7 @@ static int local_db_populate(pmdb_t *db)
pkg = _alpm_pkg_new();
if(pkg == NULL) {
closedir(dbdir);
return(-1);
RET_ERR(PM_ERR_MEMORY, -1);
}
/* split the db entry name */
if(_alpm_splitname(name, pkg) != 0) {
@ -900,11 +900,6 @@ pmdb_t *_alpm_db_register_local(void)
ALPM_LOG_FUNC;
if(handle->db_local != NULL) {
_alpm_log(PM_LOG_WARNING, _("attempt to re-register the 'local' DB\n"));
RET_ERR(PM_ERR_DB_NOT_NULL, NULL);
}
_alpm_log(PM_LOG_DEBUG, "registering local database\n");
db = _alpm_db_new("local", 1);

View File

@ -64,21 +64,6 @@ pmdb_t SYMEXPORT *alpm_db_register_sync(const char *treename)
return(_alpm_db_register_sync(treename));
}
/** Register the local package database.
* @return a pmdb_t* representing the local database, or NULL on error
*/
pmdb_t SYMEXPORT *alpm_db_register_local(void)
{
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());
}
/* Helper function for alpm_db_unregister{_all} */
void _alpm_db_unregister(pmdb_t *db)
{

View File

@ -31,8 +31,6 @@
#include "conf.h"
#include "util.h"
extern pmdb_t *db_local;
/**
* @brief Modify the 'local' package database.
*
@ -43,6 +41,7 @@ extern pmdb_t *db_local;
int pacman_database(alpm_list_t *targets)
{
alpm_list_t *i;
pmdb_t *db_local;
int retval = 0;
pmpkgreason_t reason;
@ -65,6 +64,7 @@ int pacman_database(alpm_list_t *targets)
return(1);
}
db_local = alpm_option_get_localdb();
for(i = targets; i; i = alpm_list_next(i)) {
char *pkgname = i->data;
if(alpm_db_set_pkgreason(db_local, pkgname, reason) == -1) {

View File

@ -57,7 +57,6 @@
#include "conf.h"
#include "package.h"
pmdb_t *db_local;
/* list of targets specified on command line */
static alpm_list_t *pm_targets;
@ -1433,14 +1432,6 @@ int main(int argc, char *argv[])
list_display("Targets :", pm_targets);
}
/* Opening local database */
db_local = alpm_db_register_local();
if(db_local == NULL) {
pm_printf(PM_LOG_ERROR, _("could not register 'local' database (%s)\n"),
alpm_strerrorlast());
cleanup(EXIT_FAILURE);
}
/* Log commandline */
if(needs_root()) {
cl_to_log(argc, argv);

View File

@ -37,8 +37,6 @@
#include "conf.h"
#include "util.h"
extern pmdb_t *db_local;
static char *resolve_path(const char *file)
{
char *str = NULL;
@ -102,6 +100,7 @@ static int query_fileowner(alpm_list_t *targets)
char *append;
size_t max_length;
alpm_list_t *t;
pmdb_t *db_local;
/* This code is here for safety only */
if(targets == NULL) {
@ -117,6 +116,8 @@ static int query_fileowner(alpm_list_t *targets)
append = path + strlen(path);
max_length = PATH_MAX - (append - path) - 1;
db_local = alpm_option_get_localdb();
for(t = targets; t; t = alpm_list_next(t)) {
char *filename, *dname, *rpath;
const char *bname;
@ -220,6 +221,7 @@ static int query_search(alpm_list_t *targets)
{
alpm_list_t *i, *searchlist;
int freelist;
pmdb_t *db_local = alpm_option_get_localdb();
/* if we have a targets list, search for packages matching it */
if(targets) {
@ -286,6 +288,8 @@ static int query_group(alpm_list_t *targets)
alpm_list_t *i, *j;
char *grpname = NULL;
int ret = 0;
pmdb_t *db_local = alpm_option_get_localdb();
if(targets == NULL) {
for(j = alpm_db_get_grpcache(db_local); j; j = alpm_list_next(j)) {
pmgrp_t *grp = alpm_list_getdata(j);
@ -471,6 +475,7 @@ int pacman_query(alpm_list_t *targets)
int match = 0;
alpm_list_t *i;
pmpkg_t *pkg = NULL;
pmdb_t *db_local;
/* First: operations that do not require targets */
@ -495,6 +500,8 @@ int pacman_query(alpm_list_t *targets)
}
}
db_local = alpm_option_get_localdb();
/* operations on all packages in the local DB
* valid: no-op (plain -Q), list, info, check
* invalid: isfile, owns */

View File

@ -31,8 +31,6 @@
#include "util.h"
#include "conf.h"
extern pmdb_t *db_local;
/**
* @brief Remove a specified list of packages.
*

View File

@ -37,8 +37,6 @@
#include "package.h"
#include "conf.h"
extern pmdb_t *db_local;
/* if keep_used != 0, then the db files which match an used syncdb
* will be kept */
static int sync_cleandb(const char *dbpath, int keep_used) {
@ -144,6 +142,7 @@ static int sync_cleancache(int level)
{
alpm_list_t *i;
alpm_list_t *sync_dbs = alpm_option_get_syncdbs();
pmdb_t *db_local = alpm_option_get_localdb();
int ret = 0;
for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
@ -295,7 +294,7 @@ static int sync_synctree(int level, alpm_list_t *syncs)
return(success > 0);
}
static void print_installed(pmpkg_t *pkg)
static void print_installed(pmdb_t *db_local, pmpkg_t *pkg)
{
const char *pkgname = alpm_pkg_get_name(pkg);
const char *pkgver = alpm_pkg_get_version(pkg);
@ -316,6 +315,7 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
alpm_list_t *i, *j, *ret;
int freelist;
int found = 0;
pmdb_t *db_local = alpm_option_get_localdb();
for(i = syncs; i; i = alpm_list_next(i)) {
pmdb_t *db = alpm_list_getdata(i);
@ -366,7 +366,7 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
printf(")");
}
print_installed(pkg);
print_installed(db_local, pkg);
/* we need a newline and initial indent first */
printf("\n ");
@ -519,6 +519,7 @@ static int sync_info(alpm_list_t *syncs, alpm_list_t *targets)
static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
{
alpm_list_t *i, *j, *ls = NULL;
pmdb_t *db_local = alpm_option_get_localdb();
if(targets) {
for(i = targets; i; i = alpm_list_next(i)) {
@ -556,7 +557,7 @@ static int sync_list(alpm_list_t *syncs, alpm_list_t *targets)
if (!config->quiet) {
printf("%s %s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
alpm_pkg_get_version(pkg));
print_installed(pkg);
print_installed(db_local, pkg);
printf("\n");
} else {
printf("%s\n", alpm_pkg_get_name(pkg));

View File

@ -77,7 +77,7 @@ static int alpm_local_init(void)
return(ret);
}
db_local = alpm_db_register_local();
db_local = alpm_option_get_localdb();
if(!db_local) {
return(1);
}

View File

@ -136,7 +136,7 @@ static int check_localdb(void) {
return(ret);
}
db = alpm_db_register_local();
db = alpm_option_get_localdb();
if(db == NULL) {
fprintf(stderr, "error: could not register 'local' database (%s)\n",
alpm_strerrorlast());