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;
439 /* If we opened from a given path, do not overwrite it */
440 if (strcmp (absolute_key, "database.path") == 0 &&
441 (notmuch->params & NOTMUCH_PARAM_DATABASE) &&
445 val = g_key_file_get_string (file, *grp, *keys_p, NULL);
447 status = NOTMUCH_STATUS_FILE_ERROR;
451 normalized_val = _expand_path (notmuch, absolute_key, val);
452 _notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
454 talloc_free (absolute_key);
455 talloc_free (normalized_val);
470 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
472 const char *key_string, *val_string;
474 key_string = _notmuch_config_key_to_string (key);
476 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
479 val_string = _notmuch_string_map_get (notmuch->config, key_string);
482 return NOTMUCH_STATUS_SUCCESS;
485 if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
487 else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
490 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
492 return NOTMUCH_STATUS_SUCCESS;
496 _get_name_from_passwd_file (void *ctx)
500 struct passwd passwd, *ignored;
504 pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
505 if (pw_buf_size == -1) pw_buf_size = 64;
506 pw_buf = (char *) talloc_size (ctx, pw_buf_size);
508 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
509 pw_buf_size, &ignored)) == ERANGE) {
510 pw_buf_size = pw_buf_size * 2;
511 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
515 char *comma = strchr (passwd.pw_gecos, ',');
517 name = talloc_strndup (ctx, passwd.pw_gecos,
518 comma - passwd.pw_gecos);
520 name = talloc_strdup (ctx, passwd.pw_gecos);
522 name = talloc_strdup (ctx, "");
525 talloc_free (pw_buf);
531 _get_username_from_passwd_file (void *ctx)
535 struct passwd passwd, *ignored;
539 pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
540 if (pw_buf_size == -1) pw_buf_size = 64;
541 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
543 while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
544 pw_buf_size, &ignored)) == ERANGE) {
545 pw_buf_size = pw_buf_size * 2;
546 pw_buf = (char *) talloc_zero_size (ctx, pw_buf_size);
550 name = talloc_strdup (ctx, passwd.pw_name);
552 name = talloc_strdup (ctx, "");
554 talloc_free (pw_buf);
560 _get_email_from_passwd_file (void *ctx)
564 char *username = _get_username_from_passwd_file (ctx);
566 email = talloc_asprintf (ctx, "%s@localhost", username);
568 talloc_free (username);
573 _notmuch_config_key_to_string (notmuch_config_key_t key)
576 case NOTMUCH_CONFIG_DATABASE_PATH:
577 return "database.path";
578 case NOTMUCH_CONFIG_MAIL_ROOT:
579 return "database.mail_root";
580 case NOTMUCH_CONFIG_HOOK_DIR:
581 return "database.hook_dir";
582 case NOTMUCH_CONFIG_BACKUP_DIR:
583 return "database.backup_dir";
584 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
585 return "search.exclude_tags";
586 case NOTMUCH_CONFIG_NEW_TAGS:
588 case NOTMUCH_CONFIG_NEW_IGNORE:
590 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
591 return "maildir.synchronize_flags";
592 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
593 return "user.primary_email";
594 case NOTMUCH_CONFIG_OTHER_EMAIL:
595 return "user.other_email";
596 case NOTMUCH_CONFIG_USER_NAME:
598 case NOTMUCH_CONFIG_AUTOCOMMIT:
599 return "database.autocommit";
600 case NOTMUCH_CONFIG_EXTRA_HEADERS:
601 return "show.extra_headers";
608 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
611 const char *name, *email;
614 case NOTMUCH_CONFIG_DATABASE_PATH:
615 path = getenv ("MAILDIR");
617 path = talloc_strdup (notmuch, path);
619 path = talloc_asprintf (notmuch, "%s/mail",
622 case NOTMUCH_CONFIG_MAIL_ROOT:
623 /* by default, mail root is the same as database path */
624 return notmuch_database_get_path (notmuch);
625 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
627 case NOTMUCH_CONFIG_NEW_TAGS:
628 return "unread;inbox";
629 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
631 case NOTMUCH_CONFIG_USER_NAME:
632 name = getenv ("NAME");
634 name = talloc_strdup (notmuch, name);
636 name = _get_name_from_passwd_file (notmuch);
638 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
639 email = getenv ("EMAIL");
641 email = talloc_strdup (notmuch, email);
643 email = _get_email_from_passwd_file (notmuch);
645 case NOTMUCH_CONFIG_NEW_IGNORE:
647 case NOTMUCH_CONFIG_AUTOCOMMIT:
649 case NOTMUCH_CONFIG_EXTRA_HEADERS:
650 case NOTMUCH_CONFIG_HOOK_DIR:
651 case NOTMUCH_CONFIG_BACKUP_DIR:
652 case NOTMUCH_CONFIG_OTHER_EMAIL:
655 case NOTMUCH_CONFIG_LAST:
656 INTERNAL_ERROR ("illegal key enum %d", key);
661 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
663 notmuch_config_key_t key;
664 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
666 if (notmuch->config == NULL)
667 notmuch->config = _notmuch_string_map_create (notmuch);
669 for (key = NOTMUCH_CONFIG_FIRST;
670 key < NOTMUCH_CONFIG_LAST;
671 key = notmuch_config_key_t (key + 1)) {
672 const char *val = notmuch_config_get (notmuch, key);
673 const char *key_string = _notmuch_config_key_to_string (key);
675 val = _notmuch_string_map_get (notmuch->config, key_string);
677 if (key == NOTMUCH_CONFIG_MAIL_ROOT && (notmuch->params & NOTMUCH_PARAM_SPLIT))
678 status = NOTMUCH_STATUS_NO_MAIL_ROOT;
680 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
688 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
691 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
695 notmuch_config_path (notmuch_database_t *notmuch)
697 return notmuch->config_path;
701 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
704 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
708 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
710 if (notmuch->config == NULL)
711 notmuch->config = _notmuch_string_map_create (notmuch);
713 _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);