Only use fcntl, simplify locking.
[apps/madmutt.git] / lib-mx / mh.c
index 7c2f7e2..dacef77 100644 (file)
@@ -16,7 +16,7 @@
 #include <lib-lib/lib-lib.h>
 #include <utime.h>
 
-#include <lib-ui/curses.h>
+#include <lib-ui/lib-ui.h>
 
 #include "mutt.h"
 #include "mx.h"
@@ -35,13 +35,12 @@ struct maildir {
   struct maildir *next;
 };
 
-struct mh_sequences {
-  int max;
-  short *flags;
-};
+typedef struct mh_sequences {
+    int size;
+    char flags[];
+} mh_sequences;
 
 /* mh_sequences support */
-
 #define MH_SEQ_UNSEEN  (1 << 0)
 #define MH_SEQ_REPLIED (1 << 1)
 #define MH_SEQ_FLAGGED (1 << 2)
@@ -51,39 +50,37 @@ static int maildir_check_empty (const char*);
 static int maildir_check_mailbox (CONTEXT*, int*, int);
 static int mh_check_mailbox (CONTEXT*, int*, int);
 
-static void mhs_alloc (struct mh_sequences *mhs, int i)
+static mh_sequences *mhs_new(void)
 {
-  int j;
-  int newmax;
-
-  if (i > mhs->max || !mhs->flags) {
-    newmax = i + 128;
-    p_realloc(&mhs->flags, newmax + 1);
-    for (j = mhs->max + 1; j <= newmax; j++)
-      mhs->flags[j] = 0;
+    mh_sequences *res = xmalloc(sizeof(mh_sequences) + 128);
+    res->size = 128;
+    return res;
+}
 
-    mhs->max = newmax;
-  }
+static void mhs_ensure(mh_sequences *mhs, int i)
+{
+    if (i > mhs->size) {
+        xrealloc((void *)&mhs, sizeof(mh_sequences) + mhs->size + 128);
+        p_clear(mhs->flags + mhs->size, 128);
+        mhs->size += 128;
+    }
 }
 
-static void mhs_free_sequences (struct mh_sequences *mhs)
+static void mhs_delete(mh_sequences **mhs)
 {
-  p_delete(&mhs->flags);
+    p_delete(mhs);
 }
 
 static short mhs_check (struct mh_sequences *mhs, int i)
 {
-  if (!mhs->flags || i > mhs->max)
-    return 0;
-  else
-    return mhs->flags[i];
+    return i > mhs->size ? 0 : mhs->flags[i];
 }
 
 static short mhs_set (struct mh_sequences *mhs, int i, short f)
 {
-  mhs_alloc (mhs, i);
-  mhs->flags[i] |= f;
-  return mhs->flags[i];
+    mhs_ensure(mhs, i);
+    mhs->flags[i] |= f;
+    return mhs->flags[i];
 }
 
 static void mh_read_token (char *t, int *first, int *last)
@@ -143,17 +140,19 @@ static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
 
 int mh_buffy (const char *path)
 {
-  int i, r = 0;
-  struct mh_sequences mhs;
-
-  p_clear(&mhs, 1);
+    mh_sequences *mhs = mhs_new();
+    int i;
+
+    mh_read_sequences(mhs, path);
+    for (i = 0; i <= mhs->size; i++) {
+        if (mhs_check(mhs, i) & MH_SEQ_UNSEEN) {
+            mhs_delete(&mhs);
+            return 1;
+        }
+    }
 
-  mh_read_sequences (&mhs, path);
-  for (i = 0; !r && i <= mhs.max; i++)
-    if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
-      r = 1;
-  mhs_free_sequences (&mhs);
-  return r;
+    mhs_delete(&mhs);
+    return 0;
 }
 
 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
@@ -164,7 +163,7 @@ static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
 
   for (;;) {
     snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
-              dest->path, NONULL(MCore.shorthost), (int) getpid (), Counter++);
+              dest->path, NONULL(mod_core.shorthost), (int) getpid (), Counter++);
     umask (Umask);
     if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
       if (errno != EEXIST) {
@@ -188,8 +187,8 @@ static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
   return 0;
 }
 
