notes
[oweals/gnunet.git] / src / util / configuration_loader.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file src/util/configuration_loader.c
21  * @brief configuration loading
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-configuration", __VA_ARGS__)
29
30
31 /**
32  * Load configuration (starts with defaults, then loads
33  * system-specific configuration).
34  *
35  * @param cfg configuration to update
36  * @param filename name of the configuration file, NULL to load defaults
37  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
38  */
39 int
40 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
41                            const char *filename)
42 {
43   char *baseconfig;
44   const char *base_config_varname;
45
46   base_config_varname = GNUNET_OS_project_data_get ()->base_config_varname;
47
48   if (NULL != (baseconfig = getenv (base_config_varname)))
49   {
50     baseconfig = GNUNET_strdup (baseconfig);
51   }
52   else
53   {
54     char *ipath;
55
56     ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
57     if (NULL == ipath)
58       return GNUNET_SYSERR;
59     GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
60     GNUNET_free (ipath);
61   }
62
63   if (GNUNET_SYSERR ==
64       GNUNET_CONFIGURATION_load_from (cfg,
65                                       baseconfig))
66   {
67     GNUNET_free (baseconfig);
68     return GNUNET_SYSERR;       /* no configuration at all found */
69   }
70   GNUNET_free (baseconfig);
71   if ((NULL != filename) &&
72       (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
73   {
74     /* specified configuration not found */
75     return GNUNET_SYSERR;
76   }
77   if (((GNUNET_YES !=
78         GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
79       (filename != NULL))
80     GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
81                                            filename);
82   return GNUNET_OK;
83 }
84
85 /* end of configuration_loader.c */