]> granicus.if.org Git - neomutt/commitdiff
use enum MailboxType
authorRichard Russon <rich@flatcap.org>
Sun, 5 Aug 2018 09:37:34 +0000 (10:37 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 5 Aug 2018 09:37:34 +0000 (10:37 +0100)
13 files changed:
browser.c
compress.c
config/magic.h
context.h
curs_main.c
mailbox.c
mailbox.h
maildir/maildir.h
maildir/mh.c
muttlib.c
mx.c
mx.h
notmuch/mutt_notmuch.c

index 74142282712816bc15bbffe64ce145525738e5d6..e4abb3b85ef67fa506469df9523056e6d061296c 100644 (file)
--- 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
index f956d07c7f55d4765fb8ca68a20f9dcc9648cdd8..91c55147eaecc0d4e4b8ea0ec8560f8a20ddb604 100644 (file)
@@ -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;
index 449ff6b1a73e7630bf5b1852a50f30131d9e9f84..7d1459b75df5719e59197deaae23d2b17c61982d 100644 (file)
@@ -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,
index 5a77eb7d5f848cf02aa249c463c171b1881764eb..92c224d19fd8c24dc6c853195dcf11a64df6c65a 100644 (file)
--- 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 */
 
index e5be2c4976786027ee349f71f5b10bd685870723..1cf65325653fcfb7e3193e524b1df8473b027cca 100644 (file)
@@ -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;
index 2b39d59b7b61c072672b2d4c8ee3f057fc1beccd..90f82244ab7c341473f471604cd03eed9f210e8a 100644 (file)
--- 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)
index 4ed4a1b317760f1069437c110d91f17ea79d6372..b6276dde185092e0c88625d5c0a553f7527105f5 100644 (file)
--- 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. */
index 84a40b3089367a20ac773fad2c9562890924500d..21f220f6018b78de4ebfaf61c5e355429f824ad7 100644 (file)
@@ -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);
index a3c06109faf27b53b109139f41decdad2cacb08c..6e6fdad03c85871a453757851f0cb2cd4c2d102c 100644 (file)
@@ -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");
index 684af9aaf2a6d0c31467599cecbafc0c7b431ba1..17469fbdcdb36c11bca4f41467888ab6e60843e5 100644 (file)
--- 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 044fab62d2e1d64737ecae3a306ae73838f3027a..0cd38f77026c055522a8a6e2c840452e8cac94c7 100644 (file)
--- 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 07b918886bde6e7947d14db4d04b3fedf75b4d0e..771f06d5a28c77b2710516933b17997b0d65ac29 100644 (file)
--- 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);
index c408c4b957fa04af2a1b05111acdfeb3b820ef8c..89340e0a554031e5340bd240b727f98094a862fe 100644 (file)
@@ -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 */
 };
 
 /**