-static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
-                                    short f, const char *tag)
+static void mhs_write_one_sequence(FILE * fp, struct mh_sequences *mhs,
+                                   short f, const char *tag)
 {
   int i;
   int first, last;
@@ -199,7 +198,7 @@ static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
   first = -1;
   last = -1;
 
-  for (i = 0; i <= mhs->max; i++) {
+  for (i = 0; i <= mhs->size; i++) {
     if ((mhs_check (mhs, i) & f)) {
       if (first < 0)
         first = i;
@@ -248,15 +247,11 @@ static void mh_update_sequences (CONTEXT * ctx)
   char seq_unseen[STRING];
   char seq_replied[STRING];
   char seq_flagged[STRING];
+  mh_sequences *mhs = mhs_new();
 
-
-  struct mh_sequences mhs;
-
-  p_clear(&mhs, 1);
-
-  snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
-  snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
-  snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
+  snprintf(seq_unseen,  sizeof(seq_unseen),  "%s:", NONULL(MhUnseen));
+  snprintf(seq_replied, sizeof(seq_replied), "%s:", NONULL(MhReplied));
+  snprintf(seq_flagged, sizeof(seq_flagged), "%s:", NONULL(MhFlagged));
 
   if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
     /* error message? */
@@ -294,29 +289,28 @@ static void mh_update_sequences (CONTEXT * ctx)
     i = atoi (p);
 
     if (!ctx->hdrs[l]->read) {
-      mhs_set (&mhs, i, MH_SEQ_UNSEEN);
+      mhs_set(mhs, i, MH_SEQ_UNSEEN);
       unseen++;
     }
     if (ctx->hdrs[l]->flagged) {
-      mhs_set (&mhs, i, MH_SEQ_FLAGGED);
+      mhs_set(mhs, i, MH_SEQ_FLAGGED);
       flagged++;
     }
     if (ctx->hdrs[l]->replied) {
-      mhs_set (&mhs, i, MH_SEQ_REPLIED);
+      mhs_set(mhs, i, MH_SEQ_REPLIED);
       replied++;
     }
   }
 
   /* write out the new sequences */
   if (unseen)
-    mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
+    mhs_write_one_sequence(nfp, mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
   if (flagged)
-    mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
+    mhs_write_one_sequence(nfp, mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
   if (replied)
-    mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
-
-  mhs_free_sequences (&mhs);
+    mhs_write_one_sequence(nfp, mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
 
+  mhs_delete(&mhs);
 
   /* try to commit the changes - no guarantee here */
   m_fclose(&nfp);
@@ -662,7 +656,7 @@ static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
     /* FOO - really ignore the return value? */
 
     maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
-#if HAVE_DIRENT_D_INO
+#ifdef HAVE_DIRENT_D_INO
                          de->d_ino
 #else
                          0
@@ -736,7 +730,7 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
   struct stat lastchanged;
   int ret;
 
-  hc = mutt_hcache_open (HeaderCache, ctx->path);
+  hc = mutt_hcache_open(ctx->path);
 #endif
 
   for (p = md, count = 0; p; p = p->next, count++) {
@@ -786,26 +780,19 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
  */
 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
 {
-  struct maildir *md;
-  struct mh_sequences mhs;
-  struct maildir **last;
-  int count;
-
-
-  p_clear(&mhs, 1);
+  struct maildir *md = NULL, **last = &md;
+  int count = 0;
 
   maildir_update_mtime (ctx);
 
-  md = NULL;
-  last = &md;
-  count = 0;
   if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
     return -1;
 
   if (ctx->magic == M_MH) {
-    mh_read_sequences (&mhs, ctx->path);
-    mh_update_maildir (md, &mhs);
-    mhs_free_sequences (&mhs);
+    mh_sequences *mhs = mhs_new();
+    mh_read_sequences(mhs, ctx->path);
+    mh_update_maildir(md, mhs);
+    mhs_delete(&mhs);
   }
 
   if (ctx->magic == M_MAILDIR)
@@ -911,7 +898,7 @@ static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr
   for (;;) {
     snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
               dest->path, subdir, (long) time (NULL),
-              (unsigned int) getpid (), Counter++, NONULL (MCore.shorthost), suffix);
+              (unsigned int) getpid (), Counter++, NONULL (mod_core.shorthost), suffix);
 
     umask (Umask);
     if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
@@ -984,7 +971,7 @@ static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
   for (;;) {
     snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
               (long) time (NULL), (unsigned int) getpid (), Counter++,
-              NONULL (MCore.shorthost), suffix);
+              NONULL (mod_core.shorthost), suffix);
     snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
 
     if (safe_rename (msg->path, full) == 0) {
@@ -1242,7 +1229,7 @@ static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)),
   int i, j;
 
 #ifdef USE_HCACHE
-  void *hc = NULL;
+  hcache_t *hc = NULL;
 #endif /* USE_HCACHE */
 
   if (ctx->magic == M_MH)
@@ -1255,7 +1242,7 @@ static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)),
 
 #ifdef USE_HCACHE
   if (ctx->magic == M_MAILDIR)
-    hc = mutt_hcache_open (HeaderCache, ctx->path);
+    hc = mutt_hcache_open(ctx->path);
 #endif /* USE_HCACHE */
 
   for (i = 0; i < ctx->msgcount; i++) {
@@ -1299,7 +1286,7 @@ static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)),
 
 #ifdef USE_HCACHE
   if (ctx->magic == M_MAILDIR)
-    mutt_hcache_close (hc);
+    mutt_hcache_close (&hc);
 #endif /* USE_HCACHE */
 
   if (ctx->magic == M_MH)
@@ -1324,7 +1311,7 @@ static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)),
 err:
 #ifdef USE_HCACHE
   if (ctx->magic == M_MAILDIR)
