commit 429ab1b06c0215cc94ea299cdaa5abcdb7e46b27
parent ebb97540bfbddcf4a4de619086ad8fa43a733eba
Author: Colin Leroy <colin@colino.net>
Date: Sun, 7 Oct 2018 15:40:38 +0200
Fix performance loss on certain file I/O that doesn't need
to be safe
Diffstat:
13 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/src/common/file-utils.c b/src/common/file-utils.c
@@ -513,10 +513,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);
@@ -539,7 +540,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;
diff --git a/src/common/file-utils.h b/src/common/file-utils.h
@@ -82,8 +82,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);
@@ -100,6 +98,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
diff --git a/src/compose.c b/src/compose.c
@@ -5924,7 +5924,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. */
@@ -11690,7 +11690,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);
diff --git a/src/crash.c b/src/crash.c
@@ -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);
}
diff --git a/src/main.c b/src/main.c
@@ -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);
diff --git a/src/news.c b/src/news.c
@@ -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;
}
diff --git a/src/plugins/fancy/fancy_prefs.c b/src/plugins/fancy/fancy_prefs.c
@@ -365,7 +365,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());
}
diff --git a/src/plugins/smime/smime.c b/src/plugins/smime/smime.c
@@ -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;
diff --git a/src/plugins/spamassassin/spamassassin.c b/src/plugins/spamassassin/spamassassin.c
@@ -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;
}
diff --git a/src/plugins/vcalendar/vcal_manager.c b/src/plugins/vcalendar/vcal_manager.c
@@ -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
diff --git a/src/plugins/vcalendar/vcal_meeting_gtk.c b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -1854,7 +1854,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);
@@ -1914,7 +1914,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;
}
}
@@ -1935,7 +1935,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");
}
@@ -1948,7 +1948,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);
@@ -2097,14 +2097,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);
diff --git a/src/prefs_account.c b/src/prefs_account.c
@@ -4554,7 +4554,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());
}
diff --git a/src/wizard.c b/src/wizard.c
@@ -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);
}