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