NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
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() -flags may also contain MUTT_NEWFOLDER. More...
 
static bool imap_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool maildir_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool mh_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 
static bool mbox_mbox_open_append (struct Mailbox *m, OpenMailboxFlags flags)
 Open a Mailbox for appending - Implements MxOps::mbox_open_append() -. More...
 

Detailed Description

Open a Mailbox for appending.

Parameters
mMailbox to open
flagsFlags, see OpenMailboxFlags
Return values
trueSuccess
falseFailure

Contract

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

499 {
500  /* If this succeeds, we know there's an open-hook */
501  struct CompressInfo *ci = set_compress_info(m);
502  if (!ci)
503  return false;
504 
505  /* To append we need an append-hook or a close-hook */
506  if (!ci->cmd_append && !ci->cmd_close)
507  {
508  mutt_error(_("Can't append without an append-hook or close-hook : %s"),
509  mailbox_path(m));
510  goto cmoa_fail1;
511  }
512 
513  if (setup_paths(m) != 0)
514  goto cmoa_fail2;
515 
516  /* Lock the realpath for the duration of the append.
517  * It will be unlocked in the close */
518  if (!lock_realpath(m, true))
519  {
520  mutt_error(_("Unable to lock mailbox"));
521  goto cmoa_fail2;
522  }
523 
524  /* Open the existing mailbox, unless we are appending */
525  if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
526  {
527  int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
528  if (rc == 0)
529  {
530  mutt_error(_("Compress command failed: %s"), ci->cmd_open);
531  goto cmoa_fail2;
532  }
534  }
535  else
536  {
537  m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
538  }
539 
540  /* We can only deal with mbox and mmdf mailboxes */
541  if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
542  {
543  mutt_error(_("Unsupported mailbox type for appending"));
544  goto cmoa_fail2;
545  }
546 
547  ci->child_ops = mx_get_ops(m->type);
548  if (!ci->child_ops)
549  {
550  mutt_error(_("Can't find mailbox ops for mailbox type %d"), m->type);
551  goto cmoa_fail2;
552  }
553 
554  if (!ci->child_ops->mbox_open_append(m, flags))
555  goto cmoa_fail2;
556 
557  return true;
558 
559 cmoa_fail2:
560  /* remove the partial uncompressed file */
561  remove(mailbox_path(m));
562 cmoa_fail1:
563  /* Free the compress_info to prevent close from trying to recompress */
565 
566  return false;
567 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:215
const struct MxOps * mx_get_ops(enum MailboxType type)
Get mailbox operations.
Definition: mx.c:141
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
const char * cmd_append
append-hook command
Definition: lib.h:48
static void compress_info_free(struct Mailbox *m)
Frees the compress info members and structure.
Definition: compress.c:226
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1498
#define mutt_error(...)
Definition: logging.h:88
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
#define _(a)
Definition: message.h:28
Container for Accounts, Notifications.
Definition: neomutt.h:36
const char * cmd_close
close-hook command
Definition: lib.h:49
static struct CompressInfo * set_compress_info(struct Mailbox *m)
Find the compress hooks for a mailbox.
Definition: compress.c:196
'mmdf' Mailbox type
Definition: mailbox.h:49
Private data for compress.
Definition: lib.h:46
static int execute_command(struct Mailbox *m, const char *command, const char *progress)
Run a system command.
Definition: compress.c:319
'mbox' Mailbox type
Definition: mailbox.h:48
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition: mx.c:1317
bool(* mbox_open_append)(struct Mailbox *m, OpenMailboxFlags flags)
Definition: mxapi.h:167
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:97
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the ctx->realpath.
Definition: compress.c:85
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:52
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
const char * cmd_open
open-hook command
Definition: lib.h:50
static int setup_paths(struct Mailbox *m)
Set the mailbox paths.
Definition: compress.c:150
+ 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 2135 of file imap.c.

2136 {
2137  if (!m->account)
2138  return false;
2139 
2140  /* in APPEND mode, we appear to hijack an existing IMAP connection -
2141  * ctx is brand new and mostly empty */
2142  struct ImapAccountData *adata = imap_adata_get(m);
2143  struct ImapMboxData *mdata = imap_mdata_get(m);
2144 
2145  int rc = imap_mailbox_status(m, false);
2146  if (rc >= 0)
2147  return true;
2148  if (rc == -1)
2149  return false;
2150 
2151  char buf[PATH_MAX + 64];
2152  snprintf(buf, sizeof(buf), _("Create %s?"), mdata->name);
2153  const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
2154  if (c_confirm_create && (mutt_yesorno(buf, MUTT_YES) != MUTT_YES))
2155  return false;
2156 
2157  if (imap_create_mailbox(adata, mdata->name) < 0)
2158  return false;
2159 
2160  return true;
2161 }
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
#define _(a)
Definition: message.h:28
Container for Accounts, Notifications.
Definition: neomutt.h:36
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition: mdata.c:59
void * mdata
Driver specific data.
Definition: mailbox.h:136
#define PATH_MAX
Definition: mutt.h:40
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
char * name
Mailbox name.
Definition: mdata.h:40
int imap_mailbox_status(struct Mailbox *m, bool queue)
Refresh the number of total and new messages.
Definition: imap.c:1270
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:90
int imap_create_mailbox(struct ImapAccountData *adata, char *mailbox)
Create a new mailbox.
Definition: imap.c:448
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:180
IMAP-specific Account data -.
Definition: adata.h:39
struct Account * account
Account that owns this Mailbox.
Definition: mailbox.h:131
IMAP-specific Mailbox data -.
Definition: mdata.h:38
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
+ Here is the call graph for this function:

◆ maildir_mbox_open_append()

static bool maildir_mbox_open_append ( struct Mailbox m,
OpenMailboxFlags  flags 
)
static

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

Definition at line 1105 of file maildir.c.

1106 {
1108  {
1109  return true;
1110  }
1111 
1112  errno = 0;
1113  if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
1114  {
1116  return false;
1117  }
1118 
1119  char tmp[PATH_MAX];
1120  snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1121  errno = 0;
1122  if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1123  {
1124  mutt_perror(tmp);
1125  rmdir(mailbox_path(m));
1126  return false;
1127  }
1128 
1129  snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1130  errno = 0;
1131  if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1132  {
1133  mutt_perror(tmp);
1134  snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1135  rmdir(tmp);
1136  rmdir(mailbox_path(m));
1137  return false;
1138  }
1139 
1140  snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
1141  errno = 0;
1142  if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1143  {
1144  mutt_perror(tmp);
1145  snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1146  rmdir(tmp);
1147  snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1148  rmdir(tmp);
1149  rmdir(mailbox_path(m));
1150  return false;
1151  }
1152 
1153  return true;
1154 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
#define MUTT_APPEND
Open mailbox for appending messages.
Definition: mxapi.h:62
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn&#39;t exist.
Definition: mxapi.h:69
#define mutt_perror(...)
Definition: logging.h:89
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:877
#define PATH_MAX
Definition: mutt.h:40
struct ListHead flags
Definition: mdata.h:49
#define MUTT_NEWFOLDER
Create a new folder - same as MUTT_APPEND,.
Definition: mxapi.h:65
+ 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 850 of file mh.c.

851 {
852  if (!(flags & (MUTT_APPENDNEW | MUTT_NEWFOLDER)))
853  return true;
854 
855  if (mutt_file_mkdir(mailbox_path(m), S_IRWXU))
856  {
858  return false;
859  }
860 
861  char tmp[PATH_MAX];
862  snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", mailbox_path(m));
863  const int i = creat(tmp, S_IRWXU);
864  if (i == -1)
865  {
866  mutt_perror(tmp);
867  rmdir(mailbox_path(m));
868  return false;
869  }
870  close(i);
871 
872  return true;
873 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn&#39;t exist.
Definition: mxapi.h:69
#define mutt_perror(...)
Definition: logging.h:89
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:877
#define PATH_MAX
Definition: mutt.h:40
struct ListHead flags
Definition: mdata.h:49
#define MUTT_NEWFOLDER
Create a new folder - same as MUTT_APPEND,.
Definition: mxapi.h:65
+ 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 974 of file mbox.c.

975 {
976  if (init_mailbox(m) != 0)
977  return false;
978 
979  struct MboxAccountData *adata = mbox_adata_get(m);
980  if (!adata)
981  return false;
982 
983  if (!adata->fp)
984  {
985  // create dir recursively
986  char *tmp_path = mutt_path_dirname(mailbox_path(m));
987  if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
988  {
990  FREE(&tmp_path);
991  return false;
992  }
993  FREE(&tmp_path);
994 
995  adata->fp =
996  mutt_file_fopen(mailbox_path(m), (flags & MUTT_NEWFOLDER) ? "w+" : "a+");
997  if (!adata->fp)
998  {
1000  return false;
1001  }
1002 
1003  if (mbox_lock_mailbox(m, true, true) != false)
1004  {
1005  mutt_error(_("Couldn't lock %s"), mailbox_path(m));
1006  mutt_file_fclose(&adata->fp);
1007  return false;
1008  }
1009  }
1010 
1011  fseek(adata->fp, 0, SEEK_END);
1012 
1013  return true;
1014 }
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
#define mutt_error(...)
Definition: logging.h:88
Mbox-specific Account data -.
Definition: lib.h:48
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final &#39;/&#39;.
Definition: path.c:376
#define _(a)
Definition: message.h:28
#define mutt_perror(...)
Definition: logging.h:89
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition: mbox.c:140
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:877
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
#define MUTT_NEWFOLDER
Create a new folder - same as MUTT_APPEND,.
Definition: mxapi.h:65
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:100
#define FREE(x)
Definition: memory.h:40
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:589
FILE * fp
Mailbox file.
Definition: lib.h:50
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition: mbox.c:118
+ Here is the call graph for this function: