From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org
Subject: [PATCH v2 6/7] lib: parse messages only once
Date: Sat, 30 Nov 2013 17:33:55 +0200 [thread overview]
Message-ID: <36ef439cffa92d7889a5b1ae984016d3e944edc1.1385825425.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1385825425.git.jani@nikula.org>
In-Reply-To: <cover.1385825425.git.jani@nikula.org>
Make the necessary changes to only do one gmime parse pass during
indexing.
---
lib/database.cc | 2 +-
lib/index.cc | 59 ++++++---------------------------------------------
lib/message-file.c | 9 ++++++++
lib/notmuch-private.h | 16 ++++++++++++--
4 files changed, 30 insertions(+), 56 deletions(-)
diff --git a/lib/database.cc b/lib/database.cc
index d1bea88..3a29fe7 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -2029,7 +2029,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
date = notmuch_message_file_get_header (message_file, "date");
_notmuch_message_set_header_values (message, date, from, subject);
- ret = _notmuch_message_index_file (message, filename);
+ ret = _notmuch_message_index_file (message, message_file);
if (ret)
goto DONE;
} else {
diff --git a/lib/index.cc b/lib/index.cc
index 976e49f..71397da 100644
--- a/lib/index.cc
+++ b/lib/index.cc
@@ -425,52 +425,15 @@ _index_mime_part (notmuch_message_t *message,
notmuch_status_t
_notmuch_message_index_file (notmuch_message_t *message,
- const char *filename)
+ notmuch_message_file_t *message_file)
{
- GMimeStream *stream = NULL;
- GMimeParser *parser = NULL;
- GMimeMessage *mime_message = NULL;
+ GMimeMessage *mime_message;
InternetAddressList *addresses;
- FILE *file = NULL;
const char *from, *subject;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
- static int initialized = 0;
- char from_buf[5];
- bool is_mbox = false;
-
- if (! initialized) {
- g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
- initialized = 1;
- }
-
- file = fopen (filename, "r");
- if (! file) {
- fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
- ret = NOTMUCH_STATUS_FILE_ERROR;
- goto DONE;
- }
-
- /* Is this mbox? */
- if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
- strncmp (from_buf, "From ", 5) == 0)
- is_mbox = true;
- rewind (file);
-
- /* Evil GMime steals my FILE* here so I won't fclose it. */
- stream = g_mime_stream_file_new (file);
-
- parser = g_mime_parser_new_with_stream (stream);
- g_mime_parser_set_scan_from (parser, is_mbox);
- mime_message = g_mime_parser_construct_message (parser);
-
- if (is_mbox) {
- if (!g_mime_parser_eos (parser)) {
- /* This is a multi-message mbox. */
- ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
- goto DONE;
- }
- }
+ mime_message = notmuch_message_file_get_mime_message (message_file);
+ if (! mime_message)
+ return NOTMUCH_STATUS_FILE_NOT_EMAIL; /* more like internal error */
from = g_mime_message_get_sender (mime_message);
@@ -491,15 +454,5 @@ _notmuch_message_index_file (notmuch_message_t *message,
_index_mime_part (message, g_mime_message_get_mime_part (mime_message));
- DONE:
- if (mime_message)
- g_object_unref (mime_message);
-
- if (parser)
- g_object_unref (parser);
-
- if (stream)
- g_object_unref (stream);
-
- return ret;
+ return NOTMUCH_STATUS_SUCCESS;
}
diff --git a/lib/message-file.c b/lib/message-file.c
index 3c653d1..0bd8c2e 100644
--- a/lib/message-file.c
+++ b/lib/message-file.c
@@ -247,6 +247,15 @@ mboxes is deprecated and may be removed in the future.\n", message->filename);
return NOTMUCH_STATUS_SUCCESS;
}
+GMimeMessage *
+notmuch_message_file_get_mime_message (notmuch_message_file_t *message)
+{
+ if (! message->parsed)
+ return NULL;
+
+ return message->message;
+}
+
/* return NULL on errors, empty string for non-existing headers */
const char *
notmuch_message_file_get_header (notmuch_message_file_t *message,
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 7277df1..7559521 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -46,6 +46,8 @@ NOTMUCH_BEGIN_DECLS
#include <talloc.h>
+#include <gmime/gmime.h>
+
#include "xutil.h"
#include "error_util.h"
@@ -320,9 +322,11 @@ notmuch_message_get_author (notmuch_message_t *message);
/* index.cc */
+typedef struct _notmuch_message_file notmuch_message_file_t;
+
notmuch_status_t
_notmuch_message_index_file (notmuch_message_t *message,
- const char *filename);
+ notmuch_message_file_t *message_file);
/* message-file.c */
@@ -330,7 +334,6 @@ _notmuch_message_index_file (notmuch_message_t *message,
* into the public interface in notmuch.h
*/
-typedef struct _notmuch_message_file notmuch_message_file_t;
/* Open a file containing a single email message.
*
@@ -377,6 +380,15 @@ void
notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
va_list va_headers);
+/* Get the gmime message of a parsed message file.
+ *
+ * Returns NULL if the message file has not been parsed.
+ *
+ * XXX: Would be nice to not have to expose GMimeMessage here.
+ */
+GMimeMessage *
+notmuch_message_file_get_mime_message (notmuch_message_file_t *message);
+
/* Get the value of the specified header from the message as a UTF-8 string.
*
* The header name is case insensitive.
--
1.8.4.2
next prev parent reply other threads:[~2013-11-30 15:34 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-30 15:33 [PATCH v2 0/7] lib: replace the message header parser with gmime Jani Nikula
2013-11-30 15:33 ` [PATCH v2 1/7] cli: sanitize tabs and newlines to spaces in notmuch search Jani Nikula
2013-11-30 15:33 ` [PATCH v2 2/7] cli: refactor reply from guessing Jani Nikula
2014-02-02 18:21 ` Mark Walters
2013-11-30 15:33 ` [PATCH v2 3/7] util: make sanitize string available in string util for reuse Jani Nikula
2014-02-02 18:24 ` Mark Walters
2013-11-30 15:33 ` [PATCH v2 4/7] cli: sanitize the received header before scanning for replies Jani Nikula
2013-11-30 15:33 ` [PATCH v2 5/7] lib: replace the header parser with gmime Jani Nikula
2013-11-30 15:33 ` Jani Nikula [this message]
2013-11-30 15:33 ` [PATCH v2 7/7] HACK: fix broken messages in the perf test corpus Jani Nikula
2013-11-30 17:48 ` David Bremner
2014-01-15 18:03 ` [PATCH v2 0/7] lib: replace the message header parser with gmime David Bremner
2014-02-02 13:03 ` Jani Nikula
2014-02-02 18:15 ` Mark Walters
2014-02-02 19:32 ` Jani Nikula
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=36ef439cffa92d7889a5b1ae984016d3e944edc1.1385825425.git.jani@nikula.org \
--to=jani@nikula.org \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).