NeoMutt  2024-03-23-23-gec7045
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() -.

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

504{
505 /* If this succeeds, we know there's an open-hook */
506 struct CompressInfo *ci = set_compress_info(m);
507 if (!ci)
508 return false;
509
510 /* To append we need an append-hook or a close-hook */
511 if (!ci->cmd_append && !ci->cmd_close)
512 {
513 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
514 mailbox_path(m));
515 goto cmoa_fail1;
516 }
517
518 if (setup_paths(m) != 0)
519 goto cmoa_fail2;
520
521 /* Lock the realpath for the duration of the append.
522 * It will be unlocked in the close */
523 if (!lock_realpath(m, true))
524 {
525 mutt_error(_("Unable to lock mailbox"));
526 goto cmoa_fail2;
527 }
528
529 /* Open the existing mailbox, unless we are appending */
530 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
531 {
532 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
533 {
534 mutt_error(_("Compress command failed: %s"), ci->cmd_open->string);
535 goto cmoa_fail2;
536 }
538 }
539 else
540 {
541 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
542 }
543
544 /* We can only deal with mbox and mmdf mailboxes */
545 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
546 {
547 mutt_error(_("Unsupported mailbox type for appending"));
548 goto cmoa_fail2;
549 }
550
551 ci->child_ops = mx_get_ops(m->type);
552 if (!ci->child_ops)
553 {
554 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
555 goto cmoa_fail2;
556 }
557
558 if (!ci->child_ops->mbox_open_append(m, flags))
559 goto cmoa_fail2;
560
561 return true;
562
563cmoa_fail2:
564 /* remove the partial uncompressed file */
565 (void) remove(mailbox_path(m));
566cmoa_fail1:
567 /* Free the compress_info to prevent close from trying to recompress */
569
570 return false;
571}
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:174
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:107
static bool execute_command(struct Mailbox *m, const struct Expando *exp, const char *progress)
Run a system command.
Definition: compress.c:323
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:72
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:223
@ 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:1560
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
const struct MxOps * mx_get_ops(enum MailboxType type)
Get mailbox operations.
Definition: mx.c:125
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1319
Private data for compress.
Definition: lib.h:58
struct Expando * cmd_open
open-hook command
Definition: lib.h:61
struct Expando * cmd_append
append-hook command
Definition: lib.h:59
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:63
struct Expando * cmd_close
close-hook command
Definition: lib.h:60
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: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 2071 of file imap.c.

2072{
2073 if (!m->account)
2074 return false;
2075
2076 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2077 * mailbox is brand new and mostly empty */
2079 struct ImapMboxData *mdata = imap_mdata_get(m);
2080
2081 int rc = imap_mailbox_status(m, false);
2082 if (rc >= 0)
2083 return true;
2084 if (rc == -1)
2085 return false;
2086
2087 char buf[PATH_MAX + 64];
2088 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2089 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2090 if (c_confirm_create &&
2091 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2092 return false;
2093
2094 if (imap_create_mailbox(adata, mdata->name) < 0)
2095 return false;
2096
2097 return true;
2098}
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:1206
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition: imap.c:437
#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:342
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 746 of file mailbox.c.

747{
748 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW | MUTT_NEWFOLDER)))
749 {
750 return true;
751 }
752
753 errno = 0;
754 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
755 {
756 mutt_perror("%s", mailbox_path(m));
757 return false;
758 }
759
760 char tmp[PATH_MAX] = { 0 };
761 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
762 errno = 0;
763 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
764 {
765 mutt_perror("%s", tmp);
766 rmdir(mailbox_path(m));
767 return false;
768 }
769
770 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
771 errno = 0;
772 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
773 {
774 mutt_perror("%s", tmp);
775 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
776 rmdir(tmp);
777 rmdir(mailbox_path(m));
778 return false;
779 }
780
781 snprintf(tmp, sizeof(tmp), "%s/tmp", 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 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
789 rmdir(tmp);
790 rmdir(mailbox_path(m));
791 return false;
792 }
793
794 return true;
795}
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:1015
#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:

◆ 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 876 of file mbox.c.

877{
878 if (init_mailbox(m) != 0)
879 return false;
880
882 if (!adata)
883 return false;
884
885 if (!adata->fp)
886 {
887 // create dir recursively
888 char *tmp_path = mutt_path_dirname(mailbox_path(m));
889 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
890 {
891 mutt_perror("%s", mailbox_path(m));
892 FREE(&tmp_path);
893 return false;
894 }
895 FREE(&tmp_path);
896
897 adata->fp = mutt_file_fopen(mailbox_path(m), (flags & MUTT_NEWFOLDER) ? "w+" : "a+");
898 if (!adata->fp)
899 {
900 mutt_perror("%s", mailbox_path(m));
901 return false;
902 }
903
904 if (mbox_lock_mailbox(m, true, true) != false)
905 {
906 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
908 return false;
909 }
910 }
911
912 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
913 {
915 return false;
916 }
917
918 return true;
919}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:771
#define mutt_file_fclose(FP)
Definition: file.h:148
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:147
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:311
Mbox-specific Account data -.
Definition: lib.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 823 of file mh.c.

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