]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
lib: replace use of static_cast for writable databases
[notmuch] / lib / config.cc
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 static const std::string CONFIG_PREFIX = "C";
26
27 struct _notmuch_config_list {
28     notmuch_database_t *notmuch;
29     Xapian::TermIterator iterator;
30     char *current_key;
31     char *current_val;
32 };
33
34 static int
35 _notmuch_config_list_destroy (notmuch_config_list_t *list)
36 {
37     /* invoke destructor w/o deallocating memory */
38     list->iterator.~TermIterator();
39     return 0;
40 }
41
42 notmuch_status_t
43 notmuch_database_set_config (notmuch_database_t *notmuch,
44                              const char *key,
45                              const char *value)
46 {
47     notmuch_status_t status;
48
49     status = _notmuch_database_ensure_writable (notmuch);
50     if (status)
51         return status;
52
53     try {
54         notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
55     } catch (const Xapian::Error &error) {
56         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
57         notmuch->exception_reported = true;
58         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
59                                error.get_msg ().c_str ());
60     }
61     return status;
62 }
63
64 static notmuch_status_t
65 _metadata_value (notmuch_database_t *notmuch,
66                  const char *key,
67                  std::string &value)
68 {
69     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
70
71     try {
72         value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
73     } catch (const Xapian::Error &error) {
74         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
75         notmuch->exception_reported = true;
76         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
77                                error.get_msg ().c_str ());
78     }
79     return status;
80 }
81
82 notmuch_status_t
83 notmuch_database_get_config (notmuch_database_t *notmuch,
84                              const char *key,
85                              char **value)
86 {
87     std::string strval;
88     notmuch_status_t status;
89
90     if (! value)
91         return NOTMUCH_STATUS_NULL_POINTER;
92
93     status = _metadata_value (notmuch, key, strval);
94     if (status)
95         return status;
96
97     *value = strdup (strval.c_str ());
98
99     return NOTMUCH_STATUS_SUCCESS;
100 }
101
102 notmuch_status_t
103 notmuch_database_get_config_list (notmuch_database_t *notmuch,
104                                   const char *prefix,
105                                   notmuch_config_list_t **out)
106 {
107     notmuch_config_list_t *list = NULL;
108     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
109
110     list = talloc (notmuch, notmuch_config_list_t);
111     if (! list) {
112         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
113         goto DONE;
114     }
115
116     talloc_set_destructor (list, _notmuch_config_list_destroy);
117     list->notmuch = notmuch;
118     list->current_key = NULL;
119     list->current_val = NULL;
120
121     try {
122
123         new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
124                                                          (CONFIG_PREFIX + (prefix ? prefix : "")));
125
126     } catch (const Xapian::Error &error) {
127         _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
128                                error.get_msg ().c_str ());
129         notmuch->exception_reported = true;
130         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
131     }
132
133     *out = list;
134
135   DONE:
136     if (status && list)
137         talloc_free (list);
138
139     return status;
140 }
141
142 notmuch_bool_t
143 notmuch_config_list_valid (notmuch_config_list_t *metadata)
144 {
145     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
146         return false;
147
148     return true;
149 }
150
151 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
152     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
153 }
154
155 const char *
156 notmuch_config_list_key (notmuch_config_list_t *list)
157 {
158     if (list->current_key)
159         talloc_free (list->current_key);
160
161     list->current_key = _key_from_iterator (list);
162
163     return list->current_key;
164 }
165
166 const char *
167 notmuch_config_list_value (notmuch_config_list_t *list)
168 {
169     std::string strval;
170     notmuch_status_t status;
171     char *key = _key_from_iterator (list);
172
173     /* TODO: better error reporting?? */
174     status = _metadata_value (list->notmuch, key, strval);
175     if (status)
176         return NULL;
177
178     if (list->current_val)
179         talloc_free (list->current_val);
180
181     list->current_val = talloc_strdup (list, strval.c_str ());
182     talloc_free (key);
183     return list->current_val;
184 }
185
186 void
187 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
188 {
189     list->iterator++;
190 }
191
192 void
193 notmuch_config_list_destroy (notmuch_config_list_t *list)
194 {
195     talloc_free (list);
196 }