]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
debian: drop patches
[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     list->notmuch = notmuch;
117     list->current_key = NULL;
118     list->current_val = NULL;
119
120     try {
121
122         new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
123                                                          (CONFIG_PREFIX + (prefix ? prefix : "")));
124         talloc_set_destructor (list, _notmuch_config_list_destroy);
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) {
137         if (list) {
138             talloc_free (list);
139             if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
140                 _notmuch_config_list_destroy (list);
141         }
142     }  else {
143         talloc_set_destructor (list, _notmuch_config_list_destroy);
144     }
145
146     return status;
147 }
148
149 notmuch_bool_t
150 notmuch_config_list_valid (notmuch_config_list_t *metadata)
151 {
152     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
153         return false;
154
155     return true;
156 }
157
158 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
159     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
160 }
161
162 const char *
163 notmuch_config_list_key (notmuch_config_list_t *list)
164 {
165     if (list->current_key)
166         talloc_free (list->current_key);
167
168     list->current_key = _key_from_iterator (list);
169
170     return list->current_key;
171 }
172
173 const char *
174 notmuch_config_list_value (notmuch_config_list_t *list)
175 {
176     std::string strval;
177     notmuch_status_t status;
178     char *key = _key_from_iterator (list);
179
180     /* TODO: better error reporting?? */
181     status = _metadata_value (list->notmuch, key, strval);
182     if (status)
183         return NULL;
184
185     if (list->current_val)
186         talloc_free (list->current_val);
187
188     list->current_val = talloc_strdup (list, strval.c_str ());
189     talloc_free (key);
190     return list->current_val;
191 }
192
193 void
194 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
195 {
196     list->iterator++;
197 }
198
199 void
200 notmuch_config_list_destroy (notmuch_config_list_t *list)
201 {
202     talloc_free (list);
203 }