1 /* notmuch-private.h - Internal interfaces for notmuch.
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #ifndef NOTMUCH_PRIVATE_H
22 #define NOTMUCH_PRIVATE_H
25 #define _GNU_SOURCE /* For getline and asprintf */
37 #include <sys/types.h>
52 # define DEBUG_DATABASE_SANITY 1
53 # define DEBUG_QUERY 1
56 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
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
62 * Note that PRINTF_ATTRIBUTE comes from talloc.h
65 _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);
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
71 * Note that __location__ comes from talloc.h.
73 #define INTERNAL_ERROR(format, ...) \
74 _internal_error (format " (%s).\n", \
75 ##__VA_ARGS__, __location__)
77 #define unused(x) x __attribute__ ((unused))
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.
82 /* these macros gain us a few percent of speed on gcc */
84 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
85 as its first argument */
87 #define likely(x) __builtin_expect(!!(x), 1)
90 #define unlikely(x) __builtin_expect(!!(x), 0)
97 #define unlikely(x) (x)
102 NOTMUCH_VALUE_TIMESTAMP = 0,
103 NOTMUCH_VALUE_MESSAGE_ID
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
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_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_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,
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,
126 NOTMUCH_PRIVATE_STATUS_LAST_STATUS
127 } notmuch_private_status_t;
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.)
135 #define COERCE_STATUS(private_status, format, ...) \
136 ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
138 (notmuch_status_t) _internal_error (format " (%s).\n", \
142 (notmuch_status_t) private_status)
146 /* Lookup a prefix value by name.
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. */
152 _find_prefix (const char *name);
155 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
158 _notmuch_database_relative_path (notmuch_database_t *notmuch,
162 _notmuch_database_split_path (void *ctx,
164 const char **directory,
165 const char **basename);
168 _notmuch_database_get_directory_db_path (const char *path);
171 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
173 notmuch_private_status_t
174 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
175 const char *prefix_name,
177 unsigned int *doc_id);
180 _notmuch_database_find_directory_id (notmuch_database_t *database,
182 unsigned int *directory_id);
185 _notmuch_database_get_directory_path (void *ctx,
186 notmuch_database_t *notmuch,
187 unsigned int doc_id);
190 _notmuch_database_filename_to_direntry (void *ctx,
191 notmuch_database_t *notmuch,
192 const char *filename,
197 notmuch_directory_t *
198 _notmuch_directory_create (notmuch_database_t *notmuch,
200 notmuch_status_t *status_ret);
203 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
208 _notmuch_thread_create (void *ctx,
209 notmuch_database_t *notmuch,
210 const char *thread_id,
211 const char *query_string,
212 notmuch_sort_t sort);
217 _notmuch_message_create (const void *talloc_owner,
218 notmuch_database_t *notmuch,
220 notmuch_private_status_t *status);
223 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
224 const char *message_id,
225 notmuch_private_status_t *status);
228 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
230 notmuch_private_status_t
231 _notmuch_message_add_term (notmuch_message_t *message,
232 const char *prefix_name,
235 notmuch_private_status_t
236 _notmuch_message_remove_term (notmuch_message_t *message,
237 const char *prefix_name,
240 notmuch_private_status_t
241 _notmuch_message_gen_terms (notmuch_message_t *message,
242 const char *prefix_name,
246 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
249 _notmuch_message_add_filename (notmuch_message_t *message,
250 const char *filename);
253 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
256 _notmuch_message_set_date (notmuch_message_t *message,
260 _notmuch_message_sync (notmuch_message_t *message);
263 _notmuch_message_close (notmuch_message_t *message);
265 /* Get a copy of the data in this message document.
267 * Caller should talloc_free the result when done.
269 * This function is intended to support database upgrade and really
270 * shouldn't be used otherwise. */
272 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
274 /* Clear the data in this message document.
276 * This function is intended to support database upgrade and really
277 * shouldn't be used otherwise. */
279 _notmuch_message_clear_data (notmuch_message_t *message);
281 /* Set the author member of 'message' - this is the representation used
282 * when displaying the message */
284 notmuch_message_set_author (notmuch_message_t *message, const char *author);
286 /* Get the author member of 'message' */
288 notmuch_message_get_author (notmuch_message_t *message);
294 _notmuch_message_index_file (notmuch_message_t *message,
295 const char *filename);
299 /* XXX: I haven't decided yet whether these will actually get exported
300 * into the public interface in notmuch.h
303 typedef struct _notmuch_message_file notmuch_message_file_t;
305 /* Open a file containing a single email message.
307 * The caller should call notmuch_message_close when done with this.
309 * Returns NULL if any error occurs.
311 notmuch_message_file_t *
312 notmuch_message_file_open (const char *filename);
314 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
315 notmuch_message_file_t *
316 _notmuch_message_file_open_ctx (void *ctx, const char *filename);
318 /* Close a notmuch message previously opened with notmuch_message_open. */
320 notmuch_message_file_close (notmuch_message_file_t *message);
322 /* Restrict 'message' to only save the named headers.
324 * When the caller is only interested in a short list of headers,
325 * known in advance, calling this function can avoid wasted time and
326 * memory parsing/saving header values that will never be needed.
328 * The variable arguments should be a list of const char * with a
329 * final '(const char *) NULL' to terminate the list.
331 * If this function is called, it must be called before any calls to
332 * notmuch_message_get_header for this message.
334 * After calling this function, if notmuch_message_get_header is
335 * called with a header name not in this list, then NULL will be
336 * returned even if that header exists in the actual message.
339 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
341 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
343 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
346 /* Get the value of the specified header from the message.
348 * The header name is case insensitive.
350 * The Received: header is special - for it all Received: headers in
351 * the message are concatenated
353 * The returned value is owned by the notmuch message and is valid
354 * only until the message is closed. The caller should copy it if
355 * needing to modify the value or to hold onto it for longer.
357 * Returns NULL if the message does not contain a header line matching
361 notmuch_message_file_get_header (notmuch_message_file_t *message,
366 typedef struct _notmuch_message_node {
367 notmuch_message_t *message;
368 struct _notmuch_message_node *next;
369 } notmuch_message_node_t;
371 typedef struct _notmuch_message_list {
372 notmuch_message_node_t *head;
373 notmuch_message_node_t **tail;
374 } notmuch_message_list_t;
376 /* There's a rumor that there's an alternate struct _notmuch_messages
377 * somewhere with some nasty C++ objects in it. We'll try to maintain
378 * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
380 struct _notmuch_messages {
381 notmuch_bool_t is_of_list_type;
382 notmuch_message_node_t *iterator;
385 notmuch_message_list_t *
386 _notmuch_message_list_create (const void *ctx);
389 _notmuch_message_list_append (notmuch_message_list_t *list,
390 notmuch_message_node_t *node);
393 _notmuch_message_list_add_message (notmuch_message_list_t *list,
394 notmuch_message_t *message);
397 _notmuch_messages_create (notmuch_message_list_t *list);
402 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
405 _notmuch_mset_messages_get (notmuch_messages_t *messages);
408 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
413 _notmuch_message_add_reply (notmuch_message_t *message,
414 notmuch_message_node_t *reply);
419 notmuch_sha1_of_string (const char *str);
422 notmuch_sha1_of_file (const char *filename);
427 _notmuch_tags_create (void *ctx);
430 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
433 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);