NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
mbox_open_append()

Open a Mailbox for appending. More...

+ Collaboration diagram for mbox_open_append():

Functions

static bool comp_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool imap_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool maildir_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool mh_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool mbox_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 

Detailed Description

Open a Mailbox for appending.

Parameters
mMailbox to open
flagsFlags, see OpenMailboxFlags
Return values
trueSuccess
falseFailure

Contract

Function Documentation

◆ comp_mbox_open_append()

static bool comp_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.

flags may also contain MUTT_NEWFOLDER

To append to a compressed mailbox we need an append-hook (or both open- and close-hooks).

Definition at line 501 of file compress.c.

502{
503 /* If this succeeds, we know there's an open-hook */
504 struct CompressInfo *ci = set_compress_info(m);
505 if (!ci)
506 return false;
507
508 /* To append we need an append-hook or a close-hook */
509 if (!ci->cmd_append && !ci->cmd_close)
510 {
511 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
512 mailbox_path(m));
513 goto cmoa_fail1;
514 }
515
516 if (setup_paths(m) != 0)
517 goto cmoa_fail2;
518
519 /* Lock the realpath for the duration of the append.
520 * It will be unlocked in the close */
521 if (!lock_realpath(m, true))
522 {
523 mutt_error(_("Unable to lock mailbox"));
524 goto cmoa_fail2;
525 }
526
527 /* Open the existing mailbox, unless we are appending */
528 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
529 {
530 int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
531 if (rc == 0)
532 {
533 mutt_error(_("Compress command failed: %s"), ci->cmd_open);
534 goto cmoa_fail2;
535 }
537 }
538 else
539 {
540 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
541 }
542
543 /* We can only deal with mbox and mmdf mailboxes */
544 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
545 {
546 mutt_error(_("Unsupported mailbox type for appending"));
547 goto cmoa_fail2;
548 }
549
550 ci->child_ops = mx_get_ops(m->type);
551 if (!ci->child_ops)
552 {
553 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
554 goto cmoa_fail2;
555 }
556
557 if (!ci->child_ops->mbox_open_append(m, flags))
558 goto cmoa_fail2;
559
560 return true;
561
562cmoa_fail2:
563 /* remove the partial uncompressed file */
564 (void) remove(mailbox_path(m));
565cmoa_fail1:
566 /* Free the compress_info to prevent close from trying to recompress */
568
569 return false;
570}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition: compress.c:197
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition: compress.c:227
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition: compress.c:151
static int execute_command(struct Mailbox *m, const char *command, const char *progress)
Run a system command.
Definition: compress.c:322
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:86
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:97
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1551
#define mutt_error(...)
Definition: logging.h:87
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
#define _(a)
Definition: message.h:28
const struct MxOps * mx_get_ops(enum MailboxType type)
Get mailbox operations.
Definition: mx.c:141
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1327
Private data for compress.
Definition: lib.h:47
const char * cmd_append
append-hook command
Definition: lib.h:48
const char * cmd_open
open-hook command
Definition: lib.h:50
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:52
const char * cmd_close
close-hook command
Definition: lib.h:49
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
bool(* mbox_open_append)(struct Mailbox *m, OpenMailboxFlags flags)
Definition: mxapi.h:175
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:

◆ imap_mbox_open_append()

static bool imap_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.

Definition at line 2133 of file imap.c.

2134{
2135 if (!m->account)
2136 return false;
2137
2138 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2139 * mailbox is brand new and mostly empty */
2141 struct ImapMboxData *mdata = imap_mdata_get(m);
2142
2143 int rc = imap_mailbox_status(m, false);
2144 if (rc >= 0)
2145 return true;
2146 if (rc == -1)
2147 return false;
2148
2149 char buf[PATH_MAX + 64];
2150 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2151 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2152 if (c_confirm_create && (mutt_yesorno(buf, MUTT_YES) != MUTT_YES))
2153 return false;
2154
2155 if (imap_create_mailbox(adata, mdata->name) < 0)
2156 return false;
2157
2158 return true;
2159}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:89
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition: mdata.c:60
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition: imap.c:1273
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition: imap.c:447
#define PATH_MAX
Definition: mutt.h:40
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:194
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
IMAP-specific Account data -.
Definition: adata.h:40
IMAP-specific Mailbox data -.
Definition: mdata.h:39
void * mdata
Driver specific data.
Definition: mailbox.h:132
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:127
+ Here is the call graph for this function:

