3 * folder_read.c -- initialize folder structure and read folder
10 /* We allocate the `mi' array 1024 elements at a time */
14 * 1) Create the folder/message structure
15 * 2) Read the directory (folder) and temporarily
16 * record the numbers of the messages we have seen.
17 * 3) Then allocate the array for message attributes and
18 * set the initial flags for all messages we've seen.
19 * 4) Read and initialize the sequence information.
23 folder_read (char *name)
25 int msgnum, prefix_len, len, *mi;
31 name = m_mailpath (name);
32 if (!(dd = opendir (name))) {
37 if (stat (name, &st) == -1) {
42 /* Allocate the main structure for folder information */
43 if (!(mp = (struct msgs *) malloc ((size_t) sizeof(*mp))))
44 adios (NULL, "unable to allocate folder storage");
46 clear_folder_flags (mp);
56 if (access (name, W_OK) == -1 || st.st_uid != getuid())
58 prefix_len = strlen(BACKUP_PREFIX);
61 * Allocate a temporary place to record the
62 * name of the messages in this folder.
65 if (!(mi = (int *) malloc ((size_t) (len * sizeof(*mi)))))
66 adios (NULL, "unable to allocate storage");
68 while ((dp = readdir (dd))) {
69 if ((msgnum = m_atoi (dp->d_name))) {
71 * Check if we need to allocate more
72 * temporary elements for message names.
74 if (mp->nummsg >= len) {
76 if (!(mi = (int *) realloc (mi,
77 (size_t) (len * sizeof(*mi))))) {
78 adios (NULL, "unable to allocate storage");
82 /* Check if this is the first message we've seen */
83 if (mp->nummsg == 0) {
87 /* Check if this is it the highest or lowest we've seen? */
88 if (msgnum < mp->lowmsg)
90 if (msgnum > mp->hghmsg)
95 * Now increment count, and record message
96 * number in a temporary place for now.
98 mi[mp->nummsg++] = msgnum;
101 switch (dp->d_name[0]) {
110 /* skip any files beginning with backup prefix */
111 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
114 /* skip the LINK file */
115 if (!strcmp (dp->d_name, LINK))
118 /* indicate that there are other files in folder */
119 set_other_files (mp);
126 mp->lowoff = max (mp->lowmsg, 1);
128 /* Go ahead and allocate space for 100 additional messages. */
129 mp->hghoff = mp->hghmsg + 100;
131 /* for testing, allocate minimal necessary space */
132 /* mp->hghoff = max (mp->hghmsg, 1); */
135 * Allocate space for status of each message.
137 if (!(mp->msgstats = malloc (MSGSTATSIZE(mp, mp->lowoff, mp->hghoff))))
138 adios (NULL, "unable to allocate storage for msgstats");
141 * Clear all the flag bits for all the message
142 * status entries we just allocated.
144 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
145 clear_msg_flags (mp, msgnum);
148 * Scan through the array of messages we've seen and
149 * setup the initial flags for those messages in the
150 * newly allocated mp->msgstats area.
152 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
153 set_exists (mp, mi[msgnum]);
155 free (mi); /* We don't need this anymore */
158 * Read and initialize the sequence information.