3 * folder_read.c -- initialize folder structure and read folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 /* We allocate the `mi' array 1024 elements at a time */
17 * 1) Create the folder/message structure
18 * 2) Read the directory (folder) and temporarily
19 * record the numbers of the messages we have seen.
20 * 3) Then allocate the array for message attributes and
21 * set the initial flags for all messages we've seen.
22 * 4) Read and initialize the sequence information.
26 folder_read (char *name)
28 int msgnum, prefix_len, len, *mi;
34 name = m_mailpath (name);
35 if (!(dd = opendir (name))) {
40 if (stat (name, &st) == -1) {
45 /* Allocate the main structure for folder information */
46 mp = (struct msgs *) mh_xmalloc ((size_t) sizeof(*mp));
48 clear_folder_flags (mp);
58 if (access (name, W_OK) == -1)
60 prefix_len = strlen(BACKUP_PREFIX);
63 * Allocate a temporary place to record the
64 * name of the messages in this folder.
67 mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
69 while ((dp = readdir (dd))) {
70 if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
72 * Check if we need to allocate more
73 * temporary elements for message names.
75 if (mp->nummsg >= len) {
77 mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
80 /* Check if this is the first message we've seen */
81 if (mp->nummsg == 0) {
85 /* Check if this is it the highest or lowest we've seen? */
86 if (msgnum < mp->lowmsg)
88 if (msgnum > mp->hghmsg)
93 * Now increment count, and record message
94 * number in a temporary place for now.
96 mi[mp->nummsg++] = msgnum;
99 switch (dp->d_name[0]) {
105 /* skip any files beginning with backup prefix */
106 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
109 /* skip the LINK file */
110 if (!strcmp (dp->d_name, LINK))
113 /* indicate that there are other files in folder */
114 set_other_files (mp);
121 mp->lowoff = max (mp->lowmsg, 1);
123 /* Go ahead and allocate space for 100 additional messages. */
124 mp->hghoff = mp->hghmsg + 100;
126 /* for testing, allocate minimal necessary space */
127 /* mp->hghoff = max (mp->hghmsg, 1); */
130 * Allocate space for status of each message.
132 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff));
135 * Clear all the flag bits for all the message
136 * status entries we just allocated.
138 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
139 clear_msg_flags (mp, msgnum);
142 * Scan through the array of messages we've seen and
143 * setup the initial flags for those messages in the
144 * newly allocated mp->msgstats area.
146 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
147 set_exists (mp, mi[msgnum]);
149 free (mi); /* We don't need this anymore */
152 * Read and initialize the sequence information.