From: Kevin McCarthy Date: Tue, 2 Aug 2016 01:25:28 +0000 (-0700) Subject: Use a different flag in mx_open_mailbox_append() when mailbox doesn't exist. X-Git-Tag: neomutt-20160822~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=78eba0d4899f80b6f23b8743705d9fa35c546e70;p=neomutt Use a different flag in mx_open_mailbox_append() when mailbox doesn't exist. The previous commit re-used MUTT_NEWFOLDER, but the meaning of that flag is slightly different: it causes mbox to use fopen with mode "w", and is used only for the case of a brand-new mktemp-generated mbox. Setting it for other non-existing mbox files leads to a race condition between the stat and the fopen/lock, and so could end up truncating an existing mailbox created in-between! Create a different flag, MUTT_APPENDNEW to notify the open_append() functions that the mailbox doesn't exist. Change maildir and mh to check for that flag to create their directory structures. --- diff --git a/mailbox.h b/mailbox.h index 1fb1efe0d..35fb6013c 100644 --- a/mailbox.h +++ b/mailbox.h @@ -25,8 +25,11 @@ #define MUTT_READONLY (1<<2) /* open in read-only mode */ #define MUTT_QUIET (1<<3) /* do not print any messages */ #define MUTT_NEWFOLDER (1<<4) /* create a new folder - same as MUTT_APPEND, but uses - * safe_fopen() for mbox-style folders. */ + * safe_fopen() with mode "w" for mbox-style folders. + * This will truncate an existing file. */ #define MUTT_PEEK (1<<5) /* revert atime back after taking a look (if applicable) */ +#define MUTT_APPENDNEW (1<<6) /* set in mx_open_mailbox_append if the mailbox doesn't + * exist. used by maildir/mh to create the mailbox. */ /* mx_open_new_message() */ #define MUTT_ADD_FROM (1<<0) /* add a From_ line */ diff --git a/mh.c b/mh.c index 3d969f628..f7ce25fc4 100644 --- a/mh.c +++ b/mh.c @@ -1294,7 +1294,7 @@ static int maildir_open_mailbox_append (CONTEXT *ctx, int flags) { char tmp[_POSIX_PATH_MAX]; - if (flags & MUTT_NEWFOLDER) + if (flags & MUTT_APPENDNEW) { if (mkdir (ctx->path, S_IRWXU)) { @@ -1346,7 +1346,7 @@ static int mh_open_mailbox_append (CONTEXT *ctx, int flags) char tmp[_POSIX_PATH_MAX]; int i; - if (flags & MUTT_NEWFOLDER) + if (flags & MUTT_APPENDNEW) { if (mkdir (ctx->path, S_IRWXU)) { diff --git a/mx.c b/mx.c index 5b08eb867..7ffe0321e 100644 --- a/mx.c +++ b/mx.c @@ -495,7 +495,7 @@ static int mx_open_mailbox_append (CONTEXT *ctx, int flags) if (errno == ENOENT) { ctx->magic = DefaultMagic; - flags |= MUTT_NEWFOLDER; + flags |= MUTT_APPENDNEW; } else {