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