Cleanup of pacman.c and addition of default paths to frontend
Instead of barfing when the root path and db path haven't been defined, have pacman set them to some sane defaults when they aren't specified on either the command line or the config file. Also do some cleaning of error output and Doxygen comments. Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
ea9a756eea
commit
178c1d228d
2 changed files with 54 additions and 40 deletions
|
@ -1,10 +1,13 @@
|
||||||
# paths set at make time
|
# paths set at make time
|
||||||
conffile = ${sysconfdir}/pacman.conf
|
conffile = ${sysconfdir}/pacman.conf
|
||||||
|
dbpath = ${localstatedir}/lib/pacman/
|
||||||
|
|
||||||
bin_PROGRAMS = pacman pacman.static
|
bin_PROGRAMS = pacman pacman.static
|
||||||
|
|
||||||
DEFS = -DLOCALEDIR=\"@localedir@\" \
|
DEFS = -DLOCALEDIR=\"@localedir@\" \
|
||||||
-DCONFFILE=\"$(conffile)\" \
|
-DCONFFILE=\"$(conffile)\" \
|
||||||
|
-DROOTDIR=\"$(ROOTDIR)\" \
|
||||||
|
-DDBPATH=\"$(dbpath)\" \
|
||||||
@DEFS@
|
@DEFS@
|
||||||
INCLUDES = -I$(top_srcdir)/lib/libalpm
|
INCLUDES = -I$(top_srcdir)/lib/libalpm
|
||||||
|
|
||||||
|
|
|
@ -56,9 +56,7 @@ pmdb_t *db_local;
|
||||||
/* list of targets specified on command line */
|
/* list of targets specified on command line */
|
||||||
static alpm_list_t *pm_targets;
|
static alpm_list_t *pm_targets;
|
||||||
|
|
||||||
/**
|
/** Display usage/syntax for the specified operation.
|
||||||
* @brief Display usage/syntax for the specified operation.
|
|
||||||
*
|
|
||||||
* @param op the operation code requested
|
* @param op the operation code requested
|
||||||
* @param myname basename(argv[0])
|
* @param myname basename(argv[0])
|
||||||
*/
|
*/
|
||||||
|
@ -155,8 +153,7 @@ static void usage(int op, char *myname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Output pacman version and copyright.
|
||||||
* @brief Output pacman version and copyright.
|
|
||||||
*/
|
*/
|
||||||
static void version(void)
|
static void version(void)
|
||||||
{
|
{
|
||||||
|
@ -170,9 +167,7 @@ static void version(void)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Sets up gettext localization. Safe to call multiple times.
|
||||||
* @brief Sets up gettext localization.
|
|
||||||
* Safe to call multiple times.
|
|
||||||
*/
|
*/
|
||||||
/* Inspired by the monotone function localize_monotone. */
|
/* Inspired by the monotone function localize_monotone. */
|
||||||
static void localize(void)
|
static void localize(void)
|
||||||
|
@ -186,8 +181,7 @@ static void localize(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Set user agent environment variable.
|
||||||
* @brief Set user agent environment variable.
|
|
||||||
*/
|
*/
|
||||||
static void setuseragent(void)
|
static void setuseragent(void)
|
||||||
{
|
{
|
||||||
|
@ -202,11 +196,8 @@ static void setuseragent(void)
|
||||||
setenv("HTTP_USER_AGENT", agent, 0);
|
setenv("HTTP_USER_AGENT", agent, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Catches thrown signals. Performs necessary cleanup to ensure database is
|
||||||
* @brief Catches thrown signals.
|
* in a consistant state.
|
||||||
* Performs necessary cleanup to ensure database is in a consistant
|
|
||||||
* state.
|
|
||||||
*
|
|
||||||
* @param signum the thrown signal
|
* @param signum the thrown signal
|
||||||
*/
|
*/
|
||||||
static void cleanup(int signum)
|
static void cleanup(int signum)
|
||||||
|
@ -225,7 +216,7 @@ static void cleanup(int signum)
|
||||||
|
|
||||||
/* free alpm library resources */
|
/* free alpm library resources */
|
||||||
if(alpm_release() == -1) {
|
if(alpm_release() == -1) {
|
||||||
fprintf(stderr, _("error: %s\n"), alpm_strerror(pm_errno));
|
pm_printf(PM_LOG_ERROR, alpm_strerror(pm_errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free memory */
|
/* free memory */
|
||||||
|
@ -238,12 +229,9 @@ static void cleanup(int signum)
|
||||||
exit(signum);
|
exit(signum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Parse command-line arguments for each operation.
|
||||||
* @brief Parse command-line arguments for each operation
|
|
||||||
*
|
|
||||||
* @param argc argc
|
* @param argc argc
|
||||||
* @param argv argv
|
* @param argv argv
|
||||||
*
|
|
||||||
* @return 0 on success, 1 on error
|
* @return 0 on success, 1 on error
|
||||||
*/
|
*/
|
||||||
static int parseargs(int argc, char *argv[])
|
static int parseargs(int argc, char *argv[])
|
||||||
|
@ -327,7 +315,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
config->logmask |= PM_LOG_DEBUG;
|
config->logmask |= PM_LOG_DEBUG;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, _("error: '%s' is not a valid debug level\n"),
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
@ -343,7 +331,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
case 1007:
|
case 1007:
|
||||||
/* TODO redo this logic- check path somewhere else, delete other cachedirs, etc */
|
/* TODO redo this logic- check path somewhere else, delete other cachedirs, etc */
|
||||||
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
fprintf(stderr, _("error: '%s' is not a valid cache directory\n"),
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid cache directory\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
@ -365,7 +353,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
case 'V': config->version = 1; break;
|
case 'V': config->version = 1; break;
|
||||||
case 'b':
|
case 'b':
|
||||||
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
fprintf(stderr, _("error: '%s' is not a valid db path\n"),
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid db path\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
@ -396,7 +384,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
if(stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
fprintf(stderr, _("error: '%s' is not a valid root path\n"),
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid root path\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
@ -427,7 +415,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
if(config->op == 0) {
|
if(config->op == 0) {
|
||||||
fprintf(stderr, _("error: only one operation may be used at a time\n"));
|
pm_printf(PM_LOG_ERROR, _("only one operation may be used at a time\n"));
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,6 +447,7 @@ static int _parseconfig(const char *file, const char *givensection,
|
||||||
int linenum = 0;
|
int linenum = 0;
|
||||||
char *ptr, *section = NULL;
|
char *ptr, *section = NULL;
|
||||||
pmdb_t *db = NULL;
|
pmdb_t *db = NULL;
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file);
|
pm_printf(PM_LOG_DEBUG, "config: attempting to read file %s\n", file);
|
||||||
fp = fopen(file, "r");
|
fp = fopen(file, "r");
|
||||||
|
@ -621,15 +610,32 @@ static int _parseconfig(const char *file, const char *givensection,
|
||||||
alpm_option_add_holdpkg(p);
|
alpm_option_add_holdpkg(p);
|
||||||
pm_printf(PM_LOG_DEBUG, "config: holdpkg: %s\n", p);
|
pm_printf(PM_LOG_DEBUG, "config: holdpkg: %s\n", p);
|
||||||
} else if(strcmp(key, "DBPath") == 0 || strcmp(upperkey, "DBPATH") == 0) {
|
} else if(strcmp(key, "DBPath") == 0 || strcmp(upperkey, "DBPATH") == 0) {
|
||||||
|
/* don't overwrite a path specified on the command line */
|
||||||
if(alpm_option_get_dbpath() == NULL) {
|
if(alpm_option_get_dbpath() == NULL) {
|
||||||
|
if(stat(ptr, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid db path\n"),
|
||||||
|
ptr);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
alpm_option_set_dbpath(ptr);
|
alpm_option_set_dbpath(ptr);
|
||||||
pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", ptr);
|
pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", ptr);
|
||||||
}
|
}
|
||||||
} else if(strcmp(key, "CacheDir") == 0 || strcmp(upperkey, "CACHEDIR") == 0) {
|
} else if(strcmp(key, "CacheDir") == 0 || strcmp(upperkey, "CACHEDIR") == 0) {
|
||||||
|
if(stat(ptr, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
|
pm_printf(PM_LOG_WARNING, _("'%s' is not a valid cache directory\n"),
|
||||||
|
ptr);
|
||||||
|
} else {
|
||||||
alpm_option_add_cachedir(ptr);
|
alpm_option_add_cachedir(ptr);
|
||||||
pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", ptr);
|
pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", ptr);
|
||||||
|
}
|
||||||
} else if(strcmp(key, "RootDir") == 0 || strcmp(upperkey, "ROOTDIR") == 0) {
|
} else if(strcmp(key, "RootDir") == 0 || strcmp(upperkey, "ROOTDIR") == 0) {
|
||||||
|
/* don't overwrite a path specified on the command line */
|
||||||
if(alpm_option_get_root() == NULL) {
|
if(alpm_option_get_root() == NULL) {
|
||||||
|
if(stat(ptr, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
|
pm_printf(PM_LOG_ERROR, _("'%s' is not a valid root path\n"),
|
||||||
|
ptr);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
alpm_option_set_root(ptr);
|
alpm_option_set_root(ptr);
|
||||||
pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", ptr);
|
pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", ptr);
|
||||||
}
|
}
|
||||||
|
@ -685,12 +691,9 @@ int parseconfig(const char *file)
|
||||||
return(_parseconfig(file, NULL, NULL));
|
return(_parseconfig(file, NULL, NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Main function.
|
||||||
* @brief Main function.
|
|
||||||
*
|
|
||||||
* @param argc argc
|
* @param argc argc
|
||||||
* @param argv argv
|
* @param argv argv
|
||||||
*
|
|
||||||
* @return A return code indicating success, failure, etc.
|
* @return A return code indicating success, failure, etc.
|
||||||
*/
|
*/
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -725,9 +728,9 @@ int main(int argc, char *argv[])
|
||||||
config->noprogressbar = 1;
|
config->noprogressbar = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize pm library */
|
/* initialize library */
|
||||||
if(alpm_initialize() == -1) {
|
if(alpm_initialize() == -1) {
|
||||||
fprintf(stderr, _("error: failed to initialize alpm library (%s)\n"),
|
pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"),
|
||||||
alpm_strerror(pm_errno));
|
alpm_strerror(pm_errno));
|
||||||
cleanup(EXIT_FAILURE);
|
cleanup(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -753,6 +756,14 @@ int main(int argc, char *argv[])
|
||||||
cleanup(ret);
|
cleanup(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ensure root and dbpath were defined */
|
||||||
|
if(alpm_option_get_root() == NULL) {
|
||||||
|
alpm_option_set_root(ROOTDIR);
|
||||||
|
}
|
||||||
|
if(alpm_option_get_dbpath() == NULL) {
|
||||||
|
alpm_option_set_dbpath(DBPATH);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(HAVE_GETEUID)
|
#if defined(HAVE_GETEUID)
|
||||||
/* check if we have sufficient permission for the requested operation */
|
/* check if we have sufficient permission for the requested operation */
|
||||||
if(0) {
|
if(0) {
|
||||||
|
@ -767,7 +778,7 @@ if(0) {
|
||||||
/* special case: ignore root user check if -r is specified, fall back on
|
/* special case: ignore root user check if -r is specified, fall back on
|
||||||
* normal FS checking */
|
* normal FS checking */
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, _("error: you cannot perform this operation unless you are root.\n"));
|
pm_printf(PM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
|
||||||
cleanup(EXIT_FAILURE);
|
cleanup(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -793,7 +804,7 @@ if(0) {
|
||||||
/* Opening local database */
|
/* Opening local database */
|
||||||
db_local = alpm_db_register("local");
|
db_local = alpm_db_register("local");
|
||||||
if(db_local == NULL) {
|
if(db_local == NULL) {
|
||||||
fprintf(stderr, _("error: could not register 'local' database (%s)\n"),
|
pm_printf(PM_LOG_ERROR, _("could not register 'local' database (%s)\n"),
|
||||||
alpm_strerror(pm_errno));
|
alpm_strerror(pm_errno));
|
||||||
cleanup(EXIT_FAILURE);
|
cleanup(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -819,7 +830,7 @@ if(0) {
|
||||||
ret = pacman_deptest(pm_targets);
|
ret = pacman_deptest(pm_targets);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, _("error: no operation specified (use -h for help)\n"));
|
pm_printf(PM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
|
||||||
ret = EXIT_FAILURE;
|
ret = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue