From: Richard Russon Date: Fri, 28 Dec 2018 14:51:34 +0000 (+0000) Subject: add mx_path_resolve() X-Git-Tag: 2019-10-25~373^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5f853e482a6e5ab1e3ff592a1f0db026376a99e6;p=neomutt add mx_path_resolve() --- diff --git a/commands.c b/commands.c index 547b0bc5d..bc2e07e82 100644 --- a/commands.c +++ b/commands.c @@ -1067,9 +1067,13 @@ int mutt_save_message(struct Mailbox *m, struct EmailList *el, bool delete, bool } #endif - savectx = mx_mbox_open(NULL, buf, MUTT_APPEND); + struct Mailbox *m_save = mx_path_resolve(buf); + savectx = mx_mbox_open(m_save, NULL, MUTT_APPEND); if (!savectx) + { + mailbox_free(&m_save); return -1; + } #ifdef USE_COMPRESSED /* If we're saving to a compressed mailbox, the stats won't be updated diff --git a/compose.c b/compose.c index 944f3ef52..f05cc445e 100644 --- a/compose.c +++ b/compose.c @@ -1450,10 +1450,12 @@ int mutt_compose_menu(struct Email *msg, char *fcc, size_t fcclen, struct Email menu->redraw = REDRAW_FULL; - ctx = mx_mbox_open(NULL, fname, MUTT_READONLY); + struct Mailbox *m = mx_path_resolve(fname); + ctx = mx_mbox_open(m, NULL, MUTT_READONLY); if (!ctx) { mutt_error(_("Unable to open mailbox %s"), fname); + mailbox_free(&m); break; } diff --git a/editmsg.c b/editmsg.c index 9955fb5fe..eed210915 100644 --- a/editmsg.c +++ b/editmsg.c @@ -68,12 +68,14 @@ static int ev_message(enum EvMessage action, struct Mailbox *m, struct Email *e) enum MailboxType omagic = MboxType; MboxType = MUTT_MBOX; - struct Context *tmpctx = mx_mbox_open(NULL, fname, MUTT_NEWFOLDER); + struct Mailbox *m_fname = mx_path_resolve(fname); + struct Context *tmpctx = mx_mbox_open(m_fname, NULL, MUTT_NEWFOLDER); MboxType = omagic; if (!tmpctx) { mutt_error(_("could not create temporary folder: %s"), strerror(errno)); + mailbox_free(&m_fname); return -1; } diff --git a/index.c b/index.c index b215f4fa3..deed43199 100644 --- a/index.c +++ b/index.c @@ -644,7 +644,13 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m, ? MUTT_READONLY : 0; - Context = mx_mbox_open(m, buf, flags); + bool free_m = false; + if (!m) + { + m = mx_path_resolve(buf); + free_m = true; + } + Context = mx_mbox_open(m, NULL, flags); if (Context) { menu->current = ci_first_message(); @@ -653,7 +659,11 @@ static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m, #endif } else + { menu->current = 0; + if (free_m) + mailbox_free(&m); + } if (((Sort & SORT_MASK) == SORT_THREADS) && CollapseAll) collapse_all(menu, 0); diff --git a/main.c b/main.c index 8b18ad845..9b8d33396 100644 --- a/main.c +++ b/main.c @@ -1208,8 +1208,12 @@ int main(int argc, char *argv[], char *envp[]) mutt_startup_shutdown_hook(MUTT_STARTUP_HOOK); repeat_error = true; - Context = mx_mbox_open(NULL, folder, - ((flags & MUTT_RO) || ReadOnly) ? MUTT_READONLY : 0); + struct Mailbox *m = mx_path_resolve(folder); + Context = mx_mbox_open(m, NULL, ((flags & MUTT_RO) || ReadOnly) ? MUTT_READONLY : 0); + if (!Context) + { + mailbox_free(&m); + } if (Context || !explicit_folder) { #ifdef USE_SIDEBAR diff --git a/mutt_attach.c b/mutt_attach.c index 7c7002648..4f6664e90 100644 --- a/mutt_attach.c +++ b/mutt_attach.c @@ -813,9 +813,13 @@ int mutt_save_attachment(FILE *fp, struct Body *m, char *path, int flags, struct return -1; if (!fgets(buf, sizeof(buf), fp)) return -1; - struct Context *ctx = mx_mbox_open(NULL, path, MUTT_APPEND | MUTT_QUIET); + struct Mailbox *m_att = mx_path_resolve(path); + struct Context *ctx = mx_mbox_open(m_att, NULL, MUTT_APPEND | MUTT_QUIET); if (!ctx) + { + mailbox_free(&m_att); return -1; + } msg = mx_msg_open_new(ctx->mailbox, en, is_from(buf, NULL, 0, NULL) ? 0 : MUTT_ADD_FROM); if (!msg) { diff --git a/mx.c b/mx.c index d1542b5b5..f684794bf 100644 --- a/mx.c +++ b/mx.c @@ -525,7 +525,8 @@ static int trash_append(struct Mailbox *m) } #endif - struct Context *ctx_trash = mx_mbox_open(NULL, Trash, MUTT_APPEND); + struct Mailbox *m_trash = mx_path_resolve(Trash); + struct Context *ctx_trash = mx_mbox_open(m_trash, NULL, MUTT_APPEND); if (ctx_trash) { /* continue from initial scan above */ @@ -546,6 +547,7 @@ static int trash_append(struct Mailbox *m) else { mutt_error(_("Can't open trash folder")); + mailbox_free(&m_trash); return -1; } @@ -700,29 +702,33 @@ int mx_mbox_close(struct Context **pctx) else /* use regular append-copy mode */ #endif { - struct Context *f = mx_mbox_open(NULL, mbox, MUTT_APPEND); - if (!f) + struct Mailbox *m_read = mx_path_resolve(mbox); + struct Context *ctx_read = mx_mbox_open(m_read, NULL, MUTT_APPEND); + if (!ctx_read) + { + mailbox_free(&m_read); return -1; + } for (i = 0; i < m->msg_count; i++) { if (m->emails[i]->read && !m->emails[i]->deleted && !(m->emails[i]->flagged && KeepFlagged)) { - if (mutt_append_message(f->mailbox, ctx->mailbox, m->emails[i], 0, CH_UPDATE_LEN) == 0) + if (mutt_append_message(ctx_read->mailbox, ctx->mailbox, m->emails[i], 0, CH_UPDATE_LEN) == 0) { mutt_set_flag(m, m->emails[i], MUTT_DELETE, 1); mutt_set_flag(m, m->emails[i], MUTT_PURGE, 1); } else { - mx_mbox_close(&f); + mx_mbox_close(&ctx_read); return -1; } } } - mx_mbox_close(&f); + mx_mbox_close(&ctx_read); } } else if (!m->changed && m->msg_deleted == 0) @@ -1526,6 +1532,26 @@ struct Mailbox *mx_mbox_find2(const char *path) return NULL; } +/** + * mx_path_resolve - XXX + */ +struct Mailbox *mx_path_resolve(const char *path) +{ + if (!path) + return NULL; + + struct Mailbox *m = mx_mbox_find2(path); + if (m) + return m; + + m = mailbox_new(); + m->flags = MB_HIDDEN; + mutt_str_strfcpy(m->path, path, sizeof(m->path)); + mx_path_canon2(m, Folder); + + return m; +} + /** * mx_ac_add - Add a Mailbox to an Account - Wrapper for MxOps::ac_add */ diff --git a/mx.h b/mx.h index 8bfff7b77..a41f12e1a 100644 --- a/mx.h +++ b/mx.h @@ -284,6 +284,7 @@ int mx_path_canon2 (struct Mailbox *m, const char *folder); int mx_path_parent (char *buf, size_t buflen); int mx_path_pretty (char *buf, size_t buflen, const char *folder); enum MailboxType mx_path_probe (const char *path, struct stat *st); +struct Mailbox *mx_path_resolve (const char *path); int mx_tags_commit (struct Mailbox *m, struct Email *e, char *tags); int mx_tags_edit (struct Mailbox *m, const char *tags, char *buf, size_t buflen); diff --git a/pop/pop.c b/pop/pop.c index d0289225e..cd0ed0a21 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -655,9 +655,13 @@ void pop_fetch_mail(void) goto finish; } - struct Context *ctx = mx_mbox_open(NULL, Spoolfile, MUTT_APPEND); + struct Mailbox *m_spool = mx_path_resolve(Spoolfile); + struct Context *ctx = mx_mbox_open(m_spool, NULL, MUTT_APPEND); if (!ctx) + { + mailbox_free(&m_spool); goto finish; + } delanswer = query_quadoption(PopDelete, _("Delete messages from server?")); diff --git a/postpone.c b/postpone.c index 157a5e9b7..5b13c9d68 100644 --- a/postpone.c +++ b/postpone.c @@ -170,9 +170,13 @@ int mutt_num_postponed(struct Mailbox *m, bool force) if (optnews) OptNews = false; #endif - struct Context *ctx = mx_mbox_open(NULL, Postponed, MUTT_NOSORT | MUTT_QUIET); + struct Mailbox *m_post = mx_path_resolve(Postponed); + struct Context *ctx = mx_mbox_open(m_post, NULL, MUTT_NOSORT | MUTT_QUIET); if (!ctx) + { + mailbox_free(&m_post); PostCount = 0; + } else PostCount = ctx->mailbox->msg_count; mx_fastclose_mailbox(ctx->mailbox); @@ -295,16 +299,17 @@ int mutt_get_postponed(struct Context *ctx, struct Email *hdr, if (!Postponed) return -1; - struct Mailbox *m = mx_mbox_find2(Postponed); + struct Mailbox *m = mx_path_resolve(Postponed); if (ctx->mailbox == m) PostContext = ctx; else - PostContext = mx_mbox_open(m, Postponed, MUTT_NOSORT); + PostContext = mx_mbox_open(m, NULL, MUTT_NOSORT); if (!PostContext) { PostCount = 0; mutt_error(_("No postponed messages")); + mailbox_free(&m); return -1; } diff --git a/sendlib.c b/sendlib.c index 6dfd20135..08180f49f 100644 --- a/sendlib.c +++ b/sendlib.c @@ -3159,24 +3159,26 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid, #ifdef RECORD_FOLDER_HOOK mutt_folder_hook(path, NULL); #endif - struct Context *f = mx_mbox_open(NULL, path, MUTT_APPEND | MUTT_QUIET); - if (!f) + struct Mailbox *m_fcc = mx_path_resolve(path); + struct Context *ctx_fcc = mx_mbox_open(m_fcc, NULL, MUTT_APPEND | MUTT_QUIET); + if (!ctx_fcc) { mutt_debug(1, "unable to open mailbox %s in append-mode, aborting.\n", path); + mailbox_free(&m_fcc); goto done; } /* We need to add a Content-Length field to avoid problems where a line in * the message body begins with "From " */ - if ((f->mailbox->magic == MUTT_MMDF) || (f->mailbox->magic == MUTT_MBOX)) + if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX)) { mutt_mktemp(tempfile, sizeof(tempfile)); tempfp = mutt_file_fopen(tempfile, "w+"); if (!tempfp) { mutt_perror(tempfile); - mx_mbox_close(&f); + mx_mbox_close(&ctx_fcc); goto done; } /* remember new mail status before appending message */ @@ -3188,11 +3190,11 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid, onm_flags = MUTT_ADD_FROM; if (post) onm_flags |= MUTT_SET_DRAFT; - msg = mx_msg_open_new(f->mailbox, e, onm_flags); + msg = mx_msg_open_new(ctx_fcc->mailbox, e, onm_flags); if (!msg) { mutt_file_fclose(&tempfp); - mx_mbox_close(&f); + mx_mbox_close(&ctx_fcc); goto done; } @@ -3218,7 +3220,7 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid, if (post && fcc) fprintf(msg->fp, "X-Mutt-Fcc: %s\n", fcc); - if ((f->mailbox->magic == MUTT_MMDF) || (f->mailbox->magic == MUTT_MBOX)) + if ((ctx_fcc->mailbox->magic == MUTT_MMDF) || (ctx_fcc->mailbox->magic == MUTT_MBOX)) fprintf(msg->fp, "Status: RO\n"); /* mutt_rfc822_write_header() only writes out a Date: header with @@ -3310,9 +3312,9 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid, mutt_debug(1, "%s: write failed.\n", tempfile); mutt_file_fclose(&tempfp); unlink(tempfile); - mx_msg_commit(f->mailbox, msg); /* XXX really? */ - mx_msg_close(f->mailbox, &msg); - mx_mbox_close(&f); + mx_msg_commit(ctx_fcc->mailbox, msg); /* XXX really? */ + mx_msg_close(ctx_fcc->mailbox, &msg); + mx_mbox_close(&ctx_fcc); goto done; } @@ -3338,12 +3340,12 @@ int mutt_write_fcc(const char *path, struct Email *e, const char *msgid, rc = mutt_write_mime_body(e->content, msg->fp); } - if (mx_msg_commit(f->mailbox, msg) != 0) + if (mx_msg_commit(ctx_fcc->mailbox, msg) != 0) rc = -1; else if (finalpath) *finalpath = mutt_str_strdup(msg->committed_path); - mx_msg_close(f->mailbox, &msg); - mx_mbox_close(&f); + mx_msg_close(ctx_fcc->mailbox, &msg); + mx_mbox_close(&ctx_fcc); if (!post && need_mailbox_cleanup) mutt_mailbox_cleanup(path, &st);