1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-08-13 17:03:46 -04:00

conf.c: parse config in a single pass

Fixes FS#28255

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
This commit is contained in:
Andrew Gregory 2014-04-26 12:19:03 -04:00 committed by Allan McRae
parent 95121cc4f1
commit 7ed0d60a0d
2 changed files with 57 additions and 76 deletions

View File

@ -131,6 +131,9 @@ int config_free(config_t *oldconfig)
alpm_list_free(oldconfig->explicit_adds); alpm_list_free(oldconfig->explicit_adds);
alpm_list_free(oldconfig->explicit_removes); alpm_list_free(oldconfig->explicit_removes);
alpm_list_free_inner(config->repos, (alpm_list_fn_free) config_repo_free);
alpm_list_free(config->repos);
FREELIST(oldconfig->holdpkg); FREELIST(oldconfig->holdpkg);
FREELIST(oldconfig->ignorepkg); FREELIST(oldconfig->ignorepkg);
FREELIST(oldconfig->ignoregrp); FREELIST(oldconfig->ignoregrp);
@ -156,6 +159,7 @@ void config_repo_free(config_repo_t *repo)
if(repo == NULL) { if(repo == NULL) {
return; return;
} }
free(repo->name);
FREELIST(repo->servers); FREELIST(repo->servers);
free(repo); free(repo);
} }
@ -638,6 +642,40 @@ static int _add_mirror(alpm_db_t *db, char *value)
return 0; return 0;
} }
static int register_repo(config_repo_t *repo)
{
alpm_list_t *i;
alpm_db_t *db;
repo->siglevel = merge_siglevel(config->siglevel,
repo->siglevel, repo->siglevel_mask);
db = alpm_register_syncdb(config->handle, repo->name, repo->siglevel);
if(db == NULL) {
pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"),
repo->name, alpm_strerror(alpm_errno(config->handle)));
return 1;
}
pm_printf(ALPM_LOG_DEBUG,
"setting usage of %d for %s repository\n",
repo->usage == 0 ? ALPM_DB_USAGE_ALL : repo->usage,
repo->name);
alpm_db_set_usage(db, repo->usage == 0 ? ALPM_DB_USAGE_ALL : repo->usage);
for(i = repo->servers; i; i = alpm_list_next(i)) {
char *value = i->data;
if(_add_mirror(db, value) != 0) {
pm_printf(ALPM_LOG_ERROR,
_("could not add mirror '%s' to database '%s' (%s)\n"),
value, repo->name, alpm_strerror(alpm_errno(config->handle)));
return 1;
}
}
return 0;
}
/** Sets up libalpm global stuff in one go. Called after the command line /** Sets up libalpm global stuff in one go. Called after the command line
* and initial config file parsing. Once this is complete, we can see if any * and initial config file parsing. Once this is complete, we can see if any
* paths were defined. If a rootdir was defined and nothing else, we want all * paths were defined. If a rootdir was defined and nothing else, we want all
@ -725,6 +763,10 @@ static int setup_libalpm(void)
alpm_option_set_local_file_siglevel(handle, config->localfilesiglevel); alpm_option_set_local_file_siglevel(handle, config->localfilesiglevel);
alpm_option_set_remote_file_siglevel(handle, config->remotefilesiglevel); alpm_option_set_remote_file_siglevel(handle, config->remotefilesiglevel);
for(i = config->repos; i; i = alpm_list_next(i)) {
register_repo(i->data);
}
if(config->xfercommand) { if(config->xfercommand) {
alpm_option_set_fetchcb(handle, download_with_xfercommand); alpm_option_set_fetchcb(handle, download_with_xfercommand);
} else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER)) { } else if(!(alpm_capabilities() & ALPM_CAPABILITY_DOWNLOADER)) {
@ -771,8 +813,6 @@ static int setup_libalpm(void)
struct section_t { struct section_t {
const char *name; const char *name;
config_repo_t *repo; config_repo_t *repo;
int is_options;
int parse_options;
}; };
static int process_usage(alpm_list_t *values, alpm_db_usage_t *usage, static int process_usage(alpm_list_t *values, alpm_db_usage_t *usage,
@ -849,84 +889,31 @@ static int _parse_repo(const char *key, char *value, const char *file,
} else { } else {
pm_printf(ALPM_LOG_WARNING, pm_printf(ALPM_LOG_WARNING,
_("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"), _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
file, line, key, section->name); file, line, key, repo->name);
} }
return ret; return ret;
} }
/**
* Wrap up a section once we have reached the end of it. This should be called
* when a subsequent section is encountered, or when we have reached the end of
* the root config file. Once called, all existing saved config pieces on the
* section struct are freed.
* @param section the current parsed and saved section data
* @param parse_options whether we are parsing options or repo data
* @return 0 on success, 1 on failure
*/
static int finish_section(struct section_t *section)
{
alpm_list_t *i;
alpm_db_t *db;
config_repo_t *repo = section->repo;
pm_printf(ALPM_LOG_DEBUG, "config: finish section '%s'\n", section->name);
/* parsing options (or nothing)- nothing to do except free the pieces */
if(section->parse_options || !section->repo) {
return 0;
}
/* if we are not looking at options sections only, register a db */
repo->siglevel = merge_siglevel(config->siglevel,
repo->siglevel, repo->siglevel_mask);
db = alpm_register_syncdb(config->handle, section->name, repo->siglevel);
if(db == NULL) {
pm_printf(ALPM_LOG_ERROR, _("could not register '%s' database (%s)\n"),
section->name, alpm_strerror(alpm_errno(config->handle)));
return 1;
}
pm_printf(ALPM_LOG_DEBUG,
"setting usage of %d for %s repository\n",
repo->usage == 0 ? ALPM_DB_USAGE_ALL : repo->usage,
section->name);
alpm_db_set_usage(db, repo->usage == 0 ? ALPM_DB_USAGE_ALL : repo->usage);
for(i = repo->servers; i; i = alpm_list_next(i)) {
char *value = i->data;
if(_add_mirror(db, value) != 0) {
pm_printf(ALPM_LOG_ERROR,
_("could not add mirror '%s' to database '%s' (%s)\n"),
value, section->name, alpm_strerror(alpm_errno(config->handle)));
return 1;
}
}
return 0;
}
static int _parse_directive(const char *file, int linenum, const char *name, static int _parse_directive(const char *file, int linenum, const char *name,
char *key, char *value, void *data) char *key, char *value, void *data)
{ {
struct section_t *section = data; struct section_t *section = data;
if(!key && !value) { if(!key && !value) {
int ret = finish_section(data);
section->name = name; section->name = name;
pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name); pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
config_repo_free(section->repo);
section->repo = NULL;
section->is_options = 0;
if(!name) { if(!name) {
/* end of file, do nothing */ /* end of file, do nothing */
} else if(strcmp(name, "options") == 0) { } else if(strcmp(name, "options") == 0) {
section->is_options = 1; section->repo = NULL;
} else if(!section->parse_options) { } else {
section->repo = calloc(sizeof(config_repo_t), 1); section->repo = calloc(sizeof(config_repo_t), 1);
section->repo->name = strdup(name);
section->repo->siglevel = ALPM_SIG_USE_DEFAULT; section->repo->siglevel = ALPM_SIG_USE_DEFAULT;
section->repo->usage = 0; section->repo->usage = 0;
config->repos = alpm_list_add(config->repos, section->repo);
} }
return ret; return 0;
} }
if(section->name == NULL) { if(section->name == NULL) {
@ -935,15 +922,13 @@ static int _parse_directive(const char *file, int linenum, const char *name,
return 1; return 1;
} }
if(section->parse_options && section->is_options) { if(!section->repo) {
/* we are either in options ... */ /* we are either in options ... */
return _parse_options(key, value, file, linenum); return _parse_options(key, value, file, linenum);
} else if(!section->parse_options && section->repo) { } else {
/* ... or in a repo section */ /* ... or in a repo section */
return _parse_repo(key, value, file, linenum, section); return _parse_repo(key, value, file, linenum, section);
} }
return 0;
} }
/** Parse a configuration file. /** Parse a configuration file.
@ -955,23 +940,16 @@ int parseconfig(const char *file)
int ret; int ret;
struct section_t section; struct section_t section;
memset(&section, 0, sizeof(struct section_t)); memset(&section, 0, sizeof(struct section_t));
/* the config parse is a two-pass affair. We first parse the entire thing for
* the [options] section so we can get all default and path options set.
* Next, we go back and parse everything but [options]. */
/* call the real parseconfig function with a null section & db argument */
pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n");
section.parse_options = 1;
if((ret = parse_ini(file, _parse_directive, &section))) { if((ret = parse_ini(file, _parse_directive, &section))) {
return ret; return ret;
} }
if((ret = setup_libalpm())) { if((ret = setup_libalpm())) {
return ret; return ret;
} }
/* second pass, repo section parsing */ alpm_list_free_inner(config->repos, (alpm_list_fn_free) config_repo_free);
pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n"); alpm_list_free(config->repos);
section.parse_options = 0; config->repos = NULL;
return parse_ini(file, _parse_directive, &section); return ret;
} }
/* vim: set noet: */ /* vim: set noet: */

View File

@ -35,6 +35,7 @@ typedef struct __colstr_t {
} colstr_t; } colstr_t;
typedef struct __config_repo_t { typedef struct __config_repo_t {
char *name;
alpm_list_t *servers; alpm_list_t *servers;
alpm_db_usage_t usage; alpm_db_usage_t usage;
alpm_siglevel_t siglevel; alpm_siglevel_t siglevel;
@ -125,6 +126,8 @@ typedef struct __config_t {
/* Color strings for output */ /* Color strings for output */
colstr_t colstr; colstr_t colstr;
alpm_list_t *repos;
} config_t; } config_t;
/* Operations */ /* Operations */