From: Karel Zak Date: Wed, 14 Dec 2011 13:07:58 +0000 (+0100) Subject: consolidate msg sync code X-Git-Tag: neomutt-20160404~13^2~106 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0098ca2a9475e3efc26ff6dd4f410a063868327a;p=neomutt consolidate msg sync code - 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 --- diff --git a/mailbox.h b/mailbox.h index 224966136..f8446c7a7 100644 --- 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 3b0aa6c92..5a988aa22 100644 --- 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 25d823221..4c49c6b10 100644 --- a/mx.c +++ b/mx.c @@ -1531,6 +1531,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 5d769e5c6..fc67a5002 100644 --- 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 +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 *);