From e6bafd5779a02882a20b1f5d65b6598341217ed6 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Mon, 1 Aug 2016 15:04:45 -0700 Subject: [PATCH] Convert mx_open_mailbox_append() to use ctx->mx_ops. Set the flag MUTT_NEWFOLDER to signal Maildir and MH to create the directory structure. Distribute the "open append" code to mbox, mh, and imap/imap.c. Set pop's mx_ops handler to NULL to signal it is not supported. --- imap/imap.c | 4 +- imap/imap.h | 1 - mbox.c | 23 ++++++++++ mh.c | 75 ++++++++++++++++++++++++++++++++ mutt.h | 5 ++- mx.c | 121 ++++++++-------------------------------------------- pop.c | 1 + 7 files changed, 122 insertions(+), 108 deletions(-) diff --git a/imap/imap.c b/imap/imap.c index 460a3d68..2e3d27d3 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -771,7 +771,7 @@ static int imap_open_mailbox (CONTEXT* ctx) return -1; } -int imap_open_mailbox_append (CONTEXT *ctx) +static int imap_open_mailbox_append (CONTEXT *ctx, int flags) { IMAP_DATA *idata; char buf[LONG_STRING]; @@ -791,7 +791,6 @@ int imap_open_mailbox_append (CONTEXT *ctx) return -1; } - ctx->magic = MUTT_IMAP; ctx->data = idata; imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox)); @@ -2174,6 +2173,7 @@ int imap_fast_trash (CONTEXT* ctx, char* dest) struct mx_ops mx_imap_ops = { .open = imap_open_mailbox, + .open_append = imap_open_mailbox_append, .close = imap_close_mailbox, .open_msg = imap_fetch_message, .close_msg = imap_close_message, diff --git a/imap/imap.h b/imap/imap.h index 442b284c..94506ea9 100644 --- a/imap/imap.h +++ b/imap/imap.h @@ -35,7 +35,6 @@ typedef struct int imap_access (const char*, int); int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force); int imap_delete_mailbox (CONTEXT* idata, IMAP_MBOX mx); -int imap_open_mailbox_append (CONTEXT *ctx); int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint); int imap_close_mailbox (CONTEXT *ctx); int imap_buffy_check (int force, int check_stats); diff --git a/mbox.c b/mbox.c index 1aabe9e5..d3c2ec46 100644 --- a/mbox.c +++ b/mbox.c @@ -442,6 +442,27 @@ static int mbox_open_mailbox (CONTEXT *ctx) return (rc); } +static int mbox_open_mailbox_append (CONTEXT *ctx, int flags) +{ + ctx->fp = safe_fopen (ctx->path, flags & MUTT_NEWFOLDER ? "w" : "a"); + if (!ctx->fp) + { + mutt_perror (ctx->path); + return -1; + } + + if (mbox_lock_mailbox (ctx, 1, 1) != 0) + { + mutt_error (_("Couldn't lock %s\n"), ctx->path); + safe_fclose (&ctx->fp); + return -1; + } + + fseek (ctx->fp, 0, 2); + + return 0; +} + static int mbox_close_mailbox (CONTEXT *ctx) { return 0; @@ -1318,6 +1339,7 @@ int mbox_check_empty (const char *path) struct mx_ops mx_mbox_ops = { .open = mbox_open_mailbox, + .open_append = mbox_open_mailbox_append, .close = mbox_close_mailbox, .open_msg = mbox_open_message, .close_msg = mbox_close_message, @@ -1328,6 +1350,7 @@ struct mx_ops mx_mbox_ops = { struct mx_ops mx_mmdf_ops = { .open = mbox_open_mailbox, + .open_append = mbox_open_mailbox_append, .close = mbox_close_mailbox, .open_msg = mbox_open_message, .close_msg = mbox_close_message, diff --git a/mh.c b/mh.c index 3898f259..3d969f62 100644 --- a/mh.c +++ b/mh.c @@ -1290,11 +1290,84 @@ static int maildir_open_mailbox (CONTEXT *ctx) return maildir_read_dir (ctx); } +static int maildir_open_mailbox_append (CONTEXT *ctx, int flags) +{ + char tmp[_POSIX_PATH_MAX]; + + if (flags & MUTT_NEWFOLDER) + { + if (mkdir (ctx->path, S_IRWXU)) + { + mutt_perror (ctx->path); + return (-1); + } + + snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); + if (mkdir (tmp, S_IRWXU)) + { + mutt_perror (tmp); + rmdir (ctx->path); + return (-1); + } + + snprintf (tmp, sizeof (tmp), "%s/new", ctx->path); + if (mkdir (tmp, S_IRWXU)) + { + mutt_perror (tmp); + snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); + rmdir (tmp); + rmdir (ctx->path); + return (-1); + } + + snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path); + if (mkdir (tmp, S_IRWXU)) + { + mutt_perror (tmp); + snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); + rmdir (tmp); + snprintf (tmp, sizeof (tmp), "%s/new", ctx->path); + rmdir (tmp); + rmdir (ctx->path); + return (-1); + } + } + + return 0; +} + static int mh_open_mailbox (CONTEXT *ctx) { return mh_read_dir (ctx, NULL); } +static int mh_open_mailbox_append (CONTEXT *ctx, int flags) +{ + char tmp[_POSIX_PATH_MAX]; + int i; + + if (flags & MUTT_NEWFOLDER) + { + if (mkdir (ctx->path, S_IRWXU)) + { + mutt_perror (ctx->path); + return (-1); + } + + snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path); + if ((i = creat (tmp, S_IRWXU)) == -1) + { + mutt_perror (tmp); + rmdir (ctx->path); + return (-1); + } + close (i); + } + + return 0; +} + + /* * Open a new (temporary) message in an MH folder. */ @@ -2453,6 +2526,7 @@ int mx_is_mh (const char *path) struct mx_ops mx_maildir_ops = { .open = maildir_open_mailbox, + .open_append = maildir_open_mailbox_append, .close = mh_close_mailbox, .open_msg = maildir_open_message, .close_msg = mh_close_message, @@ -2463,6 +2537,7 @@ struct mx_ops mx_maildir_ops = { struct mx_ops mx_mh_ops = { .open = mh_open_mailbox, + .open_append = mh_open_mailbox_append, .close = mh_close_mailbox, .open_msg = mh_open_message, .close_msg = mh_close_message, diff --git a/mutt.h b/mutt.h index 18fb2d16..2cf55b10 100644 --- a/mutt.h +++ b/mutt.h @@ -894,8 +894,9 @@ struct _message; */ struct mx_ops { - int (*open)(struct _context *); - int (*close)(struct _context *); + int (*open) (struct _context *); + int (*open_append) (struct _context *, int flags); + int (*close) (struct _context *); int (*check) (struct _context *ctx, int *index_hint); int (*open_msg) (struct _context *, struct _message *, int msgno); int (*close_msg) (struct _context *, struct _message *); diff --git a/mx.c b/mx.c index e44ae5af..5b08eb86 100644 --- a/mx.c +++ b/mx.c @@ -481,122 +481,37 @@ static int mx_open_mailbox_append (CONTEXT *ctx, int flags) struct stat sb; ctx->append = 1; - -#ifdef USE_IMAP - - if(mx_is_imap(ctx->path)) - return imap_open_mailbox_append (ctx); - -#endif - - if(stat(ctx->path, &sb) == 0) + ctx->magic = mx_get_magic (ctx->path); + if (ctx->magic == 0) { - ctx->magic = mx_get_magic (ctx->path); - - switch (ctx->magic) - { - case 0: - mutt_error (_("%s is not a mailbox."), ctx->path); - /* fall through */ - case -1: - return (-1); - } + mutt_error (_("%s is not a mailbox."), ctx->path); + return -1; } - else if (errno == ENOENT) - { - ctx->magic = DefaultMagic; - if (ctx->magic == MUTT_MH || ctx->magic == MUTT_MAILDIR) + if (ctx->magic < 0) + { + if (stat (ctx->path, &sb) == -1) { - char tmp[_POSIX_PATH_MAX]; - - if (mkdir (ctx->path, S_IRWXU)) - { - mutt_perror (ctx->path); - return (-1); - } - - if (ctx->magic == MUTT_MAILDIR) + if (errno == ENOENT) { - snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); - if (mkdir (tmp, S_IRWXU)) - { - mutt_perror (tmp); - rmdir (ctx->path); - return (-1); - } - - snprintf (tmp, sizeof (tmp), "%s/new", ctx->path); - if (mkdir (tmp, S_IRWXU)) - { - mutt_perror (tmp); - snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); - rmdir (tmp); - rmdir (ctx->path); - return (-1); - } - snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path); - if (mkdir (tmp, S_IRWXU)) - { - mutt_perror (tmp); - snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path); - rmdir (tmp); - snprintf (tmp, sizeof (tmp), "%s/new", ctx->path); - rmdir (tmp); - rmdir (ctx->path); - return (-1); - } + ctx->magic = DefaultMagic; + flags |= MUTT_NEWFOLDER; } else { - int i; - - snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path); - if ((i = creat (tmp, S_IRWXU)) == -1) - { - mutt_perror (tmp); - rmdir (ctx->path); - return (-1); - } - close (i); + mutt_perror (ctx->path); + return -1; } } - } - else - { - mutt_perror (ctx->path); - return (-1); + else + return -1; } - switch (ctx->magic) - { - case MUTT_MBOX: - case MUTT_MMDF: - if ((ctx->fp = safe_fopen (ctx->path, flags & MUTT_NEWFOLDER ? "w" : "a")) == NULL || - mbox_lock_mailbox (ctx, 1, 1) != 0) - { - if (!ctx->fp) - mutt_perror (ctx->path); - else - { - mutt_error (_("Couldn't lock %s\n"), ctx->path); - safe_fclose (&ctx->fp); - } - return (-1); - } - fseek (ctx->fp, 0, 2); - break; - - case MUTT_MH: - case MUTT_MAILDIR: - /* nothing to do */ - break; - - default: - return (-1); - } + ctx->mx_ops = mx_get_ops (ctx->magic); + if (!ctx->mx_ops || !ctx->mx_ops->open_append) + return -1; - return 0; + return ctx->mx_ops->open_append (ctx, flags); } /* close a mailbox opened in write-mode */ diff --git a/pop.c b/pop.c index 972b7152..b6f89169 100644 --- a/pop.c +++ b/pop.c @@ -937,6 +937,7 @@ fail: struct mx_ops mx_pop_ops = { .open = pop_open_mailbox, + .open_append = NULL, .close = pop_close_mailbox, .open_msg = pop_fetch_message, .close_msg = pop_close_message, -- 2.40.0