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;
35 _notmuch_config_list_destroy (notmuch_config_list_t *list)
37 /* invoke destructor w/o deallocating memory */
38 list->iterator.~TermIterator();
43 notmuch_database_set_config (notmuch_database_t *notmuch,
47 notmuch_status_t status;
49 status = _notmuch_database_ensure_writable (notmuch);
53 if (! notmuch->config) {
54 if ((status = _notmuch_config_load_from_database (notmuch)))
59 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
60 } catch (const Xapian::Error &error) {
61 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
62 notmuch->exception_reported = true;
63 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
64 error.get_msg ().c_str ());
70 _notmuch_string_map_set (notmuch->config, key, value);
72 return NOTMUCH_STATUS_SUCCESS;
75 static notmuch_status_t
76 _metadata_value (notmuch_database_t *notmuch,
80 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
83 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
84 } catch (const Xapian::Error &error) {
85 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
86 notmuch->exception_reported = true;
87 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
88 error.get_msg ().c_str ());
94 notmuch_database_get_config (notmuch_database_t *notmuch,
98 const char* stored_val;
99 notmuch_status_t status;
101 if (! notmuch->config) {
102 if ((status = _notmuch_config_load_from_database (notmuch)))
107 return NOTMUCH_STATUS_NULL_POINTER;
109 stored_val = _notmuch_string_map_get (notmuch->config, key);
111 /* XXX in principle this API should be fixed so empty string
112 * is distinguished from not found */
115 *value = strdup (stored_val);
118 return NOTMUCH_STATUS_SUCCESS;
122 notmuch_database_get_config_list (notmuch_database_t *notmuch,
124 notmuch_config_list_t **out)
126 notmuch_config_list_t *list = NULL;
127 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
129 list = talloc (notmuch, notmuch_config_list_t);
131 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
135 list->notmuch = notmuch;
136 list->current_key = NULL;
137 list->current_val = NULL;
141 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
142 (CONFIG_PREFIX + (prefix ? prefix : "")));
143 talloc_set_destructor (list, _notmuch_config_list_destroy);
145 } catch (const Xapian::Error &error) {
146 _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
147 error.get_msg ().c_str ());
148 notmuch->exception_reported = true;
149 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
158 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
159 _notmuch_config_list_destroy (list);
162 talloc_set_destructor (list, _notmuch_config_list_destroy);
169 notmuch_config_list_valid (notmuch_config_list_t *metadata)
171 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
177 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
178 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
182 notmuch_config_list_key (notmuch_config_list_t *list)
184 if (list->current_key)
185 talloc_free (list->current_key);
187 list->current_key = _key_from_iterator (list);
189 return list->current_key;
193 notmuch_config_list_value (notmuch_config_list_t *list)
196 notmuch_status_t status;
197 char *key = _key_from_iterator (list);
199 /* TODO: better error reporting?? */
200 status = _metadata_value (list->notmuch, key, strval);
204 if (list->current_val)
205 talloc_free (list->current_val);
207 list->current_val = talloc_strdup (list, strval.c_str ());
209 return list->current_val;
213 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
219 notmuch_config_list_destroy (notmuch_config_list_t *list)
225 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
227 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
228 notmuch_config_list_t *list;
230 if (notmuch->config == NULL)
231 notmuch->config = _notmuch_string_map_create (notmuch);
233 if (unlikely(notmuch->config == NULL))
234 return NOTMUCH_STATUS_OUT_OF_MEMORY;
236 status = notmuch_database_get_config_list (notmuch, "", &list);
240 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
241 _notmuch_string_map_append (notmuch->config,
242 notmuch_config_list_key (list),
243 notmuch_config_list_value (list));
250 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
253 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
254 gchar **groups,**keys, *val;
256 if (notmuch->config == NULL)
257 notmuch->config = _notmuch_string_map_create (notmuch);
259 if (unlikely(notmuch->config == NULL)) {
260 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
264 for (groups = g_key_file_get_groups (file, NULL); *groups; groups++) {
265 for (keys = g_key_file_get_keys (file, *groups, NULL, NULL); *keys; keys++) {
266 char *absolute_key = talloc_asprintf(notmuch, "%s.%s", *groups, *keys);
267 val = g_key_file_get_value (file, *groups, *keys, NULL);
269 status = NOTMUCH_STATUS_FILE_ERROR;
272 _notmuch_string_map_set (notmuch->config, absolute_key, val);
273 talloc_free (absolute_key);