NeoMutt  2024-11-14-138-ge5ca67
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 475 of file compress.c.

476{
477 /* If this succeeds, we know there's an open-hook */
478 struct CompressInfo *ci = set_compress_info(m);
479 if (!ci)
480 return false;
481
482 /* To append we need an append-hook or a close-hook */
483 if (!ci->cmd_append && !ci->cmd_close)
484 {
485 mutt_error(_("Can't append without an append-hook or close-hook : %s"),
486 mailbox_path(m));
487 goto cmoa_fail1;
488 }
489
490 if (setup_paths(m) != 0)
491 goto cmoa_fail2;
492
493 /* Lock the realpath for the duration of the append.
494 * It will be unlocked in the close */
495 if (!lock_realpath(m, true))
496 {
497 mutt_error(_("Unable to lock mailbox"));
498 goto cmoa_fail2;
499 }
500
501 /* Open the existing mailbox, unless we are appending */
502 if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
503 {
504 if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
505 {
506 mutt_error(_("Compress command failed: %s"), ci->cmd_open->string);
507 goto cmoa_fail2;
508 }
510 }
511 else
512 {
513 m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
514 }
515
516 /* We can only deal with mbox and mmdf mailboxes */
517 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
518 {
519 mutt_error(_("Unsupported mailbox type for appending"));
520 goto cmoa_fail2;
521 }
522
523 ci->child_ops = mx_get_ops(m->type);
524 if (!ci->child_ops)
525 {
526 mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
527 goto cmoa_fail2;
528 }
529
530 if (!ci->child_ops->mbox_open_append(m, flags))
531 goto cmoa_fail2;
532
533 return true;
534
535cmoa_fail2:
536 /* remove the partial uncompressed file */
537 (void) remove(mailbox_path(m));
538cmoa_fail1:
539 /* Free the compress_info to prevent close from trying to recompress */
541
542 return false;
543}
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition: compress.c:238
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition: compress.c:268
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition: compress.c:173
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:106
static bool execute_command(struct Mailbox *m, const struct Expando *exp, const char *progress)
Run a system command.
Definition: compress.c:294
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: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:1519
#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:127
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1321
Private data for compress.
Definition: lib.h:60
struct Expando * cmd_open
open-hook command
Definition: lib.h:63
struct Expando * cmd_append
append-hook command
Definition: lib.h:61
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:65
struct Expando * cmd_close
close-hook command
Definition: lib.h:62
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:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ 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 2073 of file imap.c.

2074{
2075 if (!m->account)
2076 return false;
2077
2078 /* in APPEND mode, we appear to hijack an existing IMAP connection -
2079 * mailbox is brand new and mostly empty */
2081 struct ImapMboxData *mdata = imap_mdata_get(m);
2082
2083 int rc = imap_mailbox_status(m, false);
2084 if (rc >= 0)
2085 return true;
2086 if (rc == -1)
2087 return false;
2088
2089 char buf[PATH_MAX + 64];
2090 snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2091 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2092 if (c_confirm_create &&
2093 (query_yesorno_help(buf, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
2094 return false;
2095
2096 if (imap_create_mailbox(adata, mdata->name) < 0)
2097 return false;
2098
2099 return true;
2100}
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:1206
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition: imap.c:435
#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:355
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:974
#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 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}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:778
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
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:55
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: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 825 of file mh.c.

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