]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
xutil.c: remove duplicate copies, create new library libutil.a to contain xutil.
[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 #include "error_util.h"
51
52 #pragma GCC visibility push(hidden)
53
54 #ifdef DEBUG
55 # define DEBUG_DATABASE_SANITY 1
56 # define DEBUG_QUERY 1
57 #endif
58
59 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
60
61 #define STRNCMP_LITERAL(var, literal) \
62     strncmp ((var), (literal), sizeof (literal) - 1)
63
64 #define unused(x) x __attribute__ ((unused))
65
66 #ifdef __cplusplus
67 # define visible __attribute__((visibility("default")))
68 #else
69 # define visible
70 #endif
71
72 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
73  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
74  */
75 /* these macros gain us a few percent of speed on gcc */
76 #if (__GNUC__ >= 3)
77 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
78    as its first argument */
79 #ifndef likely
80 #define likely(x)   __builtin_expect(!!(x), 1)
81 #endif
82 #ifndef unlikely
83 #define unlikely(x) __builtin_expect(!!(x), 0)
84 #endif
85 #else
86 #ifndef likely
87 #define likely(x) (x)
88 #endif
89 #ifndef unlikely
90 #define unlikely(x) (x)
91 #endif
92 #endif
93
94 typedef enum {
95     NOTMUCH_VALUE_TIMESTAMP = 0,
96     NOTMUCH_VALUE_MESSAGE_ID
97 } notmuch_value_t;
98
99 /* Xapian (with flint backend) complains if we provide a term longer
100  * than this, but I haven't yet found a way to query the limit
101  * programmatically. */
102 #define NOTMUCH_TERM_MAX 245
103
104 #define NOTMUCH_METADATA_THREAD_ID_PREFIX "thread_id_"
105
106 /* For message IDs we have to be even more restrictive. Beyond fitting
107  * into the term limit, we also use message IDs to construct
108  * metadata-key values. And the documentation says that these should
109  * be restricted to about 200 characters. (The actual limit for the
110  * chert backend at least is 252.)
111  */
112 #define NOTMUCH_MESSAGE_ID_MAX (200 - sizeof (NOTMUCH_METADATA_THREAD_ID_PREFIX))
113
114 typedef enum _notmuch_private_status {
115     /* First, copy all the public status values. */
116     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
117     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
118     NOTMUCH_PRIVATE_STATUS_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_DATABASE,
119     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
120     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
121     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
122     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
123     NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
124
125     /* Then add our own private values. */
126     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
127     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
128
129     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
130 } notmuch_private_status_t;
131
132 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
133  * value, generating an internal error if the private value is equal
134  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
135  * that the caller has previously handled any expected
136  * notmuch_private_status_t values.)
137  */
138 #define COERCE_STATUS(private_status, format, ...)                      \
139     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
140      ?                                                                  \
141      (notmuch_status_t) _internal_error (format " (%s).\n",             \
142                                          ##__VA_ARGS__,                 \
143                                          __location__)                  \
144      :                                                                  \
145      (notmuch_status_t) private_status)
146
147 typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t;
148
149 /* database.cc */
150
151 /* Lookup a prefix value by name.
152  *
153  * XXX: This should really be static inside of message.cc, and we can
154  * do that once we convert database.cc to use the
155  * _notmuch_message_add/remove_term functions. */
156 const char *
157 _find_prefix (const char *name);
158
159 notmuch_status_t
160 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
161
162 const char *
163 _notmuch_database_relative_path (notmuch_database_t *notmuch,
164                                  const char *path);
165
166 notmuch_status_t
167 _notmuch_database_split_path (void *ctx,
168                               const char *path,
169                               const char **directory,
170                               const char **basename);
171
172 const char *
173 _notmuch_database_get_directory_db_path (const char *path);
174
175 unsigned int
176 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
177
178 notmuch_private_status_t
179 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
180                                       const char *prefix_name,
181                                       const char *value,
182                                       unsigned int *doc_id);
183
184 notmuch_status_t
185 _notmuch_database_find_directory_id (notmuch_database_t *database,
186                                      const char *path,
187                                      unsigned int *directory_id);
188
189 const char *
190 _notmuch_database_get_directory_path (void *ctx,
191                                       notmuch_database_t *notmuch,
192                                       unsigned int doc_id);
193
194 notmuch_status_t
195 _notmuch_database_filename_to_direntry (void *ctx,
196                                         notmuch_database_t *notmuch,
197                                         const char *filename,
198                                         char **direntry);
199
200 /* directory.cc */
201
202 notmuch_directory_t *
203 _notmuch_directory_create (notmuch_database_t *notmuch,
204                            const char *path,
205                            notmuch_status_t *status_ret);
206
207 unsigned int
208 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
209
210 /* thread.cc */
211
212 notmuch_thread_t *
213 _notmuch_thread_create (void *ctx,
214                         notmuch_database_t *notmuch,
215                         unsigned int seed_doc_id,
216                         notmuch_doc_id_set_t *match_set,
217                         notmuch_sort_t sort);
218
219 /* message.cc */
220
221 notmuch_message_t *
222 _notmuch_message_create (const void *talloc_owner,
223                          notmuch_database_t *notmuch,
224                          unsigned int doc_id,
225                          notmuch_private_status_t *status);
226
227 notmuch_message_t *
228 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
229                                         const char *message_id,
230                                         notmuch_private_status_t *status);
231
232 unsigned int
233 _notmuch_message_get_doc_id (notmuch_message_t *message);
234
235 const char *
236 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
237
238 notmuch_private_status_t
239 _notmuch_message_add_term (notmuch_message_t *message,
240                            const char *prefix_name,
241                            const char *value);
242
243 notmuch_private_status_t
244 _notmuch_message_remove_term (notmuch_message_t *message,
245                               const char *prefix_name,
246                               const char *value);
247
248 notmuch_private_status_t
249 _notmuch_message_gen_terms (notmuch_message_t *message,
250                             const char *prefix_name,
251                             const char *text);
252
253 void
254 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
255
256 notmuch_status_t
257 _notmuch_message_add_filename (notmuch_message_t *message,
258                                const char *filename);
259
260 notmuch_status_t
261 _notmuch_message_remove_filename (notmuch_message_t *message,
262                                   const char *filename);
263
264 notmuch_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 notmuch_status_t
279 _notmuch_message_delete (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 visible _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 notmuch_bool_t
430 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
431                               unsigned int doc_id);
432
433 void
434 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
435                             unsigned int doc_id);
436
437 /* message.cc */
438
439 void
440 _notmuch_message_add_reply (notmuch_message_t *message,
441                             notmuch_message_node_t *reply);
442
443 /* sha1.c */
444
445 char *
446 notmuch_sha1_of_string (const char *str);
447
448 char *
449 notmuch_sha1_of_file (const char *filename);
450
451 /* string-list.c */
452
453 typedef struct _notmuch_string_node {
454     char *string;
455     struct _notmuch_string_node *next;
456 } notmuch_string_node_t;
457
458 typedef struct _notmuch_string_list {
459     int length;
460     notmuch_string_node_t *head;
461     notmuch_string_node_t **tail;
462 } notmuch_string_list_t;
463
464 notmuch_string_list_t *
465 _notmuch_string_list_create (const void *ctx);
466
467 /* Add 'string' to 'list'.
468  *
469  * The list will create its own talloced copy of 'string'.
470  */
471 void
472 _notmuch_string_list_append (notmuch_string_list_t *list,
473                              const char *string);
474
475 void
476 _notmuch_string_list_sort (notmuch_string_list_t *list);
477
478 /* tags.c */
479
480 notmuch_tags_t *
481 _notmuch_tags_create (const void *ctx, notmuch_string_list_t *list);
482
483 /* filenames.c */
484
485 /* The notmuch_filenames_t iterates over a notmuch_string_list_t of
486  * file names */
487 notmuch_filenames_t *
488 _notmuch_filenames_create (const void *ctx,
489                            notmuch_string_list_t *list);
490
491 #pragma GCC visibility pop
492
493 NOTMUCH_END_DECLS
494
495 #endif