◆ maildir_mbox_open_append()

static bool maildir_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.

Definition at line 1106 of file maildir.c.

1107{
1108 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW | MUTT_NEWFOLDER)))
1109 {
1110 return true;
1111 }
1112
1113 errno = 0;
1114 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
1115 {
1117 return false;
1118 }
1119
1120 char tmp[PATH_MAX] = { 0 };
1121 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1122 errno = 0;
1123 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1124 {
1125 mutt_perror(tmp);
1126 rmdir(mailbox_path(m));
1127 return false;
1128 }
1129
1130 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1131 errno = 0;
1132 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1133 {
1134 mutt_perror(tmp);
1135 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1136 rmdir(tmp);
1137 rmdir(mailbox_path(m));
1138 return false;
1139 }
1140
1141 snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
1142 errno = 0;
1143 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1144 {
1145 mutt_perror(tmp);
1146 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1147 rmdir(tmp);
1148 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1149 rmdir(tmp);
1150 rmdir(mailbox_path(m));
1151 return false;
1152 }
1153
1154 return true;
1155}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:930
#define mutt_perror(...)
Definition: logging.h:88
#define MUTT_NEWFOLDER
Create a new folder - same as MUTT_APPEND, but uses mutt_file_fopen() with mode "w" for mbox-style fo...
Definition: mxapi.h:66
#define MUTT_APPEND
Open mailbox for appending messages.
Definition: mxapi.h:63
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn't exist.
Definition: mxapi.h:70
+ Here is the call graph for this function:

◆ mh_mbox_open_append()

static bool mh_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.

Definition at line 857 of file mh.c.

858{
859 if (!(flags & (MUTT_APPENDNEW | MUTT_NEWFOLDER)))
860 return true;
861
862 if (mutt_file_mkdir(mailbox_path(m), S_IRWXU))
863 {
865 return false;
866 }
867
868 char tmp[PATH_MAX] = { 0 };
869 snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", mailbox_path(m));
870 const int i = creat(tmp, S_IRWXU);
871 if (i == -1)
872 {
873 mutt_perror(tmp);
874 rmdir(mailbox_path(m));
875 return false;
876 }
877 close(i);
878
879 return true;
880}
+ Here is the call graph for this function:

◆ mbox_mbox_open_append()

static bool mbox_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.

Definition at line 974 of file mbox.c.

975{
976 if (init_mailbox(m) != 0)
977 return false;
978
980 if (!adata)
981 return false;
982
983 if (!adata->fp)
984 {
985 // create dir recursively
986 char *tmp_path = mutt_path_dirname(mailbox_path(m));
987 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
988 {
990 FREE(&tmp_path);
991 return false;
992 }
993 FREE(&tmp_path);
994
995 adata->fp = mutt_file_fopen(mailbox_path(m), (flags & MUTT_NEWFOLDER) ? "w+" : "a+");
996 if (!adata->fp)
997 {
999 return false;
1000 }
1001
1002 if (mbox_lock_mailbox(m, true, true) != false)
1003 {
1004 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
1005 mutt_file_fclose(&adata->fp);
1006 return false;
1007 }
1008 }
1009
1010 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
1011 {
1012 mutt_file_fclose(&adata->fp);
1013 return false;
1014 }
1015
1016 return true;
1017}
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:618
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:690
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition: mbox.c:134
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:119
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition: mbox.c:100
#define FREE(x)
Definition: memory.h:43
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final '/'.
Definition: path.c:376
Mbox-specific Account data -.
Definition: lib.h:49
+ Here is the call graph for this function: