]> git.notmuchmail.org Git - notmuch/commitdiff
Setup the GMimeStream only when needed
authornstraz@redhat.com <nstraz@redhat.com>
Thu, 1 Apr 2010 13:47:21 +0000 (09:47 -0400)
committerCarl Worth <cworth@cworth.org>
Thu, 1 Apr 2010 21:27:01 +0000 (14:27 -0700)
I ran into this while looking at the vim plugin.  Vim's system() call
redirects output to a file and it was missing many of the part{ lines.

If stream_stdout is setup too early, it will overwrite the part start
when notmuch is redirected to a file.

Reviewed-by Carl Worth <cworth@cworth.org>: GMime is calling fseek
before every write to reset the FILE* to the position it believes is
correct based on the writes it has seen. Our code was getting
incorrect results because our GMime writes were interleaved with
non-GMime writes via printf.

The bug appears when writing to a file because it's seekable, but not
when writing to a pipe which is not.

notmuch-show.c

index ff1fecb227f2e74cebd9342eb554f86d8363b37b..96647c1205d32ad036d6920f07dae7aee9547a4c 100644 (file)
@@ -236,9 +236,6 @@ format_part_text (GMimeObject *part, int *part_count)
 {
     GMimeContentDisposition *disposition;
     GMimeContentType *content_type;
-    GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
-
-    g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
 
     disposition = g_mime_object_get_content_disposition (part);
     if (disposition &&
@@ -256,14 +253,14 @@ format_part_text (GMimeObject *part, int *part_count)
        if (g_mime_content_type_is_type (content_type, "text", "*") &&
            !g_mime_content_type_is_type (content_type, "text", "html"))
        {
+           GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+           g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
            show_part_content (part, stream_stdout);
+           g_object_unref(stream_stdout);
        }
 
        printf ("\fattachment}\n");
 
-       if (stream_stdout)
-           g_object_unref(stream_stdout);
-
        return;
     }
 
@@ -276,7 +273,10 @@ format_part_text (GMimeObject *part, int *part_count)
     if (g_mime_content_type_is_type (content_type, "text", "*") &&
        !g_mime_content_type_is_type (content_type, "text", "html"))
     {
+       GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+       g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
        show_part_content (part, stream_stdout);
+       g_object_unref(stream_stdout);
     }
     else
     {
@@ -285,9 +285,6 @@ format_part_text (GMimeObject *part, int *part_count)
     }
 
     printf ("\fpart}\n");
-
-    if (stream_stdout)
-       g_object_unref(stream_stdout);
 }
 
 static void