Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 37227)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2009-02-05  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #333716
+
+	* em-format-html.c: (emfh_gethttp):
+	Do not store incomplete or broken files in a cache.
+
 2009-02-04  Gilles Dartiguelongue  <gdartigu@svn.gnome.org>
 
 	** Fix for bug #568176
Index: mail/em-format-html.c
===================================================================
--- mail/em-format-html.c	(revision 37227)
+++ mail/em-format-html.c	(working copy)
@@ -531,17 +531,12 @@ static void emfh_gethttp(struct _EMForma
 				camel_operation_progress(NULL, pc_complete);
 			}
 			d(printf("  read %d bytes\n", n));
-			if (costream && camel_stream_write(costream, buffer, n) == -1) {
-				camel_data_cache_remove(emfh_http_cache, EMFH_HTTP_CACHE_PATH, job->u.uri, NULL);
-				camel_object_unref(costream);
-				costream = NULL;
+			if (costream && camel_stream_write (costream, buffer, n) == -1) {
+				n = -1;
+				break;
 			}
 
 			camel_stream_write(job->stream, buffer, n);
-		} else if (n < 0 && costream) {
-			camel_data_cache_remove(emfh_http_cache, EMFH_HTTP_CACHE_PATH, job->u.uri, NULL);
-			camel_object_unref(costream);
-			costream = NULL;
 		}
 	} while (n>0);
 
@@ -549,8 +544,12 @@ static void emfh_gethttp(struct _EMForma
 	if (n == 0)
 		camel_stream_close(job->stream);
 
-	if (costream)
+	if (costream) {
+		/* do not store broken files in a cache */
+		if (n != 0)
+			camel_data_cache_remove(emfh_http_cache, EMFH_HTTP_CACHE_PATH, job->u.uri, NULL);
 		camel_object_unref(costream);
+	}
 
 	camel_object_unref(instream);
 done:
