1 /* config.cc - API for database metadata
3 * Copyright © 2016 David Bremner
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.
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.
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/ .
18 * Author: David Bremner <david@tethera.net>
22 #include "notmuch-private.h"
23 #include "database-private.h"
28 static const std::string CONFIG_PREFIX = "C";
30 struct _notmuch_config_list {
31 notmuch_database_t *notmuch;
32 Xapian::TermIterator iterator;
37 struct _notmuch_config_values {
41 void *children; /* talloc_context */
44 struct _notmuch_config_pairs {
45 notmuch_string_map_iterator_t *iter;
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);
52 _notmuch_config_list_destroy (notmuch_config_list_t *list)
54 /* invoke destructor w/o deallocating memory */
55 list->iterator.~TermIterator();
60 notmuch_database_set_config (notmuch_database_t *notmuch,
64 notmuch_status_t status;
66 status = _notmuch_database_ensure_writable (notmuch);
70 if (! notmuch->config) {
71 if ((status = _notmuch_config_load_from_database (notmuch)))
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 ());
87 _notmuch_string_map_set (notmuch->config, key, value);
89 return NOTMUCH_STATUS_SUCCESS;
92 static notmuch_status_t
93 _metadata_value (notmuch_database_t *notmuch,
97 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
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 ());
111 notmuch_database_get_config (notmuch_database_t *notmuch,
115 const char *stored_val;
116 notmuch_status_t status;
118 if (! notmuch->config) {
119 if ((status = _notmuch_config_load_from_database (notmuch)))
124 return NOTMUCH_STATUS_NULL_POINTER;
126 stored_val = _notmuch_string_map_get (notmuch->config, key);
128 /* XXX in principle this API should be fixed so empty string
129 * is distinguished from not found */
130 *value = strdup ("");
132 *value = strdup (stored_val);
135 return NOTMUCH_STATUS_SUCCESS;
139 notmuch_database_get_config_list (notmuch_database_t *notmuch,
141 notmuch_config_list_t **out)
143 notmuch_config_list_t *list = NULL;
144 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
146 list = talloc (notmuch, notmuch_config_list_t);
148 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
152 list->notmuch = notmuch;
153 list->current_key = NULL;
154 list->current_val = NULL;
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);
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;
176 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
177 _notmuch_config_list_destroy (list);
180 talloc_set_destructor (list, _notmuch_config_list_destroy);
187 notmuch_config_list_valid (notmuch_config_list_t *metadata)
189 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
196 _key_from_iterator (notmuch_config_list_t *list)
198 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
202 notmuch_config_list_key (notmuch_config_list_t *list)
204 if (list->current_key)
205 talloc_free (list->current_key);
207 list->current_key = _key_from_iterator (list);
209 return list->current_key;
213 notmuch_config_list_value (notmuch_config_list_t *list)
216 notmuch_status_t status;
217 char *key = _key_from_iterator (list);
219 /* TODO: better error reporting?? */
220 status = _metadata_value (list->notmuch, key, strval);
224 if (list->current_val)
225 talloc_free (list->current_val);
227 list->current_val = talloc_strdup (list, strval.c_str ());
229 return list->current_val;
233 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
239 notmuch_config_list_destroy (notmuch_config_list_t *list)
245 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
247 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
248 notmuch_config_list_t *list;
250 if (notmuch->config == NULL)
251 notmuch->config = _notmuch_string_map_create (notmuch);
253 if (unlikely (notmuch->config == NULL))
254 return NOTMUCH_STATUS_OUT_OF_MEMORY;
256 status = notmuch_database_get_config_list (notmuch, "", &list);
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;
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) &&
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);
278 notmuch_config_values_t *
279 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
281 const char *key_str = _notmuch_config_key_to_string (key);
286 return notmuch_config_get_values_string (notmuch, key_str);
289 notmuch_config_values_t *
290 notmuch_config_get_values_string (notmuch_database_t *notmuch, const char *key_str)
292 notmuch_config_values_t *values = NULL;
295 values = talloc (notmuch, notmuch_config_values_t);
296 if (unlikely (! values))
299 values->children = talloc_new (values);
301 values->string = _notmuch_string_map_get (notmuch->config, key_str);
302 if (! values->string)
305 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
311 talloc_free (values);
318 notmuch_config_values_valid (notmuch_config_values_t *values)
323 return (values->iterator != NULL);
327 notmuch_config_values_get (notmuch_config_values_t *values)
329 return talloc_strndup (values->children, values->iterator, values->tok_len);
333 notmuch_config_values_start (notmuch_config_values_t *values)
337 if (values->children) {
338 talloc_free (values->children);
341 values->children = talloc_new (values);
343 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
347 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
349 values->iterator += values->tok_len;
350 values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
354 notmuch_config_values_destroy (notmuch_config_values_t *values)
356 talloc_free (values);
359 notmuch_config_pairs_t *
360 notmuch_config_get_pairs (notmuch_database_t *notmuch,
363 notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t);
365 pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false);
370 notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs)
372 return _notmuch_string_map_iterator_valid (pairs->iter);
376 notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs)
378 _notmuch_string_map_iterator_move_to_next (pairs->iter);
382 notmuch_config_pairs_key (notmuch_config_pairs_t *pairs)
384 return _notmuch_string_map_iterator_key (pairs->iter);
388 notmuch_config_pairs_value (notmuch_config_pairs_t *pairs)
390 return _notmuch_string_map_iterator_value (pairs->iter);
394 notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
396 _notmuch_string_map_iterator_destroy (pairs->iter);
401 _expand_path (void *ctx, const char *key, const char *val)
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 ) &&
410 expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
412 expanded_val = talloc_strdup (ctx, val);
418 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
421 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
422 gchar **groups = NULL, **keys, *val;
424 if (notmuch->config == NULL)
425 notmuch->config = _notmuch_string_map_create (notmuch);
427 if (unlikely (notmuch->config == NULL)) {
428 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
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);
440 status = NOTMUCH_STATUS_FILE_ERROR;
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) &&
450 normalized_val = _expand_path (notmuch, absolute_key, val);
451 _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
453 talloc_free (absolute_key);
454 talloc_free (normalized_val);
469 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
471 const char *key_string, *val_string;
473 key_string = _notmuch_config_key_to_string (key);
475 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
478 val_string = _notmuch_string_map_get (notmuch->config, key_string);
481 return NOTMUCH_STATUS_SUCCESS;
484 if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
486 else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
489 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
491 return NOTMUCH_STATUS_SUCCESS;
495 _get_name_from_passwd_file (void *ctx)
499 struct passwd passwd, *ignored;
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);
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);
514 char *comma = strchr (passwd.pw_gecos, ',');
516 name = talloc_strndup (ctx, passwd.pw_gecos,
517 comma - passwd.pw_gecos);
519 name = talloc_strdup (ctx, passwd.pw_gecos);
521 name = talloc_strdup (ctx, "");
524 talloc_free (pw_buf);
530 _get_username_from_passwd_file (void *ctx)
534 struct passwd passwd, *ignored;
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);
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);
549 name = talloc_strdup (ctx, passwd.pw_name);
551 name = talloc_strdup (ctx, "");
553 talloc_free (pw_buf);
559 _get_email_from_passwd_file (void *ctx)
563 char *username = _get_username_from_passwd_file (ctx);
565 email = talloc_asprintf (ctx, "%s@localhost", username);
567 talloc_free (username);
572 _notmuch_config_key_to_string (notmuch_config_key_t 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:
587 case NOTMUCH_CONFIG_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:
597 case NOTMUCH_CONFIG_AUTOCOMMIT:
598 return "database.autocommit";
605 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
608 const char *name, *email;
611 case NOTMUCH_CONFIG_DATABASE_PATH:
612 path = getenv ("MAILDIR");
614 path = talloc_strdup (notmuch, path);
616 path = talloc_asprintf (notmuch, "%s/mail",
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:
624 case NOTMUCH_CONFIG_NEW_TAGS:
625 return "unread;inbox";
626 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
628 case NOTMUCH_CONFIG_USER_NAME:
629 name = getenv ("NAME");
631 name = talloc_strdup (notmuch, name);
633 name = _get_name_from_passwd_file (notmuch);
635 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
636 email = getenv ("EMAIL");
638 email = talloc_strdup (notmuch, email);
640 email = _get_email_from_passwd_file (notmuch);
642 case NOTMUCH_CONFIG_NEW_IGNORE:
644 case NOTMUCH_CONFIG_AUTOCOMMIT:
646 case NOTMUCH_CONFIG_HOOK_DIR:
647 case NOTMUCH_CONFIG_BACKUP_DIR:
648 case NOTMUCH_CONFIG_OTHER_EMAIL:
651 case NOTMUCH_CONFIG_LAST:
652 INTERNAL_ERROR ("illegal key enum %d", key);
657 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
659 notmuch_config_key_t key;
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);
667 val = _notmuch_string_map_get (notmuch->config, key_string);
669 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
673 return NOTMUCH_STATUS_SUCCESS;
677 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
680 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
684 notmuch_config_path (notmuch_database_t *notmuch)
686 return notmuch->config_path;
690 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
693 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
697 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
699 if (notmuch->config == NULL)
700 notmuch->config = _notmuch_string_map_create (notmuch);
702 _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);