From 9556aba0145ed838246e510cdc9e093cec59da5c Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Sun, 5 Aug 2018 10:37:34 +0100 Subject: [PATCH] use enum MailboxType --- browser.c | 3 ++- compress.c | 2 +- config/magic.h | 8 +++++--- context.h | 2 +- curs_main.c | 4 +++- mailbox.c | 14 +++++++------- mailbox.h | 2 +- maildir/maildir.h | 4 ++-- maildir/mh.c | 4 ++-- muttlib.c | 7 +++---- mx.c | 27 +++++++++++++++------------ mx.h | 4 ++-- notmuch/mutt_notmuch.c | 4 ++-- 13 files changed, 46 insertions(+), 39 deletions(-) diff --git a/browser.c b/browser.c index 741422827..e4abb3b85 100644 --- a/browser.c +++ b/browser.c @@ -1556,7 +1556,8 @@ void mutt_select_file(char *file, size_t filelen, int flags, char ***files, int sizeof(buf)); } - if ((mx_get_magic(buf) <= 0) + enum MailboxType magic = mx_get_magic(buf); + if ((magic == MUTT_MAILBOX_ERROR) || (magic == MUTT_UNKNOWN) #ifdef USE_IMAP || state.entry[menu->current].inferiors #endif diff --git a/compress.c b/compress.c index f956d07c7..91c55147e 100644 --- a/compress.c +++ b/compress.c @@ -471,7 +471,7 @@ static int comp_mbox_open(struct Context *ctx) unlock_realpath(ctx); ctx->magic = mx_get_magic(ctx->path); - if (ctx->magic == 0) + if (ctx->magic == MUTT_UNKNOWN) { mutt_error(_("Can't identify the contents of the compressed file")); goto or_fail; diff --git a/config/magic.h b/config/magic.h index 449ff6b1a..7d1459b75 100644 --- a/config/magic.h +++ b/config/magic.h @@ -28,11 +28,13 @@ struct ConfigSet; extern const char *magic_values[]; /** - * enum MailboxTypes - Supported mailbox formats + * enum MailboxType - Supported mailbox formats */ -enum MailboxTypes +enum MailboxType { - MUTT_MBOX = 1, + MUTT_MAILBOX_ERROR = -1, + MUTT_UNKNOWN = 0, + MUTT_MBOX, MUTT_MMDF, MUTT_MH, MUTT_MAILDIR, diff --git a/context.h b/context.h index 5a77eb7d5..92c224d19 100644 --- a/context.h +++ b/context.h @@ -82,7 +82,7 @@ struct Context struct Menu *menu; /**< needed for pattern compilation */ - short magic; /**< mailbox type */ + enum MailboxType magic; /**< mailbox type */ unsigned char rights[(RIGHTSMAX + 7) / 8]; /**< ACL bits */ diff --git a/curs_main.c b/curs_main.c index e5be2c497..1cf653256 100644 --- a/curs_main.c +++ b/curs_main.c @@ -497,7 +497,9 @@ static int main_change_folder(struct Menu *menu, int op, char *buf, else #endif mutt_expand_path(buf, buflen); - if (mx_get_magic(buf) <= 0) + + enum MailboxType magic = mx_get_magic(buf); + if ((magic == MUTT_MAILBOX_ERROR) || (magic == MUTT_UNKNOWN)) { mutt_error(_("%s is not a mailbox."), buf); return -1; diff --git a/mailbox.c b/mailbox.c index 2b39d59b7..90f82244a 100644 --- a/mailbox.c +++ b/mailbox.c @@ -152,11 +152,10 @@ static bool test_new_folder(const char *path) { FILE *f = NULL; bool rc = false; - int typ; - typ = mx_get_magic(path); + enum MailboxType magic = mx_get_magic(path); - if (typ != MUTT_MBOX && typ != MUTT_MMDF) + if ((magic != MUTT_MBOX) && (magic != MUTT_MMDF)) return false; f = fopen(path, "rb"); @@ -182,7 +181,7 @@ static struct Mailbox *mailbox_new(const char *path) mutt_str_strfcpy(mailbox->path, path, sizeof(mailbox->path)); char *r = realpath(path, rp); mutt_str_strfcpy(mailbox->realpath, r ? rp : path, sizeof(mailbox->realpath)); - mailbox->magic = 0; + mailbox->magic = MUTT_UNKNOWN; return mailbox; } @@ -239,7 +238,7 @@ static int mailbox_maildir_check_dir(struct Mailbox *mailbox, const char *dir_na dirp = opendir(path); if (!dirp) { - mailbox->magic = 0; + mailbox->magic = MUTT_UNKNOWN; return 0; } @@ -409,12 +408,12 @@ static void mailbox_check(struct Mailbox *tmp, struct stat *contex_sb, bool chec else #endif if (stat(tmp->path, &sb) != 0 || (S_ISREG(sb.st_mode) && sb.st_size == 0) || - (!tmp->magic && (tmp->magic = mx_get_magic(tmp->path)) <= 0)) + ((tmp->magic == MUTT_UNKNOWN) && (tmp->magic = mx_get_magic(tmp->path)) <= 0)) { /* if the mailbox still doesn't exist, set the newly created flag to be * ready for when it does. */ tmp->newly_created = true; - tmp->magic = 0; + tmp->magic = MUTT_UNKNOWN; tmp->size = 0; return; } @@ -463,6 +462,7 @@ static void mailbox_check(struct Mailbox *tmp, struct stat *contex_sb, bool chec } break; #endif + default:; /* do nothing */ } } else if (CheckMboxSize && Context && Context->path) diff --git a/mailbox.h b/mailbox.h index 4ed4a1b31..b6276dde1 100644 --- a/mailbox.h +++ b/mailbox.h @@ -61,7 +61,7 @@ struct Mailbox int msg_flagged; /**< number of flagged messages */ bool notified; /**< user has been notified */ - short magic; /**< mailbox type */ + enum MailboxType magic; /**< mailbox type */ bool newly_created; /**< mbox or mmdf just popped into existence */ time_t last_visited; /**< time of last exit from this mailbox */ time_t stats_last_checked; /**< mtime of mailbox the last time stats where checked. */ diff --git a/maildir/maildir.h b/maildir/maildir.h index 84a40b308..21f220f60 100644 --- a/maildir/maildir.h +++ b/maildir/maildir.h @@ -59,8 +59,8 @@ int maildir_check_empty(const char *path); void maildir_flags(char *dest, size_t destlen, struct Header *hdr); FILE * maildir_open_find_message(const char *folder, const char *msg, char **newname); void maildir_parse_flags(struct Header *h, const char *path); -struct Header *maildir_parse_message(int magic, const char *fname, bool is_old, struct Header *h); -struct Header *maildir_parse_stream(int magic, FILE *f, const char *fname, bool is_old, struct Header *h); +struct Header *maildir_parse_message(enum MailboxType magic, const char *fname, bool is_old, struct Header *h); +struct Header *maildir_parse_stream(enum MailboxType magic, FILE *f, const char *fname, bool is_old, struct Header *h); bool maildir_update_flags(struct Context *ctx, struct Header *o, struct Header *n); bool mh_mailbox(struct Mailbox *mailbox, bool check_stats); diff --git a/maildir/mh.c b/maildir/mh.c index a3c06109f..6e6fdad03 100644 --- a/maildir/mh.c +++ b/maildir/mh.c @@ -881,7 +881,7 @@ static void maildir_update_mtime(struct Context *ctx) * Actually parse a maildir message. This may also be used to fill * out a fake header structure generated by lazy maildir parsing. */ -struct Header *maildir_parse_stream(int magic, FILE *f, const char *fname, +struct Header *maildir_parse_stream(enum MailboxType magic, FILE *f, const char *fname, bool is_old, struct Header *h) { struct stat st; @@ -923,7 +923,7 @@ struct Header *maildir_parse_stream(int magic, FILE *f, const char *fname, * This may also be used to fill out a fake header structure generated by lazy * maildir parsing. */ -struct Header *maildir_parse_message(int magic, const char *fname, bool is_old, +struct Header *maildir_parse_message(enum MailboxType magic, const char *fname, bool is_old, struct Header *h) { FILE *f = fopen(fname, "r"); diff --git a/muttlib.c b/muttlib.c index 684af9aaf..17469fbdc 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1398,9 +1398,8 @@ int mutt_save_confirm(const char *s, struct stat *st) char tmp[PATH_MAX]; int ret = 0; int rc; - int magic = 0; - magic = mx_get_magic(s); + enum MailboxType magic = mx_get_magic(s); #ifdef USE_POP if (magic == MUTT_POP) @@ -1410,7 +1409,7 @@ int mutt_save_confirm(const char *s, struct stat *st) } #endif - if (magic > 0 && !mx_access(s, W_OK)) + if ((magic != MUTT_MAILBOX_ERROR) && (magic != MUTT_UNKNOWN) && !mx_access(s, W_OK)) { if (Confirmappend) { @@ -1433,7 +1432,7 @@ int mutt_save_confirm(const char *s, struct stat *st) if (stat(s, st) != -1) { - if (magic == -1) + if (magic == MUTT_MAILBOX_ERROR) { mutt_error(_("%s is not a mailbox!"), s); return 1; diff --git a/mx.c b/mx.c index 044fab62d..0cd38f770 100644 --- a/mx.c +++ b/mx.c @@ -94,7 +94,7 @@ char *Trash; ///< Config: Folder to put deleted emails * @retval ptr Mailbox function * @retval NULL Error */ -struct MxOps *mx_get_ops(int magic) +struct MxOps *mx_get_ops(enum MailboxType magic) { switch (magic) { @@ -236,11 +236,10 @@ bool mx_is_notmuch(const char *p) * @retval -1 Error, can't identify mailbox * @retval >0 Success, e.g. #MUTT_IMAP */ -int mx_get_magic(const char *path) +enum MailboxType mx_get_magic(const char *path) { - struct stat st; - int magic = 0; - FILE *f = NULL; + if (!path) + return MUTT_UNKNOWN; #ifdef USE_IMAP if (mx_is_imap(path)) @@ -262,6 +261,10 @@ int mx_get_magic(const char *path) return MUTT_NOTMUCH; #endif + struct stat st; + enum MailboxType magic = MUTT_UNKNOWN; + FILE *f = NULL; + if (stat(path, &st) == -1) { mutt_debug(1, "unable to stat %s: %s (errno %d).\n", path, strerror(errno), errno); @@ -327,13 +330,13 @@ int mx_get_magic(const char *path) else { mutt_debug(1, "unable to open file %s for reading.\n", path); - return -1; + return MUTT_MAILBOX_ERROR; } #ifdef USE_COMPRESSED /* If there are no other matches, see if there are any * compress hooks that match */ - if ((magic == 0) && mutt_comp_can_read(path)) + if ((magic == MUTT_UNKNOWN) && mutt_comp_can_read(path)) return MUTT_COMPRESSED; #endif return magic; @@ -372,13 +375,13 @@ static int mx_open_mailbox_append(struct Context *ctx, int flags) ctx->append = true; ctx->magic = mx_get_magic(ctx->path); - if (ctx->magic == 0) + if (ctx->magic == MUTT_UNKNOWN) { mutt_error(_("%s is not a mailbox."), ctx->path); return -1; } - if (ctx->magic < 0) + if (ctx->magic == MUTT_MAILBOX_ERROR) { if (stat(ctx->path, &sb) == -1) { @@ -475,11 +478,11 @@ struct Context *mx_mbox_open(const char *path, int flags, struct Context *pctx) ctx->magic = mx_get_magic(path); ctx->mx_ops = mx_get_ops(ctx->magic); - if (ctx->magic <= 0 || !ctx->mx_ops) + if ((ctx->magic == MUTT_UNKNOWN) || (ctx->magic == MUTT_MAILBOX_ERROR) || !ctx->mx_ops) { - if (ctx->magic == -1) + if (ctx->magic == MUTT_MAILBOX_ERROR) mutt_perror(path); - else if (ctx->magic == 0 || !ctx->mx_ops) + else if (ctx->magic == MUTT_UNKNOWN || !ctx->mx_ops) mutt_error(_("%s is not a mailbox."), path); mx_fastclose_mailbox(ctx); diff --git a/mx.h b/mx.h index 07b918886..771f06d5a 100644 --- a/mx.h +++ b/mx.h @@ -155,7 +155,7 @@ void mx_alloc_memory(struct Context *ctx); void mx_update_context(struct Context *ctx, int new_messages); void mx_update_tables(struct Context *ctx, bool committing); -struct MxOps *mx_get_ops(int magic); +struct MxOps *mx_get_ops(enum MailboxType magic); /* This variable is backing for a config item */ WHERE short MboxType; ///< Config: Default type for creating new mailboxes @@ -220,7 +220,7 @@ int mx_tags_commit (struct Context *ctx, struct Header *hdr, char *t void mx_fastclose_mailbox(struct Context *ctx); -int mx_get_magic(const char *path); +enum MailboxType mx_get_magic(const char *path); int mx_check_mailbox(struct Context *ctx, int *index_hint); #ifdef USE_IMAP bool mx_is_imap(const char *p); diff --git a/notmuch/mutt_notmuch.c b/notmuch/mutt_notmuch.c index c408c4b95..89340e0a5 100644 --- a/notmuch/mutt_notmuch.c +++ b/notmuch/mutt_notmuch.c @@ -114,8 +114,8 @@ struct NmHdrData { char *folder; /**< Location of the email */ char *oldpath; - char *virtual_id; /**< Unique Notmuch Id */ - int magic; /**< Type of mailbox the email is in */ + char *virtual_id; /**< Unique Notmuch Id */ + enum MailboxType magic; /**< Type of mailbox the email is in */ }; /** -- 2.40.0