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>
51 #pragma GCC visibility push(hidden)
54 # define DEBUG_DATABASE_SANITY 1
55 # define DEBUG_QUERY 1
58 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
60 /* There's no point in continuing when we've detected that we've done
61 * something wrong internally (as opposed to the user passing in a
64 * Note that PRINTF_ATTRIBUTE comes from talloc.h
67 _internal_error (const char *format, ...) PRINTF_ATTRIBUTE (1, 2);
69 /* There's no point in continuing when we've detected that we've done
70 * something wrong internally (as opposed to the user passing in a
73 * Note that __location__ comes from talloc.h.
75 #define INTERNAL_ERROR(format, ...) \
76 _internal_error (format " (%s).\n", \
77 ##__VA_ARGS__, __location__)
79 #define unused(x) x __attribute__ ((unused))
81 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
82 * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
84 /* these macros gain us a few percent of speed on gcc */
86 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
87 as its first argument */
89 #define likely(x) __builtin_expect(!!(x), 1)
92 #define unlikely(x) __builtin_expect(!!(x), 0)
99 #define unlikely(x) (x)
104 NOTMUCH_VALUE_TIMESTAMP = 0,
105 NOTMUCH_VALUE_MESSAGE_ID
108 /* Xapian (with flint backend) complains if we provide a term longer
109 * than this, but I haven't yet found a way to query the limit
110 * programmatically. */
111 #define NOTMUCH_TERM_MAX 245
113 #define NOTMUCH_METADATA_THREAD_ID_PREFIX "thread_id_"
115 /* For message IDs we have to be even more restrictive. Beyond fitting
116 * into the term limit, we also use message IDs to construct
117 * metadata-key values. And the documentation says that these should
118 * be restricted to about 200 characters. (The actual limit for the
119 * chert backend at least is 252.)
121 #define NOTMUCH_MESSAGE_ID_MAX (200 - sizeof (NOTMUCH_METADATA_THREAD_ID_PREFIX))
123 typedef enum _notmuch_private_status {
124 /* First, copy all the public status values. */
125 NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
126 NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
127 NOTMUCH_PRIVATE_STATUS_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_DATABASE,
128 NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
129 NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
130 NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
131 NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
132 NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
134 /* Then add our own private values. */
135 NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
136 NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
138 NOTMUCH_PRIVATE_STATUS_LAST_STATUS
139 } notmuch_private_status_t;
141 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
142 * value, generating an internal error if the private value is equal
143 * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
144 * that the caller has previously handled any expected
145 * notmuch_private_status_t values.)
147 #define COERCE_STATUS(private_status, format, ...) \
148 ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
150 (notmuch_status_t) _internal_error (format " (%s).\n", \
154 (notmuch_status_t) private_status)
158 /* Lookup a prefix value by name.
160 * XXX: This should really be static inside of message.cc, and we can
161 * do that once we convert database.cc to use the
162 * _notmuch_message_add/remove_term functions. */
164 _find_prefix (const char *name);
167 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
170 _notmuch_database_relative_path (notmuch_database_t *notmuch,
174 _notmuch_database_split_path (void *ctx,
176 const char **directory,
177 const char **basename);
180 _notmuch_database_get_directory_db_path (const char *path);
183 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
185 notmuch_private_status_t
186 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
187 const char *prefix_name,
189 unsigned int *doc_id);
192 _notmuch_database_find_directory_id (notmuch_database_t *database,
194 unsigned int *directory_id);
197 _notmuch_database_get_directory_path (void *ctx,
198 notmuch_database_t *notmuch,
199 unsigned int doc_id);
202 _notmuch_database_filename_to_direntry (void *ctx,
203 notmuch_database_t *notmuch,
204 const char *filename,
209 notmuch_directory_t *
210 _notmuch_directory_create (notmuch_database_t *notmuch,
212 notmuch_status_t *status_ret);
215 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
220 _notmuch_thread_create (void *ctx,
221 notmuch_database_t *notmuch,
222 const char *thread_id,
223 const char *query_string,
224 notmuch_sort_t sort);
229 _notmuch_message_create (const void *talloc_owner,
230 notmuch_database_t *notmuch,
232 notmuch_private_status_t *status);
235 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
236 const char *message_id,
237 notmuch_private_status_t *status);
240 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
242 notmuch_private_status_t
243 _notmuch_message_add_term (notmuch_message_t *message,
244 const char *prefix_name,
247 notmuch_private_status_t
248 _notmuch_message_remove_term (notmuch_message_t *message,
249 const char *prefix_name,
252 notmuch_private_status_t
253 _notmuch_message_gen_terms (notmuch_message_t *message,
254 const char *prefix_name,
258 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
261 _notmuch_message_add_filename (notmuch_message_t *message,
262 const char *filename);
265 _notmuch_message_rename (notmuch_message_t *message,
266 const char *new_filename);
269 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
272 _notmuch_message_set_date (notmuch_message_t *message,
276 _notmuch_message_sync (notmuch_message_t *message);
279 _notmuch_message_close (notmuch_message_t *message);
281 /* Get a copy of the data in this message document.
283 * Caller should talloc_free the result when done.
285 * This function is intended to support database upgrade and really
286 * shouldn't be used otherwise. */
288 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
290 /* Clear the data in this message document.
292 * This function is intended to support database upgrade and really
293 * shouldn't be used otherwise. */
295 _notmuch_message_clear_data (notmuch_message_t *message);
297 /* Set the author member of 'message' - this is the representation used
298 * when displaying the message */
300 notmuch_message_set_author (notmuch_message_t *message, const char *author);
302 /* Get the author member of 'message' */
304 notmuch_message_get_author (notmuch_message_t *message);
310 _notmuch_message_index_file (notmuch_message_t *message,
311 const char *filename);
315 /* XXX: I haven't decided yet whether these will actually get exported
316 * into the public interface in notmuch.h
319 typedef struct _notmuch_message_file notmuch_message_file_t;
321 /* Open a file containing a single email message.
323 * The caller should call notmuch_message_close when done with this.
325 * Returns NULL if any error occurs.
327 notmuch_message_file_t *
328 notmuch_message_file_open (const char *filename);
330 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
331 notmuch_message_file_t *
332 _notmuch_message_file_open_ctx (void *ctx, const char *filename);
334 /* Close a notmuch message previously opened with notmuch_message_open. */
336 notmuch_message_file_close (notmuch_message_file_t *message);
338 /* Restrict 'message' to only save the named headers.
340 * When the caller is only interested in a short list of headers,
341 * known in advance, calling this function can avoid wasted time and
342 * memory parsing/saving header values that will never be needed.
344 * The variable arguments should be a list of const char * with a
345 * final '(const char *) NULL' to terminate the list.
347 * If this function is called, it must be called before any calls to
348 * notmuch_message_get_header for this message.
350 * After calling this function, if notmuch_message_get_header is
351 * called with a header name not in this list, then NULL will be
352 * returned even if that header exists in the actual message.
355 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);
357 /* Identical to notmuch_message_restrict_headers but accepting a va_list. */
359 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
362 /* Get the value of the specified header from the message.
364 * The header name is case insensitive.
366 * The Received: header is special - for it all Received: headers in
367 * the message are concatenated
369 * The returned value is owned by the notmuch message and is valid
370 * only until the message is closed. The caller should copy it if
371 * needing to modify the value or to hold onto it for longer.
373 * Returns NULL if the message does not contain a header line matching
377 notmuch_message_file_get_header (notmuch_message_file_t *message,
382 typedef struct _notmuch_message_node {
383 notmuch_message_t *message;
384 struct _notmuch_message_node *next;
385 } notmuch_message_node_t;
387 typedef struct _notmuch_message_list {
388 notmuch_message_node_t *head;
389 notmuch_message_node_t **tail;
390 } notmuch_message_list_t;
392 /* There's a rumor that there's an alternate struct _notmuch_messages
393 * somewhere with some nasty C++ objects in it. We'll try to maintain
394 * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
396 struct _notmuch_messages {
397 notmuch_bool_t is_of_list_type;
398 notmuch_message_node_t *iterator;
401 notmuch_message_list_t *
402 _notmuch_message_list_create (const void *ctx);
405 _notmuch_message_list_append (notmuch_message_list_t *list,
406 notmuch_message_node_t *node);
409 _notmuch_message_list_add_message (notmuch_message_list_t *list,
410 notmuch_message_t *message);
413 _notmuch_messages_create (notmuch_message_list_t *list);
418 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
421 _notmuch_mset_messages_get (notmuch_messages_t *messages);
424 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
429 _notmuch_message_add_reply (notmuch_message_t *message,
430 notmuch_message_node_t *reply);
435 notmuch_sha1_of_string (const char *str);
438 notmuch_sha1_of_file (const char *filename);
443 _notmuch_tags_create (void *ctx);
446 _notmuch_tags_add_tag (notmuch_tags_t *tags, const char *tag);
449 _notmuch_tags_prepare_iterator (notmuch_tags_t *tags);
453 typedef struct _notmuch_filename_node {
455 struct _notmuch_filename_node *next;
456 } notmuch_filename_node_t;
458 typedef struct _notmuch_filename_list {
459 notmuch_filename_node_t *head;
460 notmuch_filename_node_t **tail;
461 } notmuch_filename_list_t;
463 notmuch_filename_list_t *
464 _notmuch_filename_list_create (const void *ctx);
466 /* Add 'filename' to 'list'.
468 * The list will create its own talloced copy of 'filename'.
471 _notmuch_filename_list_add_filename (notmuch_filename_list_t *list,
472 const char *filename);
475 _notmuch_filename_list_destroy (notmuch_filename_list_t *list);
477 /* The notmuch_filenames_t is an iterator object for a
478 * notmuch_filename_list_t */
479 notmuch_filenames_t *
480 _notmuch_filenames_create (const void *ctx,
481 notmuch_filename_list_t *list);
483 #pragma GCC visibility pop