summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-05-05 11:12:49 -0500
committerDan McGee <dan@archlinux.org>2011-05-05 11:12:49 -0500
commitbda208f82325812749396efacf828066947872e5 (patch)
tree5c061743f54a503b1549a04a6b68ab65abb8812d
parentd360153bc6d4368e18a6d2e169d7d667e1640992 (diff)
downloadpacman-bda208f82325812749396efacf828066947872e5.tar.xz
Move parseconfig to conf.c
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--src/pacman/conf.c600
-rw-r--r--src/pacman/conf.h2
-rw-r--r--src/pacman/pacman.c595
-rw-r--r--src/pacman/pacman.h7
4 files changed, 611 insertions, 593 deletions
diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 69110ab9..04ec3385 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -20,13 +20,20 @@
#include "config.h"
+#include <errno.h>
+#include <glob.h>
+#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* strdup */
+#include <sys/stat.h>
+#include <sys/utsname.h> /* uname */
+#include <unistd.h>
/* pacman */
#include "conf.h"
#include "util.h"
+#include "pacman.h"
/* global config variable */
config_t *config = NULL;
@@ -69,4 +76,597 @@ int config_free(config_t *oldconfig)
return 0;
}
+/** Helper function for download_with_xfercommand() */
+static char *get_filename(const char *url) {
+ char *filename = strrchr(url, '/');
+ if(filename != NULL) {
+ filename++;
+ }
+ return filename;
+}
+
+/** Helper function for download_with_xfercommand() */
+static char *get_destfile(const char *path, const char *filename) {
+ char *destfile;
+ /* len = localpath len + filename len + null */
+ size_t len = strlen(path) + strlen(filename) + 1;
+ destfile = calloc(len, sizeof(char));
+ snprintf(destfile, len, "%s%s", path, filename);
+
+ return destfile;
+}
+
+/** Helper function for download_with_xfercommand() */
+static char *get_tempfile(const char *path, const char *filename) {
+ char *tempfile;
+ /* len = localpath len + filename len + '.part' len + null */
+ size_t len = strlen(path) + strlen(filename) + 6;
+ tempfile = calloc(len, sizeof(char));
+ snprintf(tempfile, len, "%s%s.part", path, filename);
+
+ return tempfile;
+}
+
+/** External fetch callback */
+static int download_with_xfercommand(const char *url, const char *localpath,
+ int force) {
+ int ret = 0;
+ int retval;
+ int usepart = 0;
+ struct stat st;
+ char *parsedcmd,*tempcmd;
+ char cwd[PATH_MAX];
+ int restore_cwd = 0;
+ char *destfile, *tempfile, *filename;
+
+ if(!config->xfercommand) {
+ return -1;
+ }
+
+ filename = get_filename(url);
+ if(!filename) {
+ return -1;
+ }
+ destfile = get_destfile(localpath, filename);
+ tempfile = get_tempfile(localpath, filename);
+
+ if(force && stat(tempfile, &st) == 0) {
+ unlink(tempfile);
+ }
+ if(force && stat(destfile, &st) == 0) {
+ unlink(destfile);
+ }
+
+ tempcmd = strdup(config->xfercommand);
+ /* replace all occurrences of %o with fn.part */
+ if(strstr(tempcmd, "%o")) {
+ usepart = 1;
+ parsedcmd = strreplace(tempcmd, "%o", tempfile);
+ free(tempcmd);
+ tempcmd = parsedcmd;
+ }
+ /* replace all occurrences of %u with the download URL */
+ parsedcmd = strreplace(tempcmd, "%u", url);
+ free(tempcmd);
+
+ /* save the cwd so we can restore it later */
+ if(getcwd(cwd, PATH_MAX) == NULL) {
+ pm_printf(PM_LOG_ERROR, _("could not get current working directory\n"));
+ } else {
+ restore_cwd = 1;
+ }
+
+ /* cwd to the download directory */
+ if(chdir(localpath)) {
+ pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
+ ret = -1;
+ goto cleanup;
+ }
+ /* execute the parsed command via /bin/sh -c */
+ pm_printf(PM_LOG_DEBUG, "running command: %s\n", parsedcmd);
+ retval = system(parsedcmd);
+
+ if(retval == -1) {
+ pm_printf(PM_LOG_WARNING, _("running XferCommand: fork failed!\n"));
+ ret = -1;
+ } else if(retval != 0) {
+ /* download failed */
+ pm_printf(PM_LOG_DEBUG, "XferCommand command returned non-zero status "
+ "code (%d)\n", retval);
+ ret = -1;
+ } else {
+ /* download was successful */
+ if(usepart) {
+ rename(tempfile, destfile);
+ }
+ ret = 0;
+ }
+
+cleanup:
+ /* restore the old cwd if we have it */
+ if(restore_cwd && chdir(cwd) != 0) {
+ pm_printf(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"),
+ cwd, strerror(errno));
+ }
+
+ if(ret == -1) {
+ /* hack to let an user the time to cancel a download */
+ sleep(2);
+ }
+ free(destfile);
+ free(tempfile);
+ free(parsedcmd);
+
+ return ret;
+}
+
+
+int config_set_arch(const char *arch)
+{
+ if(strcmp(arch, "auto") == 0) {
+ struct utsname un;
+ uname(&un);
+ pm_printf(PM_LOG_DEBUG, "config: Architecture: %s\n", un.machine);
+ return alpm_option_set_arch(un.machine);
+ } else {
+ pm_printf(PM_LOG_DEBUG, "config: Architecture: %s\n", arch);
+ return alpm_option_set_arch(arch);
+ }
+}
+
+
+/* helper for being used with setrepeatingoption */
+static int option_add_holdpkg(const char *name) {
+ config->holdpkg = alpm_list_add(config->holdpkg, strdup(name));
+ return 0;
+}
+
+/* helper for being used with setrepeatingoption */
+static int option_add_syncfirst(const char *name) {
+ config->syncfirst = alpm_list_add(config->syncfirst, strdup(name));
+ return 0;
+}
+
+/* helper for being used with setrepeatingoption */
+static int option_add_cleanmethod(const char *value) {
+ if(strcmp(value, "KeepInstalled") == 0) {
+ config->cleanmethod |= PM_CLEAN_KEEPINST;
+ } else if(strcmp(value, "KeepCurrent") == 0) {
+ config->cleanmethod |= PM_CLEAN_KEEPCUR;
+ } else {
+ pm_printf(PM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"),
+ value);
+ }
+ return 0;
+}
+
+/** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm
+ * settings. Refactored out of the parseconfig code since all of them did
+ * the exact same thing and duplicated code.
+ * @param ptr a pointer to the start of the multiple options
+ * @param option the string (friendly) name of the option, used for messages
+ * @param optionfunc a function pointer to an alpm_option_add_* function
+ */
+static void setrepeatingoption(char *ptr, const char *option,
+ int (*optionfunc)(const char *))
+{
+ char *q;
+
+ while((q = strchr(ptr, ' '))) {
+ *q = '\0';
+ (*optionfunc)(ptr);
+ pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr);
+ ptr = q;
+ ptr++;
+ }
+ (*optionfunc)(ptr);
+ pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr);
+}
+
+static int _parse_options(const char *key, char *value,
+ const char *file, int linenum)
+{
+ if(value == NULL) {
+ /* options without settings */
+ if(strcmp(key, "UseSyslog") == 0) {
+ alpm_option_set_usesyslog(1);
+ pm_printf(PM_LOG_DEBUG, "config: usesyslog\n");
+ } else if(strcmp(key, "ILoveCandy") == 0) {
+ config->chomp = 1;
+ pm_printf(PM_LOG_DEBUG, "config: chomp\n");
+ } else if(strcmp(key, "VerbosePkgLists") == 0) {
+ config->verbosepkglists = 1;
+ pm_printf(PM_LOG_DEBUG, "config: verbosepkglists\n");
+ } else if(strcmp(key, "UseDelta") == 0) {
+ alpm_option_set_usedelta(1);
+ pm_printf(PM_LOG_DEBUG, "config: usedelta\n");
+ } else if(strcmp(key, "TotalDownload") == 0) {
+ config->totaldownload = 1;
+ pm_printf(PM_LOG_DEBUG, "config: totaldownload\n");
+ } else if(strcmp(key, "CheckSpace") == 0) {
+ alpm_option_set_checkspace(1);
+ } else {
+ pm_printf(PM_LOG_WARNING,
+ _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
+ file, linenum, key, "options");
+ }
+ } else {
+ /* options with settings */
+ if(strcmp(key, "NoUpgrade") == 0) {
+ setrepeatingoption(value, "NoUpgrade", alpm_option_add_noupgrade);
+ } else if(strcmp(key, "NoExtract") == 0) {
+ setrepeatingoption(value, "NoExtract", alpm_option_add_noextract);
+ } else if(strcmp(key, "IgnorePkg") == 0) {
+ setrepeatingoption(value, "IgnorePkg", alpm_option_add_ignorepkg);
+ } else if(strcmp(key, "IgnoreGroup") == 0) {
+ setrepeatingoption(value, "IgnoreGroup", alpm_option_add_ignoregrp);
+ } else if(strcmp(key, "HoldPkg") == 0) {
+ setrepeatingoption(value, "HoldPkg", option_add_holdpkg);
+ } else if(strcmp(key, "SyncFirst") == 0) {
+ setrepeatingoption(value, "SyncFirst", option_add_syncfirst);
+ } else if(strcmp(key, "Architecture") == 0) {
+ if(!alpm_option_get_arch()) {
+ config_set_arch(value);
+ }
+ } else if(strcmp(key, "DBPath") == 0) {
+ /* don't overwrite a path specified on the command line */
+ if(!config->dbpath) {
+ config->dbpath = strdup(value);
+ pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", value);
+ }
+ } else if(strcmp(key, "CacheDir") == 0) {
+ if(alpm_option_add_cachedir(value) != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
+ value, alpm_strerrorlast());
+ return 1;
+ }
+ pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", value);
+ } else if(strcmp(key, "RootDir") == 0) {
+ /* don't overwrite a path specified on the command line */
+ if(!config->rootdir) {
+ config->rootdir = strdup(value);
+ pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", value);
+ }
+ } else if(strcmp(key, "GPGDir") == 0) {
+ if(!config->gpgdir) {
+ config->gpgdir = strdup(value);
+ pm_printf(PM_LOG_DEBUG, "config: gpgdir: %s\n", value);
+ }
+ } else if(strcmp(key, "LogFile") == 0) {
+ if(!config->logfile) {
+ config->logfile = strdup(value);
+ pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", value);
+ }
+ } else if(strcmp(key, "XferCommand") == 0) {
+ config->xfercommand = strdup(value);
+ alpm_option_set_fetchcb(download_with_xfercommand);
+ pm_printf(PM_LOG_DEBUG, "config: xfercommand: %s\n", value);
+ } else if(strcmp(key, "CleanMethod") == 0) {
+ setrepeatingoption(value, "CleanMethod", option_add_cleanmethod);
+ } else if(strcmp(key, "VerifySig") == 0) {
+ if(strcmp(value, "Always") == 0) {
+ alpm_option_set_default_sigverify(PM_PGP_VERIFY_ALWAYS);
+ } else if(strcmp(value, "Optional") == 0) {
+ alpm_option_set_default_sigverify(PM_PGP_VERIFY_OPTIONAL);
+ } else if(strcmp(value, "Never") == 0) {
+ alpm_option_set_default_sigverify(PM_PGP_VERIFY_NEVER);
+ } else {
+ pm_printf(PM_LOG_ERROR, _("invalid value for 'VerifySig' : '%s'\n"), value);
+ return 1;
+ }
+ pm_printf(PM_LOG_DEBUG, "config: setting default VerifySig: %s\n", value);
+ } else {
+
+ pm_printf(PM_LOG_WARNING,
+ _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
+ file, linenum, key, "options");
+ }
+
+ }
+ return 0;
+}
+
+static int _add_mirror(pmdb_t *db, char *value)
+{
+ const char *dbname = alpm_db_get_name(db);
+ /* let's attempt a replacement for the current repo */
+ char *temp = strreplace(value, "$repo", dbname);
+ /* let's attempt a replacement for the arch */
+ const char *arch = alpm_option_get_arch();
+ char *server;
+ if(arch) {
+ server = strreplace(temp, "$arch", arch);
+ free(temp);
+ } else {
+ if(strstr(temp, "$arch")) {
+ free(temp);
+ pm_printf(PM_LOG_ERROR, _("The mirror '%s' contains the $arch"
+ " variable, but no Architecture is defined.\n"), value);
+ return 1;
+ }
+ server = temp;
+ }
+
+ if(alpm_db_setserver(db, server) != 0) {
+ /* pm_errno is set by alpm_db_setserver */
+ pm_printf(PM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"),
+ dbname, server, alpm_strerrorlast());
+ free(server);
+ return 1;
+ }
+
+ free(server);
+ return 0;
+}
+
+/** Sets all libalpm required paths in one go. Called after the command line
+ * and inital 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
+ * of our paths to live under the rootdir that was specified. Safe to call
+ * multiple times (will only do anything the first time).
+ */
+static void setlibpaths(void)
+{
+ static int init = 0;
+ if(!init) {
+ int ret = 0;
+
+ pm_printf(PM_LOG_DEBUG, "setlibpaths() called\n");
+ /* Configure root path first. If it is set and dbpath/logfile were not
+ * set, then set those as well to reside under the root. */
+ if(config->rootdir) {
+ char path[PATH_MAX];
+ ret = alpm_option_set_root(config->rootdir);
+ if(ret != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem setting rootdir '%s' (%s)\n"),
+ config->rootdir, alpm_strerrorlast());
+ cleanup(ret);
+ }
+ if(!config->dbpath) {
+ /* omit leading slash from our static DBPATH, root handles it */
+ snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), DBPATH + 1);
+ config->dbpath = strdup(path);
+ }
+ if(!config->logfile) {
+ /* omit leading slash from our static LOGFILE path, root handles it */
+ snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), LOGFILE + 1);
+ config->logfile = strdup(path);
+ }
+ }
+ /* Set other paths if they were configured. Note that unless rootdir
+ * was left undefined, these two paths (dbpath and logfile) will have
+ * been set locally above, so the if cases below will now trigger. */
+ if(config->dbpath) {
+ ret = alpm_option_set_dbpath(config->dbpath);
+ if(ret != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem setting dbpath '%s' (%s)\n"),
+ config->dbpath, alpm_strerrorlast());
+ cleanup(ret);
+ }
+ }
+ if(config->logfile) {
+ ret = alpm_option_set_logfile(config->logfile);
+ if(ret != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
+ config->logfile, alpm_strerrorlast());
+ cleanup(ret);
+ }
+ }
+
+ /* Set GnuPG's home directory. This is not relative to rootdir, even if
+ * rootdir is defined. Reasoning: gpgdir contains configuration data. */
+ if(config->gpgdir) {
+ ret = alpm_option_set_signaturedir(config->gpgdir);
+ if(ret != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"),
+ config->gpgdir, alpm_strerrorlast());
+ cleanup(ret);
+ }
+ }
+
+ /* add a default cachedir if one wasn't specified */
+ if(alpm_option_get_cachedirs() == NULL) {
+ alpm_option_add_cachedir(CACHEDIR);
+ }
+ init = 1;
+ }
+}
+
+
+/* The real parseconfig. Called with a null section argument by the publicly
+ * visible parseconfig so we can recall from within ourself on an include */
+static int _parseconfig(const char *file, const char *givensection,
+ pmdb_t * const givendb)
+{
+ FILE *fp = NULL;
+ char line[PATH_MAX+1];
+ 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");
+ if(fp == NULL) {
+ pm_printf(PM_LOG_ERROR, _("config file %s could not be read.\n"), file);
+ return 1;
+ }
+
+ /* if we are passed a section, use it as our starting point */
+ if(givensection != NULL) {
+ section = strdup(givensection);
+ }
+ /* if we are passed a db, use it as our starting point */
+ if(givendb != NULL) {
+ db = givendb;
+ }
+
+ while(fgets(line, PATH_MAX, fp)) {
+ linenum++;
+ strtrim(line);
+
+ /* ignore whole line and end of line comments */
+ if(strlen(line) == 0 || line[0] == '#') {
+ continue;
+ }
+ if((ptr = strchr(line, '#'))) {
+ *ptr = '\0';
+ }
+
+ if(line[0] == '[' && line[strlen(line)-1] == ']') {
+ /* new config section, skip the '[' */
+ ptr = line;
+ ptr++;
+ if(section) {
+ free(section);
+ }
+ section = strdup(ptr);
+ section[strlen(section)-1] = '\0';
+ pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", section);
+ if(!strlen(section)) {
+ pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
+ file, linenum);
+ ret = 1;
+ goto cleanup;
+ }
+ /* if we are not looking at the options section, register a db */
+ if(strcmp(section, "options") != 0) {
+ 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;
+ }
+ }
+ continue;
+ }
+
+ /* directive */
+ char *key, *value;
+ /* strsep modifies the 'line' string: 'key \0 value' */
+ key = line;
+ value = line;
+ strsep(&value, "=");
+ strtrim(key);
+ strtrim(value);
+
+ if(key == NULL) {
+ pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
+ file, linenum);
+ 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);
+ ret = 1;
+ goto cleanup;
+ }
+ /* Include is allowed in both options and repo sections */
+ if(strcmp(key, "Include") == 0) {
+ if(value == NULL) {
+ pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
+ file, linenum, key);
+ ret = 1;
+ goto cleanup;
+ }
+ /* Ignore include failures... assume non-critical */
+ int globret;
+ glob_t globbuf;
+ globret = glob(value, GLOB_NOCHECK, NULL, &globbuf);
+ switch(globret) {
+ case GLOB_NOSPACE:
+ pm_printf(PM_LOG_DEBUG,
+ "config file %s, line %d: include globbing out of space\n",
+ file, linenum);
+ break;
+ case GLOB_ABORTED:
+ pm_printf(PM_LOG_DEBUG,
+ "config file %s, line %d: include globbing read error for %s\n",
+ file, linenum, value);
+ break;
+ case GLOB_NOMATCH:
+ pm_printf(PM_LOG_DEBUG,
+ "config file %s, line %d: no include found for %s\n",
+ file, linenum, value);
+ break;
+ default:
+ for(size_t gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
+ pm_printf(PM_LOG_DEBUG, "config file %s, line %d: including %s\n",
+ file, linenum, globbuf.gl_pathv[gindex]);
+ _parseconfig(globbuf.gl_pathv[gindex], section, db);
+ }
+ break;
+ }
+ globfree(&globbuf);
+ continue;
+ }
+ if(strcmp(section, "options") == 0) {
+ /* we are either in options ... */
+ if((ret = _parse_options(key, value, file, linenum)) != 0) {
+ goto cleanup;
+ }
+ } else {
+ /* ... or in a repo section */
+ if(strcmp(key, "Server") == 0) {
+ if(value == NULL) {
+ pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
+ file, linenum, key);
+ ret = 1;
+ goto cleanup;
+ }
+ if(_add_mirror(db, value) != 0) {
+ ret = 1;
+ goto cleanup;
+ }
+ } else if(strcmp(key, "VerifySig") == 0) {
+ if(strcmp(value, "Always") == 0) {
+ ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_ALWAYS);
+ } else if(strcmp(value, "Optional") == 0) {
+ ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_OPTIONAL);
+ } else if(strcmp(value, "Never") == 0) {
+ ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_NEVER);
+ } else {
+ pm_printf(PM_LOG_ERROR, _("invalid value for 'VerifySig' : '%s'\n"), value);
+ ret = 1;
+ goto cleanup;
+ }
+ if(ret != 0) {
+ pm_printf(PM_LOG_ERROR, _("could not add pgp verify option to database '%s': %s (%s)\n"),
+ alpm_db_get_name(db), value, alpm_strerrorlast());
+ goto cleanup;
+ }
+ pm_printf(PM_LOG_DEBUG, "config: VerifySig for %s: %s\n",alpm_db_get_name(db), value);
+ } else {
+ pm_printf(PM_LOG_WARNING,
+ _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
+ file, linenum, key, section);
+ }
+ }
+
+ }
+
+cleanup:
+ 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 ret;
+}
+
+/** Parse a configuration file.
+ * @param file path to the config file.
+ * @return 0 on success, non-zero on error
+ */
+int parseconfig(const char *file)
+{
+ /* call the real parseconfig function with a null section & db argument */
+ return _parseconfig(file, NULL, NULL);
+}
+
/* vim: set ts=2 sw=2 noet: */
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index 4f96f4f9..76c76cf5 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -126,6 +126,8 @@ extern config_t *config;
config_t *config_new(void);
int config_free(config_t *oldconfig);
+int config_set_arch(const char *arch);
+int parseconfig(const char *file);
#endif /* _PM_CONF_H */
/* vim: set ts=2 sw=2 noet: */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index a4cfe75c..56f2c4e9 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -36,11 +36,9 @@
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
-#include <sys/stat.h>
#include <sys/utsname.h> /* uname */
#include <locale.h> /* setlocale */
#include <errno.h>
-#include <glob.h>
#if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
#include <mcheck.h> /* debug tracing (mtrace) */
#endif
@@ -260,24 +258,11 @@ static void setuseragent(void)
setenv("HTTP_USER_AGENT", agent, 0);
}
-static void setarch(const char *arch)
-{
- if(strcmp(arch, "auto") == 0) {
- struct utsname un;
- uname(&un);
- pm_printf(PM_LOG_DEBUG, "config: Architecture: %s\n", un.machine);
- alpm_option_set_arch(un.machine);
- } else {
- pm_printf(PM_LOG_DEBUG, "config: Architecture: %s\n", arch);
- alpm_option_set_arch(arch);
- }
-}
-
/** Free the resources.
*
* @param ret the return value
*/
-static void cleanup(int ret) {
+void cleanup(int ret) {
/* free alpm library resources */
if(alpm_release() == -1) {
pm_printf(PM_LOG_ERROR, "%s\n", alpm_strerrorlast());
@@ -335,79 +320,6 @@ static void handler(int signum)
cleanup(signum);
}
-/** Sets all libalpm required paths in one go. Called after the command line
- * and inital 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
- * of our paths to live under the rootdir that was specified. Safe to call
- * multiple times (will only do anything the first time).
- */
-static void setlibpaths(void)
-{
- static int init = 0;
- if(!init) {
- int ret = 0;
-
- pm_printf(PM_LOG_DEBUG, "setlibpaths() called\n");
- /* Configure root path first. If it is set and dbpath/logfile were not
- * set, then set those as well to reside under the root. */
- if(config->rootdir) {
- char path[PATH_MAX];
- ret = alpm_option_set_root(config->rootdir);
- if(ret != 0) {
- pm_printf(PM_LOG_ERROR, _("problem setting rootdir '%s' (%s)\n"),
- config->rootdir, alpm_strerrorlast());
- cleanup(ret);
- }
- if(!config->dbpath) {
- /* omit leading slash from our static DBPATH, root handles it */
- snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), DBPATH + 1);
- config->dbpath = strdup(path);
- }
- if(!config->logfile) {
- /* omit leading slash from our static LOGFILE path, root handles it */
- snprintf(path, PATH_MAX, "%s%s", alpm_option_get_root(), LOGFILE + 1);
- config->logfile = strdup(path);
- }
- }
- /* Set other paths if they were configured. Note that unless rootdir
- * was left undefined, these two paths (dbpath and logfile) will have
- * been set locally above, so the if cases below will now trigger. */
- if(config->dbpath) {
- ret = alpm_option_set_dbpath(config->dbpath);
- if(ret != 0) {
- pm_printf(PM_LOG_ERROR, _("problem setting dbpath '%s' (%s)\n"),
- config->dbpath, alpm_strerrorlast());
- cleanup(ret);
- }
- }
- if(config->logfile) {
- ret = alpm_option_set_logfile(config->logfile);
- if(ret != 0) {
- pm_printf(PM_LOG_ERROR, _("problem setting logfile '%s' (%s)\n"),
- config->logfile, alpm_strerrorlast());
- cleanup(ret);
- }
- }
-
- /* Set GnuPG's home directory. This is not relative to rootdir, even if
- * rootdir is defined. Reasoning: gpgdir contains configuration data. */
- if(config->gpgdir) {
- ret = alpm_option_set_signaturedir(config->gpgdir);
- if(ret != 0) {
- pm_printf(PM_LOG_ERROR, _("problem setting gpgdir '%s' (%s)\n"),
- config->gpgdir, alpm_strerrorlast());
- cleanup(ret);
- }
- }
-
- /* add a default cachedir if one wasn't specified */
- if(alpm_option_get_cachedirs() == NULL) {
- alpm_option_add_cachedir(CACHEDIR);
- }
- init = 1;
- }
-}
-
#define check_optarg() if(!optarg) { return 1; }
typedef int (*fn_add) (const char *s);
@@ -471,7 +383,10 @@ static int parsearg_op(int opt, int dryrun)
static int parsearg_global(int opt)
{
switch(opt) {
- case OP_ARCH: check_optarg(); setarch(optarg); break;
+ case OP_ARCH:
+ check_optarg();
+ config_set_arch(optarg);
+ break;
case OP_ASK:
check_optarg();
config->noask = 1;
@@ -807,506 +722,6 @@ static int parseargs(int argc, char *argv[])
return 0;
}
-/* helper for being used with setrepeatingoption */
-static int option_add_holdpkg(const char *name) {
- config->holdpkg = alpm_list_add(config->holdpkg, strdup(name));
- return 0;
-}
-
-/* helper for being used with setrepeatingoption */
-static int option_add_syncfirst(const char *name) {
- config->syncfirst = alpm_list_add(config->syncfirst, strdup(name));
- return 0;
-}
-
-/* helper for being used with setrepeatingoption */
-static int option_add_cleanmethod(const char *value) {
- if(strcmp(value, "KeepInstalled") == 0) {
- config->cleanmethod |= PM_CLEAN_KEEPINST;
- } else if(strcmp(value, "KeepCurrent") == 0) {
- config->cleanmethod |= PM_CLEAN_KEEPCUR;
- } else {
- pm_printf(PM_LOG_ERROR, _("invalid value for 'CleanMethod' : '%s'\n"),
- value);
- }
- return 0;
-}
-
-/** Add repeating options such as NoExtract, NoUpgrade, etc to libalpm
- * settings. Refactored out of the parseconfig code since all of them did
- * the exact same thing and duplicated code.
- * @param ptr a pointer to the start of the multiple options
- * @param option the string (friendly) name of the option, used for messages
- * @param optionfunc a function pointer to an alpm_option_add_* function
- */
-static void setrepeatingoption(char *ptr, const char *option,
- int (*optionfunc)(const char *))
-{
- char *q;
-
- while((q = strchr(ptr, ' '))) {
- *q = '\0';
- (*optionfunc)(ptr);
- pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr);
- ptr = q;
- ptr++;
- }
- (*optionfunc)(ptr);
- pm_printf(PM_LOG_DEBUG, "config: %s: %s\n", option, ptr);
-}
-
-static char *get_filename(const char *url) {
- char *filename = strrchr(url, '/');
- if(filename != NULL) {
- filename++;
- }
- return filename;
-}
-
-static char *get_destfile(const char *path, const char *filename) {
- char *destfile;
- /* len = localpath len + filename len + null */
- size_t len = strlen(path) + strlen(filename) + 1;
- destfile = calloc(len, sizeof(char));
- snprintf(destfile, len, "%s%s", path, filename);
-
- return destfile;
-}
-
-static char *get_tempfile(const char *path, const char *filename) {
- char *tempfile;
- /* len = localpath len + filename len + '.part' len + null */
- size_t len = strlen(path) + strlen(filename) + 6;
- tempfile = calloc(len, sizeof(char));
- snprintf(tempfile, len, "%s%s.part", path, filename);
-
- return tempfile;
-}
-
-/** External fetch callback */
-static int download_with_xfercommand(const char *url, const char *localpath,
- int force) {
- int ret = 0;
- int retval;
- int usepart = 0;
- struct stat st;
- char *parsedcmd,*tempcmd;
- char cwd[PATH_MAX];
- int restore_cwd = 0;
- char *destfile, *tempfile, *filename;
-
- if(!config->xfercommand) {
- return -1;
- }
-
- filename = get_filename(url);
- if(!filename) {
- return -1;
- }
- destfile = get_destfile(localpath, filename);
- tempfile = get_tempfile(localpath, filename);
-
- if(force && stat(tempfile, &st) == 0) {
- unlink(tempfile);
- }
- if(force && stat(destfile, &st) == 0) {
- unlink(destfile);
- }
-
- tempcmd = strdup(config->xfercommand);
- /* replace all occurrences of %o with fn.part */
- if(strstr(tempcmd, "%o")) {
- usepart = 1;
- parsedcmd = strreplace(tempcmd, "%o", tempfile);
- free(tempcmd);
- tempcmd = parsedcmd;
- }
- /* replace all occurrences of %u with the download URL */
- parsedcmd = strreplace(tempcmd, "%u", url);
- free(tempcmd);
-
- /* save the cwd so we can restore it later */
- if(getcwd(cwd, PATH_MAX) == NULL) {
- pm_printf(PM_LOG_ERROR, _("could not get current working directory\n"));
- } else {
- restore_cwd = 1;
- }
-
- /* cwd to the download directory */
- if(chdir(localpath)) {
- pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
- ret = -1;
- goto cleanup;
- }
- /* execute the parsed command via /bin/sh -c */
- pm_printf(PM_LOG_DEBUG, "running command: %s\n", parsedcmd);
- retval = system(parsedcmd);
-
- if(retval == -1) {
- pm_printf(PM_LOG_WARNING, _("running XferCommand: fork failed!\n"));
- ret = -1;
- } else if(retval != 0) {
- /* download failed */
- pm_printf(PM_LOG_DEBUG, "XferCommand command returned non-zero status "
- "code (%d)\n", retval);
- ret = -1;
- } else {
- /* download was successful */
- if(usepart) {
- rename(tempfile, destfile);
- }
- ret = 0;
- }
-
-cleanup:
- /* restore the old cwd if we have it */
- if(restore_cwd && chdir(cwd) != 0) {
- pm_printf(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
- }
-
- if(ret == -1) {
- /* hack to let an user the time to cancel a download */
- sleep(2);
- }
- free(destfile);
- free(tempfile);
- free(parsedcmd);
-
- return ret;
-}
-
-static int _parse_options(const char *key, char *value,
- const char *file, int linenum)
-{
- if(value == NULL) {
- /* options without settings */
- if(strcmp(key, "UseSyslog") == 0) {
- alpm_option_set_usesyslog(1);
- pm_printf(PM_LOG_DEBUG, "config: usesyslog\n");
- } else if(strcmp(key, "ILoveCandy") == 0) {
- config->chomp = 1;
- pm_printf(PM_LOG_DEBUG, "config: chomp\n");
- } else if(strcmp(key, "VerbosePkgLists") == 0) {
- config->verbosepkglists = 1;
- pm_printf(PM_LOG_DEBUG, "config: verbosepkglists\n");
- } else if(strcmp(key, "UseDelta") == 0) {
- alpm_option_set_usedelta(1);
- pm_printf(PM_LOG_DEBUG, "config: usedelta\n");
- } else if(strcmp(key, "TotalDownload") == 0) {
- config->totaldownload = 1;
- pm_printf(PM_LOG_DEBUG, "config: totaldownload\n");
- } else if(strcmp(key, "CheckSpace") == 0) {
- alpm_option_set_checkspace(1);
- } else {
- pm_printf(PM_LOG_WARNING,
- _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
- file, linenum, key, "options");
- }
- } else {
- /* options with settings */
- if(strcmp(key, "NoUpgrade") == 0) {
- setrepeatingoption(value, "NoUpgrade", alpm_option_add_noupgrade);
- } else if(strcmp(key, "NoExtract") == 0) {
- setrepeatingoption(value, "NoExtract", alpm_option_add_noextract);
- } else if(strcmp(key, "IgnorePkg") == 0) {
- setrepeatingoption(value, "IgnorePkg", alpm_option_add_ignorepkg);
- } else if(strcmp(key, "IgnoreGroup") == 0) {
- setrepeatingoption(value, "IgnoreGroup", alpm_option_add_ignoregrp);
- } else if(strcmp(key, "HoldPkg") == 0) {
- setrepeatingoption(value, "HoldPkg", option_add_holdpkg);
- } else if(strcmp(key, "SyncFirst") == 0) {
- setrepeatingoption(value, "SyncFirst", option_add_syncfirst);
- } else if(strcmp(key, "Architecture") == 0) {
- if(!alpm_option_get_arch()) {
- setarch(value);
- }
- } else if(strcmp(key, "DBPath") == 0) {
- /* don't overwrite a path specified on the command line */
- if(!config->dbpath) {
- config->dbpath = strdup(value);
- pm_printf(PM_LOG_DEBUG, "config: dbpath: %s\n", value);
- }
- } else if(strcmp(key, "CacheDir") == 0) {
- if(alpm_option_add_cachedir(value) != 0) {
- pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
- value, alpm_strerrorlast());
- return 1;
- }
- pm_printf(PM_LOG_DEBUG, "config: cachedir: %s\n", value);
- } else if(strcmp(key, "RootDir") == 0) {
- /* don't overwrite a path specified on the command line */
- if(!config->rootdir) {
- config->rootdir = strdup(value);
- pm_printf(PM_LOG_DEBUG, "config: rootdir: %s\n", value);
- }
- } else if(strcmp(key, "GPGDir") == 0) {
- if(!config->gpgdir) {
- config->gpgdir = strdup(value);
- pm_printf(PM_LOG_DEBUG, "config: gpgdir: %s\n", value);
- }
- } else if(strcmp(key, "LogFile") == 0) {
- if(!config->logfile) {
- config->logfile = strdup(value);
- pm_printf(PM_LOG_DEBUG, "config: logfile: %s\n", value);
- }
- } else if(strcmp(key, "XferCommand") == 0) {
- config->xfercommand = strdup(value);
- alpm_option_set_fetchcb(download_with_xfercommand);
- pm_printf(PM_LOG_DEBUG, "config: xfercommand: %s\n", value);
- } else if(strcmp(key, "CleanMethod") == 0) {
- setrepeatingoption(value, "CleanMethod", option_add_cleanmethod);
- } else if(strcmp(key, "VerifySig") == 0) {
- if(strcmp(value, "Always") == 0) {
- alpm_option_set_default_sigverify(PM_PGP_VERIFY_ALWAYS);
- } else if(strcmp(value, "Optional") == 0) {
- alpm_option_set_default_sigverify(PM_PGP_VERIFY_OPTIONAL);
- } else if(strcmp(value, "Never") == 0) {
- alpm_option_set_default_sigverify(PM_PGP_VERIFY_NEVER);
- } else {
- pm_printf(PM_LOG_ERROR, _("invalid value for 'VerifySig' : '%s'\n"), value);
- return 1;
- }
- pm_printf(PM_LOG_DEBUG, "config: setting default VerifySig: %s\n", value);
- } else {
-
- pm_printf(PM_LOG_WARNING,
- _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
- file, linenum, key, "options");
- }
-
- }
- return 0;
-}
-
-static int _add_mirror(pmdb_t *db, char *value)
-{
- const char *dbname = alpm_db_get_name(db);
- /* let's attempt a replacement for the current repo */
- char *temp = strreplace(value, "$repo", dbname);
- /* let's attempt a replacement for the arch */
- const char *arch = alpm_option_get_arch();
- char *server;
- if(arch) {
- server = strreplace(temp, "$arch", arch);
- free(temp);
- } else {
- if(strstr(temp, "$arch")) {
- free(temp);
- pm_printf(PM_LOG_ERROR, _("The mirror '%s' contains the $arch"
- " variable, but no Architecture is defined.\n"), value);
- return 1;
- }
- server = temp;
- }
-
- if(alpm_db_setserver(db, server) != 0) {
- /* pm_errno is set by alpm_db_setserver */
- pm_printf(PM_LOG_ERROR, _("could not add server URL to database '%s': %s (%s)\n"),
- dbname, server, alpm_strerrorlast());
- free(server);
- return 1;
- }
-
- free(server);
- return 0;
-}
-
-/* The real parseconfig. Called with a null section argument by the publicly
- * visible parseconfig so we can recall from within ourself on an include */
-static int _parseconfig(const char *file, const char *givensection,
- pmdb_t * const givendb)
-{
- FILE *fp = NULL;
- char line[PATH_MAX+1];
- 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");
- if(fp == NULL) {
- pm_printf(PM_LOG_ERROR, _("config file %s could not be read.\n"), file);
- return 1;
- }
-
- /* if we are passed a section, use it as our starting point */
- if(givensection != NULL) {
- section = strdup(givensection);
- }
- /* if we are passed a db, use it as our starting point */
- if(givendb != NULL) {
- db = givendb;
- }
-
- while(fgets(line, PATH_MAX, fp)) {
- linenum++;
- strtrim(line);
-
- /* ignore whole line and end of line comments */
- if(strlen(line) == 0 || line[0] == '#') {
- continue;
- }
- if((ptr = strchr(line, '#'))) {
- *ptr = '\0';
- }
-
- if(line[0] == '[' && line[strlen(line)-1] == ']') {
- /* new config section, skip the '[' */
- ptr = line;
- ptr++;
- if(section) {
- free(section);
- }
- section = strdup(ptr);
- section[strlen(section)-1] = '\0';
- pm_printf(PM_LOG_DEBUG, "config: new section '%s'\n", section);
- if(!strlen(section)) {
- pm_printf(PM_LOG_ERROR, _("config file %s, line %d: bad section name.\n"),
- file, linenum);
- ret = 1;
- goto cleanup;
- }
- /* if we are not looking at the options section, register a db */
- if(strcmp(section, "options") != 0) {
- 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;
- }
- }
- continue;
- }
-
- /* directive */
- char *key, *value;
- /* strsep modifies the 'line' string: 'key \0 value' */
- key = line;
- value = line;
- strsep(&value, "=");
- strtrim(key);
- strtrim(value);
-
- if(key == NULL) {
- pm_printf(PM_LOG_ERROR, _("config file %s, line %d: syntax error in config file- missing key.\n"),
- file, linenum);
- 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);
- ret = 1;
- goto cleanup;
- }
- /* Include is allowed in both options and repo sections */
- if(strcmp(key, "Include") == 0) {
- if(value == NULL) {
- pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
- file, linenum, key);
- ret = 1;
- goto cleanup;
- }
- /* Ignore include failures... assume non-critical */
- int globret;
- glob_t globbuf;
- globret = glob(value, GLOB_NOCHECK, NULL, &globbuf);
- switch(globret) {
- case GLOB_NOSPACE:
- pm_printf(PM_LOG_DEBUG,
- "config file %s, line %d: include globbing out of space\n",
- file, linenum);
- break;
- case GLOB_ABORTED:
- pm_printf(PM_LOG_DEBUG,
- "config file %s, line %d: include globbing read error for %s\n",
- file, linenum, value);
- break;
- case GLOB_NOMATCH:
- pm_printf(PM_LOG_DEBUG,
- "config file %s, line %d: no include found for %s\n",
- file, linenum, value);
- break;
- default:
- for(size_t gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
- pm_printf(PM_LOG_DEBUG, "config file %s, line %d: including %s\n",
- file, linenum, globbuf.gl_pathv[gindex]);
- _parseconfig(globbuf.gl_pathv[gindex], section, db);
- }
- break;
- }
- globfree(&globbuf);
- continue;
- }
- if(strcmp(section, "options") == 0) {
- /* we are either in options ... */
- if((ret = _parse_options(key, value, file, linenum)) != 0) {
- goto cleanup;
- }
- } else {
- /* ... or in a repo section */
- if(strcmp(key, "Server") == 0) {
- if(value == NULL) {
- pm_printf(PM_LOG_ERROR, _("config file %s, line %d: directive '%s' needs a value\n"),
- file, linenum, key);
- ret = 1;
- goto cleanup;
- }
- if(_add_mirror(db, value) != 0) {
- ret = 1;
- goto cleanup;
- }
- } else if(strcmp(key, "VerifySig") == 0) {
- if(strcmp(value, "Always") == 0) {
- ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_ALWAYS);
- } else if(strcmp(value, "Optional") == 0) {
- ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_OPTIONAL);
- } else if(strcmp(value, "Never") == 0) {
- ret = alpm_db_set_pgp_verify(db, PM_PGP_VERIFY_NEVER);
- } else {
- pm_printf(PM_LOG_ERROR, _("invalid value for 'VerifySig' : '%s'\n"), value);
- ret = 1;
- goto cleanup;
- }
- if(ret != 0) {
- pm_printf(PM_LOG_ERROR, _("could not add pgp verify option to database '%s': %s (%s)\n"),
- alpm_db_get_name(db), value, alpm_strerrorlast());
- goto cleanup;
- }
- pm_printf(PM_LOG_DEBUG, "config: VerifySig for %s: %s\n",alpm_db_get_name(db), value);
- } else {
- pm_printf(PM_LOG_WARNING,
- _("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
- file, linenum, key, section);
- }
- }
-
- }
-
-cleanup:
- 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 ret;
-}
-
-/** Parse a configuration file.
- * @param file path to the config file.
- * @return 0 on success, non-zero on error
- */
-static int parseconfig(const char *file)
-{
- /* call the real parseconfig function with a null section & db argument */
- return _parseconfig(file, NULL, NULL);
-}
-
/** print commandline to logfile
*/
static void cl_to_log(int argc, char* argv[])
diff --git a/src/pacman/pacman.h b/src/pacman/pacman.h
index f5b972bb..77d83d6e 100644
--- a/src/pacman/pacman.h
+++ b/src/pacman/pacman.h
@@ -22,8 +22,12 @@
#include <alpm_list.h>
+void cleanup(int ret);
+
/* database.c */
int pacman_database(alpm_list_t *targets);
+/* deptest.c */
+int pacman_deptest(alpm_list_t *targets);
/* query.c */
int pacman_query(alpm_list_t *targets);
/* remove.c */
@@ -33,9 +37,6 @@ int pacman_sync(alpm_list_t *targets);
/* upgrade.c */
int pacman_upgrade(alpm_list_t *targets);
-/* deptest.c */
-int pacman_deptest(alpm_list_t *targets);
-
#endif /* _PM_PACMAN_H */
/* vim: set ts=2 sw=2 noet: */