]> git.notmuchmail.org Git - notmuch/blobdiff - message-file.c
Add public notmuch_thread_get_subject
[notmuch] / message-file.c
index fd7e68b48ed8d276890adac7eb8a31fedeb5120b..18275fbf853fc89ecb4c5b703c34a5a9b084e995 100644 (file)
@@ -70,12 +70,36 @@ strcase_hash (const void *ptr)
     return hash;
 }
 
+static int
+_notmuch_message_file_destructor (notmuch_message_file_t *message)
+{
+    if (message->line)
+       free (message->line);
+
+    if (message->value.size)
+       free (message->value.str);
+
+    if (message->headers)
+       g_hash_table_destroy (message->headers);
+
+    if (message->file)
+       fclose (message->file);
+
+    return 0;
+}
+
+/* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
+ * the talloc owner. */
 notmuch_message_file_t *
-notmuch_message_file_open (const char *filename)
+_notmuch_message_file_open_ctx (void *ctx, const char *filename)
 {
     notmuch_message_file_t *message;
 
-    message = xcalloc (1, sizeof (notmuch_message_file_t));
+    message = talloc_zero (ctx, notmuch_message_file_t);
+    if (unlikely (message == NULL))
+       return NULL;
+
+    talloc_set_destructor (message, _notmuch_message_file_destructor);
 
     message->file = fopen (filename, "r");
     if (message->file == NULL)
@@ -98,25 +122,16 @@ notmuch_message_file_open (const char *filename)
     return NULL;
 }
 
+notmuch_message_file_t *
+notmuch_message_file_open (const char *filename)
+{
+    return _notmuch_message_file_open_ctx (NULL, filename);
+}
+
 void
 notmuch_message_file_close (notmuch_message_file_t *message)
 {
-    if (message == NULL)
-       return;
-
-    if (message->line)
-       free (message->line);
-
-    if (message->value.size)
-       free (message->value.str);
-
-    if (message->headers)
-       g_hash_table_destroy (message->headers);
-
-    if (message->file)
-       fclose (message->file);
-
-    free (message);
+    talloc_free (message);
 }
 
 void
@@ -125,10 +140,8 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
 {
     char *header;
 
-    if (message->parsing_started ) {
-       fprintf (stderr, "Error: notmuch_message_file_restrict_headers called after parsing has started\n");
-       exit (1);
-    }
+    if (message->parsing_started)
+       INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started");
 
     while (1) {
        header = va_arg (va_headers, char*);
@@ -151,7 +164,7 @@ notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)
     notmuch_message_file_restrict_headersv (message, va_headers);
 }
 
-void
+static void
 copy_header_unfolding (header_value_closure_t *value,
                       const char *chunk)
 {
@@ -164,7 +177,7 @@ copy_header_unfolding (header_value_closure_t *value,
        chunk++;
 
     if (value->len + 1 + strlen (chunk) + 1 > value->size) {
-       int new_size = value->size;
+       unsigned int new_size = value->size;
        if (value->size == 0)
            new_size = strlen (chunk) + 1;
        else
@@ -305,11 +318,9 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
        ! g_hash_table_lookup_extended (message->headers,
                                        header_desired, NULL, NULL))
     {
-       fprintf (stderr,
-                "Internal error: Attempt to get header \"%s\" which was not\n"
-                "included in call to notmuch_message_file_restrict_headers\n",
-                header_desired);
-       exit (1);
+       INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n"
+                       "included in call to notmuch_message_file_restrict_headers\n",
+                       header_desired);
     }
 
     return NULL;