2009-04-18 08:14:26 -04:00
|
|
|
/*
|
2010-01-05 13:34:00 -05:00
|
|
|
* Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
|
2009-04-18 08:14:26 -04: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-02-02 15:00:47 -05:00
|
|
|
# include <config.h>
|
2009-04-18 08:14:26 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-04-02 12:28:43 -04:00
|
|
|
#include <stdint.h>
|
2009-04-18 08:14:26 -04:00
|
|
|
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <confuse.h>
|
|
|
|
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.h"
|
2010-04-02 12:28:43 -04:00
|
|
|
#include "misc.h"
|
2009-04-18 08:14:26 -04:00
|
|
|
#include "conffile.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static int cb_loglevel(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result);
|
|
|
|
|
|
|
|
/* general section structure */
|
|
|
|
static cfg_opt_t sec_general[] =
|
|
|
|
{
|
|
|
|
CFG_STR("uid", "nobody", CFGF_NONE),
|
|
|
|
CFG_STR("admin_password", NULL, CFGF_NONE),
|
2009-06-12 05:09:58 -04:00
|
|
|
CFG_STR("logfile", STATEDIR "/log/" PACKAGE ".log", CFGF_NONE),
|
2010-03-21 06:33:05 -04:00
|
|
|
CFG_STR("db_path", STATEDIR "/cache/" PACKAGE "/songs3.db", CFGF_NONE),
|
2009-04-18 08:14:26 -04:00
|
|
|
CFG_INT_CB("loglevel", E_LOG, CFGF_NONE, &cb_loglevel),
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
/* library section structure */
|
|
|
|
static cfg_opt_t sec_library[] =
|
|
|
|
{
|
2010-03-19 14:06:47 -04:00
|
|
|
CFG_STR("name", "My Music on %h", CFGF_NONE),
|
2009-04-18 08:14:26 -04:00
|
|
|
CFG_INT("port", 3689, CFGF_NONE),
|
|
|
|
CFG_STR("password", NULL, CFGF_NONE),
|
|
|
|
CFG_STR_LIST("directories", NULL, CFGF_NONE),
|
|
|
|
CFG_STR_LIST("compilations", NULL, CFGF_NONE),
|
2009-11-22 10:45:13 -05:00
|
|
|
CFG_BOOL("itunes_overrides", cfg_false, CFGF_NONE),
|
2009-04-18 08:14:26 -04:00
|
|
|
CFG_STR_LIST("no_transcode", NULL, CFGF_NONE),
|
|
|
|
CFG_STR_LIST("force_transcode", NULL, CFGF_NONE),
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Config file structure */
|
|
|
|
static cfg_opt_t toplvl_cfg[] =
|
|
|
|
{
|
|
|
|
CFG_SEC("general", sec_general, CFGF_NONE),
|
2010-03-19 14:06:47 -04:00
|
|
|
CFG_SEC("library", sec_library, CFGF_NONE),
|
2009-04-18 08:14:26 -04:00
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
2009-04-18 11:18:37 -04:00
|
|
|
cfg_t *cfg;
|
2010-04-02 12:28:43 -04:00
|
|
|
uint64_t libhash;
|
2009-04-18 08:14:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_loglevel(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
|
|
|
|
{
|
|
|
|
if (strcasecmp(value, "fatal") == 0)
|
|
|
|
*(long int *)result = E_FATAL;
|
|
|
|
else if (strcasecmp(value, "log") == 0)
|
|
|
|
*(long int *)result = E_LOG;
|
|
|
|
else if (strcasecmp(value, "warning") == 0)
|
|
|
|
*(long int *)result = E_WARN;
|
|
|
|
else if (strcasecmp(value, "info") == 0)
|
2009-05-08 11:52:56 -04:00
|
|
|
*(long int *)result = E_INFO;
|
2009-04-18 08:14:26 -04:00
|
|
|
else if (strcasecmp(value, "debug") == 0)
|
|
|
|
*(long int *)result = E_DBG;
|
|
|
|
else if (strcasecmp(value, "spam") == 0)
|
|
|
|
*(long int *)result = E_SPAM;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_CONF, "Unrecognised loglevel '%s'\n", value);
|
|
|
|
/* Default to warning */
|
|
|
|
*(long int *)result = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
conffile_expand_libname(cfg_t *lib)
|
|
|
|
{
|
|
|
|
char *libname;
|
|
|
|
char *hostname;
|
|
|
|
char *s;
|
|
|
|
char *d;
|
|
|
|
char *expanded;
|
|
|
|
struct utsname sysinfo;
|
|
|
|
size_t len;
|
|
|
|
size_t olen;
|
|
|
|
size_t hostlen;
|
|
|
|
size_t verlen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
libname = cfg_getstr(lib, "name");
|
2010-04-02 12:28:43 -04:00
|
|
|
olen = strlen(libname);
|
2009-04-18 08:14:26 -04:00
|
|
|
|
|
|
|
/* Fast path */
|
|
|
|
s = strchr(libname, '%');
|
|
|
|
if (!s)
|
2010-04-02 12:28:43 -04:00
|
|
|
{
|
|
|
|
libhash = murmur_hash64(libname, olen, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-18 08:14:26 -04:00
|
|
|
|
|
|
|
/* Grab what we need */
|
|
|
|
ret = uname(&sysinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_CONF, "Could not get system name: %s\n", strerror(errno));
|
|
|
|
hostname = "Unknown host";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hostname = sysinfo.nodename;
|
|
|
|
|
|
|
|
hostlen = strlen(hostname);
|
|
|
|
verlen = strlen(VERSION);
|
|
|
|
|
|
|
|
/* Compute expanded size */
|
2010-04-02 12:28:43 -04:00
|
|
|
len = olen;
|
2009-04-18 08:14:26 -04:00
|
|
|
s = libname;
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '%')
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
|
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
len += hostlen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
len += verlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
expanded = (char *)malloc(len + 1);
|
|
|
|
if (!expanded)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_CONF, "Out of memory\n");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(expanded, 0, len + 1);
|
|
|
|
|
|
|
|
/* Do the actual expansion */
|
|
|
|
s = libname;
|
|
|
|
d = expanded;
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '%')
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
|
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
strcat(d, hostname);
|
|
|
|
d += hostlen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
strcat(d, VERSION);
|
|
|
|
d += verlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*d = *s;
|
|
|
|
|
|
|
|
s++;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_setstr(lib, "name", expanded);
|
|
|
|
|
2010-04-02 12:28:43 -04:00
|
|
|
libhash = murmur_hash64(expanded, strlen(expanded), 0);
|
|
|
|
|
2009-04-18 08:14:26 -04:00
|
|
|
free(expanded);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
conffile_load(char *file)
|
|
|
|
{
|
|
|
|
cfg_t *lib;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
cfg = cfg_init(toplvl_cfg, CFGF_NONE);
|
|
|
|
|
|
|
|
ret = cfg_parse(cfg, file);
|
|
|
|
|
|
|
|
if (ret == CFG_FILE_ERROR)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_CONF, "Could not open config file %s\n", file);
|
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
goto out_fail;
|
2009-04-18 08:14:26 -04:00
|
|
|
}
|
|
|
|
else if (ret == CFG_PARSE_ERROR)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_CONF, "Parse error in config file %s\n", file);
|
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
goto out_fail;
|
2009-04-18 08:14:26 -04:00
|
|
|
}
|
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
lib = cfg_getsec(cfg, "library");
|
2009-04-18 08:14:26 -04:00
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
if (cfg_size(lib, "directories") == 0)
|
2009-04-18 08:14:26 -04:00
|
|
|
{
|
2010-03-19 14:06:47 -04:00
|
|
|
DPRINTF(E_FATAL, L_CONF, "No directories specified for library\n");
|
2009-04-18 08:14:26 -04:00
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
goto out_fail;
|
2009-04-18 08:14:26 -04:00
|
|
|
}
|
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
/* Do keyword expansion on library names */
|
|
|
|
ret = conffile_expand_libname(lib);
|
|
|
|
if (ret != 0)
|
2009-04-18 08:14:26 -04:00
|
|
|
{
|
2010-03-19 14:06:47 -04:00
|
|
|
DPRINTF(E_FATAL, L_CONF, "Could not expand library name\n");
|
2009-04-18 08:14:26 -04:00
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
goto out_fail;
|
2009-04-18 08:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-03-19 14:06:47 -04:00
|
|
|
|
|
|
|
out_fail:
|
|
|
|
cfg_free(cfg);
|
|
|
|
|
|
|
|
return -1;
|
2009-04-18 08:14:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
conffile_unload(void)
|
|
|
|
{
|
|
|
|
cfg_free(cfg);
|
|
|
|
}
|