3 * folder_read.c -- initialize folder structure and read folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 /* We allocate the `mi' array 1024 elements at a time */
18 * 1) Create the folder/message structure
19 * 2) Read the directory (folder) and temporarily
20 * record the numbers of the messages we have seen.
21 * 3) Then allocate the array for message attributes and
22 * set the initial flags for all messages we've seen.
23 * 4) Read and initialize the sequence information.
27 folder_read (char *name)
29 int msgnum, prefix_len, len, *mi;
35 name = m_mailpath (name);
36 if (!(dd = opendir (name))) {
41 if (stat (name, &st) == -1) {
46 /* Allocate the main structure for folder information */
47 if (!(mp = (struct msgs *) malloc ((size_t) sizeof(*mp))))
48 adios (NULL, "unable to allocate folder storage");
50 clear_folder_flags (mp);
60 if (access (name, W_OK) == -1 || st.st_uid != getuid())
62 prefix_len = strlen(BACKUP_PREFIX);
65 * Allocate a temporary place to record the
66 * name of the messages in this folder.
69 if (!(mi = (int *) malloc ((size_t) (len * sizeof(*mi)))))
70 adios (NULL, "unable to allocate storage");
72 while ((dp = readdir (dd))) {
73 if ((msgnum = m_atoi (dp->d_name))) {
75 * Check if we need to allocate more
76 * temporary elements for message names.
78 if (mp->nummsg >= len) {
80 if (!(mi = (int *) realloc (mi,
81 (size_t) (len * sizeof(*mi))))) {
82 adios (NULL, "unable to allocate storage");
86 /* Check if this is the first message we've seen */
87 if (mp->nummsg == 0) {
91 /* Check if this is it the highest or lowest we've seen? */
92 if (msgnum < mp->lowmsg)
94 if (msgnum > mp->hghmsg)
99 * Now increment count, and record message
100 * number in a temporary place for now.
102 mi[mp->nummsg++] = msgnum;
105 switch (dp->d_name[0]) {
114 /* skip any files beginning with backup prefix */
115 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
118 /* skip the LINK file */
119 if (!strcmp (dp->d_name, LINK))
122 /* indicate that there are other files in folder */
123 set_other_files (mp);
130 mp->lowoff = max (mp->lowmsg, 1);
132 /* Go ahead and allocate space for 100 additional messages. */
133 mp->hghoff = mp->hghmsg + 100;
135 /* for testing, allocate minimal necessary space */
136 /* mp->hghoff = max (mp->hghmsg, 1); */
139 * Allocate space for status of each message.
141 if (!(mp->msgstats = malloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff))))
142 adios (NULL, "unable to allocate storage for msgstats");
145 * Clear all the flag bits for all the message
146 * status entries we just allocated.
148 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
149 clear_msg_flags (mp, msgnum);
152 * Scan through the array of messages we've seen and
153 * setup the initial flags for those messages in the
154 * newly allocated mp->msgstats area.
156 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
157 set_exists (mp, mi[msgnum]);
159 free (mi); /* We don't need this anymore */
162 * Read and initialize the sequence information.