[PATCH 5/6] lib: parse messages only once
[notmuch-archives.git] / 92 / d95cbecffde030005c72ec9451f92b3e168fd1
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 4723A431FC9\r
6         for <notmuch@notmuchmail.org>; Wed, 16 Oct 2013 12:00:56 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 4xnvVTdj44HF for <notmuch@notmuchmail.org>;\r
17         Wed, 16 Oct 2013 12:00:50 -0700 (PDT)\r
18 Received: from mail-ee0-f47.google.com (mail-ee0-f47.google.com\r
19  [74.125.83.47])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
20  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
21  89CF6431FCB    for <notmuch@notmuchmail.org>; Wed, 16 Oct 2013 12:00:32 -0700\r
22  (PDT)\r
23 Received: by mail-ee0-f47.google.com with SMTP id d49so580916eek.34\r
24         for <notmuch@notmuchmail.org>; Wed, 16 Oct 2013 12:00:31 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
28         :references:in-reply-to:references;\r
29         bh=IH2SnVvGIXirOVHbhtKFi5hslXx4SV/xHhXd74UpFvw=;\r
30         b=YT6uMPJLlKkalWG/pBb1nW+FcpqJZ+Kkmb5kWGEWuUCTtqzNt+YCvtJzN78vD818I/\r
31         d4LsM7VZjcYakb290At19+aTPBGc6POz8fjkC4Pp+yaPAT6HJoOEDaJUe4NH80hXlUYn\r
32         7smZCIP6q+81YqiS9PAuPs1pQGjZiBUPADxO5vp/tgjeFxpTwbwbf2uioihTx1hG0eti\r
33         MsMEF9EM26DtYYlbT/pm4a3/O9m/zrpDcsOpbtnNIKxk7qdFW0GoqKRf1YJOYt1Y4bhg\r
34         1K5za0pRzGuKUav45u90UpEtqicxMHI1Kt5+4bs7YHgWMuoOUpvprsrXc2qr9Ifv5fbD\r
35         pr+g==\r
36 X-Gm-Message-State:\r
37  ALoCoQnce8xInrJkX9gAilVZY8VijkrB49iW7IgcEFKIuSiQgeOWIQkL9x49RomZdCklShSqp0Rv\r
38 X-Received: by 10.15.53.132 with SMTP id r4mr6878687eew.5.1381950031441;\r
39         Wed, 16 Oct 2013 12:00:31 -0700 (PDT)\r
40 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
41         [88.195.111.91]) by mx.google.com with ESMTPSA id\r
42         m54sm182872688eex.2.1969.12.31.16.00.00\r
43         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
44         Wed, 16 Oct 2013 12:00:30 -0700 (PDT)\r
45 From: Jani Nikula <jani@nikula.org>\r
46 To: notmuch@notmuchmail.org\r
47 Subject: [PATCH 5/6] lib: parse messages only once\r
48 Date: Wed, 16 Oct 2013 22:00:12 +0300\r
49 Message-Id:\r
50  <f499e217d35c69b3d02b9d455a6adbccf241d4f8.1381948853.git.jani@nikula.org>\r
51 X-Mailer: git-send-email 1.8.4.rc3\r
52 In-Reply-To: <cover.1381948853.git.jani@nikula.org>\r
53 References: <cover.1381948853.git.jani@nikula.org>\r
54 In-Reply-To: <cover.1381948853.git.jani@nikula.org>\r
55 References: <cover.1381948853.git.jani@nikula.org>\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Wed, 16 Oct 2013 19:00:56 -0000\r
69 \r
70 Make the necessary changes to only do one gmime parse pass during\r
71 indexing.\r
72 ---\r
73  lib/database.cc       |  2 +-\r
74  lib/index.cc          | 70 +++++----------------------------------------------\r
75  lib/message-file.c    |  9 +++++++\r
76  lib/notmuch-private.h | 15 +++++++++--\r
77  4 files changed, 29 insertions(+), 67 deletions(-)\r
78 \r
79 diff --git a/lib/database.cc b/lib/database.cc\r
80 index 45a3987..d097dda 100644\r
81 --- a/lib/database.cc\r
82 +++ b/lib/database.cc\r
83 @@ -1996,7 +1996,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
84             date = notmuch_message_file_get_header (message_file, "date");\r
85             _notmuch_message_set_header_values (message, date, from, subject);\r
86  \r
87 -           ret = _notmuch_message_index_file (message, filename);\r
88 +           ret = _notmuch_message_index_file (message, message_file);\r
89             if (ret)\r
90                 goto DONE;\r
91         } else {\r
92 diff --git a/lib/index.cc b/lib/index.cc\r
93 index 78c18cf..71397da 100644\r
94 --- a/lib/index.cc\r
95 +++ b/lib/index.cc\r
96 @@ -425,63 +425,15 @@ _index_mime_part (notmuch_message_t *message,\r
97  \r
98  notmuch_status_t\r
99  _notmuch_message_index_file (notmuch_message_t *message,\r
100 -                            const char *filename)\r
101 +                            notmuch_message_file_t *message_file)\r
102  {\r
103 -    GMimeStream *stream = NULL;\r
104 -    GMimeParser *parser = NULL;\r
105 -    GMimeMessage *mime_message = NULL;\r
106 +    GMimeMessage *mime_message;\r
107      InternetAddressList *addresses;\r
108 -    FILE *file = NULL;\r
109      const char *from, *subject;\r
110 -    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
111 -    static int initialized = 0;\r
112 -    char from_buf[5];\r
113 -    bool is_mbox = false;\r
114 -    static bool mbox_warning = false;\r
115 -\r
116 -    if (! initialized) {\r
117 -       g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
118 -       initialized = 1;\r
119 -    }\r
120 -\r
121 -    file = fopen (filename, "r");\r
122 -    if (! file) {\r
123 -       fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
124 -       ret = NOTMUCH_STATUS_FILE_ERROR;\r
125 -       goto DONE;\r
126 -    }\r
127 -\r
128 -    /* Is this mbox? */\r
129 -    if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&\r
130 -       strncmp (from_buf, "From ", 5) == 0)\r
131 -       is_mbox = true;\r
132 -    rewind (file);\r
133  \r
134 -    /* Evil GMime steals my FILE* here so I won't fclose it. */\r
135 -    stream = g_mime_stream_file_new (file);\r
136 -\r
137 -    parser = g_mime_parser_new_with_stream (stream);\r
138 -    g_mime_parser_set_scan_from (parser, is_mbox);\r
139 -\r
140 -    mime_message = g_mime_parser_construct_message (parser);\r
141 -\r
142 -    if (is_mbox) {\r
143 -       if (!g_mime_parser_eos (parser)) {\r
144 -           /* This is a multi-message mbox. */\r
145 -           ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
146 -           goto DONE;\r
147 -       }\r
148 -       /* For historical reasons, we support single-message mboxes,\r
149 -        * but this behavior is likely to change in the future, so\r
150 -        * warn. */\r
151 -       if (!mbox_warning) {\r
152 -           mbox_warning = true;\r
153 -           fprintf (stderr, "\\r
154 -Warning: %s is an mbox containing a single message,\n\\r
155 -likely caused by misconfigured mail delivery.  Support for single-message\n\\r
156 -mboxes is deprecated and may be removed in the future.\n", filename);\r
157 -       }\r
158 -    }\r
159 +    mime_message = notmuch_message_file_get_mime_message (message_file);\r
160 +    if (! mime_message)\r
161 +       return NOTMUCH_STATUS_FILE_NOT_EMAIL; /* more like internal error */\r
162  \r
163      from = g_mime_message_get_sender (mime_message);\r
164  \r
165 @@ -502,15 +454,5 @@ mboxes is deprecated and may be removed in the future.\n", filename);\r
166  \r
167      _index_mime_part (message, g_mime_message_get_mime_part (mime_message));\r
168  \r
169 -  DONE:\r
170 -    if (mime_message)\r
171 -       g_object_unref (mime_message);\r
172 -\r
173 -    if (parser)\r
174 -       g_object_unref (parser);\r
175 -\r
176 -    if (stream)\r
177 -       g_object_unref (stream);\r
178 -\r
179 -    return ret;\r
180 +    return NOTMUCH_STATUS_SUCCESS;\r
181  }\r
182 diff --git a/lib/message-file.c b/lib/message-file.c\r
183 index 9d5a3b9..7ab9e9d 100644\r
184 --- a/lib/message-file.c\r
185 +++ b/lib/message-file.c\r
186 @@ -247,6 +247,15 @@ mboxes is deprecated and may be removed in the future.\n", message->filename);\r
187      return NOTMUCH_STATUS_SUCCESS;\r
188  }\r
189  \r
190 +GMimeMessage *\r
191 +notmuch_message_file_get_mime_message (notmuch_message_file_t *message)\r
192 +{\r
193 +    if (! message->parsed)\r
194 +       return NULL;\r
195 +\r
196 +    return message->message;\r
197 +}\r
198 +\r
199  /* return NULL on errors, empty string for non-existing headers */\r
200  const char *\r
201  notmuch_message_file_get_header (notmuch_message_file_t *message,\r
202 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
203 index 7277df1..048dd6c 100644\r
204 --- a/lib/notmuch-private.h\r
205 +++ b/lib/notmuch-private.h\r
206 @@ -46,6 +46,8 @@ NOTMUCH_BEGIN_DECLS\r
207  \r
208  #include <talloc.h>\r
209  \r
210 +#include <gmime/gmime.h>\r
211 +\r
212  #include "xutil.h"\r
213  #include "error_util.h"\r
214  \r
215 @@ -320,9 +322,11 @@ notmuch_message_get_author (notmuch_message_t *message);\r
216  \r
217  /* index.cc */\r
218  \r
219 +typedef struct _notmuch_message_file notmuch_message_file_t;\r
220 +\r
221  notmuch_status_t\r
222  _notmuch_message_index_file (notmuch_message_t *message,\r
223 -                            const char *filename);\r
224 +                            notmuch_message_file_t *message_file);\r
225  \r
226  /* message-file.c */\r
227  \r
228 @@ -330,7 +334,6 @@ _notmuch_message_index_file (notmuch_message_t *message,\r
229   * into the public interface in notmuch.h\r
230   */\r
231  \r
232 -typedef struct _notmuch_message_file notmuch_message_file_t;\r
233  \r
234  /* Open a file containing a single email message.\r
235   *\r
236 @@ -377,6 +380,14 @@ void\r
237  notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
238                                         va_list va_headers);\r
239  \r
240 +/*\r
241 + * get mime message. this is an ugly interface; maybe join index.cc\r
242 + * and message-file.c, or move the top level indexing call to\r
243 + * message-file.c with helpers in index.cc\r
244 + */\r
245 +GMimeMessage *\r
246 +notmuch_message_file_get_mime_message (notmuch_message_file_t *message);\r
247 +\r
248  /* Get the value of the specified header from the message as a UTF-8 string.\r
249   *\r
250   * The header name is case insensitive.\r
251 -- \r
252 1.8.4.rc3\r
253 \r