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"
25 static const std::string CONFIG_PREFIX = "C";
27 struct _notmuch_config_list {
28 notmuch_database_t *notmuch;
29 Xapian::TermIterator iterator;
34 struct _notmuch_config_values {
38 void *children; /* talloc_context */
41 static const char *_notmuch_config_key_to_string (notmuch_config_key_t key);
44 _notmuch_config_list_destroy (notmuch_config_list_t *list)
46 /* invoke destructor w/o deallocating memory */
47 list->iterator.~TermIterator();
52 notmuch_database_set_config (notmuch_database_t *notmuch,
56 notmuch_status_t status;
58 status = _notmuch_database_ensure_writable (notmuch);
62 if (! notmuch->config) {
63 if ((status = _notmuch_config_load_from_database (notmuch)))
68 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
69 } catch (const Xapian::Error &error) {
70 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
71 notmuch->exception_reported = true;
72 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
73 error.get_msg ().c_str ());
79 _notmuch_string_map_set (notmuch->config, key, value);
81 return NOTMUCH_STATUS_SUCCESS;
84 static notmuch_status_t
85 _metadata_value (notmuch_database_t *notmuch,
89 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
92 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
93 } catch (const Xapian::Error &error) {
94 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
95 notmuch->exception_reported = true;
96 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
97 error.get_msg ().c_str ());
103 notmuch_database_get_config (notmuch_database_t *notmuch,
107 const char *stored_val;
108 notmuch_status_t status;
110 if (! notmuch->config) {
111 if ((status = _notmuch_config_load_from_database (notmuch)))
116 return NOTMUCH_STATUS_NULL_POINTER;
118 stored_val = _notmuch_string_map_get (notmuch->config, key);
120 /* XXX in principle this API should be fixed so empty string
121 * is distinguished from not found */
122 *value = strdup ("");
124 *value = strdup (stored_val);
127 return NOTMUCH_STATUS_SUCCESS;
131 notmuch_database_get_config_list (notmuch_database_t *notmuch,
133 notmuch_config_list_t **out)
135 notmuch_config_list_t *list = NULL;
136 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
138 list = talloc (notmuch, notmuch_config_list_t);
140 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
144 list->notmuch = notmuch;
145 list->current_key = NULL;
146 list->current_val = NULL;
150 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
151 (CONFIG_PREFIX + (prefix ? prefix : "")));
152 talloc_set_destructor (list, _notmuch_config_list_destroy);
154 } catch (const Xapian::Error &error) {
155 _notmuch_database_log (notmuch,
156 "A Xapian exception occurred getting metadata iterator: %s.\n",
157 error.get_msg ().c_str ());
158 notmuch->exception_reported = true;
159 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
168 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
169 _notmuch_config_list_destroy (list);
172 talloc_set_destructor (list, _notmuch_config_list_destroy);
179 notmuch_config_list_valid (notmuch_config_list_t *metadata)
181 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
188 _key_from_iterator (notmuch_config_list_t *list)
190 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
194 notmuch_config_list_key (notmuch_config_list_t *list)
196 if (list->current_key)
197 talloc_free (list->current_key);
199 list->current_key = _key_from_iterator (list);
201 return list->current_key;
205 notmuch_config_list_value (notmuch_config_list_t *list)
208 notmuch_status_t status;
209 char *key = _key_from_iterator (list);
211 /* TODO: better error reporting?? */
212 status = _metadata_value (list->notmuch, key, strval);
216 if (list->current_val)
217 talloc_free (list->current_val);
219 list->current_val = talloc_strdup (list, strval.c_str ());
221 return list->current_val;
225 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
231 notmuch_config_list_destroy (notmuch_config_list_t *list)
237 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
239 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
240 notmuch_config_list_t *list;
242 if (notmuch->config == NULL)
243 notmuch->config = _notmuch_string_map_create (notmuch);
245 if (unlikely (notmuch->config == NULL))
246 return NOTMUCH_STATUS_OUT_OF_MEMORY;
248 status = notmuch_database_get_config_list (notmuch, "", &list);
252 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
253 _notmuch_string_map_append (notmuch->config,
254 notmuch_config_list_key (list),
255 notmuch_config_list_value (list));
261 notmuch_config_values_t *
262 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
264 notmuch_config_values_t *values = NULL;
267 const char *key_str = _notmuch_config_key_to_string (key);
272 values = talloc (notmuch, notmuch_config_values_t);
273 if (unlikely (! values))
276 values->children = talloc_new (values);
278 values->string = _notmuch_string_map_get (notmuch->config, key_str);
279 if (! values->string)
282 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
288 talloc_free (values);
295 notmuch_config_values_valid (notmuch_config_values_t *values)
300 return (values->iterator != NULL);
304 notmuch_config_values_get (notmuch_config_values_t *values)
306 return talloc_strndup (values, values->iterator, values->tok_len);
310 notmuch_config_values_start (notmuch_config_values_t *values)
314 if (values->children) {
315 talloc_free (values->children);
318 values->children = talloc_new (values);
320 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
324 notmuch_config_values_move_to_next (notmuch_config_values_t *values)
326 values->iterator += values->tok_len;
327 values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
331 notmuch_config_values_destroy (notmuch_config_values_t *values)
333 talloc_free (values);
337 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
340 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
341 gchar **groups = NULL, **keys, *val;
343 if (notmuch->config == NULL)
344 notmuch->config = _notmuch_string_map_create (notmuch);
346 if (unlikely (notmuch->config == NULL)) {
347 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
351 groups = g_key_file_get_groups (file, NULL);
352 for (gchar **grp = groups; *grp; grp++) {
353 keys = g_key_file_get_keys (file, *grp, NULL, NULL);
354 for (gchar **keys_p = keys; *keys_p; keys_p++) {
355 char *absolute_key = talloc_asprintf (notmuch, "%s.%s", *grp, *keys_p);
356 val = g_key_file_get_value (file, *grp, *keys_p, NULL);
358 status = NOTMUCH_STATUS_FILE_ERROR;
361 _notmuch_string_map_set (notmuch->config, absolute_key, val);
363 talloc_free (absolute_key);
378 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
380 const char *key_string, *val_string;
382 key_string = _notmuch_config_key_to_string (key);
384 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
387 val_string = _notmuch_string_map_get (notmuch->config, key_string);
390 return NOTMUCH_STATUS_SUCCESS;
393 if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
395 else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
398 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
400 return NOTMUCH_STATUS_SUCCESS;
404 _notmuch_config_key_to_string (notmuch_config_key_t key)
407 case NOTMUCH_CONFIG_DATABASE_PATH:
408 return "database.path";
409 case NOTMUCH_CONFIG_MAIL_ROOT:
410 return "database.mail_root";
411 case NOTMUCH_CONFIG_HOOK_DIR:
412 return "database.hook_dir";
413 case NOTMUCH_CONFIG_BACKUP_DIR:
414 return "database.backup_dir";
415 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
416 return "search.exclude_tags";
417 case NOTMUCH_CONFIG_NEW_TAGS:
419 case NOTMUCH_CONFIG_NEW_IGNORE:
421 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
422 return "maildir.synchronize_flags";
423 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
424 return "user.primary_email";
425 case NOTMUCH_CONFIG_OTHER_EMAIL:
426 return "user.other_email";
427 case NOTMUCH_CONFIG_USER_NAME:
435 _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
440 case NOTMUCH_CONFIG_DATABASE_PATH:
441 path = getenv ("MAILDIR");
443 path = talloc_strdup (notmuch, path);
445 path = talloc_asprintf (notmuch, "%s/mail",
448 case NOTMUCH_CONFIG_MAIL_ROOT:
449 /* by default, mail root is the same as database path */
450 return notmuch_database_get_path (notmuch);
451 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
453 case NOTMUCH_CONFIG_NEW_TAGS:
454 return "inbox;unread";
455 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
457 case NOTMUCH_CONFIG_HOOK_DIR:
458 case NOTMUCH_CONFIG_BACKUP_DIR:
459 case NOTMUCH_CONFIG_NEW_IGNORE:
460 case NOTMUCH_CONFIG_USER_NAME:
461 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
462 case NOTMUCH_CONFIG_OTHER_EMAIL:
465 case NOTMUCH_CONFIG_LAST:
466 INTERNAL_ERROR ("illegal key enum %d", key);
471 _notmuch_config_load_defaults (notmuch_database_t *notmuch)
473 notmuch_config_key_t key;
475 for (key = NOTMUCH_CONFIG_FIRST;
476 key < NOTMUCH_CONFIG_LAST;
477 key = notmuch_config_key_t (key + 1)) {
478 const char *val = notmuch_config_get (notmuch, key);
479 const char *key_string = _notmuch_config_key_to_string (key);
481 val = _notmuch_string_map_get (notmuch->config, key_string);
483 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch,
487 return NOTMUCH_STATUS_SUCCESS;
491 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key)
494 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
498 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
501 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
505 _notmuch_config_cache (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val)
507 if (notmuch->config == NULL)
508 notmuch->config = _notmuch_string_map_create (notmuch);
510 _notmuch_string_map_set (notmuch->config, _notmuch_config_key_to_string (key), val);