]> git.notmuchmail.org Git - notmuch/blob - notmuch-private.h
tags: Replace sort() and reset() with prepare_iterator().
[notmuch] / 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 */
26 #endif
27 #include <stdio.h>
28
29 #include "notmuch.h"
30
31 NOTMUCH_BEGIN_DECLS
32
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mman.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <ctype.h>
43 #include <assert.h>
44
45 #include <talloc.h>
46
47 #include "xutil.h"
48
49 #ifdef DEBUG
50 # define DEBUG_DATABASE_SANITY 1
51 # define DEBUG_QUERY 1
52 #endif
53
54 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
55
56 /* There's no point in continuing when we've detected that we've done
57  * something wrong internally (as opposed to the user passing in a
58  * bogus value).
59  *
60  * Note that PRINTF_ATTRIBUTE comes from talloc.h
61  */
62 int
63 _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);
64
65 /* There's no point in continuing when we've detected that we've done
66  * something wrong internally (as opposed to the user passing in a
67  * bogus value).
68  *
69  * Note that __location__ comes from talloc.h.
70  */
71 #define INTERNAL_ERROR(format, ...)                     \
72     _internal_error (format " (%s).\n",                 \
73                      ##__VA_ARGS__, __location__)
74
75 #define unused(x) x __attribute__ ((unused))
76
77 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
78  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
79  */
80 /* these macros gain us a few percent of speed on gcc */
81 #if (__GNUC__ >= 3)
82 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
83    as its first argument */
84 #ifndef likely
85 #define likely(x)   __builtin_expect(!!(x), 1)
86 #endif
87 #ifndef unlikely
88 #define unlikely(x) __builtin_expect(!!(x), 0)
89 #endif
90 #else
91 #ifndef likely
92 #define likely(x) (x)
93 #endif
94 #ifndef unlikely
95 #define unlikely(x) (x)
96 #endif
97 #endif
98
99 typedef enum {
100     NOTMUCH_VALUE_TIMESTAMP = 0,
101     NOTMUCH_VALUE_MESSAGE_ID
102 } notmuch_value_t;
103
104 /* Xapian (with flint backend) complains if we provide a term longer
105  * than this, but I haven't yet found a way to query the limit
106  * programmatically. */
107 #define NOTMUCH_TERM_MAX 245
108
109 typedef enum _notmuch_private_status {
110     /* First, copy all the public status values. */
111     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
112     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
113     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
114     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
115     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
116     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
117
118     /* Then add our own private values. */
119     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG,
120     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
121
122     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
123 } notmuch_private_status_t;
124
125 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
126  * value, generating an internal error if the private value is equal
127  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
128  * that the caller has previously handled any expected
129  * notmuch_private_status_t values.)
130  */
131 #define COERCE_STATUS(private_status, format, ...)                      \
132     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
133      ?                                                                  \
134      (notmuch_status_t) _internal_error (format " (%s).\n",             \
135                                          ##__VA_ARGS__,                 \
136                                          __location__)                  \
137      :                                                                  \
138      (notmuch_status_t) private_status)
139
140 /* thread.cc */
141
142 notmuch_thread_t *
143 _notmuch_thread_create (const void *talloc_owner,
144                         notmuch_database_t *notmuch,
145                         const char *thread_id);
146
147 /* message.cc */
148
149 notmuch_message_t *
150 _notmuch_message_create (const void *talloc_owner,
151                          notmuch_database_t *notmuch,
152                          unsigned int doc_id,
153                          notmuch_private_status_t *status);
154
155 notmuch_message_t *
156 _notmuch_message_create_for_message_id (const void *talloc_owner,
157                                         notmuch_database_t *notmuch,
158                                         const char *message_id,
159                                         notmuch_status_t *status);
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_private_status_t
170 _notmuch_message_add_term (notmuch_message_t *message,
171                            const char *prefix_name,
172                            const char *value);
173
174 notmuch_private_status_t
175 _notmuch_message_remove_term (notmuch_message_t *message,
176                               const char *prefix_name,
177                               const char *value);
178
179 void
180 _notmuch_message_set_filename (notmuch_message_t *message,
181                                const char *filename);
182
183 void
184 _notmuch_message_add_thread_id (notmuch_message_t *message,
185                                 const char *thread_id);
186
187 void
188 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
189
190 void
191 _notmuch_message_set_date (notmuch_message_t *message,
192                            const char *date);
193
194 void
195 _notmuch_message_sync (notmuch_message_t *message);
196
197 /* message-file.c */
198
199 /* XXX: I haven't decided yet whether these will actually get exported
200  * into the public interface in notmuch.h
201  */
202
203 typedef struct _notmuch_message_file notmuch_message_file_t;
204
205 /* Open a file containing a single email message.
206  *
207  * The caller should call notmuch_message_close when done with this.
208  *
209  * Returns NULL if any error occurs.
210  */
211 notmuch_message_file_t *
212 notmuch_message_file_open (const char *filename);
213
214 /* Close a notmuch message preivously opened with notmuch_message_open. */
215 void
216 notmuch_message_file_close (notmuch_message_file_t *message);
217
218 /* Restrict 'message' to only save the named headers.
219  *
220  * When the caller is only interested in a short list of headers,
221  * known in advance, calling this function can avoid wasted time and
222  * memory parsing/saving header values that will never be needed.
223  *
224  * The variable arguments should be a list of const char * with a
225  * final '(const char *) NULL' to terminate the list.
226  *
227  * If this function is called, it must be called before any calls to
228  * notmuch_message_get_header for this message.
229  *
230  * After calling this function, if notmuch_message_get_header is
231  * called with a header name not in this list, then NULL will be
232  * returned even if that header exists in the actual message.
233  */
234 void
235 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
236
237 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
238 void
239 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
240                                         va_list va_headers);
241
242 /* Get the value of the specified header from the message.
243  *
244  * The header name is case insensitive.
245  *
246  * The returned value is owned by the notmuch message and is valid
247  * only until the message is closed. The caller should copy it if
248  * needing to modify the value or to hold onto it for longer.
249  *
250  * Returns NULL if the message does not contain a header line matching
251  * 'header'.
252  */
253 const char *
254 notmuch_message_file_get_header (notmuch_message_file_t *message,
255                                  const char *header);
256
257 /* date.c */
258
259 /* Parse an RFC 8222 date string to a time_t value.
260  *
261  * The tz_offset argument can be used to also obtain the time-zone
262  * offset, (but can be NULL if the call is not interested in that).
263  *
264  * Returns 0 on error.
265  */
266 time_t
267 notmuch_parse_date (const char *str, int *tz_offset);
268
269 /* sha1.c */
270
271 char *
272 notmuch_sha1_of_string (const char *str);
273
274 char *
275 notmuch_sha1_of_file (const char *filename);
276
277 /* tags.c */
278
279 notmuch_tags_t *
280 _notmuch_tags_create (void *ctx);
281
282 void
283 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
284
285 void
286 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
287
288 NOTMUCH_END_DECLS
289
290 #endif