X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fmessage-file.c;h=24c5fda43b1338fb6f5c1c45a2d79e3c51005a98;hp=214f3c1d26b12a4f57b27058f68c142e86b157e3;hb=b52cda90f0b05ce5055fb840e6d9dd88c09f1f83;hpb=58ee5d1bb5492925c6025177d149d69d72704caa diff --git a/lib/message-file.c b/lib/message-file.c index 214f3c1d..24c5fda4 100644 --- a/lib/message-file.c +++ b/lib/message-file.c @@ -27,8 +27,8 @@ #include /* GHashTable */ struct _notmuch_message_file { - /* File object */ - FILE *file; + /* open stream to (possibly gzipped) file */ + GMimeStream *stream; char *filename; /* Cache for decoded headers */ @@ -46,8 +46,8 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message) if (message->message) g_object_unref (message->message); - if (message->file) - fclose (message->file); + if (message->stream) + g_object_unref (message->stream); return 0; } @@ -64,15 +64,14 @@ _notmuch_message_file_open_ctx (notmuch_database_t *notmuch, if (unlikely (message == NULL)) return NULL; - /* Only needed for error messages during parsing. */ message->filename = talloc_strdup (message, filename); if (message->filename == NULL) goto FAIL; talloc_set_destructor (message, _notmuch_message_file_destructor); - message->file = fopen (filename, "r"); - if (message->file == NULL) + message->stream = g_mime_stream_gzfile_open (filename); + if (message->stream == NULL) goto FAIL; return message; @@ -105,17 +104,17 @@ _notmuch_message_file_close (notmuch_message_file_t *message) } static bool -_is_mbox (FILE *file) +_is_mbox (GMimeStream *stream) { char from_buf[5]; bool ret = false; /* Is this mbox? */ - if (fread (from_buf, sizeof (from_buf), 1, file) == 1 && + if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof(from_buf) && strncmp (from_buf, "From ", 5) == 0) ret = true; - rewind (file); + g_mime_stream_reset (stream); return ret; } @@ -123,7 +122,6 @@ _is_mbox (FILE *file) notmuch_status_t _notmuch_message_file_parse (notmuch_message_file_t *message) { - GMimeStream *stream; GMimeParser *parser; notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; static int initialized = 0; @@ -132,7 +130,7 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) if (message->message) return NOTMUCH_STATUS_SUCCESS; - is_mbox = _is_mbox (message->file); + is_mbox = _is_mbox (message->stream); if (! initialized) { g_mime_init (); @@ -144,15 +142,10 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) if (! message->headers) return NOTMUCH_STATUS_OUT_OF_MEMORY; - stream = g_mime_stream_file_new (message->file); - - /* We'll own and fclose the FILE* ourselves. */ - g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), false); - - parser = g_mime_parser_new_with_stream (stream); + parser = g_mime_parser_new_with_stream (message->stream); g_mime_parser_set_scan_from (parser, is_mbox); - message->message = g_mime_parser_construct_message (parser); + message->message = g_mime_parser_construct_message (parser, NULL); if (! message->message) { status = NOTMUCH_STATUS_FILE_NOT_EMAIL; goto DONE; @@ -167,7 +160,7 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) } DONE: - g_object_unref (stream); + g_mime_stream_reset (message->stream); g_object_unref (parser); if (status) { @@ -179,7 +172,6 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) message->message = NULL; } - rewind (message->file); } return status; @@ -212,7 +204,7 @@ static char * _extend_header (char *combined, const char *value) { char *decoded; - decoded = g_mime_utils_header_decode_text (value); + decoded = g_mime_utils_header_decode_text (NULL, value); if (! decoded) { if (combined) { g_free (combined); @@ -296,7 +288,7 @@ _notmuch_message_file_get_header (notmuch_message_file_t *message, value = g_mime_object_get_header (GMIME_OBJECT (message->message), header); if (value) - decoded = g_mime_utils_header_decode_text (value); + decoded = g_mime_utils_header_decode_text (NULL, value); else decoded = g_strdup (""); }