]> granicus.if.org Git - neomutt/commitdiff
More careful file writing for aliases and attachments
authorSami Farin <hvtaifwkbgefbaei@gmail.com>
Wed, 16 Oct 2013 18:34:57 +0000 (11:34 -0700)
committerRichard Russon <rich@flatcap.org>
Fri, 26 Feb 2016 16:36:03 +0000 (16:36 +0000)
Use fsync and check for errors when appending to alias file,
saving attachments, and bouncing a message.  Still more checks are needed.
Also preserve errno if safe_fsync_close fails.

alias.c
attach.c
lib.c
sendlib.c

diff --git a/alias.c b/alias.c
index a8fa61f750c7da472ae2198ece91e678ae2bdfdb..4a125c3e599443fba4b5c6f0ac74f66d71ae3edc 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -27,6 +27,7 @@
 
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
 
 ADDRESS *mutt_lookup_alias (const char *s)
 {
@@ -379,8 +380,10 @@ retry_name:
     recode_buf (buf, sizeof (buf));
     write_safe_address (rc, buf);
     fputc ('\n', rc);
-    safe_fclose (&rc);
-    mutt_message _("Alias added.");
+    if (safe_fsync_close(&rc) != 0)
+      mutt_message ("Trouble adding alias: %s.", strerror(errno));
+    else
+      mutt_message _("Alias added.");
   }
   else
     mutt_perror (buf);
index 0efeb796dcd1b07a5b5b0ce44dedf19c444dfc20..504ea4ef202683f445d7cb510c7a860b336d27dc 100644 (file)
--- a/attach.c
+++ b/attach.c
@@ -765,7 +765,7 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
       fseeko ((s.fpin = fp), m->offset, 0);
       mutt_decode_attachment (m, &s);
       
-      if (fclose (s.fpout) != 0)
+      if (safe_fsync_close (&s.fpout) != 0)
       {
        mutt_perror ("fclose");
        mutt_sleep (2);
@@ -800,7 +800,10 @@ int mutt_save_attachment (FILE *fp, BODY *m, char *path, int flags, HEADER *hdr)
       return (-1);
     }
     safe_fclose (&ofp);
-    safe_fclose (&nfp);
+    if (safe_fsync_close (&nfp) != 0) {
+      mutt_error _("Write fault!");
+      return (-1);
+    }
   }
 
   return 0;
@@ -814,6 +817,7 @@ int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
   unsigned int saved_encoding = 0;
   BODY *saved_parts = NULL;
   HEADER *saved_hdr = NULL;
+  int ret = 0;
 
   memset (&s, 0, sizeof (s));
   s.flags = displaying;
@@ -871,7 +875,10 @@ int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
 
   mutt_body_handler (m, &s);
 
-  safe_fclose (&s.fpout);
+  if (safe_fsync_close (&s.fpout) != 0) {
+    mutt_perror("fclose");
+    ret = -1;
+  }
   if (fp == NULL)
   {
     m->length = 0;
@@ -885,7 +892,7 @@ int mutt_decode_save_attachment (FILE *fp, BODY *m, char *path,
     safe_fclose (&s.fpin);
   }
 
-  return (0);
+  return ret;
 }
 
 /* Ok, the difference between send and receive:
diff --git a/lib.c b/lib.c
index 5ef88c7b5a4d4e2ce528d54b0c5f6f6926512d87..988e6a3fdaf107220fb742503bfa7915230494e9 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -219,8 +219,10 @@ int safe_fsync_close (FILE **f)
   {
     if (fflush (*f) || fsync (fileno (*f)))
     {
+      int save_errno = errno;
       r = -1;
       safe_fclose (f);
+      errno = save_errno;
     }
     else
       r = safe_fclose (f);
@@ -367,6 +369,7 @@ int mutt_copy_bytes (FILE *in, FILE *out, size_t size)
     size -= chunk;
   }
 
+  if (fflush(out) != 0) return -1;
   return 0;
 }
 
@@ -381,6 +384,7 @@ int mutt_copy_stream (FILE *fin, FILE *fout)
       return (-1);
   }
 
+  if (fflush(fout) != 0) return -1;
   return 0;
 }
 
index a74874d1312f03f45e606fb57a62aa4f86be5605..14a58b54c437baca5bb1eab3228d8a98552b659f 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -2559,9 +2559,12 @@ static int _mutt_bounce_message (FILE *fp, HEADER *h, ADDRESS *to, const char *r
     mutt_copy_header (fp, h, f, ch_flags, NULL);
     fputc ('\n', f);
     mutt_copy_bytes (fp, f, h->content->length);
-    safe_fclose (&f);
     FREE (&msgid_str);
-
+    if (safe_fclose (&f) != 0) {
+      mutt_perror(tempfile);
+      unlink(tempfile);
+      return -1;
+    }
 #if USE_SMTP
     if (SmtpUrl)
       ret = mutt_smtp_send (env_from, to, NULL, NULL, tempfile,