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