-    mutt_hcache_close (hc);
+    mutt_hcache_close (&hc);
 #endif /* USE_HCACHE */
   return -1;
 }
@@ -1553,9 +1540,8 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib
   char buf[_POSIX_PATH_MAX];
   struct stat st, st_cur;
   short modified = 0, have_new = 0, occult = 0;
-  struct maildir *md, *p;
-  struct maildir **last = NULL;
-  struct mh_sequences mhs;
+  struct maildir *md = NULL, **last = &md, *p;
+  mh_sequences *mhs;
   hash_t *fnames;
   int i;
 
@@ -1592,14 +1578,11 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib
   ctx->mtime_cur = st_cur.st_mtime;
   ctx->mtime = st.st_mtime;
 
-  p_clear(&mhs, 1);
-
-  md = NULL;
-  last = &md;
   maildir_parse_dir (ctx, &last, NULL, NULL);
-  mh_read_sequences (&mhs, ctx->path);
-  mh_update_maildir (md, &mhs);
-  mhs_free_sequences (&mhs);
+  mhs = mhs_new();
+  mh_read_sequences(mhs, ctx->path);
+  mh_update_maildir(md, mhs);
+  mhs_delete(&mhs);
 
   /* check for modifications and adjust flags */
   fnames = hash_new (1031, 0);
@@ -1638,16 +1621,13 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib
 }
 
 
-
-
 /*
  * These functions try to find a message in a maildir folder when it
  * has moved under our feet.  Note that this code is rather expensive, but
  * then again, it's called rarely.
  */
-
-static FILE *_maildir_open_find_message (const char *folder, const char *unique,
-                                         const char *subfolder)
+static FILE *_maildir_open_find_message(const char *folder, const char *unique,
+                                        const char *subfolder)
 {
   char dir[_POSIX_PATH_MAX];
   char tunique[_POSIX_PATH_MAX];
@@ -1782,52 +1762,38 @@ static int mh_check_empty (const char *path)
   return r;
 }
 
-static int mh_is_magic (const char* path, struct stat* st) {
-  char tmp[_POSIX_PATH_MAX];
-
-  if (S_ISDIR (st->st_mode)) {
-    snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-
-    snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-
-    snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-
-    snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-
-    snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-
-    /* 
-     * ok, this isn't an mh folder, but mh mode can be used to read
-     * Usenet news from the spool. ;-) 
-     */
+static int mh_is_magic(const char *path, struct stat *st)
+{
+    static char const * const files[] = {
+        ".mh_sequences", ".xmhcache", ".mew_cache",
+        ".mew-cache", ".sylpheed_cache",
+    };
+
+    if (S_ISDIR(st->st_mode)) {
+        for (int i = 0; i < countof(files); i++) {
+            char tmp[_POSIX_PATH_MAX];
+
+            snprintf(tmp, sizeof(tmp), "%s/%s", path, files[i]);
+            if (access(tmp, F_OK) == 0)
+                return M_MH;
+        }
+    }
 
-    snprintf (tmp, sizeof (tmp), "%s/.overview", path);
-    if (access (tmp, F_OK) == 0)
-      return (M_MH);
-  }
-  return (-1);
+    return -1;
 }
 
-static int maildir_is_magic (const char* path, struct stat* st) {
-  struct stat sb;
-  char tmp[_POSIX_PATH_MAX];
+static int maildir_is_magic (const char *path, struct stat *st)
+{
+    if (S_ISDIR(st->st_mode)) {
+        char tmp[_POSIX_PATH_MAX];
+        struct stat sb;
 
-  if (S_ISDIR (st->st_mode)) {
-    snprintf (tmp, sizeof (tmp), "%s/cur", path);
-    if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
-      return (M_MAILDIR);
-  }
-  return (-1);
+        snprintf(tmp, sizeof(tmp), "%s/cur", path);
+        if (stat(tmp, &sb) == 0 && S_ISDIR(sb.st_mode))
+            return M_MAILDIR;
+    }
+
+    return -1;
 }
 
 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {