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