]> granicus.if.org Git - neomutt/commitdiff
consolidate msg sync code
authorKarel Zak <kzak@redhat.com>
Wed, 14 Dec 2011 13:07:58 +0000 (14:07 +0100)
committerRichard Russon <rich@flatcap.org>
Mon, 14 Mar 2016 23:11:40 +0000 (23:11 +0000)
 - add mh_sync_mailbox_message() to sync just one message
 - add msg->commited_path to save info about the final message path
   (will be used keep notmuch DB up to date)

Signed-off-by: Karel Zak <kzak@redhat.com>
mailbox.h
mh.c
mx.c
mx.h

index 22496613695fe111505c2b619fb8c64478d75ade..f8446c7a7cee5399a0aa0e36ec2532cb16a25aa5 100644 (file)
--- a/mailbox.h
+++ b/mailbox.h
@@ -48,6 +48,7 @@ typedef struct
 {
   FILE *fp;    /* pointer to the message data */
   char *path;  /* path to temp file */
+  char *commited_path; /* the final path generated by mx_commit_message() */
   short magic; /* type of mailbox this message belongs to */
   short write; /* nonzero if message is open for writing */
   struct {
diff --git a/mh.c b/mh.c
index e59420fb5fdf58eb6b55a6b88e0725bcfc555942..3b2ae07b9b3b2c0610c9d489ee32b90132c1ad0a 100644 (file)
--- a/mh.c
+++ b/mh.c
@@ -736,7 +736,7 @@ static void maildir_update_mtime (CONTEXT * ctx)
  * Actually parse a maildir message.  This may also be used to fill
  * out a fake header structure generated by lazy maildir parsing.
  */
-static HEADER *maildir_parse_message (int magic, const char *fname,
+HEADER *maildir_parse_message (int magic, const char *fname,
                                      int is_old, HEADER * _h)
 {
   FILE *f;
@@ -1454,6 +1454,7 @@ int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
     {
       if (hdr)
        mutt_str_replace (&hdr->path, path);
+      mutt_str_replace (&msg->commited_path, full);
       FREE (&msg->path);
 
       /*
@@ -1551,6 +1552,7 @@ static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
     {
       if (hdr)
        mutt_str_replace (&hdr->path, tmp);
+      mutt_str_replace (&msg->commited_path, path);
       FREE (&msg->path);
       break;
     }
@@ -1726,96 +1728,113 @@ static int maildir_sync_message (CONTEXT * ctx, int msgno)
   return (0);
 }
 
-int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
-{
-  char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
-  int i, j;
 #if USE_HCACHE
-  header_cache_t *hc = NULL;
-#endif /* USE_HCACHE */
-  char msgbuf[STRING];
-  progress_t progress;
-
-  if (ctx->magic == M_MH)
-    i = mh_check_mailbox (ctx, index_hint);
-  else 
-    i = maildir_check_mailbox (ctx, index_hint);
-      
-  if (i != 0)
-    return i;
-
-#if USE_HCACHE
-  if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
-    hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
-#endif /* USE_HCACHE */
-
-  if (!ctx->quiet)
-  {
-    snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
-    mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
-  }
-
-  for (i = 0; i < ctx->msgcount; i++)
-  {
-    if (!ctx->quiet)
-      mutt_progress_update (&progress, i, -1);
+int mh_sync_mailbox_message (CONTEXT * ctx, int msgno, header_cache_t *hc)
+#else
+int mh_sync_mailbox_message (CONTEXT * ctx, int msgno)
+#endif
+{
+    char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
+    HEADER *h = ctx->hdrs[msgno];
 
-    if (ctx->hdrs[i]->deleted
-       && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
+    if (h->deleted && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
     {
-      snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
+      snprintf (path, sizeof (path), "%s/%s", ctx->path, h->path);
       if (ctx->magic == M_MAILDIR
          || (option (OPTMHPURGE) && ctx->magic == M_MH))
       {
 #if USE_HCACHE
-        if (ctx->magic == M_MAILDIR)
-          mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
-       else if (ctx->magic == M_MH)
-         mutt_hcache_delete (hc, ctx->hdrs[i]->path, strlen);
+       if (hc) {
+           if (ctx->magic == M_MAILDIR)
+              mutt_hcache_delete (hc, h->path + 3, &maildir_hcache_keylen);
+          else if (ctx->magic == M_MH)
+             mutt_hcache_delete (hc, h->path, strlen);
+       }
 #endif /* USE_HCACHE */
        unlink (path);
       }
       else if (ctx->magic == M_MH)
       {
        /* MH just moves files out of the way when you delete them */
-       if (*ctx->hdrs[i]->path != ',')
+       if (*h->path != ',')
        {
-         snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
-                   ctx->hdrs[i]->path);
+         snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path, h->path);
          unlink (tmp);
          rename (path, tmp);
        }
 
       }
     }
-    else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
+    else if (h->changed || h->attach_del ||
             (ctx->magic == M_MAILDIR
-             && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
-             && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
+             && (option (OPTMAILDIRTRASH) || h->trash)
+             && (h->deleted != h->trash)))
     {
       if (ctx->magic == M_MAILDIR)
       {
-       if (maildir_sync_message (ctx, i) == -1)
-         goto err;
+       if (maildir_sync_message (ctx, msgno) == -1)
+         return -1;
       }
       else
       {
-       if (mh_sync_message (ctx, i) == -1)
-         goto err;
+       if (mh_sync_message (ctx, msgno) == -1)
+         return -1;
       }
     }
 
 #if USE_HCACHE
-    if (ctx->hdrs[i]->changed)
+    if (hc && h->changed)
     {
       if (ctx->magic == M_MAILDIR)
-       mutt_hcache_store (hc, ctx->hdrs[i]->path + 3, ctx->hdrs[i],
-                          0, &maildir_hcache_keylen, M_GENERATE_UIDVALIDITY);
+       mutt_hcache_store (hc, h->path + 3, h, 0, &maildir_hcache_keylen, M_GENERATE_UIDVALIDITY);
       else if (ctx->magic == M_MH)
-       mutt_hcache_store (hc, ctx->hdrs[i]->path, ctx->hdrs[i], 0, strlen, M_GENERATE_UIDVALIDITY);
+       mutt_hcache_store (hc, h->path, h, 0, strlen, M_GENERATE_UIDVALIDITY);
     }
 #endif
 
+    return 0;
+}
+
+int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
+{
+  int i, j;
+#if USE_HCACHE
+  header_cache_t *hc = NULL;
+#endif /* USE_HCACHE */
+  char msgbuf[STRING];
+  progress_t progress;
+
+  if (ctx->magic == M_MH)
+    i = mh_check_mailbox (ctx, index_hint);
+  else
+    i = maildir_check_mailbox (ctx, index_hint);
+
+  if (i != 0)
+    return i;
+
+#if USE_HCACHE
+  if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
+    hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
+#endif /* USE_HCACHE */
+
+  if (!ctx->quiet)
+  {
+    snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
+    mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
+  }
+
+  for (i = 0; i < ctx->msgcount; i++)
+  {
+    if (!ctx->quiet)
+      mutt_progress_update (&progress, i, -1);
+
+#if USE_HCACHE
+    if (mh_sync_mailbox_message (ctx, i, hc) == -1)
+      goto err;
+#else
+    if (mh_sync_mailbox_message (ctx, i) == -1)
+      goto err;
+#endif
   }
 
 #if USE_HCACHE
diff --git a/mx.c b/mx.c
index ac05c04fe5e233b96baa9515ce88380c88824eff..fa91812a83b248b7f25b66361b835c52aef6844a 100644 (file)
--- a/mx.c
+++ b/mx.c
@@ -1524,6 +1524,7 @@ int mx_close_message (MESSAGE **msg)
     FREE (&(*msg)->path);
   }
 
+  FREE (&(*msg)->commited_path);
   FREE (msg);          /* __FREE_CHECKED__ */
   return (r);
 }
diff --git a/mx.h b/mx.h
index 57429a2fd57cea5a2abcc05a14113ffd9268e3db..754be41fd2a732a9a9823053c20df7cabdadbb39 100644 (file)
--- a/mx.h
+++ b/mx.h
@@ -67,6 +67,15 @@ int maildir_read_dir (CONTEXT *);
 int maildir_check_mailbox (CONTEXT *, int *);
 int maildir_check_empty (const char *);
 
+HEADER *maildir_parse_message (int magic, const char *fname, int is_old, HEADER * _h);
+
+#if USE_HCACHE
+#include <hcache.h>
+int mh_sync_mailbox_message (CONTEXT * ctx, int msgno, header_cache_t *hc);
+#else
+int mh_sync_mailbox_message (CONTEXT * ctx, int msgno);
+#endif
+
 int maildir_commit_message (CONTEXT *, MESSAGE *, HEADER *);
 int mh_commit_message (CONTEXT *, MESSAGE *, HEADER *);