From a50b067470a8046dabdff66f6266d2208b2f8372 Mon Sep 17 00:00:00 2001 From: Nagy Gabor Date: Mon, 17 Nov 2008 17:02:43 +0100 Subject: Give an error message on alpm_db_register_sync() error This patch slightly modifies pacman.c/_parseconfig(): See FS#12148. Now pacman prints the following error message in that case: "error: could not register 'unstable' database (could not open database)" I also added an error message for alpm_db_setserver() error. I changed the "return(1);" scheme to "ret = 1; goto cleanup;" to make sure that we free allocated memory and close open files. Signed-off-by: Nagy Gabor Signed-off-by: Dan McGee --- src/pacman/pacman.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 0e133df6..c7db3b48 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -577,6 +577,7 @@ static int _parseconfig(const char *file, const char *givensection, int linenum = 0; char *ptr, *section = NULL; pmdb_t *db = NULL; + int ret = 0; pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file); fp = fopen(file, "r"); @@ -619,7 +620,8 @@ static int _parseconfig(const char *file, const char *givensection, if(!strlen(section)) { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"), file, linenum); - return(1); + ret = 1; + goto cleanup; } /* if we are not looking at the options section, register a db and also * ensure we have set all of our library paths as the library is too stupid @@ -627,6 +629,12 @@ static int _parseconfig(const char *file, const char *givensection, if(strcmp(section, "options") != 0) { setlibpaths(); db = alpm_db_register_sync(section); + if(db == NULL) { + pm_printf(PM_LOG_ERROR, _("could not register '%s' database (%s)\n"), + section, alpm_strerrorlast()); + ret = 1; + goto cleanup; + } } } else { /* directive */ @@ -641,13 +649,15 @@ static int _parseconfig(const char *file, const char *givensection, if(key == NULL) { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"), file, linenum); - return(1); + ret = 1; + goto cleanup; } /* For each directive, compare to the camelcase string. */ if(section == NULL) { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"), file, linenum); - return(1); + ret = 1; + goto cleanup; } if(ptr == NULL && strcmp(section, "options") == 0) { /* directives without settings, all in [options] */ @@ -672,7 +682,8 @@ static int _parseconfig(const char *file, const char *givensection, } else { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"), file, linenum, key); - return(1); + ret = 1; + goto cleanup; } } else { /* directives with settings */ @@ -703,7 +714,8 @@ static int _parseconfig(const char *file, const char *givensection, if(alpm_option_add_cachedir(ptr) != 0) { pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"), ptr, alpm_strerrorlast()); - return(1); + ret = 1; + goto cleanup; } pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", ptr); } else if(strcmp(key, "RootDir") == 0) { @@ -727,13 +739,15 @@ static int _parseconfig(const char *file, const char *givensection, config->cleanmethod = PM_CLEAN_KEEPCUR; } else { pm_printf(PM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"), ptr); - return(1); + ret = 1; + goto cleanup; } pm_printf(PM_LOG_DEBUG, "config: cleanmethod: %s\n", ptr); } else { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"), file, linenum, key); - return(1); + ret = 1; + goto cleanup; } } else if(strcmp(key, "Server") == 0) { /* let's attempt a replacement for the current repo */ @@ -741,27 +755,35 @@ static int _parseconfig(const char *file, const char *givensection, if(alpm_db_setserver(db, server) != 0) { /* pm_errno is set by alpm_db_setserver */ - return(1); + pm_printf(PM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"), + alpm_db_get_name(db), server, alpm_strerrorlast()); + free(server); + ret = 1; + goto cleanup; } free(server); } else { pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' not recognized.\n"), file, linenum, key); - return(1); + ret = 1; + goto cleanup; } } } } - fclose(fp); + +cleanup: + if(fp) { + fclose(fp); + } if(section){ free(section); } - /* call setlibpaths here to ensure we have called it at least once */ setlibpaths(); pm_printf(PM_LOG_DEBUG, "config: finished parsing %s\n", file); - return(0); + return(ret); } /** Parse a configuration file. -- cgit v1.2.3-70-g09d2