New configuration file via confuse: xbps.conf.

This commit is contained in:
Juan RP
2011-12-15 11:19:20 +01:00
parent 3f45f563be
commit b0ceeaa58e
39 changed files with 336 additions and 544 deletions

View File

@ -42,129 +42,115 @@
static bool debug;
static struct xbps_handle *xhp;
#define _CONFFILE XBPS_SYSCONF_PATH "/" XBPS_CONF_PLIST
#define _REPOFILE XBPS_SYSCONF_PATH "/" XBPS_CONF_REPOS_PLIST
static int
cb_validate_virtual(cfg_t *cfg, cfg_opt_t *opt)
{
unsigned int i;
for (i = 0; i < cfg_size(cfg, "virtual-package"); i++) {
cfg_t *sec = cfg_opt_getnsec(opt, i);
if (cfg_getstr(sec, "targets") == 0) {
cfg_error(cfg, "targets must be set for "
"virtual-package %s", cfg_title(sec));
return -1;
}
}
return 0;
}
int
xbps_init(struct xbps_handle *xh)
{
prop_dictionary_t confd;
prop_string_t conffile = NULL, repofile = NULL;
const char *conf_rootdir = NULL, *conf_cachedir = NULL;
uint16_t fetch_cache_conn = 0, fetch_cache_conn_host = 0;
int rv;
cfg_opt_t vpkg_opts[] = {
CFG_STR_LIST((char *)"targets", NULL, CFGF_NONE),
CFG_END()
};
cfg_opt_t opts[] = {
/* Defaults if not set in configuration file */
CFG_STR((char *)"rootdir", (char *)"/", CFGF_NONE),
CFG_STR((char *)"cachedir", (char *)XBPS_CACHE_PATH, CFGF_NONE),
CFG_INT((char *)"fetch-cache-connections",
XBPS_FETCH_CACHECONN, CFGF_NONE),
CFG_INT((char *)"fetch-cache-connections-per-host",
XBPS_FETCH_CACHECONN_HOST, CFGF_NONE),
CFG_INT((char *)"fetch-timeout-connection",
XBPS_FETCH_TIMEOUT, CFGF_NONE),
CFG_BOOL((char *)"syslog", true, CFGF_NONE),
CFG_STR_LIST((char *)"repositories", NULL, CFGF_MULTI),
CFG_SEC((char *)"virtual-package",
vpkg_opts, CFGF_MULTI|CFGF_TITLE),
CFG_FUNC((char *)"include", &cfg_include),
CFG_END()
};
int rv, cc, cch;
assert(xh != NULL);
xhp = xh;
debug = xhp->debug;
/* If confdir not set, defaults to XBPS_SYSCONF_PATH */
if (prop_object_type(xhp->confdir) != PROP_TYPE_STRING) {
conffile = prop_string_create_cstring(_CONFFILE);
repofile = prop_string_create_cstring(_REPOFILE);
} else {
conffile = prop_string_copy(xhp->confdir);
prop_string_append_cstring(conffile, "/");
prop_string_append_cstring(conffile, XBPS_CONF_PLIST);
if (xhp->conffile == NULL)
xhp->conffile = XBPS_CONF_DEF;
repofile = prop_string_copy(xhp->confdir);
prop_string_append_cstring(repofile, "/");
prop_string_append_cstring(repofile, XBPS_CONF_REPOS_PLIST);
}
/*
* Internalize the XBPS_CONF_REPOS_PLIST array.
*/
xhp->repos_array =
prop_array_internalize_from_file(prop_string_cstring_nocopy(repofile));
/*
* Internalize the XBPS_CONF_PLIST dictionary.
*/
confd = prop_dictionary_internalize_from_file(
prop_string_cstring_nocopy(conffile));
if (confd == NULL) {
if (errno != ENOENT) {
xbps_dbg_printf("%s: cannot internalize conf "
"dictionary: %s\n", strerror(errno));
xbps_end(xh);
return errno;
}
xbps_dbg_printf("%s: conf_dictionary not internalized.\n",
__func__);
} else {
/*
* Get defaults from configuration file.
*/
prop_dictionary_get_cstring_nocopy(confd,
"root-directory", &conf_rootdir);
prop_dictionary_get_cstring_nocopy(confd,
"cache-directory", &conf_cachedir);
prop_dictionary_get_uint16(confd,
"fetch-cache-connections", &fetch_cache_conn);
prop_dictionary_get_uint16(confd,
"fetch-cache-connections-per-host", &fetch_cache_conn_host);
prop_dictionary_get_uint16(confd,
"fetch-timeout-connection", &xhp->fetch_timeout);
}
/* parse configuration file */
xhp->cfg = cfg_init(opts, CFGF_NOCASE);
cfg_set_validate_func(xhp->cfg, "virtual-package", &cb_validate_virtual);
/*
* Client supplied values in xbps_handle will be choosen over the
* same values in configuration file. If not specified, use defaults.
*/
if (prop_object_type(xhp->rootdir) != PROP_TYPE_STRING) {
if (conf_rootdir != NULL)
xhp->rootdir = prop_string_create_cstring(conf_rootdir);
else {
/* If rootdir not set, defaults to '/' */
xhp->rootdir = prop_string_create_cstring("/");
}
}
if (prop_object_type(xhp->cachedir) != PROP_TYPE_STRING) {
if (conf_cachedir != NULL) {
if (conf_cachedir[0] == '/') {
/* full path */
xhp->cachedir =
prop_string_create_cstring(conf_cachedir);
} else {
/* relative to rootdir */
xhp->cachedir = prop_string_copy(xhp->rootdir);
prop_string_append_cstring(
xhp->cachedir, "/");
prop_string_append_cstring(
xhp->cachedir, conf_cachedir);
if ((rv = cfg_parse(xhp->cfg, xhp->conffile)) != CFG_SUCCESS) {
if (rv == CFG_PARSE_ERROR) {
if (errno != ENOENT) {
/*
* Don't error out if config file not found.
* We'll use defaults without any repo or
* virtual packages.
*/
xbps_end(xh);
return rv;
}
} else {
/* If cachedir not set, defaults to XBPS_CACHE_PATH */
xhp->cachedir =
prop_string_create_cstring(XBPS_CACHE_PATH);
errno = 0;
} else if (rv == CFG_PARSE_ERROR) {
/*
* Parser error from configuration file.
*/
xbps_end(xh);
return ENOTSUP;
}
}
if (fetch_cache_conn == 0)
fetch_cache_conn = XBPS_FETCH_CACHECONN;
if (fetch_cache_conn_host == 0)
fetch_cache_conn_host = XBPS_FETCH_CACHECONN_HOST;
xbps_fetch_set_cache_connection(fetch_cache_conn,
fetch_cache_conn_host);
xbps_dbg_printf("rootdir: %s\n",
prop_string_cstring_nocopy(xhp->rootdir));
xbps_dbg_printf("cachedir: %s\n",
prop_string_cstring_nocopy(xhp->cachedir));
xbps_dbg_printf("conffile: %s\n",
prop_string_cstring_nocopy(conffile));
xbps_dbg_printf("repofile: %s\n",
prop_string_cstring_nocopy(repofile));
xbps_dbg_printf("fetch_cache_conn: %zu\n",
fetch_cache_conn);
xbps_dbg_printf("fetch_cache_conn_host: %zu\n",
fetch_cache_conn_host);
xbps_dbg_printf("fetch_timeout: %zu\n",
xhp->fetch_timeout);
prop_dictionary_get_bool(confd, "syslog-enabled", &xhp->syslog_enabled);
xbps_dbg_printf("syslog logging: %d\n", xhp->syslog_enabled);
/*
* Respect client setting in struct xbps_handle for {root,cache}dir;
* otherwise use values from configuration file or defaults if unset.
*/
if (xhp->rootdir == NULL) {
if (xhp->cfg == NULL)
xhp->rootdir = "/";
else
xhp->rootdir = cfg_getstr(xhp->cfg, "rootdir");
}
if (xhp->cachedir == NULL) {
if (xhp->cfg == NULL)
xhp->cachedir = XBPS_CACHE_PATH;
else
xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir");
}
if (xhp->cfg == NULL) {
xhp->syslog_enabled = true;
xhp->fetch_timeout = XBPS_FETCH_TIMEOUT;
cc = XBPS_FETCH_CACHECONN;
cch = XBPS_FETCH_CACHECONN_HOST;
} else {
xhp->syslog_enabled = cfg_getbool(xhp->cfg, "syslog");
xhp->fetch_timeout = cfg_getint(xhp->cfg, "fetch-timeout-connection");
cc = cfg_getint(xhp->cfg, "fetch-cache-connections");
cch = cfg_getint(xhp->cfg, "fetch-cache-connections-per-host");
}
xbps_fetch_set_cache_connection(cc, cch);
xbps_dbg_printf("rootdir=%s\n", xhp->rootdir);
xbps_dbg_printf("cachedir=%s\n", xhp->cachedir);
xbps_dbg_printf("fetch-timeout=%u\n", xhp->fetch_timeout);
xbps_dbg_printf("fetch-cacheconn=%u\n", cc);
xbps_dbg_printf("fetch-cacheconn-host=%u\n", cch);
xbps_dbg_printf("syslog=%u\n", xhp->syslog_enabled);
/*
* Initialize regpkgdb dictionary.
*/
@ -173,18 +159,9 @@ xbps_init(struct xbps_handle *xh)
xbps_dbg_printf("%s: couldn't initialize "
"regpkgdb: %s\n", strerror(rv));
xbps_end(xh);
return rv;
return EINVAL;
}
}
if (prop_object_type(confd) == PROP_TYPE_DICTIONARY)
prop_object_release(confd);
if (prop_object_type(conffile) == PROP_TYPE_STRING)
prop_object_release(conffile);
if (prop_object_type(repofile) == PROP_TYPE_STRING)
prop_object_release(repofile);
/* Initialize virtual package settings */
xbps_init_virtual_pkgs(xhp);
return 0;
}
@ -198,14 +175,8 @@ xbps_end(struct xbps_handle *xh)
if (xh == NULL)
return;
if (prop_object_type(xh->confdir) == PROP_TYPE_STRING)
prop_object_release(xh->confdir);
if (prop_object_type(xh->rootdir) == PROP_TYPE_STRING)
prop_object_release(xh->rootdir);
if (prop_object_type(xh->cachedir) == PROP_TYPE_STRING)
prop_object_release(xh->cachedir);
if (prop_object_type(xh->virtualpkgs_array) == PROP_TYPE_ARRAY)
prop_object_release(xh->virtualpkgs_array);
if (xh->cfg != NULL)
cfg_free(xh->cfg);
free(xh);
xh = NULL;