Re: [PATCH 1/4] Add a thread's tags to emacs header-line
[notmuch-archives.git] / 63 / e0acb9fb4dd8f2436a8095e28a6909a000d854
1 Return-Path: <m.walters@qmul.ac.uk>\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 DD7C1431FB6\r
6         for <notmuch@notmuchmail.org>; Thu, 28 Jun 2012 13:48:19 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.098\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
12         tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
13         NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 xiUrUEVhmjx3 for <notmuch@notmuchmail.org>;\r
17         Thu, 28 Jun 2012 13:48:16 -0700 (PDT)\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id E5E54431FAF\r
22         for <notmuch@notmuchmail.org>; Thu, 28 Jun 2012 13:48:15 -0700 (PDT)\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
24         by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
25         (envelope-from <m.walters@qmul.ac.uk>)\r
26         id 1SkLdK-0001MW-W2; Thu, 28 Jun 2012 21:48:14 +0100\r
27 Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
28         helo=localhost)\r
29         by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
30         (envelope-from <m.walters@qmul.ac.uk>)\r
31         id 1SkLdK-0004kd-8q; Thu, 28 Jun 2012 21:48:10 +0100\r
32 From: Mark Walters <markwalters1009@gmail.com>\r
33 To: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>, notmuch@notmuchmail.org\r
34 Subject: Re: [RFC PATCH 01/14] All access to mail files goes through the\r
35         mailstore module\r
36 In-Reply-To: <1340656899-5644-2-git-send-email-ethan@betacantrips.com>\r
37 References: <1340656899-5644-1-git-send-email-ethan@betacantrips.com>\r
38         <1340656899-5644-2-git-send-email-ethan@betacantrips.com>\r
39 User-Agent: Notmuch/0.13.2+63~g548a9bf (http://notmuchmail.org) Emacs/23.4.1\r
40         (x86_64-pc-linux-gnu)\r
41 Date: Thu, 28 Jun 2012 21:48:03 +0100\r
42 Message-ID: <87hatvmad8.fsf@qmul.ac.uk>\r
43 MIME-Version: 1.0\r
44 Content-Type: text/plain; charset=utf-8\r
45 Content-Transfer-Encoding: quoted-printable\r
46 X-Sender-Host-Address: 94.192.233.223\r
47 X-QM-SPAM-Info: Sender has good ham record.  :)\r
48 X-QM-Body-MD5: ed266fc29b565e957cea11b36eb1212c (of first 20000 bytes)\r
49 X-SpamAssassin-Score: -1.8\r
50 X-SpamAssassin-SpamBar: -\r
51 X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
52         determine if it is\r
53         spam. We require at least 5.0 points to mark a message as spam.\r
54         This message scored -1.8 points.\r
55         Summary of the scoring: \r
56         * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
57         *      medium trust\r
58         *      [138.37.6.40 listed in list.dnswl.org]\r
59         * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
60         provider *      (markwalters1009[at]gmail.com)\r
61         * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
62         *      domain\r
63         *  0.5 AWL AWL: From: address is in the auto white-list\r
64 X-QM-Scan-Virus: ClamAV says the message is clean\r
65 X-BeenThere: notmuch@notmuchmail.org\r
66 X-Mailman-Version: 2.1.13\r
67 Precedence: list\r
68 List-Id: "Use and development of the notmuch mail system."\r
69         <notmuch.notmuchmail.org>\r
70 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
71         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
72 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
73 List-Post: <mailto:notmuch@notmuchmail.org>\r
74 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
75 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
76         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
77 X-List-Received-Date: Thu, 28 Jun 2012 20:48:20 -0000\r
78 \r
79 \r
80 On Mon, 25 Jun 2012, Ethan Glasser-Camp <ethan.glasser.camp@gmail.com> wrot=\r
81 e:\r
82 > This commit introduces the mailstore module which provides two\r
83 > functions, notmuch_mailstore_open and notmuch_mailstore_close. These\r
84 > functions are currently just stub calls to fopen and fclose, but later\r
85 > can be made more complex in order to support mail storage systems\r
86 > where one message might not be one file.\r
87 \r
88 I would prefer this patch split into two: one doing the stuff mentioned\r
89 in the commit message and one the notmuch_sha1_of_message/\r
90 notmuch_sha1_of_file change (or at least mention this latter change in\r
91 the commit message).=20\r
92 \r
93 Best wishes\r
94 \r
95 Mark=20\r
96 \r
97 > Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
98 > ---\r
99 >  lib/Makefile.local    |    1 +\r
100 >  lib/database.cc       |    2 +-\r
101 >  lib/index.cc          |    2 +-\r
102 >  lib/mailstore.c       |   34 ++++++++++++++++++++++++\r
103 >  lib/message-file.c    |    6 ++---\r
104 >  lib/notmuch-private.h |    3 +++\r
105 >  lib/notmuch.h         |   16 +++++++++++\r
106 >  lib/sha1.c            |   70 +++++++++++++++++++++++++++++++++++++------=\r
107 ------\r
108 >  mime-node.c           |    4 +--\r
109 >  notmuch-show.c        |   12 ++++-----\r
110 >  10 files changed, 120 insertions(+), 30 deletions(-)\r
111 >  create mode 100644 lib/mailstore.c\r
112 >\r
113 > diff --git a/lib/Makefile.local b/lib/Makefile.local\r
114 > index 8a9aa28..cfc77bb 100644\r
115 > --- a/lib/Makefile.local\r
116 > +++ b/lib/Makefile.local\r
117 > @@ -51,6 +51,7 @@ libnotmuch_c_srcs =3D               \\r
118 >       $(dir)/filenames.c      \\r
119 >       $(dir)/string-list.c    \\r
120 >       $(dir)/libsha1.c        \\r
121 > +     $(dir)/mailstore.c      \\r
122 >       $(dir)/message-file.c   \\r
123 >       $(dir)/messages.c       \\r
124 >       $(dir)/sha1.c           \\r
125 > diff --git a/lib/database.cc b/lib/database.cc\r
126 > index 761dc1a..c035edc 100644\r
127 > --- a/lib/database.cc\r
128 > +++ b/lib/database.cc\r
129 > @@ -1773,7 +1773,7 @@ notmuch_database_add_message (notmuch_database_t *n=\r
130 otmuch,\r
131 >       if (message_id =3D=3D NULL ) {\r
132 >           /* No message-id at all, let's generate one by taking a\r
133 >            * hash over the file's contents. */\r
134 > -         char *sha1 =3D notmuch_sha1_of_file (filename);\r
135 > +         char *sha1 =3D notmuch_sha1_of_message (filename);\r
136 >=20=20\r
137 >           /* If that failed too, something is really wrong. Give up. */\r
138 >           if (sha1 =3D=3D NULL) {\r
139 > diff --git a/lib/index.cc b/lib/index.cc\r
140 > index e377732..b607e82 100644\r
141 > --- a/lib/index.cc\r
142 > +++ b/lib/index.cc\r
143 > @@ -441,7 +441,7 @@ _notmuch_message_index_file (notmuch_message_t *messa=\r
144 ge,\r
145 >       initialized =3D 1;\r
146 >      }\r
147 >=20=20\r
148 > -    file =3D fopen (filename, "r");\r
149 > +    file =3D notmuch_mailstore_open (filename);\r
150 >      if (! file) {\r
151 >       fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
152 >       ret =3D NOTMUCH_STATUS_FILE_ERROR;\r
153 > diff --git a/lib/mailstore.c b/lib/mailstore.c\r
154 > new file mode 100644\r
155 > index 0000000..48acd47\r
156 > --- /dev/null\r
157 > +++ b/lib/mailstore.c\r
158 > @@ -0,0 +1,34 @@\r
159 > +/* mailstore.c - code to access individual messages\r
160 > + *\r
161 > + * Copyright =C2=A9 2009 Carl Worth\r
162 > + *\r
163 > + * This program is free software: you can redistribute it and/or modify\r
164 > + * it under the terms of the GNU General Public License as published by\r
165 > + * the Free Software Foundation, either version 3 of the License, or\r
166 > + * (at your option) any later version.\r
167 > + *\r
168 > + * This program is distributed in the hope that it will be useful,\r
169 > + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
170 > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
171 > + * GNU General Public License for more details.\r
172 > + *\r
173 > + * You should have received a copy of the GNU General Public License\r
174 > + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
175 > + *\r
176 > + * Author: Carl Worth <cworth@cworth.org>\r
177 > + */\r
178 > +#include <stdio.h>\r
179 > +\r
180 > +#include "notmuch-private.h"\r
181 > +\r
182 > +FILE *\r
183 > +notmuch_mailstore_open (const char *filename)\r
184 > +{\r
185 > +    return fopen (filename, "r");\r
186 > +}\r
187 > +\r
188 > +int\r
189 > +notmuch_mailstore_close (FILE *file)\r
190 > +{\r
191 > +    return fclose (file);\r
192 > +}\r
193 > diff --git a/lib/message-file.c b/lib/message-file.c\r
194 > index 915aba8..271389c 100644\r
195 > --- a/lib/message-file.c\r
196 > +++ b/lib/message-file.c\r
197 > @@ -86,7 +86,7 @@ _notmuch_message_file_destructor (notmuch_message_file_=\r
198 t *message)\r
199 >       g_hash_table_destroy (message->headers);\r
200 >=20=20\r
201 >      if (message->file)\r
202 > -     fclose (message->file);\r
203 > +     notmuch_mailstore_close (message->file);\r
204 >=20=20\r
205 >      return 0;\r
206 >  }\r
207 > @@ -104,7 +104,7 @@ _notmuch_message_file_open_ctx (void *ctx, const char=\r
208  *filename)\r
209 >=20=20\r
210 >      talloc_set_destructor (message, _notmuch_message_file_destructor);\r
211 >=20=20\r
212 > -    message->file =3D fopen (filename, "r");\r
213 > +    message->file =3D notmuch_mailstore_open (filename);\r
214 >      if (message->file =3D=3D NULL)\r
215 >       goto FAIL;\r
216 >=20=20\r
217 > @@ -361,7 +361,7 @@ notmuch_message_file_get_header (notmuch_message_file=\r
218 _t *message,\r
219 >      }\r
220 >=20=20\r
221 >      if (message->parsing_finished) {\r
222 > -        fclose (message->file);\r
223 > +        notmuch_mailstore_close (message->file);\r
224 >          message->file =3D NULL;\r
225 >      }\r
226 >=20=20\r
227 > diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
228 > index bfb4111..5dbe821 100644\r
229 > --- a/lib/notmuch-private.h\r
230 > +++ b/lib/notmuch-private.h\r
231 > @@ -468,6 +468,9 @@ notmuch_sha1_of_string (const char *str);\r
232 >  char *\r
233 >  notmuch_sha1_of_file (const char *filename);\r
234 >=20=20\r
235 > +char *\r
236 > +notmuch_sha1_of_message (const char *filename);\r
237 > +\r
238 >  /* string-list.c */\r
239 >=20=20\r
240 >  typedef struct _notmuch_string_node {\r
241 > diff --git a/lib/notmuch.h b/lib/notmuch.h\r
242 > index 3633bed..0ca367b 100644\r
243 > --- a/lib/notmuch.h\r
244 > +++ b/lib/notmuch.h\r
245 > @@ -1233,6 +1233,22 @@ notmuch_message_thaw (notmuch_message_t *message);\r
246 >  void\r
247 >  notmuch_message_destroy (notmuch_message_t *message);\r
248 >=20=20\r
249 > +/* Get access to the underlying data of a message.\r
250 > + *\r
251 > + * Right now, all messages are simple files in maildirs, but this is\r
252 > + * hopefully subject to change.\r
253 > + *\r
254 > + * If the returned FILE* is not NULL, be sure to call\r
255 > + * notmuch_mailstore_close when you're done with it.\r
256 > + */\r
257 > +FILE *\r
258 > +notmuch_mailstore_open (const char *filename);\r
259 > +\r
260 > +/* Release any resources associated with this file.\r
261 > + */\r
262 > +int\r
263 > +notmuch_mailstore_close (FILE *file);\r
264 > +\r
265 >  /* Is the given 'tags' iterator pointing at a valid tag.\r
266 >   *\r
267 >   * When this function returns TRUE, notmuch_tags_get will return a\r
268 > diff --git a/lib/sha1.c b/lib/sha1.c\r
269 > index cc48108..462c07e 100644\r
270 > --- a/lib/sha1.c\r
271 > +++ b/lib/sha1.c\r
272 > @@ -64,19 +64,11 @@ notmuch_sha1_of_string (const char *str)\r
273 >      return _hex_of_sha1_digest (digest);\r
274 >  }\r
275 >=20=20\r
276 > -/* Create a hexadecimal string version of the SHA-1 digest of the\r
277 > - * contents of the named file.\r
278 > - *\r
279 > - * This function returns a newly allocated string which the caller\r
280 > - * should free() when finished.\r
281 > - *\r
282 > - * If any error occurs while reading the file, (permission denied,\r
283 > - * file not found, etc.), this function returns NULL.\r
284 > +/* Internal function to feed the contents of a FILE * to the sha1 functi=\r
285 ons.\r
286 >   */\r
287 > -char *\r
288 > -notmuch_sha1_of_file (const char *filename)\r
289 > -{\r
290 > -    FILE *file;\r
291 > +\r
292 > +static char *\r
293 > +_notmuch_sha1_of_filep (FILE *file){\r
294 >  #define BLOCK_SIZE 4096\r
295 >      unsigned char block[BLOCK_SIZE];\r
296 >      size_t bytes_read;\r
297 > @@ -84,14 +76,10 @@ notmuch_sha1_of_file (const char *filename)\r
298 >      unsigned char digest[SHA1_DIGEST_SIZE];\r
299 >      char *result;\r
300 >=20=20\r
301 > -    file =3D fopen (filename, "r");\r
302 > -    if (file =3D=3D NULL)\r
303 > -     return NULL;\r
304 > -\r
305 >      sha1_begin (&sha1);\r
306 >=20=20\r
307 >      while (1) {\r
308 > -     bytes_read =3D fread (block, 1, 4096, file);\r
309 > +     bytes_read =3D fread (block, 1, BLOCK_SIZE, file);\r
310 >       if (bytes_read =3D=3D 0) {\r
311 >           if (feof (file)) {\r
312 >               break;\r
313 > @@ -108,8 +96,56 @@ notmuch_sha1_of_file (const char *filename)\r
314 >=20=20\r
315 >      result =3D _hex_of_sha1_digest (digest);\r
316 >=20=20\r
317 > +    return result;\r
318 > +}\r
319 > +\r
320 > +/* Create a hexadecimal string version of the SHA-1 digest of the\r
321 > + * contents of the named file.\r
322 > + *\r
323 > + * This function returns a newly allocated string which the caller\r
324 > + * should free() when finished.\r
325 > + *\r
326 > + * If any error occurs while reading the file, (permission denied,\r
327 > + * file not found, etc.), this function returns NULL.\r
328 > + */\r
329 > +char *\r
330 > +notmuch_sha1_of_file (const char *filename)\r
331 > +{\r
332 > +    FILE *file;\r
333 > +    char *result;\r
334 > +    file =3D fopen (filename, "r");\r
335 > +    if (file =3D=3D NULL)\r
336 > +     return NULL;\r
337 > +\r
338 > +    result =3D _notmuch_sha1_of_filep (file);\r
339 > +\r
340 >      fclose (file);\r
341 >=20=20\r
342 >      return result;\r
343 >  }\r
344 >=20=20\r
345 > +/* Create a hexadecimal string version of the SHA-1 digest of the\r
346 > + * contents of the named message, which is accessed via a mailstore.\r
347 > + *\r
348 > + * This function returns a newly allocated string which the caller\r
349 > + * should free() when finished.\r
350 > + *\r
351 > + * If any error occurs while reading the file, (permission denied,\r
352 > + * file not found, etc.), this function returns NULL.\r
353 > + */\r
354 > +char *\r
355 > +notmuch_sha1_of_message (const char *filename)\r
356 > +{\r
357 > +    FILE *file;\r
358 > +    char *result;\r
359 > +\r
360 > +    file =3D notmuch_mailstore_open (filename);\r
361 > +    if (file =3D=3D NULL)\r
362 > +     return NULL;\r
363 > +\r
364 > +    result =3D _notmuch_sha1_of_filep (file);\r
365 > +\r
366 > +    notmuch_mailstore_close (file);\r
367 > +\r
368 > +    return result;\r
369 > +}\r
370 > diff --git a/mime-node.c b/mime-node.c\r
371 > index 97e8b48..a5c60d0 100644\r
372 > --- a/mime-node.c\r
373 > +++ b/mime-node.c\r
374 > @@ -49,7 +49,7 @@ _mime_node_context_free (mime_node_context_t *res)\r
375 >       g_object_unref (res->stream);\r
376 >=20=20\r
377 >      if (res->file)\r
378 > -     fclose (res->file);\r
379 > +     notmuch_mailstore_close (res->file);\r
380 >=20=20\r
381 >      return 0;\r
382 >  }\r
383 > @@ -79,7 +79,7 @@ mime_node_open (const void *ctx, notmuch_message_t *mes=\r
384 sage,\r
385 >      }\r
386 >      talloc_set_destructor (mctx, _mime_node_context_free);\r
387 >=20=20\r
388 > -    mctx->file =3D fopen (filename, "r");\r
389 > +    mctx->file =3D notmuch_mailstore_open (filename);\r
390 >      if (! mctx->file) {\r
391 >       fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
392 >       status =3D NOTMUCH_STATUS_FILE_ERROR;\r
393 > diff --git a/notmuch-show.c b/notmuch-show.c\r
394 > index 8247f1d..2847d25 100644\r
395 > --- a/notmuch-show.c\r
396 > +++ b/notmuch-show.c\r
397 > @@ -692,7 +692,7 @@ format_part_mbox (const void *ctx, mime_node_t *node,=\r
398  unused (int indent),\r
399 >       INTERNAL_ERROR ("format_part_mbox requires a root part");\r
400 >=20=20\r
401 >      filename =3D notmuch_message_get_filename (message);\r
402 > -    file =3D fopen (filename, "r");\r
403 > +    file =3D notmuch_mailstore_open (filename);\r
404 >      if (file =3D=3D NULL) {\r
405 >       fprintf (stderr, "Failed to open %s: %s\n",\r
406 >                filename, strerror (errno));\r
407 > @@ -716,7 +716,7 @@ format_part_mbox (const void *ctx, mime_node_t *node,=\r
408  unused (int indent),\r
409 >=20=20\r
410 >      printf ("\n");\r
411 >=20=20\r
412 > -    fclose (file);\r
413 > +    notmuch_mailstore_close (file);\r
414 >=20=20\r
415 >      return NOTMUCH_STATUS_SUCCESS;\r
416 >  }\r
417 > @@ -739,7 +739,7 @@ format_part_raw (unused (const void *ctx), mime_node_=\r
418 t *node,\r
419 >           return NOTMUCH_STATUS_FILE_ERROR;\r
420 >       }\r
421 >=20=20\r
422 > -     file =3D fopen (filename, "r");\r
423 > +     file =3D notmuch_mailstore_open (filename);\r
424 >       if (file =3D=3D NULL) {\r
425 >           fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, stre=\r
426 rror (errno));\r
427 >           return NOTMUCH_STATUS_FILE_ERROR;\r
428 > @@ -749,18 +749,18 @@ format_part_raw (unused (const void *ctx), mime_nod=\r
429 e_t *node,\r
430 >           size =3D fread (buf, 1, sizeof (buf), file);\r
431 >           if (ferror (file)) {\r
432 >               fprintf (stderr, "Error: Read failed from %s\n", filename);\r
433 > -             fclose (file);\r
434 > +             notmuch_mailstore_close (file);\r
435 >               return NOTMUCH_STATUS_FILE_ERROR;\r
436 >           }\r
437 >=20=20\r
438 >           if (fwrite (buf, size, 1, stdout) !=3D 1) {\r
439 >               fprintf (stderr, "Error: Write failed\n");\r
440 > -             fclose (file);\r
441 > +             notmuch_mailstore_close (file);\r
442 >               return NOTMUCH_STATUS_FILE_ERROR;\r
443 >           }\r
444 >       }\r
445 >=20=20\r
446 > -     fclose (file);\r
447 > +     notmuch_mailstore_close (file);\r
448 >       return NOTMUCH_STATUS_SUCCESS;\r
449 >      }\r
450 >=20=20\r
451 > --=20\r
452 > 1.7.9.5\r
453 >\r
454 > _______________________________________________\r
455 > notmuch mailing list\r
456 > notmuch@notmuchmail.org\r
457 > http://notmuchmail.org/mailman/listinfo/notmuch\r