2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* alpm.c
|
|
|
|
*
|
2006-02-05 04:27:26 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2006-10-15 15:31:03 -04:00
|
|
|
* Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
|
|
|
|
* Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
|
|
|
|
* Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
|
2005-03-14 20:51:43 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#include <stdarg.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "alpm.h"
|
|
|
|
#include "alpm_list.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "error.h"
|
|
|
|
#include "handle.h"
|
2007-06-03 23:57:38 -04:00
|
|
|
#include "util.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
|
|
|
|
|
2005-03-16 17:06:31 -05:00
|
|
|
/* Globals */
|
2005-03-14 20:51:43 -05:00
|
|
|
pmhandle_t *handle = NULL;
|
2007-01-30 02:47:19 -05:00
|
|
|
enum _pmerrno_t pm_errno SYMEXPORT;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
/** \addtogroup alpm_interface Interface Functions
|
2007-01-11 12:32:49 -05:00
|
|
|
* @brief Functions to initialize and release libalpm
|
2005-10-09 03:42:06 -04:00
|
|
|
* @{
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
2005-10-09 03:42:06 -04:00
|
|
|
/** Initializes the library. This must be called before any other
|
|
|
|
* functions are called.
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2007-01-31 01:10:21 -05:00
|
|
|
int SYMEXPORT alpm_initialize()
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2005-03-16 17:10:05 -05:00
|
|
|
ASSERT(handle == NULL, RET_ERR(PM_ERR_HANDLE_NOT_NULL, -1));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
handle = _alpm_handle_new();
|
2005-04-24 14:58:34 -04:00
|
|
|
if(handle == NULL) {
|
2005-04-24 16:11:10 -04:00
|
|
|
RET_ERR(PM_ERR_MEMORY, -1);
|
2005-04-24 14:58:34 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2005-10-09 03:42:06 -04:00
|
|
|
/** Release the library. This should be the last alpm call you make.
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
int SYMEXPORT alpm_release()
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-11-08 01:52:50 -05:00
|
|
|
int dbs_left = 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
2005-03-16 17:10:05 -05:00
|
|
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
/* close local database */
|
|
|
|
if(handle->db_local) {
|
2006-03-02 14:06:52 -05:00
|
|
|
alpm_db_unregister(handle->db_local);
|
2005-03-14 20:51:43 -05:00
|
|
|
handle->db_local = NULL;
|
|
|
|
}
|
|
|
|
/* and also sync ones */
|
2006-11-08 01:52:50 -05:00
|
|
|
while((dbs_left = alpm_list_count(handle->dbs_sync)) > 0) {
|
|
|
|
pmdb_t *db = (pmdb_t *)handle->dbs_sync->data;
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("removing DB %s, %d remaining..."), db->treename, dbs_left);
|
|
|
|
alpm_db_unregister(db);
|
|
|
|
db = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2007-04-26 20:29:12 -04:00
|
|
|
_alpm_handle_free(handle);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2007-01-11 12:32:49 -05:00
|
|
|
/** @} */
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** @defgroup alpm_misc Miscellaneous Functions
|
2006-02-01 12:58:02 -05:00
|
|
|
* @brief Various libalpm functions
|
2006-01-26 14:52:47 -05:00
|
|
|
*/
|
2006-10-15 15:31:03 -04:00
|
|
|
|
|
|
|
/** Parses a configuration file.
|
|
|
|
* @param file path to the config file.
|
|
|
|
* @param callback a function to be called upon new database creation
|
|
|
|
* @param this_section the config current section being parsed
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
2007-06-03 23:57:38 -04:00
|
|
|
* @addtogroup alpm_misc
|
2006-10-15 15:31:03 -04:00
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
int SYMEXPORT alpm_parse_config(char *file, alpm_cb_db_register callback, const char *this_section)
|
2006-10-15 15:31:03 -04:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char line[PATH_MAX+1];
|
|
|
|
char *ptr = NULL;
|
|
|
|
char *key = NULL;
|
|
|
|
int linenum = 0;
|
2007-04-04 01:30:14 -04:00
|
|
|
char origkey[256];
|
2006-10-15 15:31:03 -04:00
|
|
|
char section[256] = "";
|
2006-11-20 04:10:23 -05:00
|
|
|
pmdb_t *db = NULL;
|
2006-10-15 15:31:03 -04:00
|
|
|
|
2007-01-30 03:14:10 -05:00
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
fp = fopen(file, "r");
|
|
|
|
if(fp == NULL) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this_section != NULL && strlen(this_section) > 0) {
|
|
|
|
strncpy(section, this_section, min(255, strlen(this_section)));
|
2006-10-27 17:15:26 -04:00
|
|
|
if(!strcmp(section, "local")) {
|
|
|
|
RET_ERR(PM_ERR_CONF_LOCAL, -1);
|
|
|
|
}
|
|
|
|
if(strcmp(section, "options")) {
|
|
|
|
db = _alpm_db_register(section, callback);
|
|
|
|
}
|
2006-10-15 15:31:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while(fgets(line, PATH_MAX, fp)) {
|
|
|
|
linenum++;
|
|
|
|
_alpm_strtrim(line);
|
|
|
|
if(strlen(line) == 0 || line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-31 15:21:33 -04:00
|
|
|
if((ptr = strchr(line, '#'))) {
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
2006-10-15 15:31:03 -04:00
|
|
|
if(line[0] == '[' && line[strlen(line)-1] == ']') {
|
|
|
|
/* new config section */
|
|
|
|
ptr = line;
|
|
|
|
ptr++;
|
|
|
|
strncpy(section, ptr, min(255, strlen(ptr)-1));
|
|
|
|
section[min(255, strlen(ptr)-1)] = '\0';
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: new section '%s'"), section);
|
2006-10-15 15:31:03 -04:00
|
|
|
if(!strlen(section)) {
|
|
|
|
RET_ERR(PM_ERR_CONF_BAD_SECTION, -1);
|
|
|
|
}
|
|
|
|
if(!strcmp(section, "local")) {
|
|
|
|
RET_ERR(PM_ERR_CONF_LOCAL, -1);
|
|
|
|
}
|
|
|
|
if(strcmp(section, "options")) {
|
2006-10-20 02:26:55 -04:00
|
|
|
db = _alpm_db_register(section, callback);
|
2006-10-15 15:31:03 -04:00
|
|
|
if(db == NULL) {
|
|
|
|
/* pm_errno is set by alpm_db_register */
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* directive */
|
|
|
|
ptr = line;
|
|
|
|
key = strsep(&ptr, "=");
|
|
|
|
if(key == NULL) {
|
|
|
|
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
|
|
|
}
|
|
|
|
_alpm_strtrim(key);
|
2007-04-04 01:30:14 -04:00
|
|
|
strncpy(origkey, key, min(255, strlen(key)));
|
2006-10-15 15:31:03 -04:00
|
|
|
key = _alpm_strtoupper(key);
|
|
|
|
if(!strlen(section) && strcmp(key, "INCLUDE")) {
|
|
|
|
RET_ERR(PM_ERR_CONF_DIRECTIVE_OUTSIDE_SECTION, -1);
|
|
|
|
}
|
|
|
|
if(ptr == NULL) {
|
2007-04-04 01:30:14 -04:00
|
|
|
if(strcmp(origkey, "NoPassiveFTP") == 0 || strcmp(key, "NOPASSIVEFTP") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_nopassiveftp(1);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: nopassiveftp"));
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "UseSyslog") == 0 || strcmp(key, "USESYSLOG") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_usesyslog(1);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: usesyslog"));
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "ILoveCandy") == 0 || strcmp(key, "ILOVECANDY") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_chomp(1);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: chomp"));
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "UseColor") == 0 || strcmp(key, "USECOLOR") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_usecolor(1);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: usecolor"));
|
2007-06-01 11:00:39 -04:00
|
|
|
} else if(strcmp(origkey, "ShowSize") == 0 || strcmp(key, "SHOWSIZE") == 0) {
|
|
|
|
alpm_option_set_showsize(1);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: showsize"));
|
2006-10-15 15:31:03 -04:00
|
|
|
} else {
|
|
|
|
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_alpm_strtrim(ptr);
|
2007-04-04 01:30:14 -04:00
|
|
|
if(strcmp(origkey, "Include") == 0 || strcmp(key, "INCLUDE") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
char conf[PATH_MAX];
|
|
|
|
strncpy(conf, ptr, PATH_MAX);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: including %s"), conf);
|
2006-10-15 15:31:03 -04:00
|
|
|
alpm_parse_config(conf, callback, section);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(section, "options") == 0) {
|
|
|
|
if(strcmp(origkey, "NoUpgrade") == 0 || strcmp(key, "NOUPGRADE") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
char *p = ptr;
|
|
|
|
char *q;
|
2006-11-20 04:10:23 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
while((q = strchr(p, ' '))) {
|
|
|
|
*q = '\0';
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_noupgrade(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: noupgrade: %s"), p);
|
2006-10-15 15:31:03 -04:00
|
|
|
p = q;
|
|
|
|
p++;
|
|
|
|
}
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_noupgrade(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: noupgrade: %s"), p);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "NoExtract") == 0 || strcmp(key, "NOEXTRACT") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
char *p = ptr;
|
|
|
|
char *q;
|
2006-11-20 04:10:23 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
while((q = strchr(p, ' '))) {
|
|
|
|
*q = '\0';
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_noextract(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: noextract: %s"), p);
|
2006-10-15 15:31:03 -04:00
|
|
|
p = q;
|
|
|
|
p++;
|
|
|
|
}
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_noextract(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: noextract: %s"), p);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "IgnorePkg") == 0 || strcmp(key, "IGNOREPKG") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
char *p = ptr;
|
|
|
|
char *q;
|
2006-11-20 04:10:23 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
while((q = strchr(p, ' '))) {
|
|
|
|
*q = '\0';
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_ignorepkg(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: ignorepkg: %s"), p);
|
2006-10-15 15:31:03 -04:00
|
|
|
p = q;
|
|
|
|
p++;
|
|
|
|
}
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_ignorepkg(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: ignorepkg: %s"), p);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "HoldPkg") == 0 || strcmp(key, "HOLDPKG") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
char *p = ptr;
|
|
|
|
char *q;
|
2006-11-20 04:10:23 -05:00
|
|
|
|
2006-10-15 15:31:03 -04:00
|
|
|
while((q = strchr(p, ' '))) {
|
|
|
|
*q = '\0';
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_holdpkg(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: holdpkg: %s"), p);
|
2006-10-15 15:31:03 -04:00
|
|
|
p = q;
|
|
|
|
p++;
|
|
|
|
}
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_add_holdpkg(p);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: holdpkg: %s"), p);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "DBPath") == 0 || strcmp(key, "DBPATH") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_dbpath(ptr);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: dbpath: %s"), ptr);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "CacheDir") == 0 || strcmp(key, "CACHEDIR") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_cachedir(ptr);
|
2006-11-01 01:30:47 -05:00
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: cachedir: %s"), ptr);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if(strcmp(origkey, "RootDir") == 0 || strcmp(key, "ROOTDIR") == 0) {
|
|
|
|
alpm_option_set_root(ptr);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: rootdir: %s"), ptr);
|
|
|
|
} else if (strcmp(origkey, "LogFile") == 0 || strcmp(key, "LOGFILE") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_logfile(ptr);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: logfile: %s"), ptr);
|
2007-06-04 10:19:28 -04:00
|
|
|
} else if (strcmp(origkey, "LockFile") == 0 || strcmp(key, "LOCKFILE") == 0) {
|
|
|
|
alpm_option_set_lockfile(ptr);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: lockfile: %s"), ptr);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if (strcmp(origkey, "XferCommand") == 0 || strcmp(key, "XFERCOMMAND") == 0) {
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_xfercommand(ptr);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: xfercommand: %s"), ptr);
|
2007-04-04 01:30:14 -04:00
|
|
|
} else if (strcmp(origkey, "UpgradeDelay") == 0 || strcmp(key, "UPGRADEDELAY") == 0) {
|
2006-10-15 15:31:03 -04:00
|
|
|
/* The config value is in days, we use seconds */
|
2007-01-25 21:13:16 -05:00
|
|
|
time_t ud = atol(ptr) * 60 * 60 *24;
|
2006-11-20 04:10:23 -05:00
|
|
|
alpm_option_set_upgradedelay(ud);
|
|
|
|
_alpm_log(PM_LOG_DEBUG, _("config: upgradedelay: %d"), ud);
|
2006-10-15 15:31:03 -04:00
|
|
|
} else {
|
|
|
|
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
|
|
|
}
|
|
|
|
} else {
|
2007-04-04 01:30:14 -04:00
|
|
|
if(strcmp(origkey, "Server") == 0 || strcmp(key, "SERVER") == 0) {
|
2007-05-18 02:24:59 -04:00
|
|
|
/* let's attempt a replacement for the current repo */
|
|
|
|
char *server = _alpm_strreplace(ptr, "$repo", section);
|
|
|
|
|
|
|
|
if(alpm_db_setserver(db, server) != 0) {
|
2007-01-11 12:32:49 -05:00
|
|
|
/* pm_errno is set by alpm_db_setserver */
|
2006-10-15 15:31:03 -04:00
|
|
|
return(-1);
|
|
|
|
}
|
2007-05-18 02:24:59 -04:00
|
|
|
|
|
|
|
free(server);
|
2006-10-15 15:31:03 -04:00
|
|
|
} else {
|
|
|
|
RET_ERR(PM_ERR_CONF_BAD_SYNTAX, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return(0);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2006-10-15 15:31:03 -04:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|