-eddsa not yet in gcrypt
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2007, 2008, 2009, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
31
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
33
34 /**
35  * @brief configuration entry
36  */
37 struct ConfigEntry
38 {
39
40   /**
41    * This is a linked list.
42    */
43   struct ConfigEntry *next;
44
45   /**
46    * key for this entry
47    */
48   char *key;
49
50   /**
51    * current, commited value
52    */
53   char *val;
54 };
55
56
57 /**
58  * @brief configuration section
59  */
60 struct ConfigSection
61 {
62   /**
63    * This is a linked list.
64    */
65   struct ConfigSection *next;
66
67   /**
68    * entries in the section
69    */
70   struct ConfigEntry *entries;
71
72   /**
73    * name of the section
74    */
75   char *name;
76 };
77
78
79 /**
80  * @brief configuration data
81  */
82 struct GNUNET_CONFIGURATION_Handle
83 {
84   /**
85    * Configuration sections.
86    */
87   struct ConfigSection *sections;
88
89   /**
90    * Modification indication since last save
91    * #GNUNET_NO if clean, #GNUNET_YES if dirty,
92    * #GNUNET_SYSERR on error (i.e. last save failed)
93    */
94   int dirty;
95
96 };
97
98
99 /**
100  * Used for diffing a configuration object against
101  * the default one
102  */
103 struct DiffHandle
104 {
105   const struct GNUNET_CONFIGURATION_Handle *cfgDefault;
106   struct GNUNET_CONFIGURATION_Handle *cfgDiff;
107 };
108
109
110
111 /**
112  * Create a GNUNET_CONFIGURATION_Handle.
113  *
114  * @return fresh configuration object
115  */
116 struct GNUNET_CONFIGURATION_Handle *
117 GNUNET_CONFIGURATION_create ()
118 {
119   return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
120 }
121
122
123 /**
124  * Destroy configuration object.
125  *
126  * @param cfg configuration to destroy
127  */
128 void
129 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
130 {
131   struct ConfigSection *sec;
132
133   while (NULL != (sec = cfg->sections))
134     GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
135   GNUNET_free (cfg);
136 }
137
138
139 /**
140  * De-serializes configuration
141  *
142  * @param cfg configuration to update
143  * @param mem the memory block of serialized configuration
144  * @param size the size of the memory block
145  * @param allow_inline set to #GNUNET_YES if we recursively load configuration
146  *          from inlined configurations; #GNUNET_NO if not and raise warnings
147  *          when we come across them
148  * @return #GNUNET_OK on success, #GNUNET_ERROR on error
149  */
150 int
151 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
152                                   const char *mem,
153                                   const size_t size,
154                                   int allow_inline)
155 {
156   char *line;
157   char *line_orig;
158   size_t line_size;
159   char *pos;
160   unsigned int nr;
161   size_t r_bytes;
162   size_t to_read;
163   size_t i;
164   int emptyline;
165   int ret;
166   char *section;
167   char *eq;
168   char *tag;
169   char *value;
170
171   LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
172   ret = GNUNET_OK;
173   section = GNUNET_strdup ("");
174   nr = 0;
175   r_bytes = 0;
176   line_orig = NULL;
177   while (r_bytes < size)
178   {
179     GNUNET_free_non_null (line_orig);
180     /* fgets-like behaviour on buffer */
181     to_read = size - r_bytes;
182     pos = memchr (&mem[r_bytes], '\n', to_read);
183     if (NULL == pos)
184     {
185       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
186       r_bytes += line_size;
187     }
188     else
189     {
190       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
191       r_bytes += line_size + 1;
192     }
193     line = line_orig;
194     /* increment line number */
195     nr++;
196     /* tabs and '\r' are whitespace */
197     emptyline = GNUNET_YES;
198     for (i = 0; i < line_size; i++)
199     {
200       if (line[i] == '\t')
201         line[i] = ' ';
202       if (line[i] == '\r')
203         line[i] = ' ';
204       if (' ' != line[i])
205         emptyline = GNUNET_NO;
206     }
207     /* ignore empty lines */
208     if (GNUNET_YES == emptyline)
209       continue;
210
211     /* remove tailing whitespace */
212     for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
213       line[i] = '\0';
214
215     /* remove leading whitespace */
216     for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
217
218     /* ignore comments */
219     if ( ('#' == line[0]) || ('%' == line[0]) )
220       continue;
221
222     /* handle special "@INLINE@" directive */
223     if (0 == strncasecmp (line,
224                           "@INLINE@ ",
225                           strlen ("@INLINE@ ")))
226     {
227       /* @INLINE@ value */
228       value = &line[strlen ("@INLINE@ ")];
229       if (GNUNET_YES == allow_inline)
230       {
231         if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
232         {
233           ret = GNUNET_SYSERR;    /* failed to parse included config */
234           break;
235         }
236       }
237       else
238       {
239         LOG (GNUNET_ERROR_TYPE_DEBUG,
240              "Ignoring parsing @INLINE@ configurations, not allowed!\n");
241         ret = GNUNET_SYSERR;
242         break;
243       }
244       continue;
245     }
246     if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
247     {
248       /* [value] */
249       line[line_size - 1] = '\0';
250       value = &line[1];
251       GNUNET_free (section);
252       section = GNUNET_strdup (value);
253       LOG (GNUNET_ERROR_TYPE_DEBUG,
254            "Config section `%s'\n",
255            section);
256       continue;
257     }
258     if (NULL != (eq = strchr (line, '=')))
259     {
260       /* tag = value */
261       tag = GNUNET_strndup (line, eq - line);
262       /* remove tailing whitespace */
263       for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
264         tag[i] = '\0';
265
266       /* Strip whitespace */
267       value = eq + 1;
268       while (isspace ((unsigned char) value[0]))
269         value++;
270       for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
271         value[i] = '\0';
272
273       /* remove quotes */
274       i = 0;
275       if ( ('"' == value[0]) &&
276            ('"' == value[strlen (value) - 1]) )
277       {
278         value[strlen (value) - 1] = '\0';
279         value++;
280       }
281       LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
282       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
283       GNUNET_free (tag);
284       continue;
285     }
286     /* parse error */
287     LOG (GNUNET_ERROR_TYPE_WARNING,
288          _("Syntax error while deserializing in line %u\n"),
289          nr);
290     ret = GNUNET_SYSERR;
291     break;
292   }
293   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
294   GNUNET_free_non_null (line_orig);
295   GNUNET_free (section);
296   GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
297   return ret;
298 }
299
300
301 /**
302  * Parse a configuration file, add all of the options in the
303  * file to the configuration environment.
304  *
305  * @param cfg configuration to update
306  * @param filename name of the configuration file
307  * @return GNUNET_OK on success, GNUNET_SYSERR on error
308  */
309 int
310 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
311                             const char *filename)
312 {
313   uint64_t fs64;
314   size_t fs;
315   char *fn;
316   char *mem;
317   int dirty;
318   int ret;
319
320   LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
321   fn = GNUNET_STRINGS_filename_expand (filename);
322   LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
323   if (fn == NULL)
324     return GNUNET_SYSERR;
325   dirty = cfg->dirty;           /* back up value! */
326   if (GNUNET_SYSERR ==
327        GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
328   {
329     LOG (GNUNET_ERROR_TYPE_WARNING,
330          "Error while determining the file size of %s\n", fn);
331     GNUNET_free (fn);
332     return GNUNET_SYSERR;
333   }
334   if (fs64 > SIZE_MAX)
335   {
336     GNUNET_break (0);           /* File size is more than the heap size */
337     GNUNET_free (fn);
338     return GNUNET_SYSERR;
339   }
340   fs = fs64;
341   mem = GNUNET_malloc (fs);
342   if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
343   {
344     LOG (GNUNET_ERROR_TYPE_WARNING,
345          "Error while reading file %s\n", fn);
346     GNUNET_free (fn);
347     GNUNET_free (mem);
348     return GNUNET_SYSERR;
349   }
350   LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
351   GNUNET_free (fn);
352   ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
353   GNUNET_free (mem);
354   /* restore dirty flag - anything we set in the meantime
355    * came from disk */
356   cfg->dirty = dirty;
357   return ret;
358 }
359
360
361 /**
362  * Test if there are configuration options that were
363  * changed since the last save.
364  *
365  * @param cfg configuration to inspect
366  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
367  */
368 int
369 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
370 {
371   return cfg->dirty;
372 }
373
374
375 /**
376  * Serializes the given configuration.
377  *
378  * @param cfg configuration to serialize
379  * @param size will be set to the size of the serialized memory block
380  * @return the memory block where the serialized configuration is
381  *           present. This memory should be freed by the caller
382  */
383 char *
384 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
385                                 size_t *size)
386 {
387   struct ConfigSection *sec;
388   struct ConfigEntry *ent;
389   char *mem;
390   char *cbuf;
391   char *val;
392   char *pos;
393   int len;
394   size_t m_size;
395   size_t c_size;
396
397
398   /* Pass1 : calculate the buffer size required */
399   m_size = 0;
400   for (sec = cfg->sections; NULL != sec; sec = sec->next)
401   {
402     /* For each section we need to add 3 charaters: {'[',']','\n'} */
403     m_size += strlen (sec->name) + 3;
404     for (ent = sec->entries; NULL != ent; ent = ent->next)
405     {
406       if (NULL != ent->val)
407       {
408         /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
409         pos = ent->val;
410         while (NULL != (pos = strstr (pos, "\n")))
411         {
412           m_size++;
413           pos++;
414         }
415         /* For each key = value pair we need to add 4 characters (2
416            spaces and 1 equal-to character and 1 new line) */
417         m_size += strlen (ent->key) + strlen (ent->val) + 4;
418       }
419     }
420     /* A new line after section end */
421     m_size++;
422   }
423
424   /* Pass2: Allocate memory and write the configuration to it */
425   mem = GNUNET_malloc (m_size);
426   sec = cfg->sections;
427   c_size = 0;
428   *size = c_size;
429   while (NULL != sec)
430   {
431     len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
432     GNUNET_assert (0 < len);
433     memcpy (mem + c_size, cbuf, len);
434     c_size += len;
435     GNUNET_free (cbuf);
436     for (ent = sec->entries; NULL != ent; ent = ent->next)
437     {
438       if (NULL != ent->val)
439       {
440         val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
441         strcpy (val, ent->val);
442         while (NULL != (pos = strstr (val, "\n")))
443         {
444           memmove (&pos[2], &pos[1], strlen (&pos[1]));
445           pos[0] = '\\';
446           pos[1] = 'n';
447         }
448         len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
449         GNUNET_free (val);
450         memcpy (mem + c_size, cbuf, len);
451         c_size += len;
452         GNUNET_free (cbuf);
453       }
454     }
455     memcpy (mem + c_size, "\n", 1);
456     c_size ++;
457     sec = sec->next;
458   }
459   GNUNET_assert (c_size == m_size);
460   *size = c_size;
461   return mem;
462 }
463
464
465 /**
466  * Write configuration file.
467  *
468  * @param cfg configuration to write
469  * @param filename where to write the configuration
470  * @return GNUNET_OK on success, GNUNET_SYSERR on error
471  */
472 int
473 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
474                             const char *filename)
475 {
476   char *fn;
477   char *cfg_buf;
478   size_t size;
479
480   fn = GNUNET_STRINGS_filename_expand (filename);
481   if (fn == NULL)
482     return GNUNET_SYSERR;
483   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
484   {
485     GNUNET_free (fn);
486     return GNUNET_SYSERR;
487   }
488   cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
489   if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
490                                     GNUNET_DISK_PERM_USER_READ
491                                     | GNUNET_DISK_PERM_USER_WRITE
492                                     | GNUNET_DISK_PERM_GROUP_READ
493                                     | GNUNET_DISK_PERM_GROUP_WRITE))
494   {
495     GNUNET_free (fn);
496     GNUNET_free (cfg_buf);
497     LOG (GNUNET_ERROR_TYPE_WARNING,
498          "Writing configration to file: %s failed\n", filename);
499     cfg->dirty = GNUNET_SYSERR; /* last write failed */
500     return GNUNET_SYSERR;
501   }
502   GNUNET_free (fn);
503   GNUNET_free (cfg_buf);
504   cfg->dirty = GNUNET_NO;       /* last write succeeded */
505   return GNUNET_OK;
506 }
507
508
509 /**
510  * Iterate over all options in the configuration.
511  *
512  * @param cfg configuration to inspect
513  * @param iter function to call on each option
514  * @param iter_cls closure for iter
515  */
516 void
517 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
518                               GNUNET_CONFIGURATION_Iterator iter,
519                               void *iter_cls)
520 {
521   struct ConfigSection *spos;
522   struct ConfigEntry *epos;
523
524   for (spos = cfg->sections; NULL != spos; spos = spos->next)
525     for (epos = spos->entries; NULL != epos; epos = epos->next)
526       if (NULL != epos->val)
527         iter (iter_cls, spos->name, epos->key, epos->val);
528 }
529
530
531 /**
532  * Iterate over values of a section in the configuration.
533  *
534  * @param cfg configuration to inspect
535  * @param section the section
536  * @param iter function to call on each option
537  * @param iter_cls closure for iter
538  */
539 void
540 GNUNET_CONFIGURATION_iterate_section_values (const struct
541                                              GNUNET_CONFIGURATION_Handle *cfg,
542                                              const char *section,
543                                              GNUNET_CONFIGURATION_Iterator iter,
544                                              void *iter_cls)
545 {
546   struct ConfigSection *spos;
547   struct ConfigEntry *epos;
548
549   spos = cfg->sections;
550   while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
551     spos = spos->next;
552   if (NULL == spos)
553     return;
554   for (epos = spos->entries; NULL != epos; epos = epos->next)
555     if (NULL != epos->val)
556       iter (iter_cls, spos->name, epos->key, epos->val);
557 }
558
559
560 /**
561  * Iterate over all sections in the configuration.
562  *
563  * @param cfg configuration to inspect
564  * @param iter function to call on each section
565  * @param iter_cls closure for iter
566  */
567 void
568 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
569                                        *cfg,
570                                        GNUNET_CONFIGURATION_Section_Iterator
571                                        iter, void *iter_cls)
572 {
573   struct ConfigSection *spos;
574   struct ConfigSection *next;
575
576   next = cfg->sections;
577   while (next != NULL)
578   {
579     spos = next;
580     next = spos->next;
581     iter (iter_cls, spos->name);
582   }
583 }
584
585 /**
586  * Remove the given section and all options in it.
587  *
588  * @param cfg configuration to inspect
589  * @param section name of the section to remove
590  */
591 void
592 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
593                                      const char *section)
594 {
595   struct ConfigSection *spos;
596   struct ConfigSection *prev;
597   struct ConfigEntry *ent;
598
599   prev = NULL;
600   spos = cfg->sections;
601   while (NULL != spos)
602   {
603     if (0 == strcasecmp (section, spos->name))
604     {
605       if (NULL == prev)
606         cfg->sections = spos->next;
607       else
608         prev->next = spos->next;
609       while (NULL != (ent = spos->entries))
610       {
611         spos->entries = ent->next;
612         GNUNET_free (ent->key);
613         GNUNET_free_non_null (ent->val);
614         GNUNET_free (ent);
615         cfg->dirty = GNUNET_YES;
616       }
617       GNUNET_free (spos->name);
618       GNUNET_free (spos);
619       return;
620     }
621     prev = spos;
622     spos = spos->next;
623   }
624 }
625
626
627 /**
628  * Copy a configuration value to the given target configuration.
629  * Overwrites existing entries.
630  *
631  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
632  * @param section section for the value
633  * @param option option name of the value
634  * @param value value to copy
635  */
636 static void
637 copy_entry (void *cls, const char *section, const char *option,
638             const char *value)
639 {
640   struct GNUNET_CONFIGURATION_Handle *dst = cls;
641
642   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
643 }
644
645
646 /**
647  * Duplicate an existing configuration object.
648  *
649  * @param cfg configuration to duplicate
650  * @return duplicate configuration
651  */
652 struct GNUNET_CONFIGURATION_Handle *
653 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
654 {
655   struct GNUNET_CONFIGURATION_Handle *ret;
656
657   ret = GNUNET_CONFIGURATION_create ();
658   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
659   return ret;
660 }
661
662
663 /**
664  * Find a section entry from a configuration.
665  *
666  * @param cfg FIXME
667  * @param section FIXME
668  * @return matching entry, NULL if not found
669  */
670 static struct ConfigSection *
671 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
672 {
673   struct ConfigSection *pos;
674
675   pos = cfg->sections;
676   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
677     pos = pos->next;
678   return pos;
679 }
680
681
682 /**
683  * Find an entry from a configuration.
684  *
685  * @param cfg handle to the configuration
686  * @param section section the option is in
687  * @param key the option
688  * @return matching entry, NULL if not found
689  */
690 static struct ConfigEntry *
691 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
692            const char *key)
693 {
694   struct ConfigSection *sec;
695   struct ConfigEntry *pos;
696
697   if (NULL == (sec = findSection (cfg, section)))
698     return NULL;
699   pos = sec->entries;
700   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
701     pos = pos->next;
702   return pos;
703 }
704
705
706 /**
707  * A callback function, compares entries from two configurations
708  * (default against a new configuration) and write the diffs in a
709  * diff-configuration object (the callback object).
710  *
711  * @param cls the diff configuration (struct DiffHandle*)
712  * @param section section for the value (of the default conf.)
713  * @param option option name of the value (of the default conf.)
714  * @param value value to copy (of the default conf.)
715  */
716 static void
717 compare_entries (void *cls, const char *section, const char *option,
718                  const char *value)
719 {
720   struct DiffHandle *dh = cls;
721   struct ConfigEntry *entNew;
722
723   entNew = findEntry (dh->cfgDefault, section, option);
724   if ( (NULL != entNew) &&
725        (NULL != entNew->val) &&
726        (0 == strcmp (entNew->val, value)) )
727     return;
728   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
729 }
730
731
732 /**
733  * Compute configuration with only entries that have been changed
734  *
735  * @param cfgDefault original configuration
736  * @param cfgNew new configuration
737  * @return configuration with only the differences, never NULL
738  */
739 struct GNUNET_CONFIGURATION_Handle *
740 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle
741                                *cfgDefault,
742                                const struct GNUNET_CONFIGURATION_Handle
743                                *cfgNew)
744 {
745   struct DiffHandle diffHandle;
746
747   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
748   diffHandle.cfgDefault = cfgDefault;
749   GNUNET_CONFIGURATION_iterate (cfgNew, &compare_entries, &diffHandle);
750   return diffHandle.cfgDiff;
751 }
752
753
754 /**
755  * Write only configuration entries that have been changed to configuration file
756  * @param cfgDefault default configuration
757  * @param cfgNew new configuration
758  * @param filename where to write the configuration diff between default and new
759  * @return GNUNET_OK on success, GNUNET_SYSERR on error
760  */
761 int
762 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
763                                   *cfgDefault,
764                                   const struct GNUNET_CONFIGURATION_Handle
765                                   *cfgNew, const char *filename)
766 {
767   int ret;
768   struct GNUNET_CONFIGURATION_Handle *diff;
769
770   diff = GNUNET_CONFIGURATION_get_diff (cfgDefault, cfgNew);
771   ret = GNUNET_CONFIGURATION_write (diff, filename);
772   GNUNET_CONFIGURATION_destroy (diff);
773   return ret;
774 }
775
776
777 /**
778  * Set a configuration value that should be a string.
779  *
780  * @param cfg configuration to update
781  * @param section section of interest
782  * @param option option of interest
783  * @param value value to set
784  */
785 void
786 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
787                                        const char *section, const char *option,
788                                        const char *value)
789 {
790   struct ConfigSection *sec;
791   struct ConfigEntry *e;
792   char *nv;
793
794   e = findEntry (cfg, section, option);
795   if (NULL != e)
796   {
797     if (NULL == value)
798     {
799       GNUNET_free_non_null (e->val);
800       e->val = NULL;
801     }
802     else
803     {
804       nv = GNUNET_strdup (value);
805       GNUNET_free_non_null (e->val);
806       e->val = nv;
807     }
808     return;
809   }
810   sec = findSection (cfg, section);
811   if (sec == NULL)
812   {
813     sec = GNUNET_new (struct ConfigSection);
814     sec->name = GNUNET_strdup (section);
815     sec->next = cfg->sections;
816     cfg->sections = sec;
817   }
818   e = GNUNET_new (struct ConfigEntry);
819   e->key = GNUNET_strdup (option);
820   e->val = GNUNET_strdup (value);
821   e->next = sec->entries;
822   sec->entries = e;
823 }
824
825
826 /**
827  * Set a configuration value that should be a number.
828  *
829  * @param cfg configuration to update
830  * @param section section of interest
831  * @param option option of interest
832  * @param number value to set
833  */
834 void
835 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
836                                        const char *section, const char *option,
837                                        unsigned long long number)
838 {
839   char s[64];
840
841   GNUNET_snprintf (s, 64, "%llu", number);
842   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
843 }
844
845
846 /**
847  * Get a configuration value that should be a number.
848  *
849  * @param cfg configuration to inspect
850  * @param section section of interest
851  * @param option option of interest
852  * @param number where to store the numeric value of the option
853  * @return GNUNET_OK on success, GNUNET_SYSERR on error
854  */
855 int
856 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
857                                        *cfg, const char *section,
858                                        const char *option,
859                                        unsigned long long *number)
860 {
861   struct ConfigEntry *e;
862
863   if (NULL == (e = findEntry (cfg, section, option)))
864     return GNUNET_SYSERR;
865   if (NULL == e->val)
866     return GNUNET_SYSERR;
867   if (1 != SSCANF (e->val, "%llu", number))
868     return GNUNET_SYSERR;
869   return GNUNET_OK;
870 }
871
872
873 /**
874  * Get a configuration value that should be a relative time.
875  *
876  * @param cfg configuration to inspect
877  * @param section section of interest
878  * @param option option of interest
879  * @param time set to the time value stored in the configuration
880  * @return GNUNET_OK on success, GNUNET_SYSERR on error
881  */
882 int
883 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
884                                      *cfg, const char *section,
885                                      const char *option,
886                                      struct GNUNET_TIME_Relative *time)
887 {
888   struct ConfigEntry *e;
889
890   if (NULL == (e = findEntry (cfg, section, option)))
891     return GNUNET_SYSERR;
892   if (NULL == e->val)
893     return GNUNET_SYSERR;
894   return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
895 }
896
897
898 /**
899  * Get a configuration value that should be a size in bytes.
900  *
901  * @param cfg configuration to inspect
902  * @param section section of interest
903  * @param option option of interest
904  * @param size set to the size in bytes as stored in the configuration
905  * @return GNUNET_OK on success, GNUNET_SYSERR on error
906  */
907 int
908 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
909                                      *cfg, const char *section,
910                                      const char *option,
911                                      unsigned long long *size)
912 {
913   struct ConfigEntry *e;
914
915   if (NULL == (e = findEntry (cfg, section, option)))
916     return GNUNET_SYSERR;
917   if (NULL == e->val)
918     return GNUNET_SYSERR;
919   return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
920 }
921
922
923 /**
924  * Get a configuration value that should be a string.
925  *
926  * @param cfg configuration to inspect
927  * @param section section of interest
928  * @param option option of interest
929  * @param value will be set to a freshly allocated configuration
930  *        value, or NULL if option is not specified
931  * @return GNUNET_OK on success, GNUNET_SYSERR on error
932  */
933 int
934 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
935                                        *cfg, const char *section,
936                                        const char *option, char **value)
937 {
938   struct ConfigEntry *e;
939
940   LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
941   if ( (NULL == (e = findEntry (cfg, section, option))) ||
942        (NULL == e->val) )
943   {
944     *value = NULL;
945     return GNUNET_SYSERR;
946   }
947   *value = GNUNET_strdup (e->val);
948   return GNUNET_OK;
949 }
950
951
952 /**
953  * Get a configuration value that should be in a set of
954  * predefined strings
955  *
956  * @param cfg configuration to inspect
957  * @param section section of interest
958  * @param option option of interest
959  * @param choices NULL-terminated list of legal values
960  * @param value will be set to an entry in the legal list,
961  *        or NULL if option is not specified and no default given
962  * @return GNUNET_OK on success, GNUNET_SYSERR on error
963  */
964 int
965 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
966                                        *cfg, const char *section,
967                                        const char *option, const char *const *choices,
968                                        const char **value)
969 {
970   struct ConfigEntry *e;
971   unsigned int i;
972
973   if (NULL == (e = findEntry (cfg, section, option)))
974     return GNUNET_SYSERR;
975   for (i = 0; NULL != choices[i]; i++)
976     if (0 == strcasecmp (choices[i], e->val))
977       break;
978   if (NULL == choices[i])
979   {
980     LOG (GNUNET_ERROR_TYPE_ERROR,
981          _("Configuration value '%s' for '%s'"
982            " in section '%s' is not in set of legal choices\n"), e->val, option,
983          section);
984     return GNUNET_SYSERR;
985   }
986   *value = choices[i];
987   return GNUNET_OK;
988 }
989
990
991 /**
992  * Test if we have a value for a particular option
993  * @param cfg configuration to inspect
994  * @param section section of interest
995  * @param option option of interest
996  * @return #GNUNET_YES if so, #GNUNET_NO if not.
997  */
998 int
999 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1000                                  const char *section, const char *option)
1001 {
1002   struct ConfigEntry *e;
1003
1004   if ((NULL == (e = findEntry (cfg, section, option))) || (NULL == e->val))
1005     return GNUNET_NO;
1006   return GNUNET_YES;
1007 }
1008
1009
1010 /**
1011  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1012  * where either in the "PATHS" section or the environtment "FOO" is
1013  * set to "DIRECTORY".  We also support default expansion,
1014  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1015  * set in PATHS or the environment, and otherwise to "default".  Note
1016  * that "default" itself can also be a $-expression, thus
1017  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1018  * to VAR2.
1019  *
1020  * @param cfg configuration to use for path expansion
1021  * @param orig string to $-expand (will be freed!)
1022  * @param depth recursion depth, used to detect recursive expansions
1023  * @return $-expanded string
1024  */
1025 static char *
1026 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1027                char *orig,
1028                unsigned int depth)
1029 {
1030   int i;
1031   char *prefix;
1032   char *result;
1033   char *start;
1034   const char *post;
1035   const char *env;
1036   char *def;
1037   char *end;
1038   unsigned int lopen;
1039
1040   if (depth > 128)
1041   {
1042     LOG (GNUNET_ERROR_TYPE_WARNING,
1043          _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1044          orig);
1045     return orig;
1046   }
1047   LOG (GNUNET_ERROR_TYPE_DEBUG,
1048        "Asked to $-expand %s\n", orig);
1049   if ('$' != orig[0])
1050   {
1051     LOG (GNUNET_ERROR_TYPE_DEBUG,
1052          "Doesn't start with $ - not expanding\n");
1053     return orig;
1054   }
1055   if ('{' == orig[1])
1056   {
1057     start = &orig[2];
1058     lopen = 1;
1059     end = &orig[1];
1060     while (lopen > 0)
1061     {
1062       end++;
1063       switch (*end)
1064       {
1065       case '}':
1066         lopen--;
1067         break;
1068       case '{':
1069         lopen++;
1070         break;
1071       case '\0':
1072         LOG (GNUNET_ERROR_TYPE_WARNING,
1073              _("Missing closing `%s' in option `%s'\n"),
1074              "}",
1075              orig);
1076         return orig;
1077       default:
1078         break;
1079       }
1080     }
1081     *end = '\0';
1082     post = end + 1;
1083     def = strchr (orig, ':');
1084     if (NULL != def)
1085     {
1086       *def = '\0';
1087       def++;
1088       if ( ('-' == *def) ||
1089            ('=' == *def) )
1090         def++;
1091       def = GNUNET_strdup (def);
1092     }
1093   }
1094   else
1095   {
1096     start = &orig[1];
1097     def = NULL;
1098     i = 0;
1099     while ( (orig[i] != '/') &&
1100             (orig[i] != '\\') &&
1101             (orig[i] != '\0') )
1102       i++;
1103     if (orig[i] == '\0')
1104     {
1105       post = "";
1106     }
1107     else
1108     {
1109       orig[i] = '\0';
1110       post = &orig[i + 1];
1111     }
1112   }
1113   LOG (GNUNET_ERROR_TYPE_DEBUG,
1114        "Split into `%s' and `%s' with default %s\n",
1115        start,
1116        post,
1117        def);
1118   if (GNUNET_OK !=
1119       GNUNET_CONFIGURATION_get_value_filename (cfg,
1120                                                "PATHS",
1121                                                start,
1122                                                &prefix))
1123   {
1124     LOG (GNUNET_ERROR_TYPE_DEBUG,
1125          "Filename for `%s' is not in PATHS config section\n",
1126          start);
1127     if (NULL == (env = getenv (start)))
1128     {
1129       LOG (GNUNET_ERROR_TYPE_DEBUG,
1130            "`%s' is not an environment variable\n",
1131            start);
1132       /* try default */
1133       def = expand_dollar (cfg, def, depth + 1);
1134       env = def;
1135     }
1136     if (NULL == env)
1137     {
1138       orig[strlen (orig)] = DIR_SEPARATOR;
1139       LOG (GNUNET_ERROR_TYPE_DEBUG,
1140            "Expanded to `%s' (returning orig)\n",
1141            orig);
1142       return orig;
1143     }
1144     prefix = GNUNET_strdup (env);
1145   }
1146   LOG (GNUNET_ERROR_TYPE_DEBUG,
1147        "Prefix is `%s'\n",
1148        prefix);
1149   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1150   strcpy (result, prefix);
1151   if ( (0 == strlen (prefix)) ||
1152        ( (prefix[strlen (prefix) - 1] != DIR_SEPARATOR) &&
1153          (strlen (post) > 0) ) )
1154     strcat (result, DIR_SEPARATOR_STR);
1155   strcat (result, post);
1156   GNUNET_free_non_null (def);
1157   GNUNET_free (prefix);
1158   GNUNET_free (orig);
1159   LOG (GNUNET_ERROR_TYPE_DEBUG,
1160        "Expanded to `%s'\n",
1161        result);
1162   return result;
1163 }
1164
1165
1166 /**
1167  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1168  * where either in the "PATHS" section or the environtment "FOO" is
1169  * set to "DIRECTORY".  We also support default expansion,
1170  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1171  * set in PATHS or the environment, and otherwise to "default".  Note
1172  * that "default" itself can also be a $-expression, thus
1173  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1174  * to VAR2.
1175  *
1176  * @param cfg configuration to use for path expansion
1177  * @param orig string to $-expand (will be freed!)
1178  * @return $-expanded string
1179  */
1180 char *
1181 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1182                                     char *orig)
1183 {
1184   return expand_dollar (cfg, orig, 0);
1185 }
1186
1187
1188 /**
1189  * Get a configuration value that should be a string.
1190  *
1191  * @param cfg configuration to inspect
1192  * @param section section of interest
1193  * @param option option of interest
1194  * @param value will be set to a freshly allocated configuration
1195  *        value, or NULL if option is not specified
1196  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1197  */
1198 int
1199 GNUNET_CONFIGURATION_get_value_filename (const struct
1200                                          GNUNET_CONFIGURATION_Handle *cfg,
1201                                          const char *section,
1202                                          const char *option, char **value)
1203 {
1204   char *tmp;
1205
1206   LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1207   if (GNUNET_OK !=
1208       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1209   {
1210     LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1211     *value = NULL;
1212     return GNUNET_SYSERR;
1213   }
1214   LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1215   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1216   LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1217   *value = GNUNET_STRINGS_filename_expand (tmp);
1218   GNUNET_free (tmp);
1219   LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1220   if (*value == NULL)
1221     return GNUNET_SYSERR;
1222   return GNUNET_OK;
1223 }
1224
1225
1226 /**
1227  * Get a configuration value that should be in a set of
1228  * "GNUNET_YES" or "GNUNET_NO".
1229  *
1230  * @param cfg configuration to inspect
1231  * @param section section of interest
1232  * @param option option of interest
1233  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1234  */
1235 int
1236 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1237                                       *cfg, const char *section,
1238                                       const char *option)
1239 {
1240   static const char *yesno[] = { "YES", "NO", NULL };
1241   const char *val;
1242   int ret;
1243
1244   ret =
1245       GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1246   if (ret == GNUNET_SYSERR)
1247     return ret;
1248   if (val == yesno[0])
1249     return GNUNET_YES;
1250   return GNUNET_NO;
1251 }
1252
1253
1254 /**
1255  * Iterate over the set of filenames stored in a configuration value.
1256  *
1257  * @param cfg configuration to inspect
1258  * @param section section of interest
1259  * @param option option of interest
1260  * @param cb function to call on each filename
1261  * @param cb_cls closure for cb
1262  * @return number of filenames iterated over, -1 on error
1263  */
1264 int
1265 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1266                                               GNUNET_CONFIGURATION_Handle *cfg,
1267                                               const char *section,
1268                                               const char *option,
1269                                               GNUNET_FileNameCallback cb,
1270                                               void *cb_cls)
1271 {
1272   char *list;
1273   char *pos;
1274   char *end;
1275   char old;
1276   int ret;
1277
1278   if (GNUNET_OK !=
1279       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1280     return 0;
1281   GNUNET_assert (list != NULL);
1282   ret = 0;
1283   pos = list;
1284   while (1)
1285   {
1286     while (pos[0] == ' ')
1287       pos++;
1288     if (strlen (pos) == 0)
1289       break;
1290     end = pos + 1;
1291     while ((end[0] != ' ') && (end[0] != '\0'))
1292     {
1293       if (end[0] == '\\')
1294       {
1295         switch (end[1])
1296         {
1297         case '\\':
1298         case ' ':
1299           memmove (end, &end[1], strlen (&end[1]) + 1);
1300         case '\0':
1301           /* illegal, but just keep it */
1302           break;
1303         default:
1304           /* illegal, but just ignore that there was a '/' */
1305           break;
1306         }
1307       }
1308       end++;
1309     }
1310     old = end[0];
1311     end[0] = '\0';
1312     if (strlen (pos) > 0)
1313     {
1314       ret++;
1315       if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1316       {
1317         ret = GNUNET_SYSERR;
1318         break;
1319       }
1320     }
1321     if (old == '\0')
1322       break;
1323     pos = end + 1;
1324   }
1325   GNUNET_free (list);
1326   return ret;
1327 }
1328
1329
1330 /**
1331  * FIXME.
1332  *
1333  * @param value FIXME
1334  * @return FIXME
1335  */
1336 static char *
1337 escape_name (const char *value)
1338 {
1339   char *escaped;
1340   const char *rpos;
1341   char *wpos;
1342
1343   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1344   memset (escaped, 0, strlen (value) * 2 + 1);
1345   rpos = value;
1346   wpos = escaped;
1347   while (rpos[0] != '\0')
1348   {
1349     switch (rpos[0])
1350     {
1351     case '\\':
1352     case ' ':
1353       wpos[0] = '\\';
1354       wpos[1] = rpos[0];
1355       wpos += 2;
1356       break;
1357     default:
1358       wpos[0] = rpos[0];
1359       wpos++;
1360     }
1361     rpos++;
1362   }
1363   return escaped;
1364 }
1365
1366
1367 /**
1368  * FIXME.
1369  *
1370  * @param cls string we compare with (const char*)
1371  * @param fn filename we are currently looking at
1372  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1373  */
1374 static int
1375 test_match (void *cls, const char *fn)
1376 {
1377   const char *of = cls;
1378
1379   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1380 }
1381
1382
1383 /**
1384  * Append a filename to a configuration value that
1385  * represents a list of filenames
1386  *
1387  * @param cfg configuration to update
1388  * @param section section of interest
1389  * @param option option of interest
1390  * @param value filename to append
1391  * @return GNUNET_OK on success,
1392  *         GNUNET_NO if the filename already in the list
1393  *         GNUNET_SYSERR on error
1394  */
1395 int
1396 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1397                                             *cfg, const char *section,
1398                                             const char *option,
1399                                             const char *value)
1400 {
1401   char *escaped;
1402   char *old;
1403   char *nw;
1404
1405   if (GNUNET_SYSERR ==
1406       GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1407                                                     &test_match,
1408                                                     (void *) value))
1409     return GNUNET_NO;           /* already exists */
1410   if (GNUNET_OK !=
1411       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1412     old = GNUNET_strdup ("");
1413   escaped = escape_name (value);
1414   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1415   strcpy (nw, old);
1416   if (strlen (old) > 0)
1417     strcat (nw, " ");
1418   strcat (nw, escaped);
1419   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1420   GNUNET_free (old);
1421   GNUNET_free (nw);
1422   GNUNET_free (escaped);
1423   return GNUNET_OK;
1424 }
1425
1426
1427 /**
1428  * Remove a filename from a configuration value that
1429  * represents a list of filenames
1430  *
1431  * @param cfg configuration to update
1432  * @param section section of interest
1433  * @param option option of interest
1434  * @param value filename to remove
1435  * @return GNUNET_OK on success,
1436  *         GNUNET_NO if the filename is not in the list,
1437  *         GNUNET_SYSERR on error
1438  */
1439 int
1440 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1441                                             *cfg, const char *section,
1442                                             const char *option,
1443                                             const char *value)
1444 {
1445   char *list;
1446   char *pos;
1447   char *end;
1448   char *match;
1449   char old;
1450
1451   if (GNUNET_OK !=
1452       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1453     return GNUNET_NO;
1454   match = escape_name (value);
1455   pos = list;
1456   while (1)
1457   {
1458     while (pos[0] == ' ')
1459       pos++;
1460     if (strlen (pos) == 0)
1461       break;
1462     end = pos + 1;
1463     while ((end[0] != ' ') && (end[0] != '\0'))
1464     {
1465       if (end[0] == '\\')
1466       {
1467         switch (end[1])
1468         {
1469         case '\\':
1470         case ' ':
1471           end++;
1472           break;
1473         case '\0':
1474           /* illegal, but just keep it */
1475           break;
1476         default:
1477           /* illegal, but just ignore that there was a '/' */
1478           break;
1479         }
1480       }
1481       end++;
1482     }
1483     old = end[0];
1484     end[0] = '\0';
1485     if (0 == strcmp (pos, match))
1486     {
1487       if (old != '\0')
1488         memmove (pos, &end[1], strlen (&end[1]) + 1);
1489       else
1490       {
1491         if (pos != list)
1492           pos[-1] = '\0';
1493         else
1494           pos[0] = '\0';
1495       }
1496       GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1497       GNUNET_free (list);
1498       GNUNET_free (match);
1499       return GNUNET_OK;
1500     }
1501     if (old == '\0')
1502       break;
1503     end[0] = old;
1504     pos = end + 1;
1505   }
1506   GNUNET_free (list);
1507   GNUNET_free (match);
1508   return GNUNET_NO;
1509 }
1510
1511
1512 /**
1513  * Wrapper around GNUNET_CONFIGURATION_parse.
1514  *
1515  * @param cls the cfg
1516  * @param filename file to parse
1517  * @return GNUNET_OK on success
1518  */
1519 static int
1520 parse_configuration_file (void *cls, const char *filename)
1521 {
1522   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1523   char * ext;
1524   int ret;
1525
1526   /* Examine file extension */
1527   ext = strrchr (filename, '.');
1528   if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1529   {
1530     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1531     return GNUNET_OK;
1532   }
1533
1534   ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1535   return ret;
1536 }
1537
1538
1539 /**
1540  * Load default configuration.  This function will parse the
1541  * defaults from the given defaults_d directory.
1542  *
1543  * @param cfg configuration to update
1544  * @param defaults_d directory with the defaults
1545  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1546  */
1547 int
1548 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1549                                 const char *defaults_d)
1550 {
1551   if (GNUNET_SYSERR ==
1552       GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1553     return GNUNET_SYSERR;       /* no configuration at all found */
1554   return GNUNET_OK;
1555 }
1556
1557
1558 /**
1559  * Load configuration (starts with defaults, then loads
1560  * system-specific configuration).
1561  *
1562  * @param cfg configuration to update
1563  * @param filename name of the configuration file, NULL to load defaults
1564  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1565  */
1566 int
1567 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1568                            const char *filename)
1569 {
1570   char *baseconfig;
1571   char *ipath;
1572
1573   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1574   if (ipath == NULL)
1575     return GNUNET_SYSERR;
1576   baseconfig = NULL;
1577   GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1578   GNUNET_free (ipath);
1579   if (GNUNET_SYSERR ==
1580       GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1581   {
1582     GNUNET_free (baseconfig);
1583     return GNUNET_SYSERR;       /* no configuration at all found */
1584   }
1585   GNUNET_free (baseconfig);
1586   if ((filename != NULL) &&
1587       (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1588   {
1589     /* specified configuration not found */
1590     return GNUNET_SYSERR;
1591   }
1592   if (((GNUNET_YES !=
1593         GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1594       (filename != NULL))
1595     GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1596                                            filename);
1597   return GNUNET_OK;
1598 }
1599
1600
1601
1602 /* end of configuration.c */