X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-mx%2Fmbox.c;h=9eb2809388f0463841bf4a3f39559aab44efa5d9;hp=60caf04a31c7811d6d711b60d0852b94a54759b9;hb=88d239144bf6f50ca1eda6db7742281f0ad0f97f;hpb=558b0bd9de90a9dc28f409d8f46679bf48c72ded diff --git a/lib-mx/mbox.c b/lib-mx/mbox.c index 60caf04..9eb2809 100644 --- a/lib-mx/mbox.c +++ b/lib-mx/mbox.c @@ -7,11 +7,11 @@ * please see the file GPL in the top level source directory. */ -/* This file contains code to parse ``mbox'' and ``mmdf'' style mailboxes */ +/* This file contains code to parse ``mbox'' style mailboxes */ #include -#include +#include #include #include "mutt.h" @@ -25,18 +25,23 @@ /* struct used by mutt_sync_mailbox() to store new offsets */ struct m_update_t { - short valid; - off_t hdr; - off_t body; - long lines; - off_t length; + short valid; + off_t hdr; + off_t body; + long lines; + off_t length; }; +static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr) +{ + msg->fp = dest->fp; + return 0; +} -static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused))) +static int mbox_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno) { - msg->fp = dest->fp; - return 0; + msg->fp = ctx->fp; + return 0; } /* prototypes */ @@ -47,18 +52,18 @@ static int mbox_reopen_mailbox (CONTEXT*, int*); * excl - exclusive lock? * retry - should retry if unable to lock? */ -int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry) +int mbox_lock_mailbox(CONTEXT *ctx, int excl, int retry) { int r; - if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0) + if ((r = mx_lock_file(ctx->path, fileno(ctx->fp), excl, retry)) == 0) ctx->locked = 1; else if (retry && !excl) { ctx->readonly = 1; return 0; } - return (r); + return r; } static void mbox_unlock_mailbox (CONTEXT * ctx) @@ -66,127 +71,11 @@ static void mbox_unlock_mailbox (CONTEXT * ctx) if (ctx->locked) { fflush (ctx->fp); - mx_unlock_file (ctx->path, fileno (ctx->fp), 1); + mx_unlock_file(ctx->path, fileno(ctx->fp)); ctx->locked = 0; } } -static int mmdf_parse_mailbox (CONTEXT * ctx) -{ - char buf[HUGE_STRING]; - char return_path[LONG_STRING]; - int count = 0, oldmsgcount = ctx->msgcount; - int lines; - time_t t, tz; - off_t loc, tmploc; - HEADER *hdr; - struct stat sb; - - if (stat (ctx->path, &sb) == -1) { - mutt_perror (ctx->path); - return (-1); - } - ctx->mtime = sb.st_mtime; - ctx->size = sb.st_size; - - /* precompute the local timezone to speed up calculation of the - received time */ - tz = mutt_local_tz (0); - - buf[sizeof (buf) - 1] = 0; - - for (;;) { - if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) - break; - - if (m_strcmp(buf, MMDF_SEP) == 0) { - loc = ftello (ctx->fp); - - count++; - if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1)) - mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count, - (int) (loc / (ctx->size / 100 + 1))); - - - if (ctx->msgcount == ctx->hdrmax) - mx_alloc_memory (ctx); - ctx->hdrs[ctx->msgcount] = hdr = header_new(); - hdr->offset = loc; - hdr->index = ctx->msgcount; - - if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) { - /* TODO: memory leak??? */ - break; - } - - return_path[0] = 0; - - if (!is_from (buf, return_path, sizeof (return_path), &t)) { - if (fseeko (ctx->fp, loc, SEEK_SET) != 0) { - mutt_error _("Mailbox is corrupt!"); - - return (-1); - } - } - else - hdr->received = t - tz; - - hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0); - - loc = ftello (ctx->fp); - - if (hdr->content->length > 0 && hdr->lines > 0) { - tmploc = loc + hdr->content->length; - - if (0 < tmploc && tmploc < ctx->size) { - if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 || - fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL || - m_strcmp(MMDF_SEP, buf) != 0) { - fseeko (ctx->fp, loc, SEEK_SET); - hdr->content->length = -1; - } - } - else - hdr->content->length = -1; - } - else - hdr->content->length = -1; - - if (hdr->content->length < 0) { - lines = -1; - do { - loc = ftello (ctx->fp); - if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) - break; - lines++; - } while (m_strcmp(buf, MMDF_SEP) != 0); - - hdr->lines = lines; - hdr->content->length = loc - hdr->content->offset; - } - - if (!hdr->env->return_path && return_path[0]) - hdr->env->return_path = - rfc822_parse_adrlist (hdr->env->return_path, return_path); - - if (!hdr->env->from) - hdr->env->from = address_list_dup (hdr->env->return_path); - - ctx->msgcount++; - } - else { - mutt_error _("Mailbox is corrupt!"); - - return (-1); - } - } - - if (ctx->msgcount > oldmsgcount) - mx_update_context (ctx, ctx->msgcount - oldmsgcount); - - return (0); -} - /* Note that this function is also called when new mail is appended to the * currently open folder, and NOT just when the mailbox is initially read. * @@ -205,7 +94,7 @@ static int mbox_parse_mailbox (CONTEXT * ctx) /* Save information about the folder at the time we opened it. */ if (stat (ctx->path, &sb) == -1) { mutt_perror (ctx->path); - return (-1); + return -1; } ctx->size = sb.st_size; @@ -216,7 +105,8 @@ static int mbox_parse_mailbox (CONTEXT * ctx) /* precompute the local timezone to speed up calculation of the date received */ - tz = mutt_local_tz (0); + t = time(NULL); + tz = localtime(&t)->tm_gmtoff; loc = ftello (ctx->fp); while (fgets (buf, sizeof (buf), ctx->fp) != NULL) { @@ -335,36 +225,32 @@ static int mbox_parse_mailbox (CONTEXT * ctx) mx_update_context (ctx, count); } - return (0); + return 0; } #undef PREV -/* open a mbox or mmdf style mailbox */ +/* open a mbox style mailbox */ static int mbox_open_mailbox (CONTEXT * ctx) { - int rc; + int rc; - if ((ctx->fp = fopen (ctx->path, "r")) == NULL) { - mutt_perror (ctx->path); - return (-1); - } - mutt_block_signals (); - if (mbox_lock_mailbox (ctx, 0, 1) == -1) { - mutt_unblock_signals (); - return (-1); - } + if (!(ctx->fp = fopen(ctx->path, "r"))) { + mutt_perror(ctx->path); + return -1; + } - if (ctx->magic == M_MBOX) - rc = mbox_parse_mailbox (ctx); - else if (ctx->magic == M_MMDF) - rc = mmdf_parse_mailbox (ctx); - else - rc = -1; + mutt_block_signals(); + if (mbox_lock_mailbox(ctx, 0, 1) < 0) { + mutt_unblock_signals(); + return -1; + } - mbox_unlock_mailbox (ctx); - mutt_unblock_signals (); - return (rc); + rc = ctx->magic == M_MBOX ? mbox_parse_mailbox(ctx) : -1; + + mbox_unlock_mailbox(ctx); + mutt_unblock_signals(); + return rc; } /* check to see if the mailbox has changed on disk. @@ -385,12 +271,12 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) if (stat (ctx->path, &st) == 0) { if (st.st_mtime == ctx->mtime && st.st_size == ctx->size) - return (0); + return 0; if (st.st_size == ctx->size) { /* the file was touched, but it is still the same length, so just exit */ ctx->mtime = st.st_mtime; - return (0); + return 0; } if (st.st_size > ctx->size) { @@ -403,7 +289,7 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) * probably the new mail arrived: no reason to wait till we can * parse it: we'll get it on the next pass */ - return (M_LOCKED); + return M_LOCKED; } unlock = 1; } @@ -416,13 +302,9 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) */ fseeko (ctx->fp, ctx->size, SEEK_SET); if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) { - if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0) - || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) { + if (ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0) { fseeko (ctx->fp, ctx->size, SEEK_SET); - if (ctx->magic == M_MBOX) - mbox_parse_mailbox (ctx); - else - mmdf_parse_mailbox (ctx); + mbox_parse_mailbox (ctx); /* Only unlock the folder if it was locked inside of this routine. * It may have been locked elsewhere, like in @@ -434,7 +316,7 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) mutt_unblock_signals (); } - return (M_NEW_MAIL); /* signal that new mail arrived */ + return M_NEW_MAIL; /* signal that new mail arrived */ } else modified = 1; @@ -452,7 +334,7 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) mbox_unlock_mailbox (ctx); mutt_unblock_signals (); } - return (M_REOPENED); + return M_REOPENED; } } @@ -463,27 +345,28 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint) mutt_unblock_signals (); mutt_error _("Mailbox was corrupted!"); - return (-1); + return -1; } -static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) { - int rc = 0; +static int mbox_check_mailbox(CONTEXT *ctx, int *index_hint, int lock) +{ + int rc = 0; - if (lock) { - mutt_block_signals (); - if (mbox_lock_mailbox (ctx, 0, 0) == -1) { - mutt_unblock_signals (); - return M_LOCKED; + if (lock) { + mutt_block_signals(); + if (mbox_lock_mailbox(ctx, 0, 0) < 0) { + mutt_unblock_signals(); + return M_LOCKED; + } } - } - rc = _mbox_check_mailbox (ctx, index_hint); + rc = _mbox_check_mailbox(ctx, index_hint); - if (lock) { - mutt_unblock_signals (); - mbox_unlock_mailbox (ctx); - } - return rc; + if (lock) { + mutt_unblock_signals (); + mbox_unlock_mailbox (ctx); + } + return rc; } /* return values: @@ -521,7 +404,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) mx_fastclose_mailbox (ctx); mutt_error _("Fatal error! Could not reopen mailbox!"); - return (-1); + return -1; } mutt_block_signals (); @@ -543,10 +426,10 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) } else if (i < 0) /* fatal error */ - return (-1); + return -1; /* Create a temporary file to write the new version of the mailbox in. */ - fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(MCore.tmpdir), NULL); + fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL); if (fp == NULL) { mutt_error _("Could not create temporary file!"); mutt_sleep (5); @@ -575,12 +458,6 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) /* where to start overwriting */ offset = ctx->hdrs[i]->offset; - /* the offset stored in the header does not include the MMDF_SEP, so make - * sure we seek to the correct location - */ - if (ctx->magic == M_MMDF) - offset -= (sizeof MMDF_SEP - 1); - /* allocate space for the new offsets */ newOffset = p_new(struct m_update_t, ctx->msgcount - first); oldOffset = p_new(struct m_update_t, ctx->msgcount - first); @@ -603,15 +480,6 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) mutt_message (_("Writing messages... %d (%d%%)"), i, (int) (ftello (ctx->fp) / (ctx->size / 100 + 1))); - if (ctx->magic == M_MMDF) { - if (fputs (MMDF_SEP, fp) == EOF) { - mutt_perror (_("Can't create temporary file")); - mutt_sleep (5); - unlink (tempfile); - goto bail; - } - } - /* save the new offset for this message. we add `offset' because the * temporary file only contains saved message which are located after * `offset' in the real mailbox @@ -637,22 +505,11 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) ftello (fp) - ctx->hdrs[i]->content->length + offset; body_list_wipe(&ctx->hdrs[i]->content->parts); - switch (ctx->magic) { - case M_MMDF: - if (fputs (MMDF_SEP, fp) == EOF) { - mutt_perror (_("Can't create temporary file")); - mutt_sleep (5); - unlink (tempfile); - goto bail; - } - break; - default: - if (fputs ("\n", fp) == EOF) { - mutt_perror (_("Can't create temporary file")); - mutt_sleep (5); - unlink (tempfile); - goto bail; - } + if (fputs ("\n", fp) == EOF) { + mutt_perror (_("Can't create temporary file")); + mutt_sleep (5); + unlink (tempfile); + goto bail; } } } @@ -677,14 +534,14 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) mx_fastclose_mailbox (ctx); mutt_perror (_("Can't create temporary file")); mutt_sleep (5); - return (-1); + return -1; } - if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */ + if (fseeko (ctx->fp, offset, SEEK_SET) != 0 /* seek the append location */ /* do a sanity check to make sure the mailbox looks ok */ - fgets (buf, sizeof (buf), ctx->fp) == NULL || - (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) || - (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) { + || fgets (buf, sizeof (buf), ctx->fp) == NULL + || (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0)) + { i = -1; } else { @@ -718,14 +575,14 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) char savefile[_POSIX_PATH_MAX]; snprintf(savefile, sizeof (savefile), "%s/mutt.%s-%u", - NONULL(MCore.tmpdir), NONULL(MCore.username), (unsigned int)getpid()); + NONULL(mod_core.tmpdir), NONULL(mod_core.username), (unsigned int)getpid()); rename (tempfile, savefile); mutt_unblock_signals (); mx_fastclose_mailbox (ctx); mutt_pretty_mailbox (savefile); mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile); mutt_sleep (5); - return (-1); + return -1; } /* Restore the previous access/modification times */ @@ -739,7 +596,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) mutt_unblock_signals (); mx_fastclose_mailbox (ctx); mutt_error _("Fatal error! Could not reopen mailbox!"); - return (-1); + return -1; } /* update the offsets of the rewritten messages */ @@ -756,7 +613,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)) unlink (tempfile); /* remove partial copy of the mailbox */ mutt_unblock_signals (); - return (0); /* signal success */ + return 0; /* signal success */ bail: /* Come here in case of disaster */ @@ -784,7 +641,7 @@ bail: /* Come here in case of disaster */ mutt_error _("Could not reopen mailbox!"); mx_fastclose_mailbox (ctx); - return (-1); + return -1; } if (need_sort) @@ -798,7 +655,7 @@ bail: /* Come here in case of disaster */ /* close a mailbox opened in write-mode */ int mbox_close_mailbox (CONTEXT * ctx) { - mx_unlock_file (ctx->path, fileno (ctx->fp), 1); + mx_unlock_file(ctx->path, fileno(ctx->fp)); if (ctx->cinfo) mutt_slow_close_compressed (ctx); @@ -867,24 +724,11 @@ static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint) ctx->id_hash = NULL; ctx->subj_hash = NULL; - switch (ctx->magic) { - case M_MBOX: - case M_MMDF: - if (fseeko (ctx->fp, 0, SEEK_SET) != 0) { - rc = -1; - } - else { - cmp_headers = mutt_cmp_header; - if (ctx->magic == M_MBOX) - rc = mbox_parse_mailbox (ctx); - else - rc = mmdf_parse_mailbox (ctx); - } - break; - - default: + if (ctx->magic == M_MBOX && !fseeko(ctx->fp, 0, SEEK_SET)) { + cmp_headers = mutt_cmp_header; + rc = mbox_parse_mailbox (ctx); + } else { rc = -1; - break; } if (rc == -1) { @@ -894,7 +738,7 @@ static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint) p_delete(&old_hdrs); ctx->quiet = 0; - return (-1); + return -1; } /* now try to recover the old flags */ @@ -964,7 +808,7 @@ static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint) ctx->quiet = 0; - return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL); + return (ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL; } /* @@ -980,23 +824,20 @@ int mbox_check_empty (const char *path) if (stat (path, &st) == -1) return -1; - return ((st.st_size == 0)); + return st.st_size == 0; } -int mbox_is_magic (const char* path, struct stat* st) { +int mbox_is_magic (const char* path, struct stat* st) +{ int magic = -1; FILE* f; char tmp[_POSIX_PATH_MAX]; if (S_ISDIR(st->st_mode)) - return (-1); + return -1; if (st->st_size == 0) { - /* hard to tell what zero-length files are, so assume the default magic */ - if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF) - return (DefaultMagic); - else - return (M_MBOX); + return M_MBOX; } else if ((f = fopen (path, "r")) != NULL) { struct utimbuf times; @@ -1004,8 +845,6 @@ int mbox_is_magic (const char* path, struct stat* st) { fgets (tmp, sizeof (tmp), f); if (m_strncmp("From ", tmp, 5) == 0) magic = M_MBOX; - else if (m_strcmp(MMDF_SEP, tmp) == 0) - magic = M_MMDF; m_fclose(&f); /* need to restore the times here, the file was not really accessed, @@ -1017,31 +856,22 @@ int mbox_is_magic (const char* path, struct stat* st) { utime (path, ×); } else { mutt_perror (path); - return (-1); /* fopen failed */ + return -1; /* fopen failed */ } if (magic == -1 && mutt_can_read_compressed (path)) - return (M_COMPRESSED); - return (magic); + return M_COMPRESSED; + return magic; } -static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) { - if ((mbox && fputc ('\n', msg->fp) == EOF) || - (!mbox && fputs (MMDF_SEP, msg->fp) == EOF)) - return (-1); +static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) { + if (fputc ('\n', msg->fp) == EOF) + return -1; if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) { mutt_perror (_("Can't write message")); - return (-1); + return -1; } - return (0); -} - -static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) { - return (commit_message (msg, ctx, 1)); -} - -static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) { - return (commit_message (msg, ctx, 0)); + return 0; } mx_t const mbox_mx = { @@ -1052,24 +882,10 @@ mx_t const mbox_mx = { access, mbox_open_mailbox, mbox_open_new_message, + mbox_open_message, NULL, mbox_check_mailbox, NULL, mbox_sync_mailbox, mbox_commit_message, }; - -mx_t const mmdf_mx = { - M_MMDF, - 1, - mbox_is_magic, - mbox_check_empty, - access, - mbox_open_mailbox, - mbox_open_new_message, - NULL, - mbox_check_mailbox, - NULL, - mbox_sync_mailbox, - mmdf_commit_message, -};