NeoMutt  2025-12-11-694-ga89709
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() -.
 
bool maildir_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() -.
 
static bool mh_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() -.

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

Definition at line 474 of file compress.c.

475{
476 /* If this succeeds, we know there's an open-hook */
477 struct CompressInfo *ci = set_compress_info(m);
478 if (!ci)
479 return false;
480
481 /* To append we need an append-hook or a close-hook */
482 if (!ci->cmd_append && !ci->cmd_close)
483 {
484 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
485 mailbox_path(m));
486 goto cmoa_fail1;
487 }
488
489 if (setup_paths(m) != 0)
490 goto cmoa_fail2;
491
492 /* Lock the realpath for the duration of the append.
493 * It will be unlocked in the close */
494 if (!lock_realpath(m, true))
495 {
496 mutt_error(_("Unable to lock mailbox"));
497 goto cmoa_fail2;
498 }
499
500 /* Open the existing mailbox, unless we are appending */
501 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
502 {
503 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
504 {
505 mutt_error(_("Compress command failed: %s"), ci->cmd_open->string);
506 goto cmoa_fail2;
507 }
509 }
510 else
511 {
512 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
513 }
514
515 /* We can only deal with mbox and mmdf mailboxes */
516 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
517 {
518 mutt_error(_("Unsupported mailbox type for appending"));
519 goto cmoa_fail2;
520 }
521
522 ci->child_ops = mx_get_ops(m->type);
523 if (!ci->child_ops)
524 {
525 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
526 goto cmoa_fail2;
527 }
528
529 if (!ci->child_ops->mbox_open_append(m, flags))
530 goto cmoa_fail2;
531
532 return true;
533
534cmoa_fail2:
535 /* remove the partial uncompressed file */
536 (void) remove(mailbox_path(m));
537cmoa_fail1:
538 /* Free the compress_info to prevent close from trying to recompress */
540
541 return false;
542}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition compress.c:239
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition compress.c:269
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition compress.c:179
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition compress.c:108
static bool execute_command(struct Mailbox *m, const struct Expando *exp, const char *progress)
Run a system command.
Definition compress.c:295
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition helpers.c:71
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
@ MUTT_MMDF
'mmdf' Mailbox type
Definition mailbox.h:45
@ MUTT_MBOX
'mbox' Mailbox type
Definition mailbox.h:44
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1414
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
const struct MxOps * mx_get_ops(enum MailboxType type)
Get mailbox operations.
Definition mx.c:124
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition mx.c:1323
Private data for compress.
Definition lib.h:61
struct Expando * cmd_open
open-hook command
Definition lib.h:64
struct Expando * cmd_append
append-hook command
Definition lib.h:62
const struct MxOps * child_ops
callbacks of de-compressed file
Definition lib.h:66
struct Expando * cmd_close
close-hook command
Definition lib.h:63
const char * string
Pointer to the parsed string.
Definition expando.h:42
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition mailbox.h:80
enum MailboxType type
Mailbox type.
Definition mailbox.h:101
bool(* mbox_open_append)(struct Mailbox *m, OpenMailboxFlags flags)
Definition mxapi.h:146
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ 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 2270 of file imap.c.

2271{
2272 if (!m->account)
2273 return false;
2274
2275 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2276 * mailbox is brand new and mostly empty */
2278 struct ImapMboxData *mdata = imap_mdata_get(m);
2279 if (!adata || !mdata)
2280 return false;
2281
2282 int rc = imap_mailbox_status(m, false);
2283 if (rc >= 0)
2284 return true;
2285 if (rc == -1)
2286 return false;
2287
2288 char buf[PATH_MAX + 64];
2289 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2290 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2291 if (c_confirm_create &&
2292 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2293 return false;
2294
2295 if (imap_create_mailbox(adata, mdata->name) < 0)
2296 return false;
2297
2298 return true;
2299}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition adata.c:158
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition mdata.c:61
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition imap.c:1389
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition imap.c:542
#define PATH_MAX
Definition mutt.h:49
@ 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:357
void * adata
Private data (for Mailbox backends)
Definition account.h:42
IMAP-specific Account data -.
Definition adata.h:40
IMAP-specific Mailbox data -.
Definition mdata.h:40
void * mdata
Driver specific data.
Definition mailbox.h:131
struct Account * account
Account that owns this Mailbox.
Definition mailbox.h:126
+ Here is the call graph for this function:

◆ maildir_mbox_open_append()

bool maildir_mbox_open_append ( struct Mailbox * m,
OpenMailboxFlags flags )

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

Definition at line 757 of file mailbox.c.

758{
759 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW)))
760 {
761 return true;
762 }
763
764 errno = 0;
765 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
766 {
767 mutt_perror("%s", mailbox_path(m));
768 return false;
769 }
770
771 char tmp[PATH_MAX] = { 0 };
772 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
773 errno = 0;
774 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
775 {
776 mutt_perror("%s", tmp);
777 rmdir(mailbox_path(m));
778 return false;
779 }
780
781 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
782 errno = 0;
783 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
784 {
785 mutt_perror("%s", tmp);
786 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
787 rmdir(tmp);
788 rmdir(mailbox_path(m));
789 return false;
790 }
791
792 snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
793 errno = 0;
794 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
795 {
796 mutt_perror("%s", tmp);
797 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
798 rmdir(tmp);
799 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
800 rmdir(tmp);
801 rmdir(mailbox_path(m));
802 return false;
803 }
804
805 return true;
806}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition file.c:844
#define mutt_perror(...)
Definition logging2.h:95
#define MUTT_APPEND
Open mailbox for appending messages.
Definition mxapi.h:41
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn't exist.
Definition mxapi.h:45
+ 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 881 of file mbox.c.

882{
883 if (init_mailbox(m) != 0)
884 return false;
885
887 if (!adata)
888 return false;
889
890 if (!adata->fp)
891 {
892 // create dir recursively
893 char *tmp_path = mutt_path_dirname(mailbox_path(m));
894 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
895 {
896 mutt_perror("%s", mailbox_path(m));
897 FREE(&tmp_path);
898 return false;
899 }
900 FREE(&tmp_path);
901
902 adata->fp = mutt_file_fopen(mailbox_path(m), "a+");
903 if (!adata->fp)
904 {
905 mutt_perror("%s", mailbox_path(m));
906 return false;
907 }
908
909 if (mbox_lock_mailbox(m, true, true) != 0)
910 {
911 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
913 return false;
914 }
915 }
916
917 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
918 {
920 return false;
921 }
922
923 return true;
924}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:648
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition mbox.c:136
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition mbox.c:121
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition mbox.c:102
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final '/'.
Definition path.c:312
Mbox-specific Account data -.
Definition lib.h:50
+ 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 827 of file mh.c.

828{
829 if (!(flags & MUTT_APPENDNEW))
830 return true;
831
832 if (mutt_file_mkdir(mailbox_path(m), S_IRWXU))
833 {
834 mutt_perror("%s", mailbox_path(m));
835 return false;
836 }
837
838 char tmp[PATH_MAX] = { 0 };
839 snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", mailbox_path(m));
840 const int i = creat(tmp, S_IRUSR | S_IWUSR);
841 if (i == -1)
842 {
843 mutt_perror("%s", tmp);
844 rmdir(mailbox_path(m));
845 return false;
846 }
847 close(i);
848
849 return true;
850}
+ Here is the call graph for this function: