]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
cli: abstract new mailbox creation
[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 <gmime/gmime.h>
50
51 #include "xutil.h"
52 #include "error_util.h"
53
54 #pragma GCC visibility push(hidden)
55
56 #ifdef DEBUG
57 # define DEBUG_DATABASE_SANITY 1
58 # define DEBUG_QUERY 1
59 #endif
60
61 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
62
63 #define STRNCMP_LITERAL(var, literal) \
64     strncmp ((var), (literal), sizeof (literal) - 1)
65
66 /* Robust bit test/set/reset macros */
67 #define _NOTMUCH_VALID_BIT(bit) \
68     ((bit) >= 0 && ((unsigned long) bit) < CHAR_BIT * sizeof (unsigned long long))
69 #define NOTMUCH_TEST_BIT(val, bit) \
70     (_NOTMUCH_VALID_BIT(bit) ? !!((val) & (1ull << (bit))) : 0)
71 #define NOTMUCH_SET_BIT(valp, bit) \
72     (_NOTMUCH_VALID_BIT(bit) ? (*(valp) |= (1ull << (bit))) : *(valp))
73 #define NOTMUCH_CLEAR_BIT(valp,  bit) \
74     (_NOTMUCH_VALID_BIT(bit) ? (*(valp) &= ~(1ull << (bit))) : *(valp))
75
76 #define unused(x) x __attribute__ ((unused))
77
78 #ifdef __cplusplus
79 # define visible __attribute__((visibility("default")))
80 #else
81 # define visible
82 #endif
83
84 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
85  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
86  */
87 /* these macros gain us a few percent of speed on gcc */
88 #if (__GNUC__ >= 3)
89 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
90    as its first argument */
91 #ifndef likely
92 #define likely(x)   __builtin_expect(!!(x), 1)
93 #endif
94 #ifndef unlikely
95 #define unlikely(x) __builtin_expect(!!(x), 0)
96 #endif
97 #else
98 #ifndef likely
99 #define likely(x) (x)
100 #endif
101 #ifndef unlikely
102 #define unlikely(x) (x)
103 #endif
104 #endif
105
106 typedef enum {
107     NOTMUCH_VALUE_TIMESTAMP = 0,
108     NOTMUCH_VALUE_MESSAGE_ID,
109     NOTMUCH_VALUE_FROM,
110     NOTMUCH_VALUE_SUBJECT,
111     NOTMUCH_VALUE_LAST_MOD,
112 } notmuch_value_t;
113
114 /* Xapian (with flint backend) complains if we provide a term longer
115  * than this, but I haven't yet found a way to query the limit
116  * programmatically. */
117 #define NOTMUCH_TERM_MAX 245
118
119 #define NOTMUCH_METADATA_THREAD_ID_PREFIX "thread_id_"
120
121 /* For message IDs we have to be even more restrictive. Beyond fitting
122  * into the term limit, we also use message IDs to construct
123  * metadata-key values. And the documentation says that these should
124  * be restricted to about 200 characters. (The actual limit for the
125  * chert backend at least is 252.)
126  */
127 #define NOTMUCH_MESSAGE_ID_MAX (200 - sizeof (NOTMUCH_METADATA_THREAD_ID_PREFIX))
128
129 typedef enum _notmuch_private_status {
130     /* First, copy all the public status values. */
131     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
132     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
133     NOTMUCH_PRIVATE_STATUS_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_DATABASE,
134     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
135     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
136     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
137     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
138     NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
139
140     /* Then add our own private values. */
141     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
142     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
143
144     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
145 } notmuch_private_status_t;
146
147 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
148  * value, generating an internal error if the private value is equal
149  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
150  * that the caller has previously handled any expected
151  * notmuch_private_status_t values.)
152  *
153  * Note that the function _internal_error does not return. Evaluating
154  * to NOTMUCH_STATUS_SUCCESS is done purely to appease the compiler.
155  */
156 #define COERCE_STATUS(private_status, format, ...)                      \
157     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
158      ?                                                                  \
159      _internal_error (format " (%s).\n",                                \
160                       ##__VA_ARGS__,                                    \
161                       __location__),                                    \
162      (notmuch_status_t) NOTMUCH_PRIVATE_STATUS_SUCCESS                  \
163      :                                                                  \
164      (notmuch_status_t) private_status)
165
166 /* Flags shared by various lookup functions. */
167 typedef enum _notmuch_find_flags {
168     /* Lookup without creating any documents.  This is the default
169      * behavior. */
170     NOTMUCH_FIND_LOOKUP = 0,
171     /* If set, create the necessary document (or documents) if they
172      * are missing.  Requires a read/write database. */
173     NOTMUCH_FIND_CREATE = 1<<0,
174 } notmuch_find_flags_t;
175
176 typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t;
177
178 /* database.cc */
179
180 /* Lookup a prefix value by name.
181  *
182  * XXX: This should really be static inside of message.cc, and we can
183  * do that once we convert database.cc to use the
184  * _notmuch_message_add/remove_term functions. */
185 const char *
186 _find_prefix (const char *name);
187
188 char *
189 _notmuch_message_id_compressed (void *ctx, const char *message_id);
190
191 notmuch_status_t
192 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
193
194 void
195 _notmuch_database_log (notmuch_database_t *notmuch,
196                        const char *format, ...);
197
198 unsigned long
199 _notmuch_database_new_revision (notmuch_database_t *notmuch);
200
201 const char *
202 _notmuch_database_relative_path (notmuch_database_t *notmuch,
203                                  const char *path);
204
205 notmuch_status_t
206 _notmuch_database_split_path (void *ctx,
207                               const char *path,
208                               const char **directory,
209                               const char **basename);
210
211 const char *
212 _notmuch_database_get_directory_db_path (const char *path);
213
214 unsigned int
215 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
216
217 notmuch_private_status_t
218 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
219                                       const char *prefix_name,
220                                       const char *value,
221                                       unsigned int *doc_id);
222
223 notmuch_status_t
224 _notmuch_database_find_directory_id (notmuch_database_t *database,
225                                      const char *path,
226                                      notmuch_find_flags_t flags,
227                                      unsigned int *directory_id);
228
229 const char *
230 _notmuch_database_get_directory_path (void *ctx,
231                                       notmuch_database_t *notmuch,
232                                       unsigned int doc_id);
233
234 notmuch_status_t
235 _notmuch_database_filename_to_direntry (void *ctx,
236                                         notmuch_database_t *notmuch,
237                                         const char *filename,
238                                         notmuch_find_flags_t flags,
239                                         char **direntry);
240
241 /* directory.cc */
242
243 notmuch_directory_t *
244 _notmuch_directory_create (notmuch_database_t *notmuch,
245                            const char *path,
246                            notmuch_find_flags_t flags,
247                            notmuch_status_t *status_ret);
248
249 unsigned int
250 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
251
252 /* message.cc */
253
254 notmuch_message_t *
255 _notmuch_message_create (const void *talloc_owner,
256                          notmuch_database_t *notmuch,
257                          unsigned int doc_id,
258                          notmuch_private_status_t *status);
259
260 notmuch_message_t *
261 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
262                                         const char *message_id,
263                                         notmuch_private_status_t *status);
264
265 unsigned int
266 _notmuch_message_get_doc_id (notmuch_message_t *message);
267
268 const char *
269 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
270
271 notmuch_private_status_t
272 _notmuch_message_add_term (notmuch_message_t *message,
273                            const char *prefix_name,
274                            const char *value);
275
276 notmuch_private_status_t
277 _notmuch_message_remove_term (notmuch_message_t *message,
278                               const char *prefix_name,
279                               const char *value);
280
281 notmuch_private_status_t
282 _notmuch_message_gen_terms (notmuch_message_t *message,
283                             const char *prefix_name,
284                             const char *text);
285
286 void
287 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
288
289 void
290 _notmuch_message_upgrade_folder (notmuch_message_t *message);
291
292 notmuch_status_t
293 _notmuch_message_add_filename (notmuch_message_t *message,
294                                const char *filename);
295
296 notmuch_status_t
297 _notmuch_message_remove_filename (notmuch_message_t *message,
298                                   const char *filename);
299
300 notmuch_status_t
301 _notmuch_message_rename (notmuch_message_t *message,
302                          const char *new_filename);
303
304 void
305 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
306
307 void
308 _notmuch_message_set_header_values (notmuch_message_t *message,
309                                     const char *date,
310                                     const char *from,
311                                     const char *subject);
312
313 void
314 _notmuch_message_upgrade_last_mod (notmuch_message_t *message);
315
316 void
317 _notmuch_message_sync (notmuch_message_t *message);
318
319 notmuch_status_t
320 _notmuch_message_delete (notmuch_message_t *message);
321
322 notmuch_private_status_t
323 _notmuch_message_initialize_ghost (notmuch_message_t *message,
324                                    const char *thread_id);
325
326 void
327 _notmuch_message_close (notmuch_message_t *message);
328
329 /* Get a copy of the data in this message document.
330  *
331  * Caller should talloc_free the result when done.
332  *
333  * This function is intended to support database upgrade and really
334  * shouldn't be used otherwise. */
335 char *
336 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
337
338 /* Clear the data in this message document.
339  *
340  * This function is intended to support database upgrade and really
341  * shouldn't be used otherwise. */
342 void
343 _notmuch_message_clear_data (notmuch_message_t *message);
344
345 /* Set the author member of 'message' - this is the representation used
346  * when displaying the message */
347 void
348 _notmuch_message_set_author (notmuch_message_t *message, const char *author);
349
350 /* Get the author member of 'message' */
351 const char *
352 _notmuch_message_get_author (notmuch_message_t *message);
353
354 /* message-file.c */
355
356 /* XXX: I haven't decided yet whether these will actually get exported
357  * into the public interface in notmuch.h
358  */
359
360 typedef struct _notmuch_message_file notmuch_message_file_t;
361
362 /* Open a file containing a single email message.
363  *
364  * The caller should call notmuch_message_close when done with this.
365  *
366  * Returns NULL if any error occurs.
367  */
368 notmuch_message_file_t *
369 _notmuch_message_file_open (notmuch_database_t *notmuch, const char *filename);
370
371 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
372 notmuch_message_file_t *
373 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
374                                 void *ctx, const char *filename);
375
376 /* Close a notmuch message previously opened with notmuch_message_open. */
377 void
378 _notmuch_message_file_close (notmuch_message_file_t *message);
379
380 /* Parse the message.
381  *
382  * This will be done automatically as necessary on other calls
383  * depending on it, but an explicit call allows for better error
384  * status reporting.
385  */
386 notmuch_status_t
387 _notmuch_message_file_parse (notmuch_message_file_t *message);
388
389 /* Get the gmime message of a message file.
390  *
391  * The message file is parsed as necessary.
392  *
393  * The GMimeMessage* is set to *mime_message on success (which the
394  * caller must not unref).
395  *
396  * XXX: Would be nice to not have to expose GMimeMessage here.
397  */
398 notmuch_status_t
399 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
400                                         GMimeMessage **mime_message);
401
402 /* Get the value of the specified header from the message as a UTF-8 string.
403  *
404  * The message file is parsed as necessary.
405  *
406  * The header name is case insensitive.
407  *
408  * The Received: header is special - for it all Received: headers in
409  * the message are concatenated
410  *
411  * The returned value is owned by the notmuch message and is valid
412  * only until the message is closed. The caller should copy it if
413  * needing to modify the value or to hold onto it for longer.
414  *
415  * Returns NULL on errors, empty string if the message does not
416  * contain a header line matching 'header'.
417  */
418 const char *
419 _notmuch_message_file_get_header (notmuch_message_file_t *message,
420                                  const char *header);
421
422 /* index.cc */
423
424 notmuch_status_t
425 _notmuch_message_index_file (notmuch_message_t *message,
426                              notmuch_message_file_t *message_file);
427
428 /* messages.c */
429
430 typedef struct _notmuch_message_node {
431     notmuch_message_t *message;
432     struct _notmuch_message_node *next;
433 } notmuch_message_node_t;
434
435 typedef struct _notmuch_message_list {
436     notmuch_message_node_t *head;
437     notmuch_message_node_t **tail;
438 } notmuch_message_list_t;
439
440 /* There's a rumor that there's an alternate struct _notmuch_messages
441  * somewhere with some nasty C++ objects in it. We'll try to maintain
442  * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
443  */
444 struct visible _notmuch_messages {
445     notmuch_bool_t is_of_list_type;
446     notmuch_doc_id_set_t *excluded_doc_ids;
447     notmuch_message_node_t *iterator;
448 };
449
450 notmuch_message_list_t *
451 _notmuch_message_list_create (const void *ctx);
452
453 void
454 _notmuch_message_list_add_message (notmuch_message_list_t *list,
455                                    notmuch_message_t *message);
456
457 notmuch_messages_t *
458 _notmuch_messages_create (notmuch_message_list_t *list);
459
460 /* query.cc */
461
462 notmuch_bool_t
463 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
464
465 notmuch_message_t *
466 _notmuch_mset_messages_get (notmuch_messages_t *messages);
467
468 void
469 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
470
471 notmuch_bool_t
472 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
473                               unsigned int doc_id);
474
475 void
476 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
477                             unsigned int doc_id);
478
479 /* message.cc */
480
481 void
482 _notmuch_message_add_reply (notmuch_message_t *message,
483                             notmuch_message_t *reply);
484 notmuch_database_t *
485 _notmuch_message_database (notmuch_message_t *message);
486
487 /* sha1.c */
488
489 char *
490 _notmuch_sha1_of_string (const char *str);
491
492 char *
493 _notmuch_sha1_of_file (const char *filename);
494
495 /* string-list.c */
496
497 typedef struct _notmuch_string_node {
498     char *string;
499     struct _notmuch_string_node *next;
500 } notmuch_string_node_t;
501
502 typedef struct visible _notmuch_string_list {
503     int length;
504     notmuch_string_node_t *head;
505     notmuch_string_node_t **tail;
506 } notmuch_string_list_t;
507
508 notmuch_string_list_t *
509 _notmuch_string_list_create (const void *ctx);
510
511 /* Add 'string' to 'list'.
512  *
513  * The list will create its own talloced copy of 'string'.
514  */
515 void
516 _notmuch_string_list_append (notmuch_string_list_t *list,
517                              const char *string);
518
519 void
520 _notmuch_string_list_sort (notmuch_string_list_t *list);
521
522 /* tags.c */
523
524 notmuch_tags_t *
525 _notmuch_tags_create (const void *ctx, notmuch_string_list_t *list);
526
527 /* filenames.c */
528
529 /* The notmuch_filenames_t iterates over a notmuch_string_list_t of
530  * file names */
531 notmuch_filenames_t *
532 _notmuch_filenames_create (const void *ctx,
533                            notmuch_string_list_t *list);
534
535 /* thread.cc */
536
537 notmuch_thread_t *
538 _notmuch_thread_create (void *ctx,
539                         notmuch_database_t *notmuch,
540                         unsigned int seed_doc_id,
541                         notmuch_doc_id_set_t *match_set,
542                         notmuch_string_list_t *excluded_terms,
543                         notmuch_exclude_t omit_exclude,
544                         notmuch_sort_t sort);
545
546 NOTMUCH_END_DECLS
547
548 #ifdef __cplusplus
549 /* Implicit typecast from 'void *' to 'T *' is okay in C, but not in
550  * C++. In talloc_steal, an explicit cast is provided for type safety
551  * in some GCC versions. Otherwise, a cast is required. Provide a
552  * template function for this to maintain type safety, and redefine
553  * talloc_steal to use it.
554  */
555 #if !(__GNUC__ >= 3)
556 template <class T> T *
557 _notmuch_talloc_steal (const void *new_ctx, const T *ptr)
558 {
559     return static_cast<T *> (talloc_steal (new_ctx, ptr));
560 }
561 #undef talloc_steal
562 #define talloc_steal _notmuch_talloc_steal
563 #endif
564 #endif
565
566 #pragma GCC visibility pop
567
568 #endif