]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
5b32f84a75ff4c316fa64e7b278dc973a4d4c851
[notmuch] / lib / notmuch-private.h
1 /* notmuch-private.h - Internal interfaces for notmuch.
2  *
3  * Copyright © 2009 Carl Worth
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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #ifndef NOTMUCH_PRIVATE_H
22 #define NOTMUCH_PRIVATE_H
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE /* For getline and asprintf */
26 #endif
27 #include <stdio.h>
28
29 #include "compat.h"
30
31 #include "notmuch.h"
32
33 NOTMUCH_BEGIN_DECLS
34
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <assert.h>
46
47 #include <talloc.h>
48
49 #include "xutil.h"
50
51 #pragma GCC visibility push(hidden)
52
53 #ifdef DEBUG
54 # define DEBUG_DATABASE_SANITY 1
55 # define DEBUG_QUERY 1
56 #endif
57
58 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
59
60 /* There's no point in continuing when we've detected that we've done
61  * something wrong internally (as opposed to the user passing in a
62  * bogus value).
63  *
64  * Note that PRINTF_ATTRIBUTE comes from talloc.h
65  */
66 int
67 _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);
68
69 /* There's no point in continuing when we've detected that we've done
70  * something wrong internally (as opposed to the user passing in a
71  * bogus value).
72  *
73  * Note that __location__ comes from talloc.h.
74  */
75 #define INTERNAL_ERROR(format, ...)                     \
76     _internal_error (format " (%s).\n",                 \
77                      ##__VA_ARGS__, __location__)
78
79 #define unused(x) x __attribute__ ((unused))
80
81 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
82  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
83  */
84 /* these macros gain us a few percent of speed on gcc */
85 #if (__GNUC__ >= 3)
86 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
87    as its first argument */
88 #ifndef likely
89 #define likely(x)   __builtin_expect(!!(x), 1)
90 #endif
91 #ifndef unlikely
92 #define unlikely(x) __builtin_expect(!!(x), 0)
93 #endif
94 #else
95 #ifndef likely
96 #define likely(x) (x)
97 #endif
98 #ifndef unlikely
99 #define unlikely(x) (x)
100 #endif
101 #endif
102
103 typedef enum {
104     NOTMUCH_VALUE_TIMESTAMP = 0,
105     NOTMUCH_VALUE_MESSAGE_ID
106 } notmuch_value_t;
107
108 /* Xapian (with flint backend) complains if we provide a term longer
109  * than this, but I haven't yet found a way to query the limit
110  * programmatically. */
111 #define NOTMUCH_TERM_MAX 245
112
113 #define NOTMUCH_METADATA_THREAD_ID_PREFIX "thread_id_"
114
115 /* For message IDs we have to be even more restrictive. Beyond fitting
116  * into the term limit, we also use message IDs to construct
117  * metadata-key values. And the documentation says that these should
118  * be restricted to about 200 characters. (The actual limit for the
119  * chert backend at least is 252.)
120  */
121 #define NOTMUCH_MESSAGE_ID_MAX (200 - sizeof (NOTMUCH_METADATA_THREAD_ID_PREFIX))
122
123 typedef enum _notmuch_private_status {
124     /* First, copy all the public status values. */
125     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
126     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
127     NOTMUCH_PRIVATE_STATUS_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_DATABASE,
128     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
129     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
130     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
131     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
132     NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
133
134     /* Then add our own private values. */
135     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
136     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
137
138     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
139 } notmuch_private_status_t;
140
141 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
142  * value, generating an internal error if the private value is equal
143  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
144  * that the caller has previously handled any expected
145  * notmuch_private_status_t values.)
146  */
147 #define COERCE_STATUS(private_status, format, ...)                      \
148     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
149      ?                                                                  \
150      (notmuch_status_t) _internal_error (format " (%s).\n",             \
151                                          ##__VA_ARGS__,                 \
152                                          __location__)                  \
153      :                                                                  \
154      (notmuch_status_t) private_status)
155
156 /* database.cc */
157
158 /* Lookup a prefix value by name.
159  *
160  * XXX: This should really be static inside of message.cc, and we can
161  * do that once we convert database.cc to use the
162  * _notmuch_message_add/remove_term functions. */
163 const char *
164 _find_prefix (const char *name);
165
166 notmuch_status_t
167 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
168
169 const char *
170 _notmuch_database_relative_path (notmuch_database_t *notmuch,
171                                  const char *path);
172
173 notmuch_status_t
174 _notmuch_database_split_path (void *ctx,
175                               const char *path,
176                               const char **directory,
177                               const char **basename);
178
179 const char *
180 _notmuch_database_get_directory_db_path (const char *path);
181
182 unsigned int
183 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
184
185 notmuch_private_status_t
186 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
187                                       const char *prefix_name,
188                                       const char *value,
189                                       unsigned int *doc_id);
190
191 notmuch_status_t
192 _notmuch_database_find_directory_id (notmuch_database_t *database,
193                                      const char *path,
194                                      unsigned int *directory_id);
195
196 const char *
197 _notmuch_database_get_directory_path (void *ctx,
198                                       notmuch_database_t *notmuch,
199                                       unsigned int doc_id);
200
201 notmuch_status_t
202 _notmuch_database_filename_to_direntry (void *ctx,
203                                         notmuch_database_t *notmuch,
204                                         const char *filename,
205                                         char **direntry);
206
207 /* directory.cc */
208
209 notmuch_directory_t *
210 _notmuch_directory_create (notmuch_database_t *notmuch,
211                            const char *path,
212                            notmuch_status_t *status_ret);
213
214 unsigned int
215 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
216
217 /* thread.cc */
218
219 notmuch_thread_t *
220 _notmuch_thread_create (void *ctx,
221                         notmuch_database_t *notmuch,
222                         const char *thread_id,
223                         const char *query_string,
224                         notmuch_sort_t sort);
225
226 /* message.cc */
227
228 notmuch_message_t *
229 _notmuch_message_create (const void *talloc_owner,
230                          notmuch_database_t *notmuch,
231                          unsigned int doc_id,
232                          notmuch_private_status_t *status);
233
234 notmuch_message_t *
235 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
236                                         const char *message_id,
237                                         notmuch_private_status_t *status);
238
239 const char *
240 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
241
242 notmuch_private_status_t
243 _notmuch_message_add_term (notmuch_message_t *message,
244                            const char *prefix_name,
245                            const char *value);
246
247 notmuch_private_status_t
248 _notmuch_message_remove_term (notmuch_message_t *message,
249                               const char *prefix_name,
250                               const char *value);
251
252 notmuch_private_status_t
253 _notmuch_message_gen_terms (notmuch_message_t *message,
254                             const char *prefix_name,
255                             const char *text);
256
257 void
258 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
259
260 notmuch_status_t
261 _notmuch_message_add_filename (notmuch_message_t *message,
262                                const char *filename);
263
264 void
265 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
266
267 void
268 _notmuch_message_set_date (notmuch_message_t *message,
269                            const char *date);
270
271 void
272 _notmuch_message_sync (notmuch_message_t *message);
273
274 void
275 _notmuch_message_close (notmuch_message_t *message);
276
277 /* Get a copy of the data in this message document.
278  *
279  * Caller should talloc_free the result when done.
280  *
281  * This function is intended to support database upgrade and really
282  * shouldn't be used otherwise. */
283 char *
284 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
285
286 /* Clear the data in this message document.
287  *
288  * This function is intended to support database upgrade and really
289  * shouldn't be used otherwise. */
290 void
291 _notmuch_message_clear_data (notmuch_message_t *message);
292
293 /* Set the author member of 'message' - this is the representation used
294  * when displaying the message */
295 void
296 notmuch_message_set_author (notmuch_message_t *message, const char *author);
297
298 /* Get the author member of 'message' */
299 const char *
300 notmuch_message_get_author (notmuch_message_t *message);
301
302
303 /* index.cc */
304
305 notmuch_status_t
306 _notmuch_message_index_file (notmuch_message_t *message,
307                              const char *filename);
308
309 /* message-file.c */
310
311 /* XXX: I haven't decided yet whether these will actually get exported
312  * into the public interface in notmuch.h
313  */
314
315 typedef struct _notmuch_message_file notmuch_message_file_t;
316
317 /* Open a file containing a single email message.
318  *
319  * The caller should call notmuch_message_close when done with this.
320  *
321  * Returns NULL if any error occurs.
322  */
323 notmuch_message_file_t *
324 notmuch_message_file_open (const char *filename);
325
326 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
327 notmuch_message_file_t *
328 _notmuch_message_file_open_ctx (void *ctx, const char *filename);
329
330 /* Close a notmuch message previously opened with notmuch_message_open. */
331 void
332 notmuch_message_file_close (notmuch_message_file_t *message);
333
334 /* Restrict 'message' to only save the named headers.
335  *
336  * When the caller is only interested in a short list of headers,
337  * known in advance, calling this function can avoid wasted time and
338  * memory parsing/saving header values that will never be needed.
339  *
340  * The variable arguments should be a list of const char * with a
341  * final '(const char *) NULL' to terminate the list.
342  *
343  * If this function is called, it must be called before any calls to
344  * notmuch_message_get_header for this message.
345  *
346  * After calling this function, if notmuch_message_get_header is
347  * called with a header name not in this list, then NULL will be
348  * returned even if that header exists in the actual message.
349  */
350 void
351 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
352
353 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
354 void
355 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
356                                         va_list va_headers);
357
358 /* Get the value of the specified header from the message.
359  *
360  * The header name is case insensitive.
361  *
362  * The Received: header is special - for it all Received: headers in
363  * the message are concatenated
364  *
365  * The returned value is owned by the notmuch message and is valid
366  * only until the message is closed. The caller should copy it if
367  * needing to modify the value or to hold onto it for longer.
368  *
369  * Returns NULL if the message does not contain a header line matching
370  * 'header'.
371  */
372 const char *
373 notmuch_message_file_get_header (notmuch_message_file_t *message,
374                                  const char *header);
375
376 /* messages.c */
377
378 typedef struct _notmuch_message_node {
379     notmuch_message_t *message;
380     struct _notmuch_message_node *next;
381 } notmuch_message_node_t;
382
383 typedef struct _notmuch_message_list {
384     notmuch_message_node_t *head;
385     notmuch_message_node_t **tail;
386 } notmuch_message_list_t;
387
388 /* There's a rumor that there's an alternate struct _notmuch_messages
389  * somewhere with some nasty C++ objects in it. We'll try to maintain
390  * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
391  */
392 struct _notmuch_messages {
393     notmuch_bool_t is_of_list_type;
394     notmuch_message_node_t *iterator;
395 };
396
397 notmuch_message_list_t *
398 _notmuch_message_list_create (const void *ctx);
399
400 void
401 _notmuch_message_list_append (notmuch_message_list_t *list,
402                               notmuch_message_node_t *node);
403
404 void
405 _notmuch_message_list_add_message (notmuch_message_list_t *list,
406                                    notmuch_message_t *message);
407
408 notmuch_messages_t *
409 _notmuch_messages_create (notmuch_message_list_t *list);
410
411 /* query.cc */
412
413 notmuch_bool_t
414 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
415
416 notmuch_message_t *
417 _notmuch_mset_messages_get (notmuch_messages_t *messages);
418
419 void
420 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
421
422 /* message.cc */
423
424 void
425 _notmuch_message_add_reply (notmuch_message_t *message,
426                             notmuch_message_node_t *reply);
427
428 /* sha1.c */
429
430 char *
431 notmuch_sha1_of_string (const char *str);
432
433 char *
434 notmuch_sha1_of_file (const char *filename);
435
436 /* tags.c */
437
438 notmuch_tags_t *
439 _notmuch_tags_create (void *ctx);
440
441 void
442 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
443
444 void
445 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
446
447 #pragma GCC visibility pop
448
449 NOTMUCH_END_DECLS
450
451 #endif