talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 30d1a28ae115c9aa91c308768e9e392fd71b481c
parent dab376b8b67c043a8a5e937abc4452cdd533eaac
Author: Oliver Lowe <o@olowe.co>
Date:   Sat, 29 Nov 2025 12:28:06 +1100

Dump "safe" fclose

fclose(2) already calls fflush for us.

Diffstat:
MREADME.md | 2++
Msrc/addrbook.c | 2+-
Msrc/addrindex.c | 7+------
Msrc/common/file-utils.c | 62+++++++++++++++++++-------------------------------------------
Msrc/common/file-utils.h | 6+-----
Msrc/common/prefs.c | 2+-
Msrc/common/ssl_certificate.c | 4++--
Msrc/common/utils.c | 2+-
Msrc/compose.c | 12++++++------
Msrc/etpan/imap-thread.c | 2+-
Msrc/mbox.c | 8++++----
Msrc/msgcache.c | 4++--
Msrc/pop.c | 4++--
Msrc/prefs_account.c | 2+-
Msrc/prefs_common.c | 4++--
Msrc/procmsg.c | 4++--
16 files changed, 48 insertions(+), 79 deletions(-)

diff --git a/README.md b/README.md @@ -45,6 +45,7 @@ To run the app: ## Goals Many features of Claws Mail have been removed to make it easier to maintain by a single person. +As of the end of 2025, Talons is less than half the size of Claws Mail; 120KLOC. versus Claws Mail 4.3.1 at around 295KLOC. To be honest I can't even remember how many or which features have been removed. Off the top of my head: @@ -55,6 +56,7 @@ Off the top of my head: - no localisation (sorry) - no actions - no client-side mail filtering +- no LDAP, Palm Pilot address books [Zig]: https://ziglang.org [Claws Mail]: https://claws-mail.org diff --git a/src/addrbook.c b/src/addrbook.c @@ -1328,7 +1328,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile) book->retVal = MGU_SUCCESS; #ifdef DEV_STANDALONE - safe_fclose(fp); + fclose(fp); #else if (prefs_file_close( pfile ) < 0) book->retVal = MGU_ERROR_WRITE; diff --git a/src/addrindex.c b/src/addrindex.c @@ -1051,13 +1051,8 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) goto fail; addrIndex->retVal = MGU_SUCCESS; -#ifdef DEV_STANDALONE - safe_fclose( fp ); -#else - if( prefs_file_close( pfile ) < 0 ) { + if (prefs_file_close(pfile) < 0) addrIndex->retVal = MGU_ERROR_WRITE; - } -#endif } fileSpec = NULL; diff --git a/src/common/file-utils.c b/src/common/file-utils.c @@ -20,6 +20,7 @@ #include <sys/wait.h> +#include <err.h> #include <errno.h> #include <stdio.h> #include <unistd.h> @@ -30,17 +31,6 @@ #include "utils.h" #include "file-utils.h" -int safe_fclose(FILE *fp) -{ - if (fflush(fp) != 0) { - return EOF; - } - if (fsync(fileno(fp)) != 0) { - return EOF; - } - return fclose(fp); -} - gint file_strip_crs(const gchar *file) { FILE *fp = NULL, *outfp = NULL; @@ -69,7 +59,7 @@ gint file_strip_crs(const gchar *file) } fclose(fp); - if (safe_fclose(outfp) == EOF) { + if (fclose(outfp) == EOF) { goto unlinkout; } @@ -204,7 +194,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup) err = TRUE; } fclose(src_fp); - if (safe_fclose(dest_fp) == EOF) { + if (fclose(dest_fp) == EOF) { FILE_OP_ERROR(dest, "fclose"); err = TRUE; } @@ -302,7 +292,7 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest) if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0) err = TRUE; - if (safe_fclose(dest_fp) == EOF) { + if (fclose(dest_fp) == EOF) { FILE_OP_ERROR(dest, "fclose"); err = TRUE; } @@ -384,7 +374,7 @@ gint canonicalize_file(const gchar *src, const gchar *dest) err = TRUE; } fclose(src_fp); - if (safe_fclose(dest_fp) == EOF) { + if (fclose(dest_fp) == EOF) { FILE_OP_ERROR(dest, "fclose"); err = TRUE; } @@ -420,45 +410,31 @@ gint canonicalize_file_replace(const gchar *file) } -gint str_write_to_file(const gchar *str, const gchar *file, gboolean safe) +gint str_write_to_file(const gchar *str, char *file) { - FILE *fp; - size_t len; - int r; - - cm_return_val_if_fail(str != NULL, -1); - cm_return_val_if_fail(file != NULL, -1); + if (strlen(str) == 0) + return 0; - if ((fp = g_fopen(file, "wb")) == NULL) { - FILE_OP_ERROR(file, "g_fopen"); + FILE *fp = fopen(file, "wb"); + if (fp == NULL) { + warn("open %s", file); return -1; } - len = strlen(str); - if (len == 0) { + size_t len = strlen(str); + size_t n = fwrite(str, 1, strlen(str), fp); + if (n != len) { + warn("short write to %s: expected %d bytes but wrote %d", file, len, n); fclose(fp); - return 0; - } - - if (fwrite(str, 1, len, fp) != len) { - FILE_OP_ERROR(file, "fwrite"); - fclose(fp); - unlink(file); + remove(file); return -1; } - if (safe) { - r = safe_fclose(fp); - } else { - r = fclose(fp); - } - - if (r == EOF) { - FILE_OP_ERROR(file, "fclose"); - unlink(file); + if (fclose(fp) == EOF) { + warn("close %s", file); + remove(file); return -1; } - return 0; } diff --git a/src/common/file-utils.h b/src/common/file-utils.h @@ -22,8 +22,6 @@ #include <stdio.h> #include <glib.h> -int safe_fclose(FILE *fp); - gint file_strip_crs (const gchar *file); gint append_file (const gchar *src, const gchar *dest, @@ -60,9 +58,7 @@ FILE *my_tmpfile (void); 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, - gboolean safe); +gint str_write_to_file (const gchar *str, char *file); gint prefs_chmod_mode (gchar *chmod_pref); diff --git a/src/common/prefs.c b/src/common/prefs.c @@ -155,7 +155,7 @@ gint prefs_file_close(PrefFile *pfile) tmppath = g_strconcat(path, ".tmp", NULL); - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(tmppath, "fclose"); unlink(tmppath); g_free(path); diff --git a/src/common/ssl_certificate.c b/src/common/ssl_certificate.c @@ -371,7 +371,7 @@ static void ssl_certificate_save (SSLCertificate *cert) gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER); g_free(file); - safe_fclose(fp); + fclose(fp); } @@ -676,7 +676,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const } if (fp) - safe_fclose(fp); + fclose(fp); } gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, diff --git a/src/common/utils.c b/src/common/utils.c @@ -3499,7 +3499,7 @@ void mailcap_update_default(const gchar *type, const gchar *command) if (fp) fclose(fp); - if (safe_fclose(outfp) == EOF) + if (fclose(outfp) == EOF) err = TRUE; if (!err) diff --git a/src/compose.c b/src/compose.c @@ -4936,7 +4936,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, TRUE); + str_write_to_file(content, tmp_enc_file); g_free(content); /* Now write the unencrypted body. */ @@ -5021,7 +5021,7 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file) g_free(chars); - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(file, "fclose"); unlink(file); return -1; @@ -5286,7 +5286,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold g_free(tmp); return COMPOSE_QUEUE_ERROR_WITH_ERRNO; } - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(tmp, "fclose"); unlink(tmp); g_free(tmp); @@ -8448,7 +8448,7 @@ gboolean compose_draft (gpointer data, guint action) fclose(fp); goto warn_err; } - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { goto warn_err; } @@ -8948,7 +8948,7 @@ int attach_image(Compose *compose, GtkSelectionData *data, const gchar *subtype) return -1; } - r = safe_fclose(fp); + r = fclose(fp); if (r == EOF) { FILE_OP_ERROR(file, "fclose"); @@ -9625,7 +9625,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, TRUE); + str_write_to_file(tmpdata, tmpfile); g_free(tmpdata); compose_insert_file(compose, tmpfile); unlink(tmpfile); diff --git a/src/etpan/imap-thread.c b/src/etpan/imap-thread.c @@ -2532,7 +2532,7 @@ static void fetch_content_run(struct etpan_thread_op * op) goto do_fclose; } - if (safe_fclose(f) == EOF) { + if (fclose(f) == EOF) { result->error = MAILIMAP_ERROR_FETCH; goto unlink; } diff --git a/src/mbox.c b/src/mbox.c @@ -202,7 +202,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, PrefsAccount *account) return -1; } - if (safe_fclose(tmp_fp) == EOF) { + if (fclose(tmp_fp) == EOF) { FILE_OP_ERROR(tmp_file, "fclose"); g_warning("can't write to temporary file"); fclose(mbox_fp); @@ -284,7 +284,7 @@ gint copy_mbox(gint srcfd, const gchar *dest) err = TRUE; } - if (safe_fclose(dest_fp) == EOF) { + if (fclose(dest_fp) == EOF) { FILE_OP_ERROR(dest, "fclose"); err = TRUE; } @@ -395,7 +395,7 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox) goto out; } - safe_fclose(msg_fp); + fclose(msg_fp); statusbar_progress_all(msgs++,total, 500); if (msgs%500 == 0) GTK_EVENTS_FLUSH(); @@ -405,7 +405,7 @@ out: statusbar_progress_all(0,0,0); statusbar_pop_all(); - safe_fclose(mbox_fp); + fclose(mbox_fp); return err; } diff --git a/src/msgcache.c b/src/msgcache.c @@ -975,9 +975,9 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar /* close files */ if (write_fps.cache_fp) - write_fps.error |= (safe_fclose(write_fps.cache_fp) != 0); + write_fps.error |= (fclose(write_fps.cache_fp) != 0); if (write_fps.mark_fp) - write_fps.error |= (safe_fclose(write_fps.mark_fp) != 0); + write_fps.error |= (fclose(write_fps.mark_fp) != 0); if (write_fps.error != 0) { /* in case of error, forget all */ diff --git a/src/pop.c b/src/pop.c @@ -631,7 +631,7 @@ gint pop3_write_uidl_list(Pop3Session *session) > 0); } - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(tmp_path, "fclose"); fp = NULL; goto err_write; @@ -729,7 +729,7 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data, } } - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(file, "fclose"); unlink(file); return -1; diff --git a/src/prefs_account.c b/src/prefs_account.c @@ -3442,7 +3442,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, "", TRUE); + str_write_to_file(sigpath, ""); open_txt_editor(sigpath, prefs_common_get_ext_editor_cmd()); } diff --git a/src/prefs_common.c b/src/prefs_common.c @@ -1018,7 +1018,7 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar * fputc('\n', fp) != EOF); } - if (safe_fclose(fp) == EOF) { + if (fclose(fp) == EOF) { FILE_OP_ERROR(tmp_path, "fclose"); fp = NULL; goto out; @@ -1031,7 +1031,7 @@ static void prefs_common_save_history_to_dir(const gchar *dirname, const gchar * out: if (fp) - safe_fclose(fp); + fclose(fp); g_free(tmp_path); g_free(path); } diff --git a/src/procmsg.c b/src/procmsg.c @@ -1998,14 +1998,14 @@ MsgInfo *procmsg_msginfo_new_from_mimeinfo(MsgInfo *src_msginfo, MimeInfo *mimei } if (fp && procmime_write_mimeinfo(mimeinfo, fp) >= 0) { - safe_fclose(fp); + fclose(fp); fp = NULL; tmp_msginfo = procheader_parse_file( tmpfile, flags, TRUE, FALSE); } if (fp) - safe_fclose(fp); + fclose(fp); if (tmp_msginfo != NULL) { if (src_msginfo)