Fix performance loss on certain file I/O that doesn't need
authorColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 13:40:38 +0000 (15:40 +0200)
committerColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 13:49:31 +0000 (15:49 +0200)
to be safe

13 files changed:
src/common/file-utils.c
src/common/file-utils.h
src/compose.c
src/crash.c
src/main.c
src/news.c
src/plugins/fancy/fancy_prefs.c
src/plugins/smime/smime.c
src/plugins/spamassassin/spamassassin.c
src/plugins/vcalendar/vcal_manager.c
src/plugins/vcalendar/vcal_meeting_gtk.c
src/prefs_account.c
src/wizard.c

index 765f3ef..8f58fec 100644 (file)
@@ -511,10 +511,11 @@ gint canonicalize_file_replace(const gchar *file)
 }
 
 
-gint str_write_to_file(const gchar *str, const gchar *file)
+gint str_write_to_file(const gchar *str, const gchar *file, gboolean safe)
 {
        FILE *fp;
        size_t len;
+       int r;
 
        cm_return_val_if_fail(str != NULL, -1);
        cm_return_val_if_fail(file != NULL, -1);
@@ -537,7 +538,13 @@ gint str_write_to_file(const gchar *str, const gchar *file)
                return -1;
        }
 
-       if (claws_safe_fclose(fp) == EOF) {
+       if (safe) {
+               r = claws_safe_fclose(fp);
+       } else {
+               r = claws_fclose(fp);
+       }
+
+       if (r == EOF) {
                FILE_OP_ERROR(file, "claws_fclose");
                claws_unlink(file);
                return -1;
index 05bcbfb..10926f0 100644 (file)
@@ -84,8 +84,6 @@ gint copy_file_part           (FILE           *fp,
 gint canonicalize_file         (const gchar    *src,
                                 const gchar    *dest);
 gint canonicalize_file_replace (const gchar    *file);
-gint str_write_to_file         (const gchar    *str,
-                                const gchar    *file);
 gchar *file_read_to_str                (const gchar    *file);
 gchar *file_read_to_str_no_recode(const gchar  *file);
 gchar *file_read_stream_to_str (FILE           *fp);
@@ -103,6 +101,7 @@ FILE *get_tmpfile_in_dir    (const gchar    *dir,
                                 gchar         **filename);
 FILE *str_open_as_stream       (const gchar    *str);
 gint str_write_to_file         (const gchar    *str,
-                                const gchar    *file);
+                                const gchar    *file,
+                                gboolean        safe);
 
 #endif
index bf33007..0d8f774 100644 (file)
@@ -5904,7 +5904,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                                        rewind(fp);
                                        content = file_read_stream_to_str(fp);
 
-                                       str_write_to_file(content, tmp_enc_file);
+                                       str_write_to_file(content, tmp_enc_file, TRUE);
                                        g_free(content);
 
                                        /* Now write the unencrypted body. */
@@ -11674,7 +11674,7 @@ static void compose_insert_drag_received_cb (GtkWidget          *widget,
                        /* Assume a list of no files, and data has ://, is a remote link */
                        gchar *tmpdata = g_strstrip(g_strdup(ddata));
                        gchar *tmpfile = get_tmp_file();
-                       str_write_to_file(tmpdata, tmpfile);
+                       str_write_to_file(tmpdata, tmpfile, TRUE);
                        g_free(tmpdata);  
                        compose_insert_file(compose, tmpfile);
                        claws_unlink(tmpfile);
index ae1d4b1..edac5c7 100644 (file)
@@ -290,7 +290,7 @@ static void crash_create_debugger_file(void)
 {
        gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
        
-       str_write_to_file(DEBUG_SCRIPT, filespec);
+       str_write_to_file(DEBUG_SCRIPT, filespec, TRUE);
        g_free(filespec);
 }
 
@@ -310,7 +310,7 @@ static void crash_save_crash_log(GtkButton *button, const gchar *text)
        strftime(buf, sizeof buf, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
        if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
        &&  *filename)
-               str_write_to_file(text, filename);
+               str_write_to_file(text, filename, TRUE);
        g_free(filename);       
 }
 
index 65a3ec1..0f17016 100644 (file)
@@ -1440,7 +1440,7 @@ int main(int argc, char *argv[])
                folder_item_update_freeze();
        }
        /* make the crash-indicator file */
-       str_write_to_file("foo", get_crashfile_name());
+       str_write_to_file("foo", get_crashfile_name(), FALSE);
 
        inc_autocheck_timer_init(mainwin);
 
index 70eb4d6..61120c4 100644 (file)
@@ -819,7 +819,7 @@ static gint news_get_article(Folder *folder, gint num, gchar *filename)
        r = nntp_threaded_article(folder, num, &result, &len);
        
        if (r == NEWSNNTP_NO_ERROR) {
-               if (str_write_to_file(result, filename) < 0) {
+               if (str_write_to_file(result, filename, FALSE) < 0) {
                        mmap_string_unref(result);
                        return -1;
                }
index ce677ee..7c553ba 100644 (file)
@@ -364,7 +364,7 @@ static void fancy_prefs_stylesheet_edit_cb(GtkWidget *widget, gpointer data)
 {
        const gchar *stylesheet = gtk_entry_get_text(GTK_ENTRY(data));
        if (!is_file_exist(stylesheet))
-               str_write_to_file(stylesheet, "");
+               str_write_to_file(stylesheet, "", TRUE);
        open_txt_editor(stylesheet, prefs_common_get_ext_editor_cmd());
 }
 
index ea999c1..2546343 100644 (file)
@@ -286,7 +286,7 @@ static gint smime_check_signature(MimeInfo *mimeinfo)
                                gchar *tmp_file = get_tmp_file();
                                MimeInfo *newinfo = NULL, *decinfo = NULL, *parentinfo = NULL;
 
-                               str_write_to_file(textstr, tmp_file);
+                               str_write_to_file(textstr, tmp_file, TRUE);
                                newinfo = procmime_scan_file(tmp_file);
                                decinfo = g_node_first_child(newinfo->node) != NULL ?
                                        g_node_first_child(newinfo->node)->data : NULL;
index 0cd725a..e5a39b2 100644 (file)
@@ -389,7 +389,7 @@ gchar* spamassassin_create_tmp_spamc_wrapper(gboolean spam)
                                                config.username, config.timeout,
                                                config.max_size * 1024, config.compress?"-z":"",
                                                spam?"spam":"ham");
-               if (str_write_to_file(contents, fname) < 0) {
+               if (str_write_to_file(contents, fname, TRUE) < 0) {
                        g_free(fname);
                        fname = NULL;
                }
index 009cdbd..fe71bf6 100644 (file)
@@ -502,7 +502,7 @@ gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_
                               "\n"
                               "%s", headers, icalcomponent_as_ical_string(calendar));
        
-       if (str_write_to_file(body, tmpfile) < 0) {
+       if (str_write_to_file(body, tmpfile, FALSE) < 0) {
                g_free(tmpfile);
                tmpfile = NULL;
        }
@@ -640,7 +640,7 @@ gchar *vcal_manager_dateevent_dump(const gchar *uid, FolderItem *item)
                               "\n"
                               "%s", headers, lines);
        g_free(lines);
-       if (str_write_to_file(body, tmpfile) < 0) {
+       if (str_write_to_file(body, tmpfile, FALSE) < 0) {
                g_free(tmpfile);
                tmpfile = NULL;
        } else
@@ -754,7 +754,7 @@ gchar *vcal_manager_icalevent_dump(icalcomponent *event, gchar *orga, icalcompon
                               "\n"
                               "%s", headers, qpbody);
        
-       if (str_write_to_file(body, tmpfile) < 0) {
+       if (str_write_to_file(body, tmpfile, FALSE) < 0) {
                g_free(tmpfile);
                tmpfile = NULL;
        } else
index 8e7dff1..c5ff05b 100644 (file)
@@ -1840,7 +1840,7 @@ void multisync_export(void)
                    );  
                vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
                tmp = g_strconcat(path, G_DIR_SEPARATOR_S, file, NULL);
-               str_write_to_file(icalcomponent_as_ical_string(calendar), tmp);
+               str_write_to_file(icalcomponent_as_ical_string(calendar), tmp, TRUE);
                g_free(tmp);
                files = g_slist_append(files, file);
                vcal_manager_free_event(event);
@@ -1900,7 +1900,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
                                NULL, ALERT_NOTICE);
                        return FALSE;
                } else {
-                       str_write_to_file("", tmpfile);
+                       str_write_to_file("", tmpfile, TRUE);
                        goto putfile;
                }
        }
@@ -1921,7 +1921,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
                vcal_manager_free_event(event);
        }
 
-       if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
+       if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file, TRUE) < 0) {
                g_warning("can't export internal cal");
        }
        
@@ -1934,7 +1934,7 @@ gboolean vcal_meeting_export_calendar(const gchar *path,
        }
 
        if (vcalprefs.export_enable || path == NULL) {
-               if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
+               if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile, TRUE) < 0) {
                        alertpanel_error(_("Could not export the calendar."));
                        g_free(tmpfile);
                        icalcomponent_free(calendar);
@@ -2083,14 +2083,14 @@ gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
 
        icalcomponent_add_component(calendar, vfreebusy);
        
-       if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
+       if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file, TRUE) < 0) {
                g_warning("can't export freebusy");
        }
        
        g_free(internal_file);
 
        if (vcalprefs.export_freebusy_enable) {
-               if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
+               if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile, TRUE) < 0) {
                        alertpanel_error(_("Could not export the freebusy info."));
                        g_free(tmpfile);
                        icalcomponent_free(calendar);
index 1eb6cee..5ed582d 100644 (file)
@@ -4557,7 +4557,7 @@ static void prefs_account_signature_edit_cb(GtkWidget *widget, gpointer data)
 {
        const gchar *sigpath = gtk_entry_get_text(GTK_ENTRY(data));
        if (!is_file_exist(sigpath))
-               str_write_to_file(sigpath, "");
+               str_write_to_file(sigpath, "", TRUE);
        open_txt_editor(sigpath, prefs_common_get_ext_editor_cmd());
 }
 
index a942347..a3c53da 100644 (file)
@@ -403,7 +403,7 @@ static void wizard_read_defaults(void)
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "accountrc.tmpl", NULL);
        if (!is_file_exist(rcpath)) {
-               str_write_to_file(accountrc_tmpl, rcpath);
+               str_write_to_file(accountrc_tmpl, rcpath, TRUE);
        }
 
        prefs_read_config(template_params, "AccountTemplate", rcpath, NULL);
@@ -591,7 +591,7 @@ static void write_welcome_email(WizardWindow *wizard)
        msg = g_strconcat(head, body, NULL);
 
        if (inbox && inbox->total_msgs == 0
-        && str_write_to_file(msg, file) >= 0) {
+        && str_write_to_file(msg, file, TRUE) >= 0) {
                MsgFlags flags = { MSG_UNREAD|MSG_NEW, 0};
                folder_item_add_msg(inbox, file, &flags, FALSE);
        }