]> diplodocus.org Git - nmh/blobdiff - sbr/folder_read.c
Assume POSIX ctype.h; don't vet tolower()'s parameter.
[nmh] / sbr / folder_read.c
index 2e67917257f57944b649045848b76a4a33f32a11..e4bac9045e38e01015ad2a045f4c425a12045a53 100644 (file)
@@ -2,14 +2,13 @@
 /*
  * folder_read.c -- initialize folder structure and read folder
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 /* We allocate the `mi' array 1024 elements at a time */
 #define        NUMMSGS  1024
  */
 
 struct msgs *
-folder_read (char *name)
+folder_read (char *name, int lockflag)
 {
     int msgnum, prefix_len, len, *mi;
     struct msgs *mp;
-    struct stat st;
     struct dirent *dp;
     DIR *dd;
+    bvector_t *v;
+    size_t i;
 
     name = m_mailpath (name);
     if (!(dd = opendir (name))) {
@@ -38,15 +38,8 @@ folder_read (char *name)
        return NULL;
     }
 
-    if (stat (name, &st) == -1) {
-       free (name);
-       return NULL;
-    }
-
     /* Allocate the main structure for folder information */
-    if (!(mp = (struct msgs *) malloc ((size_t) sizeof(*mp))))
-       adios (NULL, "unable to allocate folder storage");
-
+    NEW(mp);
     clear_folder_flags (mp);
     mp->foldpath = name;
     mp->lowmsg = 0;
@@ -56,8 +49,10 @@ folder_read (char *name)
     mp->hghsel = 0;
     mp->numsel = 0;
     mp->nummsg = 0;
+    mp->seqhandle = NULL;
+    mp->seqname = NULL;
 
-    if (access (name, W_OK) == -1 || st.st_uid != getuid())
+    if (access (name, W_OK) == -1)
        set_readonly (mp);
     prefix_len = strlen(BACKUP_PREFIX);
 
@@ -66,8 +61,7 @@ folder_read (char *name)
      * name of the messages in this folder.
      */
     len = NUMMSGS;
-    if (!(mi = (int *) malloc ((size_t) (len * sizeof(*mi)))))
-       adios (NULL, "unable to allocate storage");
+    mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
 
     while ((dp = readdir (dd))) {
        if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
@@ -77,10 +71,7 @@ folder_read (char *name)
             */
            if (mp->nummsg >= len) {
                len += NUMMSGS;
-               if (!(mi = (int *) realloc (mi,
-                       (size_t) (len * sizeof(*mi))))) {
-                   adios (NULL, "unable to allocate storage");
-               }
+               mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
            }
 
            /* Check if this is the first message we've seen */
@@ -105,9 +96,6 @@ folder_read (char *name)
            switch (dp->d_name[0]) {
                case '.': 
                case ',': 
-#ifdef MHE
-               case '+': 
-#endif /* MHE */
                    continue;
 
                default: 
@@ -135,11 +123,26 @@ folder_read (char *name)
     /* for testing, allocate minimal necessary space */
     /* mp->hghoff = max (mp->hghmsg, 1); */
 
+    /*
+     * If for some reason hghoff < lowoff (like we got an integer overflow)
+     * the complain about this now.
+     */
+
+    if (mp->hghoff < mp->lowoff) {
+       adios(NULL, "Internal failure: high message limit < low message "
+             "limit; possible overflow?");
+    }
+
     /*
      * Allocate space for status of each message.
      */
-    if (!(mp->msgstats = malloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff))))
-       adios (NULL, "unable to allocate storage for msgstats");
+    mp->num_msgstats = MSGSTATNUM (mp->lowoff, mp->hghoff);
+    mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp));
+    for (i = 0, v = mp->msgstats; i < mp->num_msgstats; ++i, ++v) {
+       *v = bvector_create (0);
+    }
+
+    mp->msgattrs = svector_create (0);
 
     /*
      * Clear all the flag bits for all the message
@@ -161,7 +164,15 @@ folder_read (char *name)
     /*
      * Read and initialize the sequence information.
      */
-    seq_read (mp);
+    if (seq_read (mp, lockflag) == NOTOK) {
+        char seqfile[PATH_MAX];
+
+        /* Failed to lock sequence file. */
+        snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
+        advise (seqfile, "failed to lock");
+
+        return NULL;
+    }
 
     return mp;
 }