Revert all memory-based tmpfile I/O, it comes with too much drawbacks
authorColin Leroy <colin@colino.net>
Wed, 10 Oct 2018 12:34:34 +0000 (14:34 +0200)
committerColin Leroy <colin@colino.net>
Wed, 10 Oct 2018 12:34:34 +0000 (14:34 +0200)
(like having to know the filesize before writing to it)

src/common/file-utils.c
src/common/file-utils.h
src/procmime.c

index 97f1f50..f7e2da7 100644 (file)
@@ -827,34 +827,6 @@ FILE *my_tmpfile(void)
        return tmpfile();
 }
 
-/* Returns a memory-backed FILE pointer to avoid file I/O
- * where unnecessary. The "file" size is passed in the len
- * parameter and is fixed: it's up to the caller to pass a
- * large enough length to write to the FILE pointer. If the
- * precise length isn't known, it is possible to ask for more.
- * 
- * In this case, once writing to the pointer is done, the 
- * caller is responsible for calling ftruncate(fileno(fp), ftell(fp))
- * to make sure re-reading the stream will return EOF at the
- * end of what we wrote. 
- * Otherwise, re-reading the stream will return uninitialized
- * memory at the end of the stream.
- */
-FILE *my_tmpfile_with_len(size_t len)
-{
-#if HAVE_FMEMOPEN
-       FILE *tmpfp = fmemopen(NULL, len, "w+b");
-
-       if (tmpfp == NULL) {
-               return my_tmpfile();
-       }
-
-       return tmpfp;
-#else
-       return my_tmpfile();
-#endif
-}
-
 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
 {
        int fd;
@@ -874,7 +846,7 @@ FILE *str_open_as_stream(const gchar *str)
 
        len = strlen(str);
 
-       fp = my_tmpfile_with_len(len);
+       fp = my_tmpfile();
 
        if (!fp) {
                FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
index 398bfde..d89b069 100644 (file)
@@ -94,7 +94,6 @@ gint copy_dir                 (const gchar    *src,
 gint change_file_mode_rw       (FILE           *fp,
                                 const gchar    *file);
 FILE *my_tmpfile               (void);
-FILE *my_tmpfile_with_len      (size_t          len);
 FILE *get_tmpfile_in_dir       (const gchar    *dir,
                                 gchar         **filename);
 FILE *str_open_as_stream       (const gchar    *str);
index 8b42d20..bd3239e 100644 (file)
@@ -377,7 +377,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                if (mimeinfo->type == MIMETYPE_TEXT ||
                    mimeinfo->type == MIMETYPE_MESSAGE) {
                        uncanonicalize = TRUE;
-                       tmpfp = my_tmpfile_with_len(mimeinfo->length * 2);
+                       tmpfp = my_tmpfile();
                        if (!tmpfp) {
                                perror("my_tmpfile");
                                if (tmp_file) 
@@ -429,7 +429,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        }
                }
                if (tmpfp != outfp) {
-                       ftruncate(fileno(tmpfp), ftell(tmpfp));
                        claws_fclose(tmpfp);
                }
        } else if (encoding == ENC_X_UUENCODE) {
@@ -467,7 +466,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        g_warning("write error");
        }
 
-       ftruncate(fileno(outfp), ftell(outfp));
        claws_fclose(outfp);
        claws_fclose(infp);
 
@@ -744,7 +742,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        if (!procmime_decode_content(mimeinfo))
                return TRUE;
 
-       tmpfp = my_tmpfile_with_len(mimeinfo->length * 2);
+       tmpfp = my_tmpfile();
 
        if (tmpfp == NULL) {
                FILE_OP_ERROR("tmpfile", "open");
@@ -834,14 +832,13 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        FILE *outfp;
        gboolean err;
 
-       if ((outfp = my_tmpfile_with_len(mimeinfo->length * 2)) == NULL) {
+       if ((outfp = my_tmpfile()) == NULL) {
                perror("my_tmpfile");
                return NULL;
        }
 
        err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
 
-       ftruncate(fileno(outfp), ftell(outfp));
        rewind(outfp);
        if (err == TRUE) {
                claws_fclose(outfp);
@@ -860,12 +857,12 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
        if (!procmime_decode_content(mimeinfo))
                return NULL;
 
-       outfp = my_tmpfile_with_len(mimeinfo->length * 2);
+       outfp = my_tmpfile();
 
        if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
                return NULL;
        }
-       ftruncate(fileno(outfp), ftell(outfp));
+
        return outfp;
 }