NeoMutt  2025-12-11-117-gc1a713
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 470 of file compress.c.

471{
472 /* If this succeeds, we know there's an open-hook */
473 struct CompressInfo *ci = set_compress_info(m);
474 if (!ci)
475 return false;
476
477 /* To append we need an append-hook or a close-hook */
478 if (!ci->cmd_append && !ci->cmd_close)
479 {
480 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
481 mailbox_path(m));
482 goto cmoa_fail1;
483 }
484
485 if (setup_paths(m) != 0)
486 goto cmoa_fail2;
487
488 /* Lock the realpath for the duration of the append.
489 * It will be unlocked in the close */
490 if (!lock_realpath(m, true))
491 {
492 mutt_error(_("Unable to lock mailbox"));
493 goto cmoa_fail2;
494 }
495
496 /* Open the existing mailbox, unless we are appending */
497 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
498 {
499 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
500 {
501 mutt_error(_("Compress command failed: %s"), ci->cmd_open->string);
502 goto cmoa_fail2;
503 }
505 }
506 else
507 {
508 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
509 }
510
511 /* We can only deal with mbox and mmdf mailboxes */
512 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
513 {
514 mutt_error(_("Unsupported mailbox type for appending"));
515 goto cmoa_fail2;
516 }
517
518 ci->child_ops = mx_get_ops(m->type);
519 if (!ci->child_ops)
520 {
521 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
522 goto cmoa_fail2;
523 }
524
525 if (!ci->child_ops->mbox_open_append(m, flags))
526 goto cmoa_fail2;
527
528 return true;
529
530cmoa_fail2:
531 /* remove the partial uncompressed file */
532 (void) remove(mailbox_path(m));
533cmoa_fail1:
534 /* Free the compress_info to prevent close from trying to recompress */
536
537 return false;
538}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition compress.c:235
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition compress.c:265
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition compress.c:175
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:291
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:214
@ MUTT_MMDF
'mmdf' Mailbox type
Definition mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition mailbox.h:45
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1413
#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:127
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition mx.c:1326
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:81
enum MailboxType type
Mailbox type.
Definition mailbox.h:102
bool(* mbox_open_append)(struct Mailbox *m, OpenMailboxFlags flags)
Definition mxapi.h:147
Container for Accounts, Notifications.
Definition neomutt.h:128
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:134
+ 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 2195 of file imap.c.

2196{
2197 if (!m->account)
2198 return false;
2199
2200 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2201 * mailbox is brand new and mostly empty */
2203 struct ImapMboxData *mdata = imap_mdata_get(m);
2204 if (!adata || !mdata)
2205 return false;
2206
2207 int rc = imap_mailbox_status(m, false);
2208 if (rc >= 0)
2209 return true;
2210 if (rc == -1)
2211 return false;
2212
2213 char buf[PATH_MAX + 64];
2214 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2215 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2216 if (c_confirm_create &&
2217 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2218 return false;
2219
2220 if (imap_create_mailbox(adata, mdata->name) < 0)
2221 return false;
2222
2223 return true;
2224}
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:123
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:1316
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition imap.c:535
#define PATH_MAX
Definition mutt.h:42
@ 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:354
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: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()

bool maildir_mbox_open_append ( struct Mailbox * m,
OpenMailboxFlags flags )

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

Definition at line 752 of file mailbox.c.

753{
754 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW)))
755 {
756 return true;
757 }
758
759 errno = 0;
760 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
761 {
762 mutt_perror("%s", mailbox_path(m));
763 return false;
764 }
765
766 char tmp[PATH_MAX] = { 0 };
767 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
768 errno = 0;
769 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
770 {
771 mutt_perror("%s", tmp);
772 rmdir(mailbox_path(m));
773 return false;
774 }
775
776 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
777 errno = 0;
778 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
779 {
780 mutt_perror("%s", tmp);
781 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
782 rmdir(tmp);
783 rmdir(mailbox_path(m));
784 return false;
785 }
786
787 snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
788 errno = 0;
789 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
790 {
791 mutt_perror("%s", tmp);
792 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
793 rmdir(tmp);
794 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
795 rmdir(tmp);
796 rmdir(mailbox_path(m));
797 return false;
798 }
799
800 return true;
801}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition file.c:852
#define mutt_perror(...)
Definition logging2.h:95
#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:46
+ 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), "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}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:656
#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:139
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition mbox.c:124
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition mbox.c:105
#define FREE(x)
Definition memory.h:63
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 824 of file mh.c.

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