]> granicus.if.org Git - neomutt/commitdiff
use mx_path_probe()
authorRichard Russon <rich@flatcap.org>
Sat, 25 Aug 2018 14:35:45 +0000 (15:35 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 26 Aug 2018 21:11:03 +0000 (22:11 +0100)
14 files changed:
browser.c
compress.c
compress.h
curs_main.c
imap/imap.h
mailbox.c
maildir/maildir.h
mbox/mbox.h
muttlib.c
mx.c
mx.h
nntp/nntp.h
notmuch/mutt_notmuch.h
pop/pop.h

index 3c0a1ebbc2629a6cdccdf40b74ec60bd2d75e368..0a33fe2afea1c9bf257827bef91b1db941951da1 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -1430,7 +1430,7 @@ void mutt_select_file(char *file, size_t filelen, int flags, char ***files, int
           /* If browsing in "local"-mode, than we chose to define LastDir to
            * MailDir
            */
-          switch (mx_get_magic(CurrentFolder))
+          switch (mx_path_probe(CurrentFolder, NULL))
           {
             case MUTT_IMAP:
             case MUTT_MAILDIR:
@@ -1566,7 +1566,7 @@ void mutt_select_file(char *file, size_t filelen, int flags, char ***files, int
                                   sizeof(buf));
           }
 
-          enum MailboxType magic = mx_get_magic(buf);
+          enum MailboxType magic = mx_path_probe(buf, NULL);
           if ((magic == MUTT_MAILBOX_ERROR) || (magic == MUTT_UNKNOWN)
 #ifdef USE_IMAP
               || state.entry[menu->current].inferiors
index 7deccc67293c6f0cb794ca99a909d93838aed9c7..4c9b959e0cd6db82abd601ebf66551894af8dac6 100644 (file)
@@ -470,7 +470,7 @@ static int comp_mbox_open(struct Context *ctx)
 
   unlock_realpath(ctx);
 
-  ctx->magic = mx_get_magic(ctx->path);
+  ctx->magic = mx_path_probe(ctx->path, NULL);
   if (ctx->magic == MUTT_UNKNOWN)
   {
     mutt_error(_("Can't identify the contents of the compressed file"));
@@ -538,7 +538,7 @@ static int comp_mbox_open_append(struct Context *ctx, int flags)
       mutt_error(_("Compress command failed: %s"), ci->open);
       goto oa_fail2;
     }
-    ctx->magic = mx_get_magic(ctx->path);
+    ctx->magic = mx_path_probe(ctx->path, NULL);
   }
   else
     ctx->magic = MboxType;
index 0b450bbc6c1624e035d945b49f911469ecb8b915..52be19f8cb4a71f2f3a369d72ddc0f2926224e13 100644 (file)
@@ -32,6 +32,7 @@ struct Context;
 bool mutt_comp_can_append(struct Context *ctx);
 bool mutt_comp_can_read(const char *path);
 int mutt_comp_valid_command(const char *cmd);
+int comp_path_probe(const char *path, const struct stat *st);
 
 extern struct MxOps mx_comp_ops;
 
index cd3024ee5943ae53fce7ac4b7f813254d0bcba2b..158a3c305dadcdb525ed4e6746bfc0bc9b2f8d50 100644 (file)
@@ -498,7 +498,7 @@ static int main_change_folder(struct Menu *menu, int op, char *buf,
 #endif
     mutt_expand_path(buf, buflen);
 
-  enum MailboxType magic = mx_get_magic(buf);
+  enum MailboxType magic = mx_path_probe(buf, NULL);
   if ((magic == MUTT_MAILBOX_ERROR) || (magic == MUTT_UNKNOWN))
   {
     mutt_error(_("%s is not a mailbox"), buf);
index 2964d9df41787f93b098f27ef207460c4f295cc5..fc9ee7bee9fbe4e719e14eda6a1c8f132d19b267 100644 (file)
@@ -93,6 +93,7 @@ int imap_search(struct Context *ctx, const struct Pattern *pat);
 int imap_subscribe(char *path, bool subscribe);
 int imap_complete(char *buf, size_t buflen, char *path);
 int imap_fast_trash(struct Context *ctx, char *dest);
+int imap_path_probe(const char *path, const struct stat *st);
 
 extern struct MxOps mx_imap_ops;
 
index 365e2acde073a15b5c3b8f44e3d31f5303be6279..ed39d56bbdb419b89935bcdded88f9207b10583a 100644 (file)
--- a/mailbox.c
+++ b/mailbox.c
@@ -153,7 +153,7 @@ static bool test_new_folder(const char *path)
   FILE *f = NULL;
   bool rc = false;
 
-  enum MailboxType magic = mx_get_magic(path);
+  enum MailboxType magic = mx_path_probe(path, NULL);
 
   if ((magic != MUTT_MBOX) && (magic != MUTT_MMDF))
     return false;
@@ -408,7 +408,7 @@ 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 == MUTT_UNKNOWN) && (tmp->magic = mx_get_magic(tmp->path)) <= 0))
+            ((tmp->magic == MUTT_UNKNOWN) && (tmp->magic = mx_path_probe(tmp->path, NULL)) <= 0))
     {
       /* if the mailbox still doesn't exist, set the newly created flag to be
        * ready for when it does. */
index 21f220f6018b78de4ebfaf61c5e355429f824ad7..065abaa25a4f856941e339bd157d7bfd5afefb80 100644 (file)
@@ -68,6 +68,8 @@ int            mh_check_empty(const char *path);
 
 bool           mx_is_maildir(const char *path);
 bool           mx_is_mh(const char *path);
+int            maildir_path_probe(const char *path, const struct stat *st);
+int            mh_path_probe(const char *path, const struct stat *st);
 
 #ifdef USE_HCACHE
 int            mh_sync_mailbox_message(struct Context *ctx, int msgno, header_cache_t *hc);
index 7c3b41fefceb9d92330a8349f901ce47a9d577ac..6f963b616108ce29a1f439a809a703bf9d4d12ad 100644 (file)
@@ -42,5 +42,6 @@ extern struct MxOps mx_mmdf_ops;
 #define MMDF_SEP "\001\001\001\001\n"
 
 void mbox_reset_atime(struct Context *ctx, struct stat *st);
+int mbox_path_probe(const char *path, const struct stat *st);
 
 #endif /* _MBOX_MBOX_H */
index f6829d5622b3dec36e329e479eb1aa036eeafb96..3cdca4f4084df4ebe335ed16f37d19713d75cfcf 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -1334,7 +1334,7 @@ int mutt_save_confirm(const char *s, struct stat *st)
   int ret = 0;
   int rc;
 
-  enum MailboxType magic = mx_get_magic(s);
+  enum MailboxType magic = mx_path_probe(s, NULL);
 
 #ifdef USE_POP
   if (magic == MUTT_POP)
diff --git a/mx.c b/mx.c
index f1d43c185a591632d9636fc9b10b035f3f937c87..e411b9c25abdf0d775edd14b18e51263f540fc78 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -94,25 +94,30 @@ char *Trash;        ///< Config: Folder to put deleted emails
 /**
  * mx_ops - All the Mailbox backends
  */
-const struct MxOps *mx_ops[] = {
-#ifdef USE_COMPRESSED
-  &mx_comp_ops,
-#endif
+static const struct MxOps *mx_ops[] = {
+  /* These mailboxes can be recognised by their Url scheme */
 #ifdef USE_IMAP
   &mx_imap_ops,
-#endif
-  &mx_maildir_ops,
-  &mx_mbox_ops,
-  &mx_mh_ops,
-  &mx_mmdf_ops,
-#ifdef USE_NNTP
-  &mx_nntp_ops,
 #endif
 #ifdef USE_NOTMUCH
   &mx_notmuch_ops,
 #endif
 #ifdef USE_POP
   &mx_pop_ops,
+#endif
+#ifdef USE_NNTP
+  &mx_nntp_ops,
+#endif
+
+  /* Local mailboxes */
+  &mx_maildir_ops,
+  &mx_mbox_ops,
+  &mx_mh_ops,
+  &mx_mmdf_ops,
+
+  /* If everything else fails... */
+#ifdef USE_COMPRESSED
+  &mx_comp_ops,
 #endif
   NULL,
 };
@@ -229,118 +234,6 @@ bool mx_is_notmuch(const char *p)
 }
 #endif
 
-/**
- * mx_get_magic - Identify the type of mailbox
- * @param path Mailbox path to test
- * @retval -1 Error, can't identify mailbox
- * @retval >0 Success, e.g. #MUTT_IMAP
- */
-enum MailboxType mx_get_magic(const char *path)
-{
-  if (!path)
-    return MUTT_UNKNOWN;
-
-#ifdef USE_IMAP
-  if (mx_is_imap(path))
-    return MUTT_IMAP;
-#endif /* USE_IMAP */
-
-#ifdef USE_POP
-  if (mx_is_pop(path))
-    return MUTT_POP;
-#endif /* USE_POP */
-
-#ifdef USE_NNTP
-  if (mx_is_nntp(path))
-    return MUTT_NNTP;
-#endif /* USE_NNTP */
-
-#ifdef USE_NOTMUCH
-  if (mx_is_notmuch(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);
-    return -1;
-  }
-
-  if (S_ISDIR(st.st_mode))
-  {
-    /* check for maildir-style mailbox */
-    if (mx_is_maildir(path))
-      return MUTT_MAILDIR;
-
-    /* check for mh-style mailbox */
-    if (mx_is_mh(path))
-      return MUTT_MH;
-  }
-  else if (st.st_size == 0)
-  {
-    /* hard to tell what zero-length files are, so assume the default magic */
-    if (MboxType == MUTT_MBOX || MboxType == MUTT_MMDF)
-      return MboxType;
-    else
-      return MUTT_MBOX;
-  }
-  else if ((f = fopen(path, "r")))
-  {
-    struct utimbuf times;
-    int ch;
-
-    /* Some mailbox creation tools erroneously append a blank line to
-     * a file before appending a mail message.  This allows neomutt to
-     * detect magic for and thus open those files. */
-    while ((ch = fgetc(f)) != EOF)
-    {
-      if (ch != '\n' && ch != '\r')
-      {
-        ungetc(ch, f);
-        break;
-      }
-    }
-
-    char tmp[STRING];
-    if (fgets(tmp, sizeof(tmp), f))
-    {
-      if (mutt_str_strncmp("From ", tmp, 5) == 0)
-        magic = MUTT_MBOX;
-      else if (mutt_str_strcmp(MMDF_SEP, tmp) == 0)
-        magic = MUTT_MMDF;
-    }
-    mutt_file_fclose(&f);
-
-    if (!CheckMboxSize)
-    {
-      /* need to restore the times here, the file was not really accessed,
-       * only the type was accessed.  This is important, because detection
-       * of "new mail" depends on those times set correctly.
-       */
-      times.actime = st.st_atime;
-      times.modtime = st.st_mtime;
-      utime(path, &times);
-    }
-  }
-  else
-  {
-    mutt_debug(1, "unable to open file %s for reading.\n", path);
-    return MUTT_MAILBOX_ERROR;
-  }
-
-#ifdef USE_COMPRESSED
-  /* If there are no other matches, see if there are any
-   * compress hooks that match */
-  if ((magic == MUTT_UNKNOWN) && mutt_comp_can_read(path))
-    return MUTT_COMPRESSED;
-#endif
-  return magic;
-}
-
 /**
  * mx_access - Wrapper for access, checks permissions on a given mailbox
  * @param path  Path of mailbox
@@ -373,7 +266,7 @@ static int mx_open_mailbox_append(struct Context *ctx, int flags)
   struct stat sb;
 
   ctx->append = true;
-  ctx->magic = mx_get_magic(ctx->path);
+  ctx->magic = mx_path_probe(ctx->path, NULL);
   if (ctx->magic == MUTT_UNKNOWN)
   {
     mutt_error(_("%s is not a mailbox"), ctx->path);
@@ -474,7 +367,7 @@ struct Context *mx_mbox_open(const char *path, int flags, struct Context *pctx)
     return ctx;
   }
 
-  ctx->magic = mx_get_magic(path);
+  ctx->magic = mx_path_probe(path, NULL);
   ctx->mx_ops = mx_get_ops(ctx->magic);
 
   if ((ctx->magic == MUTT_UNKNOWN) || (ctx->magic == MUTT_MAILBOX_ERROR) || !ctx->mx_ops)
@@ -1437,7 +1330,7 @@ void mx_update_context(struct Context *ctx, int new_messages)
  */
 int mx_check_empty(const char *path)
 {
-  switch (mx_get_magic(path))
+  switch (mx_path_probe(path, NULL))
   {
     case MUTT_MBOX:
     case MUTT_MMDF:
diff --git a/mx.h b/mx.h
index cc5fa7668990a1b4eebe7523f84c96171e300fdd..8517fd32ace9d9e5cefe82ad039c2369f0b1b9d7 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -247,7 +247,6 @@ void                mx_alloc_memory(struct Context *ctx);
 int                 mx_check_empty(const char *path);
 int                 mx_check_mailbox(struct Context *ctx, int *index_hint);
 void                mx_fastclose_mailbox(struct Context *ctx);
-enum MailboxType    mx_get_magic(const char *path);
 const struct MxOps *mx_get_ops(enum MailboxType magic);
 bool                mx_tags_is_supported(struct Context *ctx);
 void                mx_update_context(struct Context *ctx, int new_messages);
index 0ae3ea17233729e7427c4cb5fffaf0468abd0778..54518756bd22923a383ac2d433526a7905400dd9 100644 (file)
@@ -174,6 +174,7 @@ void nntp_article_status(struct Context *ctx, struct Header *hdr, char *group, a
 extern struct NntpServer *CurrentNewsSrv;
 
 int nntp_compare_order(const void *a, const void *b);
+int nntp_path_probe(const char *path, const struct stat *st);
 
 extern struct MxOps mx_nntp_ops;
 
index 56f79c2aaa48fc2d4e83d361bb4b9d56745ade3c..2c4da68004ab68e88562295654fa89a394ef8fcd 100644 (file)
@@ -77,6 +77,7 @@ int nm_get_all_tags(struct Context *ctx, char **tag_list, int *tag_count);
  * functions usable outside notmuch Context
  */
 int nm_nonctx_get_count(char *path, int *all, int *new);
+int nm_path_probe(const char *path, const struct stat *st);
 
 extern struct MxOps mx_notmuch_ops;
 
index aeee4e5ba0a36738d108d82679968870bfed6a01..35d6a86eb92fe42afe8504bc9e7f9fb02b9fefb6 100644 (file)
--- a/pop/pop.h
+++ b/pop/pop.h
@@ -54,5 +54,6 @@ extern unsigned char PopReconnect;
 extern struct MxOps mx_pop_ops;
 
 void pop_fetch_mail(void);
+int pop_path_probe(const char *path, const struct stat *st);
 
 #endif /* _POP_POP_H */