1 /* config.cc - API for database metadata
2  *
3  * Copyright © 2016 David Bremner
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20 
21 #include "notmuch.h"
22 #include "notmuch-private.h"
23 #include "database-private.h"
24 
25 #include <pwd.h>
26 #include <netdb.h>
27 
28 static const std::string CONFIG_PREFIX = "C";
29 
30 struct _notmuch_config_list {
31     notmuch_database_t *notmuch;
32     Xapian::TermIterator iterator;
33     char *current_key;
34     char *current_val;
35 };
36 
37 struct _notmuch_config_values {
38     const char *iterator;
39     size_t tok_len;
40     const char *string;
41     void *children; /* talloc_context */
42 };
43 
44 struct _notmuch_config_pairs {
45     notmuch_string_map_iterator_t *iter;
46 };
47 
48 static const char *_notmuch_config_key_to_string (notmuch_config_key_t key);
49 static char *_expand_path (void *ctx, const char *key, const char *val);
50 
51 static int
_notmuch_config_list_destroy(notmuch_config_list_t * list)52 _notmuch_config_list_destroy (notmuch_config_list_t *list)
53 {
54     /* invoke destructor w/o deallocating memory */
55     list->iterator.~TermIterator();
56     return 0;
57 }
58 
59 notmuch_status_t
notmuch_database_set_config(notmuch_database_t * notmuch,const char * key,const char * value)60 notmuch_database_set_config (notmuch_database_t *notmuch,
61 			     const char *key,
62 			     const char *value)
63 {
64     notmuch_status_t status;
65 
66     status = _notmuch_database_ensure_writable (notmuch);
67     if (status)
68 	return status;
69 
70     if (! notmuch->config) {
71 	if ((status = _notmuch_config_load_from_database (notmuch)))
72 	    return status;
73     }
74 
75     try {
76 	notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
77     } catch (const Xapian::Error &error) {
78 	status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
79 	notmuch->exception_reported = true;
80 	_notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
81 			       error.get_msg ().c_str ());
82     }
83 
84     if (status)
85 	return status;
86 
87     _notmuch_string_map_set (notmuch->config, key, value);
88 
89     return NOTMUCH_STATUS_SUCCESS;
90 }
91 
92 static notmuch_status_t
_metadata_value(notmuch_database_t * notmuch,const char * key,std::string & value)93 _metadata_value (notmuch_database_t *notmuch,
94 		 const char *key,
95 		 std::string &value)
96 {
97     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
98 
99     try {
100 	value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
101     } catch (const Xapian::Error &error) {
102 	status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
103 	notmuch->exception_reported = true;
104 	_notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
105 			       error.get_msg ().c_str ());
106     }
107     return status;
108 }
109 
110 notmuch_status_t
notmuch_database_get_config(notmuch_database_t * notmuch,const char * key,char ** value)111 notmuch_database_get_config (notmuch_database_t *notmuch,
112 			     const char *key,
113 			     char **value)
114 {
115     const char *stored_val;
116     notmuch_status_t status;
117 
118     if (! notmuch->config) {
119 	if ((status = _notmuch_config_load_from_database (notmuch)))
120 	    return status;
121     }
122 
123     if (! value)
124 	return NOTMUCH_STATUS_NULL_POINTER;
125 
126     stored_val = _notmuch_string_map_get (notmuch->config, key);
127     if (! stored_val) {
128 	/* XXX in principle this API should be fixed so empty string
129 	 * is distinguished from not found */
130 	*value = strdup ("");
131     } else {
132 	*value = strdup (stored_val);
133     }
134 
135     return NOTMUCH_STATUS_SUCCESS;
136 }
137 
138 notmuch_status_t
notmuch_database_get_config_list(notmuch_database_t * notmuch,const char * prefix,notmuch_config_list_t ** out)139 notmuch_database_get_config_list (notmuch_database_t *notmuch,
140 				  const char *prefix,
141 				  notmuch_config_list_t **out)
142 {
143     notmuch_config_list_t *list = NULL;
144     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
145 
146     list = talloc (notmuch, notmuch_config_list_t);
147     if (! list) {
148 	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
149 	goto DONE;
150     }
151 
152     list->notmuch = notmuch;
153     list->current_key = NULL;
154     list->current_val = NULL;
155 
156     try {
157 
158 	new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
159 							 (CONFIG_PREFIX + (prefix ? prefix : "")));
160 	talloc_set_destructor (list, _notmuch_config_list_destroy);
161 
162     } catch (const Xapian::Error &error) {
163 	_notmuch_database_log (notmuch,
164 			       "A Xapian exception occurred getting metadata iterator: %s.\n",
165 			       error.get_msg ().c_str ());
166 	notmuch->exception_reported = true;
167 	status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
168     }
169 
170     *out = list;
171 
172   DONE:
173     if (status) {
174 	if (list) {
175 	    talloc_free (list);
176 	    if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
177 		_notmuch_config_list_destroy (list);
178 	}
179     } else {
180 	talloc_set_destructor (list, _notmuch_config_list_destroy);
181     }
182 
183     return status;
184 }
185 
186 notmuch_bool_t
notmuch_config_list_valid(notmuch_config_list_t * metadata)187 notmuch_config_list_valid (notmuch_config_list_t *metadata)
188 {
189     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
190 	return false;
191 
192     return true;
193 }
194 
195 static inline char *
_key_from_iterator(notmuch_config_list_t * list)196 _key_from_iterator (notmuch_config_list_t *list)
197 {
198     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
199 }
200 
201 const char *
notmuch_config_list_key(notmuch_config_list_t * list)202 notmuch_config_list_key (notmuch_config_list_t *list)
203 {
204     if (list->current_key)
205 	talloc_free (list->current_key);
206 
207     list->current_key = _key_from_iterator (list);
208 
209     return list->current_key;
210 }
211 
212 const char *
notmuch_config_list_value(notmuch_config_list_t * list)213 notmuch_config_list_value (notmuch_config_list_t *list)
214 {
215     std::string strval;
216     notmuch_status_t status;
217     char *key = _key_from_iterator (list);
218 
219     /* TODO: better error reporting?? */
220     status = _metadata_value (list->notmuch, key, strval);
221     if (status)
222 	return NULL;
223 
224     if (list->current_val)
225 	talloc_free (list->current_val);
226 
227     list->current_val = talloc_strdup (list, strval.c_str ());
228     talloc_free (key);
229     return list->current_val;
230 }
231 
232 void
notmuch_config_list_move_to_next(notmuch_config_list_t * list)233 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
234 {
235     list->iterator++;
236 }
237 
238 void
notmuch_config_list_destroy(notmuch_config_list_t * list)239 notmuch_config_list_destroy (notmuch_config_list_t *list)
240 {
241     talloc_free (list);
242 }
243 
244 notmuch_status_t
_notmuch_config_load_from_database(notmuch_database_t * notmuch)245 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
246 {
247     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
248     notmuch_config_list_t *list;
249 
250     if (notmuch->config == NULL)
251 	notmuch->config = _notmuch_string_map_create (notmuch);
252 
253     if (unlikely (notmuch->config == NULL))
254 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
255 
256     status = notmuch_database_get_config_list (notmuch, "", &list);
257     if (status)
258 	return status;
259 
260     for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
261 	const char *key = notmuch_config_list_key (list);
262 	char *normalized_val = NULL;
263 
264 	/* If we opened from a given path, do not overwrite it */
265 	if (strcmp (key, "database.path") == 0 &&
266 	    (notmuch->params & NOTMUCH_PARAM_DATABASE) &&
267 	    notmuch->xapian_db)
268 	    continue;
269 
270 	normalized_val = _expand_path (list, key, notmuch_config_list_value (list));
271 	_notmuch_string_map_append (notmuch->config, key, normalized_val);
272 	talloc_free (normalized_val);
273     }
274 
275     return status;
276 }
277 
278 notmuch_config_values_t *
notmuch_config_get_values(notmuch_database_t * notmuch,notmuch_config_key_t key)279 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
280 {
281     const char *key_str = _notmuch_config_key_to_string (key);
282 
283     if (! key_str)
284 	return NULL;
285 
286     return notmuch_config_get_values_string (notmuch, key_str);
287 }
288 
289 notmuch_config_values_t *
notmuch_config_get_values_string(notmuch_database_t * notmuch,const char * key_str)290 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
291 {
292     notmuch_config_values_t *values = NULL;
293     bool ok = false;
294 
295     values = talloc (notmuch, notmuch_config_values_t);
296     if (unlikely (! values))
297 	goto DONE;
298 
299     values->children = talloc_new (values);
300 
301     values->string = _notmuch_string_map_get (notmuch->config, key_str);
302     if (! values->string)
303 	goto DONE;
304 
305     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
306     ok = true;
307 
308   DONE:
309     if (! ok) {
310 	if (values)
311 	    talloc_free (values);
312 	return NULL;
313     }
314     return values;
315 }
316 
317 notmuch_bool_t
notmuch_config_values_valid(notmuch_config_values_t * values)318 notmuch_config_values_valid (notmuch_config_values_t *values)
319 {
320     if (! values)
321 	return false;
322 
323     return (values->iterator != NULL);
324 }
325 
326 const char *
notmuch_config_values_get(notmuch_config_values_t * values)327 notmuch_config_values_get (notmuch_config_values_t *values)
328 {
329     return talloc_strndup (values->children, values->iterator, values->tok_len);
330 }
331 
332 void
notmuch_config_values_start(notmuch_config_values_t * values)333 notmuch_config_values_start (notmuch_config_values_t *values)
334 {
335     if (values == NULL)
336 	return;
337     if (values->children) {
338 	talloc_free (values->children);
339     }
340 
341     values->children = talloc_new (values);
342 
343     values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
344 }
345 
346 void
notmuch_config_values_move_to_next(notmuch_config_values_t * values)347 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
348 {
349     values->iterator += values->tok_len;
350     values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
351 }
352 
353 void
notmuch_config_values_destroy(notmuch_config_values_t * values)354 notmuch_config_values_destroy (notmuch_config_values_t *values)
355 {
356     talloc_free (values);
357 }
358 
359 notmuch_config_pairs_t *
notmuch_config_get_pairs(notmuch_database_t * notmuch,const char * prefix)360 notmuch_config_get_pairs (notmuch_database_t *notmuch,
361 			  const char *prefix)
362 {
363     notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
364 
365     pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
366     return pairs;
367 }
368 
369 notmuch_bool_t
notmuch_config_pairs_valid(notmuch_config_pairs_t * pairs)370 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
371 {
372     return _notmuch_string_map_iterator_valid (pairs->iter);
373 }
374 
375 void
notmuch_config_pairs_move_to_next(notmuch_config_pairs_t * pairs)376 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
377 {
378     _notmuch_string_map_iterator_move_to_next (pairs->iter);
379 }
380 
381 const char *
notmuch_config_pairs_key(notmuch_config_pairs_t * pairs)382 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
383 {
384     return _notmuch_string_map_iterator_key (pairs->iter);
385 }
386 
387 const char *
notmuch_config_pairs_value(notmuch_config_pairs_t * pairs)388 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
389 {
390     return _notmuch_string_map_iterator_value (pairs->iter);
391 }
392 
393 void
notmuch_config_pairs_destroy(notmuch_config_pairs_t * pairs)394 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
395 {
396     _notmuch_string_map_iterator_destroy (pairs->iter);
397     talloc_free (pairs);
398 }
399 
400 static char *
_expand_path(void * ctx,const char * key,const char * val)401 _expand_path (void *ctx, const char *key, const char *val)
402 {
403     char *expanded_val;
404 
405     if ((strcmp (key, "database.path") == 0 ||
406 	 strcmp (key, "database.mail_root") == 0 ||
407 	 strcmp (key, "database.hook_dir") == 0 ||
408 	 strcmp (key, "database.backup_path") == 0 ) &&
409 	val[0] != '/')
410 	expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
411     else
412 	expanded_val = talloc_strdup (ctx, val);
413 
414     return expanded_val;
415 }
416 
417 notmuch_status_t
_notmuch_config_load_from_file(notmuch_database_t * notmuch,GKeyFile * file)418 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
419 				GKeyFile *file)
420 {
421     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
422     gchar **groups = NULL, **keys, *val;
423 
424     if (notmuch->config == NULL)
425 	notmuch->config = _notmuch_string_map_create (notmuch);
426 
427     if (unlikely (notmuch->config == NULL)) {
428 	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
429 	goto DONE;
430     }
431 
432     groups = g_key_file_get_groups (file, NULL);
433     for (gchar **grp = groups; *grp; grp++) {
434 	keys = g_key_file_get_keys (file, *grp, NULL, NULL);
435 	for (gchar **keys_p = keys; *keys_p; keys_p++) {
436 	    char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp,  *keys_p);
437 	    char *normalized_val;
438 	    val = g_key_file_get_value (file, *grp, *keys_p, NULL);
439 	    if (! val) {
440 		status = NOTMUCH_STATUS_FILE_ERROR;
441 		goto DONE;
442 	    }
443 
444 	    /* If we opened from a given path, do not overwrite it */
445 	    if (strcmp (absolute_key, "database.path") == 0 &&
446 		(notmuch->params & NOTMUCH_PARAM_DATABASE) &&
447 		notmuch->xapian_db)
448 		continue;
449 
450 	    normalized_val = _expand_path (notmuch, absolute_key, val);
451 	    _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
452 	    g_free (val);
453 	    talloc_free (absolute_key);
454 	    talloc_free (normalized_val);
455 	    if (status)
456 		goto DONE;
457 	}
458 	g_strfreev (keys);
459     }
460 
461   DONE:
462     if (groups)
463 	g_strfreev (groups);
464 
465     return status;
466 }
467 
468 notmuch_status_t
notmuch_config_get_bool(notmuch_database_t * notmuch,notmuch_config_key_t key,notmuch_bool_t * val)469 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
470 {
471     const char *key_string, *val_string;
472 
473     key_string = _notmuch_config_key_to_string (key);
474     if (! key_string) {
475 	return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
476     }
477 
478     val_string = _notmuch_string_map_get (notmuch->config, key_string);
479     if (! val_string) {
480 	*val = FALSE;
481 	return NOTMUCH_STATUS_SUCCESS;
482     }
483 
484     if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
485 	*val = FALSE;
486     else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
487 	*val = TRUE;
488     else
489 	return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
490 
491     return NOTMUCH_STATUS_SUCCESS;
492 }
493 
494 static const char *
_get_name_from_passwd_file(void * ctx)495 _get_name_from_passwd_file (void *ctx)
496 {
497     long pw_buf_size;
498     char *pw_buf;
499     struct passwd passwd, *ignored;
500     const char *name;
501     int e;
502 
503     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
504     if (pw_buf_size == -1) pw_buf_size = 64;
505     pw_buf = (char *) talloc_size (ctx, pw_buf_size);
506 
507     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
508 			    pw_buf_size, &ignored)) == ERANGE) {
509 	pw_buf_size = pw_buf_size * 2;
510 	pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
511     }
512 
513     if (e == 0) {
514 	char *comma = strchr (passwd.pw_gecos, ',');
515 	if (comma)
516 	    name = talloc_strndup (ctx, passwd.pw_gecos,
517 				   comma - passwd.pw_gecos);
518 	else
519 	    name = talloc_strdup (ctx, passwd.pw_gecos);
520     } else {
521 	name = talloc_strdup (ctx, "");
522     }
523 
524     talloc_free (pw_buf);
525 
526     return name;
527 }
528 
529 static char *
_get_username_from_passwd_file(void * ctx)530 _get_username_from_passwd_file (void *ctx)
531 {
532     long pw_buf_size;
533     char *pw_buf;
534     struct passwd passwd, *ignored;
535     char *name;
536     int e;
537 
538     pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
539     if (pw_buf_size == -1) pw_buf_size = 64;
540     pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
541 
542     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
543 			    pw_buf_size, &ignored)) == ERANGE) {
544 	pw_buf_size = pw_buf_size * 2;
545 	pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
546     }
547 
548     if (e == 0)
549 	name = talloc_strdup (ctx, passwd.pw_name);
550     else
551 	name = talloc_strdup (ctx, "");
552 
553     talloc_free (pw_buf);
554 
555     return name;
556 }
557 
558 static const char *
_get_email_from_passwd_file(void * ctx)559 _get_email_from_passwd_file (void *ctx)
560 {
561     char *email;
562 
563     char *username = _get_username_from_passwd_file (ctx);
564 
565     email = talloc_asprintf (ctx, "%s@localhost", username);
566 
567     talloc_free (username);
568     return email;
569 }
570 
571 static const char *
_notmuch_config_key_to_string(notmuch_config_key_t key)572 _notmuch_config_key_to_string (notmuch_config_key_t key)
573 {
574     switch (key) {
575     case NOTMUCH_CONFIG_DATABASE_PATH:
576 	return "database.path";
577     case NOTMUCH_CONFIG_MAIL_ROOT:
578 	return "database.mail_root";
579     case NOTMUCH_CONFIG_HOOK_DIR:
580 	return "database.hook_dir";
581     case NOTMUCH_CONFIG_BACKUP_DIR:
582 	return "database.backup_dir";
583     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
584 	return "search.exclude_tags";
585     case NOTMUCH_CONFIG_NEW_TAGS:
586 	return "new.tags";
587     case NOTMUCH_CONFIG_NEW_IGNORE:
588 	return "new.ignore";
589     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
590 	return "maildir.synchronize_flags";
591     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
592 	return "user.primary_email";
593     case NOTMUCH_CONFIG_OTHER_EMAIL:
594 	return "user.other_email";
595     case NOTMUCH_CONFIG_USER_NAME:
596 	return "user.name";
597     case NOTMUCH_CONFIG_AUTOCOMMIT:
598 	return "database.autocommit";
599     default:
600 	return NULL;
601     }
602 }
603 
604 static const char *
_notmuch_config_default(notmuch_database_t * notmuch,notmuch_config_key_t key)605 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
606 {
607     char *path;
608     const char *name, *email;
609 
610     switch (key) {
611     case NOTMUCH_CONFIG_DATABASE_PATH:
612 	path = getenv ("MAILDIR");
613 	if (path)
614 	    path = talloc_strdup (notmuch, path);
615 	else
616 	    path = talloc_asprintf (notmuch, "%s/mail",
617 				    getenv ("HOME"));
618 	return path;
619     case NOTMUCH_CONFIG_MAIL_ROOT:
620 	/* by default, mail root is the same as database path */
621 	return notmuch_database_get_path (notmuch);
622     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
623 	return "";
624     case NOTMUCH_CONFIG_NEW_TAGS:
625 	return "unread;inbox";
626     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
627 	return "true";
628     case NOTMUCH_CONFIG_USER_NAME:
629 	name = getenv ("NAME");
630 	if (name)
631 	    name = talloc_strdup (notmuch, name);
632 	else
633 	    name = _get_name_from_passwd_file (notmuch);
634 	return name;
635     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
636 	email = getenv ("EMAIL");
637 	if (email)
638 	    email = talloc_strdup (notmuch, email);
639 	else
640 	    email = _get_email_from_passwd_file (notmuch);
641 	return email;
642     case NOTMUCH_CONFIG_NEW_IGNORE:
643 	return "";
644     case NOTMUCH_CONFIG_AUTOCOMMIT:
645 	return "8000";
646     case NOTMUCH_CONFIG_HOOK_DIR:
647     case NOTMUCH_CONFIG_BACKUP_DIR:
648     case NOTMUCH_CONFIG_OTHER_EMAIL:
649 	return NULL;
650     default:
651     case NOTMUCH_CONFIG_LAST:
652 	INTERNAL_ERROR ("illegal key enum %d", key);
653     }
654 }
655 
656 notmuch_status_t
_notmuch_config_load_defaults(notmuch_database_t * notmuch)657 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
658 {
659     notmuch_config_key_t key;
660 
661     for (key = NOTMUCH_CONFIG_FIRST;
662 	 key < NOTMUCH_CONFIG_LAST;
663 	 key = notmuch_config_key_t (key + 1)) {
664 	const char *val = notmuch_config_get (notmuch, key);
665 	const char *key_string = _notmuch_config_key_to_string (key);
666 
667 	val = _notmuch_string_map_get (notmuch->config, key_string);
668 	if (! val) {
669 	    _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
670 											   key));
671 	}
672     }
673     return NOTMUCH_STATUS_SUCCESS;
674 }
675 
676 const char *
notmuch_config_get(notmuch_database_t * notmuch,notmuch_config_key_t key)677 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
678 {
679 
680     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
681 }
682 
683 const char *
notmuch_config_path(notmuch_database_t * notmuch)684 notmuch_config_path (notmuch_database_t *notmuch)
685 {
686     return notmuch->config_path;
687 }
688 
689 notmuch_status_t
notmuch_config_set(notmuch_database_t * notmuch,notmuch_config_key_t key,const char * val)690 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
691 {
692 
693     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
694 }
695 
696 void
_notmuch_config_cache(notmuch_database_t * notmuch,notmuch_config_key_t key,const char * val)697 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
698 {
699     if (notmuch->config == NULL)
700 	notmuch->config = _notmuch_string_map_create (notmuch);
701 
702     _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);
703 }
704