configuration_loader.c 2.8 KB

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