NeoMutt  2025-12-11-911-gd8d604
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 473 of file compress.c.

474{
475 /* If this succeeds, we know there's an open-hook */
476 struct CompressInfo *ci = set_compress_info(m);
477 if (!ci)
478 return false;
479
480 /* To append we need an append-hook or a close-hook */
481 if (!ci->cmd_append && !ci->cmd_close)
482 {
483 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
484 mailbox_path(m));
485 goto cmoa_fail1;
486 }
487
488 if (setup_paths(m) != 0)
489 goto cmoa_fail2;
490
491 /* Lock the realpath for the duration of the append.
492 * It will be unlocked in the close */
493 if (!lock_realpath(m, true))
494 {
495 mutt_error(_("Unable to lock mailbox"));
496 goto cmoa_fail2;
497 }
498
499 /* Open the existing mailbox, unless we are appending */
500 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
501 {
502 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
503 {
504 mutt_error(_("Compress command failed: %s"), ci->cmd_open->string);
505 goto cmoa_fail2;
506 }
508 }
509 else
510 {
511 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
512 }
513
514 /* We can only deal with mbox and mmdf mailboxes */
515 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
516 {
517 mutt_error(_("Unsupported mailbox type for appending"));
518 goto cmoa_fail2;
519 }
520
521 ci->child_ops = mx_get_ops(m->type);
522 if (!ci->child_ops)
523 {
524 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
525 goto cmoa_fail2;
526 }
527
528 if (!ci->child_ops->mbox_open_append(m, flags))
529 goto cmoa_fail2;
530
531 return true;
532
533cmoa_fail2:
534 /* remove the partial uncompressed file */
535 (void) remove(mailbox_path(m));
536cmoa_fail1:
537 /* Free the compress_info to prevent close from trying to recompress */
539
540 return false;
541}
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:216
@ 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:83
enum MailboxType type
Mailbox type.
Definition mailbox.h:104
bool(* mbox_open_append)(struct Mailbox *m, OpenMailboxFlags flags)
Definition mxapi.h:157
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 2405 of file imap.c.

2406{
2407 if (!m->account)
2408 return false;
2409
2410 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2411 * mailbox is brand new and mostly empty */
2413 struct ImapMboxData *mdata = imap_mdata_get(m);
2414 if (!adata || !mdata)
2415 return false;
2416
2417 int rc = imap_mailbox_status(m, false);
2418 if (rc >= 0)
2419 return true;
2420 if (rc == -1)
2421 return false;
2422
2423 char buf[PATH_MAX + 64];
2424 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2425 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2426 if (c_confirm_create &&
2427 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2428 return false;
2429
2430 if (imap_create_mailbox(adata, mdata->name) < 0)
2431 return false;
2432
2433 return true;
2434}
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:162
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:1397
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:134
struct Account * account
Account that owns this Mailbox.
Definition mailbox.h:129
+ 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 758 of file mailbox.c.

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

905{
906 if (init_mailbox(m) != 0)
907 return false;
908
910 if (!adata)
911 return false;
912
913 if (!adata->fp)
914 {
915 // create dir recursively
916 char *tmp_path = mutt_path_dirname(mailbox_path(m));
917 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
918 {
919 mutt_perror("%s", mailbox_path(m));
920 FREE(&tmp_path);
921 return false;
922 }
923 FREE(&tmp_path);
924
925 adata->fp = mutt_file_fopen(mailbox_path(m), "a+");
926 if (!adata->fp)
927 {
928 mutt_perror("%s", mailbox_path(m));
929 return false;
930 }
931
932 if (mbox_lock_mailbox(m, true, true) != 0)
933 {
934 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
936 return false;
937 }
938 }
939
940 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
941 {
943 return false;
944 }
945
946 return true;
947}
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:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
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) != 0)
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: