]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
d602e157c80e8184bd5d23ece56dc6a4afb8343d
[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 notmuch_private_status_t
265 _notmuch_message_rename (notmuch_message_t *message,
266                          const char *new_filename);
267
268 void
269 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
270
271 void
272 _notmuch_message_set_date (notmuch_message_t *message,
273                            const char *date);
274
275 void
276 _notmuch_message_sync (notmuch_message_t *message);
277
278 void
279 _notmuch_message_close (notmuch_message_t *message);
280
281 /* Get a copy of the data in this message document.
282  *
283  * Caller should talloc_free the result when done.
284  *
285  * This function is intended to support database upgrade and really
286  * shouldn't be used otherwise. */
287 char *
288 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
289
290 /* Clear the data in this message document.
291  *
292  * This function is intended to support database upgrade and really
293  * shouldn't be used otherwise. */
294 void
295 _notmuch_message_clear_data (notmuch_message_t *message);
296
297 /* Set the author member of 'message' - this is the representation used
298  * when displaying the message */
299 void
300 notmuch_message_set_author (notmuch_message_t *message, const char *author);
301
302 /* Get the author member of 'message' */
303 const char *
304 notmuch_message_get_author (notmuch_message_t *message);
305
306
307 /* index.cc */
308
309 notmuch_status_t
310 _notmuch_message_index_file (notmuch_message_t *message,
311                              const char *filename);
312
313 /* message-file.c */
314
315 /* XXX: I haven't decided yet whether these will actually get exported
316  * into the public interface in notmuch.h
317  */
318
319 typedef struct _notmuch_message_file notmuch_message_file_t;
320
321 /* Open a file containing a single email message.
322  *
323  * The caller should call notmuch_message_close when done with this.
324  *
325  * Returns NULL if any error occurs.
326  */
327 notmuch_message_file_t *
328 notmuch_message_file_open (const char *filename);
329
330 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
331 notmuch_message_file_t *
332 _notmuch_message_file_open_ctx (void *ctx, const char *filename);
333
334 /* Close a notmuch message previously opened with notmuch_message_open. */
335 void
336 notmuch_message_file_close (notmuch_message_file_t *message);
337
338 /* Restrict 'message' to only save the named headers.
339  *
340  * When the caller is only interested in a short list of headers,
341  * known in advance, calling this function can avoid wasted time and
342  * memory parsing/saving header values that will never be needed.
343  *
344  * The variable arguments should be a list of const char * with a
345  * final '(const char *) NULL' to terminate the list.
346  *
347  * If this function is called, it must be called before any calls to
348  * notmuch_message_get_header for this message.
349  *
350  * After calling this function, if notmuch_message_get_header is
351  * called with a header name not in this list, then NULL will be
352  * returned even if that header exists in the actual message.
353  */
354 void
355 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
356
357 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
358 void
359 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
360                                         va_list va_headers);
361
362 /* Get the value of the specified header from the message.
363  *
364  * The header name is case insensitive.
365  *
366  * The Received: header is special - for it all Received: headers in
367  * the message are concatenated
368  *
369  * The returned value is owned by the notmuch message and is valid
370  * only until the message is closed. The caller should copy it if
371  * needing to modify the value or to hold onto it for longer.
372  *
373  * Returns NULL if the message does not contain a header line matching
374  * 'header'.
375  */
376 const char *
377 notmuch_message_file_get_header (notmuch_message_file_t *message,
378                                  const char *header);
379
380 /* messages.c */
381
382 typedef struct _notmuch_message_node {
383     notmuch_message_t *message;
384     struct _notmuch_message_node *next;
385 } notmuch_message_node_t;
386
387 typedef struct _notmuch_message_list {
388     notmuch_message_node_t *head;
389     notmuch_message_node_t **tail;
390 } notmuch_message_list_t;
391
392 /* There's a rumor that there's an alternate struct _notmuch_messages
393  * somewhere with some nasty C++ objects in it. We'll try to maintain
394  * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
395  */
396 struct _notmuch_messages {
397     notmuch_bool_t is_of_list_type;
398     notmuch_message_node_t *iterator;
399 };
400
401 notmuch_message_list_t *
402 _notmuch_message_list_create (const void *ctx);
403
404 void
405 _notmuch_message_list_append (notmuch_message_list_t *list,
406                               notmuch_message_node_t *node);
407
408 void
409 _notmuch_message_list_add_message (notmuch_message_list_t *list,
410                                    notmuch_message_t *message);
411
412 notmuch_messages_t *
413 _notmuch_messages_create (notmuch_message_list_t *list);
414
415 /* query.cc */
416
417 notmuch_bool_t
418 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
419
420 notmuch_message_t *
421 _notmuch_mset_messages_get (notmuch_messages_t *messages);
422
423 void
424 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
425
426 /* message.cc */
427
428 void
429 _notmuch_message_add_reply (notmuch_message_t *message,
430                             notmuch_message_node_t *reply);
431
432 /* sha1.c */
433
434 char *
435 notmuch_sha1_of_string (const char *str);
436
437 char *
438 notmuch_sha1_of_file (const char *filename);
439
440 /* tags.c */
441
442 notmuch_tags_t *
443 _notmuch_tags_create (void *ctx);
444
445 void
446 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
447
448 void
449 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
450
451 #pragma GCC visibility pop
452
453 NOTMUCH_END_DECLS
454
455 #endif