]> git.notmuchmail.org Git - notmuch/blob - notmuch-private.h
Add an INTERNAL_ERROR macro and use it for all internal errors.
[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 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
50
51 /* There's no point in continuing when we've detected that we've done
52  * something wrong internally (as opposed to the user passing in a
53  * bogus value).
54  *
55  * Note that __location__ comes from talloc.h.
56  */
57 #define INTERNAL_ERROR(format, ...)                     \
58     do {                                                \
59         fprintf(stderr,                                 \
60                 "Internal error: " format " (%s)\n",    \
61                 ##__VA_ARGS__, __location__);           \
62         exit (1);                                       \
63     } while (0)
64
65 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
66  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
67  */
68 /* these macros gain us a few percent of speed on gcc */
69 #if (__GNUC__ >= 3)
70 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
71    as its first argument */
72 #ifndef likely
73 #define likely(x)   __builtin_expect(!!(x), 1)
74 #endif
75 #ifndef unlikely
76 #define unlikely(x) __builtin_expect(!!(x), 0)
77 #endif
78 #else
79 #ifndef likely
80 #define likely(x) (x)
81 #endif
82 #ifndef unlikely
83 #define unlikely(x) (x)
84 #endif
85 #endif
86
87 typedef enum {
88     NOTMUCH_VALUE_TIMESTAMP = 0,
89     NOTMUCH_VALUE_MESSAGE_ID
90 } notmuch_value_t;
91
92 /* Xapian (with flint backend) complains if we provide a term longer
93  * than this, but I haven't yet found a way to query the limit
94  * programmatically. */
95 #define NOTMUCH_TERM_MAX 245
96
97 typedef enum _notmuch_private_status {
98     /* First, copy all the public status values. */
99     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
100     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
101     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
102     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
103     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
104     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
105
106     /* Then add our own private values. */
107     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG,
108     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
109
110     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
111 } notmuch_private_status_t;
112
113 /* message.cc */
114
115 notmuch_message_t *
116 _notmuch_message_create (const void *talloc_owner,
117                          notmuch_database_t *notmuch,
118                          unsigned int doc_id,
119                          notmuch_private_status_t *status);
120
121 notmuch_message_t *
122 _notmuch_message_create_for_message_id (const void *talloc_owner,
123                                         notmuch_database_t *notmuch,
124                                         const char *message_id,
125                                         notmuch_status_t *status);
126
127 /* Lookup a prefix value by name.
128  *
129  * XXX: This should really be static inside of message.cc, and we can
130  * do that once we convert database.cc to use the
131  * _notmuch_message_add/remove_term functions. */
132 const char *
133 _find_prefix (const char *name);
134
135 notmuch_private_status_t
136 _notmuch_message_add_term (notmuch_message_t *message,
137                            const char *prefix_name,
138                            const char *value);
139
140 notmuch_private_status_t
141 _notmuch_message_remove_term (notmuch_message_t *message,
142                               const char *prefix_name,
143                               const char *value);
144
145 void
146 _notmuch_message_set_filename (notmuch_message_t *message,
147                                const char *filename);
148
149 void
150 _notmuch_message_add_thread_id (notmuch_message_t *message,
151                                 const char *thread_id);
152
153 void
154 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
155
156 void
157 _notmuch_message_set_date (notmuch_message_t *message,
158                            const char *date);
159
160 void
161 _notmuch_message_sync (notmuch_message_t *message);
162
163 /* message-file.c */
164
165 /* XXX: I haven't decided yet whether these will actually get exported
166  * into the public interface in notmuch.h
167  */
168
169 typedef struct _notmuch_message_file notmuch_message_file_t;
170
171 /* Open a file containing a single email message.
172  *
173  * The caller should call notmuch_message_close when done with this.
174  *
175  * Returns NULL if any error occurs.
176  */
177 notmuch_message_file_t *
178 notmuch_message_file_open (const char *filename);
179
180 /* Close a notmuch message preivously opened with notmuch_message_open. */
181 void
182 notmuch_message_file_close (notmuch_message_file_t *message);
183
184 /* Restrict 'message' to only save the named headers.
185  *
186  * When the caller is only interested in a short list of headers,
187  * known in advance, calling this function can avoid wasted time and
188  * memory parsing/saving header values that will never be needed.
189  *
190  * The variable arguments should be a list of const char * with a
191  * final '(const char *) NULL' to terminate the list.
192  *
193  * If this function is called, it must be called before any calls to
194  * notmuch_message_get_header for this message.
195  *
196  * After calling this function, if notmuch_message_get_header is
197  * called with a header name not in this list, then NULL will be
198  * returned even if that header exists in the actual message.
199  */
200 void
201 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
202
203 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
204 void
205 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
206                                         va_list va_headers);
207
208 /* Get the value of the specified header from the message.
209  *
210  * The header name is case insensitive.
211  *
212  * The returned value is owned by the notmuch message and is valid
213  * only until the message is closed. The caller should copy it if
214  * needing to modify the value or to hold onto it for longer.
215  *
216  * Returns NULL if the message does not contain a header line matching
217  * 'header'.
218  */
219 const char *
220 notmuch_message_file_get_header (notmuch_message_file_t *message,
221                                  const char *header);
222
223 /* date.c */
224
225 /* Parse an RFC 8222 date string to a time_t value.
226  *
227  * The tz_offset argument can be used to also obtain the time-zone
228  * offset, (but can be NULL if the call is not interested in that).
229  *
230  * Returns 0 on error.
231  */
232 time_t
233 notmuch_parse_date (const char *str, int *tz_offset);
234
235 /* sha1.c */
236
237 char *
238 notmuch_sha1_of_string (const char *str);
239
240 char *
241 notmuch_sha1_of_file (const char *filename);
242
243 NOTMUCH_END_DECLS
244
245 #endif