Add a memory-backed my_tmpfile() implementation.
authorColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 13:32:38 +0000 (15:32 +0200)
committerColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 13:32:38 +0000 (15:32 +0200)
src/common/file-utils.c
src/common/file-utils.h
src/procmime.c

index 8157de5..765f3ef 100644 (file)
@@ -818,6 +818,21 @@ FILE *my_tmpfile(void)
        return tmpfile();
 }
 
+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;
index 6c5ad17..05bcbfb 100644 (file)
@@ -98,6 +98,7 @@ 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 9bd6ef2..8920c1f 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();
+                       tmpfp = my_tmpfile_with_len(mimeinfo->length * 2);
                        if (!tmpfp) {
                                perror("my_tmpfile");
                                if (tmp_file) 
@@ -733,7 +733,6 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        gchar buf[BUFFSIZE];
        gchar *str;
        gboolean scan_ret = FALSE;
-       gchar *tmpfile = NULL;
        int r;
 
        cm_return_val_if_fail(mimeinfo != NULL, TRUE);
@@ -742,27 +741,15 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        if (!procmime_decode_content(mimeinfo))
                return TRUE;
 
-#if HAVE_FMEMOPEN
-       tmpfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
-#else
-       tmpfile = procmime_get_tmp_file_name(mimeinfo);
-       if (tmpfile == NULL) {
-               g_warning("no filename\n");
-               return TRUE;
-       }
-
-       tmpfp = claws_fopen(tmpfile, "w+");
-#endif
+       tmpfp = my_tmpfile_with_len(mimeinfo->length * 2);
 
        if (tmpfp == NULL) {
-               g_free(tmpfile);
-               FILE_OP_ERROR(tmpfile, "open");
+               FILE_OP_ERROR("tmpfile", "open");
                return TRUE;
        }
 
        if ((r = procmime_get_part_to_stream(tmpfp, mimeinfo)) < 0) {
                g_warning("procmime_get_part_to_stream error %d\n", r);
-               g_free(tmpfile);
                return TRUE;
        }
 
@@ -828,11 +815,6 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
 
        claws_fclose(tmpfp);
 
-#if !HAVE_FMEMOPEN
-       claws_unlink(tmpfile);
-       g_free(tmpfile);
-#endif
-
        return scan_ret;
 }
 
@@ -849,7 +831,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        FILE *outfp;
        gboolean err;
 
-       if ((outfp = my_tmpfile()) == NULL) {
+       if ((outfp = my_tmpfile_with_len(mimeinfo->length * 2)) == NULL) {
                perror("my_tmpfile");
                return NULL;
        }
@@ -868,29 +850,13 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
 {
        FILE *outfp;
-#if !HAVE_FMEMOPEN
-       gchar *tmpfile = NULL;
-#endif
 
        cm_return_val_if_fail(mimeinfo != NULL, NULL);
 
        if (!procmime_decode_content(mimeinfo))
                return NULL;
 
-#if HAVE_FMEMOPEN
-       outfp = fmemopen(NULL, mimeinfo->length * 2, "w+");
-#else
-       tmpfile = procmime_get_tmp_file_name(mimeinfo);
-       if (tmpfile == NULL) {
-               g_warning("no filename\n");
-               return TRUE;
-       }
-
-       outfp = procmime_fopen(tmpfile, "w+");
-
-       g_unlink(tmpfile);
-       g_free(tmpfile);
-#endif
+       outfp = my_tmpfile_with_len(mimeinfo->length * 2);
 
        if (procmime_get_part_to_stream(outfp, mimeinfo) < 0) {
                return NULL;