NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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() -.
 
static bool imap_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
 
static bool maildir_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
 
static bool mh_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
 
static bool mbox_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
 

Detailed Description

Open a Mailbox for appending.

Parameters
mMailbox to open
flagsFlags, see OpenMailboxFlags
Return values
trueSuccess
falseFailure
Precondition
m is not NULL

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 505 of file compress.c.

506{
507 /* If this succeeds, we know there's an open-hook */
508 struct CompressInfo *ci = set_compress_info(m);
509 if (!ci)
510 return false;
511
512 /* To append we need an append-hook or a close-hook */
513 if (!ci->cmd_append && !ci->cmd_close)
514 {
515 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
516 mailbox_path(m));
517 goto cmoa_fail1;
518 }
519
520 if (setup_paths(m) != 0)
521 goto cmoa_fail2;
522
523 /* Lock the realpath for the duration of the append.
524 * It will be unlocked in the close */
525 if (!lock_realpath(m, true))
526 {
527 mutt_error(_("Unable to lock mailbox"));
528 goto cmoa_fail2;
529 }
530
531 /* Open the existing mailbox, unless we are appending */
532 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
533 {
534 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
535 {
536 mutt_error(_("Compress command failed: %s"), ci->cmd_open);
537 goto cmoa_fail2;
538 }
540 }
541 else
542 {
543 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
544 }
545
546 /* We can only deal with mbox and mmdf mailboxes */
547 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
548 {
549 mutt_error(_("Unsupported mailbox type for appending"));
550 goto cmoa_fail2;
551 }
552
553 ci->child_ops = mx_get_ops(m->type);
554 if (!ci->child_ops)
555 {
556 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
557 goto cmoa_fail2;
558 }
559
560 if (!ci->child_ops->mbox_open_append(m, flags))
561 goto cmoa_fail2;
562
563 return true;
564
565cmoa_fail2:
566 /* remove the partial uncompressed file */
567 (void) remove(mailbox_path(m));
568cmoa_fail1:
569 /* Free the compress_info to prevent close from trying to recompress */
571
572 return false;
573}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition: compress.c:202
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition: compress.c:232
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition: compress.c:156
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:89
static bool execute_command(struct Mailbox *m, const char *command, const char *progress)
Run a system command.
Definition: compress.c:327
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:72
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1560
#define mutt_error(...)
Definition: logging2.h:92
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:139
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1343
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:150
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ 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 2061 of file imap.c.

2062{
2063 if (!m->account)
2064 return false;
2065
2066 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2067 * mailbox is brand new and mostly empty */
2069 struct ImapMboxData *mdata = imap_mdata_get(m);
2070
2071 int rc = imap_mailbox_status(m, false);
2072 if (rc >= 0)
2073 return true;
2074 if (rc == -1)
2075 return false;
2076
2077 char buf[PATH_MAX + 64];
2078 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2079 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2080 if (c_confirm_create &&
2081 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2082 return false;
2083
2084 if (imap_create_mailbox(adata, mdata->name) < 0)
2085 return false;
2086
2087 return true;
2088}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:123
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:1192
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition: imap.c:429
#define PATH_MAX
Definition: mutt.h:41
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition: question.c:345
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:40
void * mdata
Driver specific data.
Definition: mailbox.h:133
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:128
+ 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 1136 of file maildir.c.

1137{
1138 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW | MUTT_NEWFOLDER)))
1139 {
1140 return true;
1141 }
1142
1143 errno = 0;
1144 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
1145 {
1146 mutt_perror("%s", mailbox_path(m));
1147 return false;
1148 }
1149
1150 char tmp[PATH_MAX] = { 0 };
1151 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1152 errno = 0;
1153 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1154 {
1155 mutt_perror("%s", tmp);
1156 rmdir(mailbox_path(m));
1157 return false;
1158 }
1159
1160 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1161 errno = 0;
1162 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1163 {
1164 mutt_perror("%s", tmp);
1165 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1166 rmdir(tmp);
1167 rmdir(mailbox_path(m));
1168 return false;
1169 }
1170
1171 snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
1172 errno = 0;
1173 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1174 {
1175 mutt_perror("%s", tmp);
1176 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1177 rmdir(tmp);
1178 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1179 rmdir(tmp);
1180 rmdir(mailbox_path(m));
1181 return false;
1182 }
1183
1184 return true;
1185}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:977
#define mutt_perror(...)
Definition: logging2.h:93
#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:45
#define MUTT_APPEND
Open mailbox for appending messages.
Definition: mxapi.h:42
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn't exist.
Definition: mxapi.h:49
+ 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 843 of file mh.c.

844{
845 if (!(flags & (MUTT_APPENDNEW | MUTT_NEWFOLDER)))
846 return true;
847
848 if (mutt_file_mkdir(mailbox_path(m), S_IRWXU))
849 {
850 mutt_perror("%s", mailbox_path(m));
851 return false;
852 }
853
854 char tmp[PATH_MAX] = { 0 };
855 snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", mailbox_path(m));
856 const int i = creat(tmp, S_IRWXU);
857 if (i == -1)
858 {
859 mutt_perror("%s", tmp);
860 rmdir(mailbox_path(m));
861 return false;
862 }
863 close(i);
864
865 return true;
866}
+ 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 877 of file mbox.c.

878{
879 if (init_mailbox(m) != 0)
880 return false;
881
883 if (!adata)
884 return false;
885
886 if (!adata->fp)
887 {
888 // create dir recursively
889 char *tmp_path = mutt_path_dirname(mailbox_path(m));
890 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
891 {
892 mutt_perror("%s", mailbox_path(m));
893 FREE(&tmp_path);
894 return false;
895 }
896 FREE(&tmp_path);
897
898 adata->fp = mutt_file_fopen(mailbox_path(m), (flags & MUTT_NEWFOLDER) ? "w+" : "a+");
899 if (!adata->fp)
900 {
901 mutt_perror("%s", mailbox_path(m));
902 return false;
903 }
904
905 if (mbox_lock_mailbox(m, true, true) != false)
906 {
907 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
909 return false;
910 }
911 }
912
913 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
914 {
916 return false;
917 }
918
919 return true;
920}
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
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:733
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition: mbox.c:138
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:123
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition: mbox.c:104
#define FREE(x)
Definition: memory.h:45
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final '/'.
Definition: path.c:381
Mbox-specific Account data -.
Definition: lib.h:49
+ Here is the call graph for this function: