]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
lib: Implement new notmuch_directory_t API.
[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 typedef enum _notmuch_private_status {
112     /* First, copy all the public status values. */
113     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
114     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
115     NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,
116     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
117     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
118     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
119     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
120     NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
121
122     /* Then add our own private values. */
123     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
124     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
125
126     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
127 } notmuch_private_status_t;
128
129 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
130  * value, generating an internal error if the private value is equal
131  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
132  * that the caller has previously handled any expected
133  * notmuch_private_status_t values.)
134  */
135 #define COERCE_STATUS(private_status, format, ...)                      \
136     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
137      ?                                                                  \
138      (notmuch_status_t) _internal_error (format " (%s).\n",             \
139                                          ##__VA_ARGS__,                 \
140                                          __location__)                  \
141      :                                                                  \
142      (notmuch_status_t) private_status)
143
144 /* database.cc */
145
146 /* Lookup a prefix value by name.
147  *
148  * XXX: This should really be static inside of message.cc, and we can
149  * do that once we convert database.cc to use the
150  * _notmuch_message_add/remove_term functions. */
151 const char *
152 _find_prefix (const char *name);
153
154 const char *
155 _notmuch_database_relative_path (notmuch_database_t *notmuch,
156                                  const char *path);
157
158 notmuch_status_t
159 _notmuch_database_split_path (void *ctx,
160                               const char *path,
161                               const char **directory,
162                               const char **basename);
163
164 const char *
165 _notmuch_database_get_directory_db_path (const char *path);
166
167 notmuch_private_status_t
168 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
169                                       const char *prefix_name,
170                                       const char *value,
171                                       unsigned int *doc_id);
172
173 notmuch_status_t
174 _notmuch_database_find_directory_id (notmuch_database_t *database,
175                                      const char *path,
176                                      unsigned int *directory_id);
177
178 const char *
179 _notmuch_database_get_directory_path (void *ctx,
180                                       notmuch_database_t *notmuch,
181                                       unsigned int doc_id);
182
183 notmuch_status_t
184 _notmuch_database_filename_to_direntry (void *ctx,
185                                         notmuch_database_t *notmuch,
186                                         const char *filename,
187                                         char **direntry);
188
189 /* directory.cc */
190
191 notmuch_directory_t *
192 _notmuch_directory_create (notmuch_database_t *notmuch,
193                            const char *path,
194                            notmuch_status_t *status_ret);
195
196 unsigned int
197 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
198
199 /* thread.cc */
200
201 notmuch_thread_t *
202 _notmuch_thread_create (void *ctx,
203                         notmuch_database_t *notmuch,
204                         const char *thread_id,
205                         const char *query_string);
206
207 /* message.cc */
208
209 notmuch_message_t *
210 _notmuch_message_create (const void *talloc_owner,
211                          notmuch_database_t *notmuch,
212                          unsigned int doc_id,
213                          notmuch_private_status_t *status);
214
215 notmuch_message_t *
216 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
217                                         const char *message_id,
218                                         notmuch_private_status_t *status);
219
220 const char *
221 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
222
223 notmuch_private_status_t
224 _notmuch_message_add_term (notmuch_message_t *message,
225                            const char *prefix_name,
226                            const char *value);
227
228 notmuch_private_status_t
229 _notmuch_message_remove_term (notmuch_message_t *message,
230                               const char *prefix_name,
231                               const char *value);
232
233 notmuch_private_status_t
234 _notmuch_message_gen_terms (notmuch_message_t *message,
235                             const char *prefix_name,
236                             const char *text);
237
238 notmuch_status_t
239 _notmuch_message_add_filename (notmuch_message_t *message,
240                                const char *filename);
241
242 void
243 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
244
245 void
246 _notmuch_message_set_date (notmuch_message_t *message,
247                            const char *date);
248
249 void
250 _notmuch_message_sync (notmuch_message_t *message);
251
252 void
253 _notmuch_message_close (notmuch_message_t *message);
254
255 /* index.cc */
256
257 notmuch_status_t
258 _notmuch_message_index_file (notmuch_message_t *message,
259                              const char *filename);
260
261 /* message-file.c */
262
263 /* XXX: I haven't decided yet whether these will actually get exported
264  * into the public interface in notmuch.h
265  */
266
267 typedef struct _notmuch_message_file notmuch_message_file_t;
268
269 /* Open a file containing a single email message.
270  *
271  * The caller should call notmuch_message_close when done with this.
272  *
273  * Returns NULL if any error occurs.
274  */
275 notmuch_message_file_t *
276 notmuch_message_file_open (const char *filename);
277
278 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
279 notmuch_message_file_t *
280 _notmuch_message_file_open_ctx (void *ctx, const char *filename);
281
282 /* Close a notmuch message previously opened with notmuch_message_open. */
283 void
284 notmuch_message_file_close (notmuch_message_file_t *message);
285
286 /* Restrict 'message' to only save the named headers.
287  *
288  * When the caller is only interested in a short list of headers,
289  * known in advance, calling this function can avoid wasted time and
290  * memory parsing/saving header values that will never be needed.
291  *
292  * The variable arguments should be a list of const char * with a
293  * final '(const char *) NULL' to terminate the list.
294  *
295  * If this function is called, it must be called before any calls to
296  * notmuch_message_get_header for this message.
297  *
298  * After calling this function, if notmuch_message_get_header is
299  * called with a header name not in this list, then NULL will be
300  * returned even if that header exists in the actual message.
301  */
302 void
303 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
304
305 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
306 void
307 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
308                                         va_list va_headers);
309
310 /* Get the value of the specified header from the message.
311  *
312  * The header name is case insensitive.
313  *
314  * The returned value is owned by the notmuch message and is valid
315  * only until the message is closed. The caller should copy it if
316  * needing to modify the value or to hold onto it for longer.
317  *
318  * Returns NULL if the message does not contain a header line matching
319  * 'header'.
320  */
321 const char *
322 notmuch_message_file_get_header (notmuch_message_file_t *message,
323                                  const char *header);
324
325 /* messages.c */
326
327 typedef struct _notmuch_message_node {
328     notmuch_message_t *message;
329     struct _notmuch_message_node *next;
330 } notmuch_message_node_t;
331
332 typedef struct _notmuch_message_list {
333     notmuch_message_node_t *head;
334     notmuch_message_node_t **tail;
335 } notmuch_message_list_t;
336
337 /* There's a rumor that there's an alternate struct _notmuch_messages
338  * somewhere with some nasty C++ objects in it. We'll try to maintain
339  * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
340  */
341 struct _notmuch_messages {
342     notmuch_bool_t is_of_list_type;
343     notmuch_message_node_t *iterator;
344 };
345
346 notmuch_message_list_t *
347 _notmuch_message_list_create (const void *ctx);
348
349 void
350 _notmuch_message_list_append (notmuch_message_list_t *list,
351                               notmuch_message_node_t *node);
352
353 void
354 _notmuch_message_list_add_message (notmuch_message_list_t *list,
355                                    notmuch_message_t *message);
356
357 notmuch_messages_t *
358 _notmuch_messages_create (notmuch_message_list_t *list);
359
360 /* query.cc */
361
362 notmuch_bool_t
363 _notmuch_mset_messages_has_more (notmuch_messages_t *messages);
364
365 notmuch_message_t *
366 _notmuch_mset_messages_get (notmuch_messages_t *messages);
367
368 void
369 _notmuch_mset_messages_advance (notmuch_messages_t *messages);
370
371 /* message.cc */
372
373 void
374 _notmuch_message_add_reply (notmuch_message_t *message,
375                             notmuch_message_node_t *reply);
376
377 /* sha1.c */
378
379 char *
380 notmuch_sha1_of_string (const char *str);
381
382 char *
383 notmuch_sha1_of_file (const char *filename);
384
385 /* tags.c */
386
387 notmuch_tags_t *
388 _notmuch_tags_create (void *ctx);
389
390 void
391 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
392
393 void
394 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
395
396 NOTMUCH_END_DECLS
397
398 #endif