Skip to content

Commit

Permalink
More careful file writing for aliases and attachments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Safari77 authored and karelzak committed Dec 15, 2015
1 parent 7efc815 commit 51e6d38
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
7 changes: 5 additions & 2 deletions alias.c
Expand Up @@ -27,6 +27,7 @@

#include <string.h>
#include <ctype.h>
#include <errno.h>

ADDRESS *mutt_lookup_alias (const char *s)
{
Expand Down Expand Up @@ -379,8 +380,10 @@ void mutt_create_alias (ENVELOPE *cur, ADDRESS *iadr)
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);
Expand Down
15 changes: 11 additions & 4 deletions attach.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions lib.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -381,6 +384,7 @@ int mutt_copy_stream (FILE *fin, FILE *fout)
return (-1);
}

if (fflush(fout) != 0) return -1;
return 0;
}

Expand Down
7 changes: 5 additions & 2 deletions sendlib.c
Expand Up @@ -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,
Expand Down

0 comments on commit 51e6d38

Please sign in to comment.