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