]> diplodocus.org Git - nmh/blob - sbr/folder_read.c
Another pass at cleaning up (some of) the manpages.
[nmh] / sbr / folder_read.c
1
2 /*
3 * folder_read.c -- initialize folder structure and read folder
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /* We allocate the `mi' array 1024 elements at a time */
14 #define NUMMSGS 1024
15
16 /*
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.
23 */
24
25 struct msgs *
26 folder_read (char *name, int lockflag)
27 {
28 int msgnum, prefix_len, len, *mi;
29 struct msgs *mp;
30 struct dirent *dp;
31 DIR *dd;
32 bvector_t *v;
33 size_t i;
34
35 name = m_mailpath (name);
36 if (!(dd = opendir (name))) {
37 free (name);
38 return NULL;
39 }
40
41 /* Allocate the main structure for folder information */
42 mp = (struct msgs *) mh_xmalloc ((size_t) sizeof(*mp));
43
44 clear_folder_flags (mp);
45 mp->foldpath = name;
46 mp->lowmsg = 0;
47 mp->hghmsg = 0;
48 mp->curmsg = 0;
49 mp->lowsel = 0;
50 mp->hghsel = 0;
51 mp->numsel = 0;
52 mp->nummsg = 0;
53 mp->seqhandle = NULL;
54 mp->seqname = NULL;
55
56 if (access (name, W_OK) == -1)
57 set_readonly (mp);
58 prefix_len = strlen(BACKUP_PREFIX);
59
60 /*
61 * Allocate a temporary place to record the
62 * name of the messages in this folder.
63 */
64 len = NUMMSGS;
65 mi = (int *) mh_xmalloc ((size_t) (len * sizeof(*mi)));
66
67 while ((dp = readdir (dd))) {
68 if ((msgnum = m_atoi (dp->d_name)) && msgnum > 0) {
69 /*
70 * Check if we need to allocate more
71 * temporary elements for message names.
72 */
73 if (mp->nummsg >= len) {
74 len += NUMMSGS;
75 mi = (int *) mh_xrealloc (mi, (size_t) (len * sizeof(*mi)));
76 }
77
78 /* Check if this is the first message we've seen */
79 if (mp->nummsg == 0) {
80 mp->lowmsg = msgnum;
81 mp->hghmsg = msgnum;
82 } else {
83 /* Check if this is it the highest or lowest we've seen? */
84 if (msgnum < mp->lowmsg)
85 mp->lowmsg = msgnum;
86 if (msgnum > mp->hghmsg)
87 mp->hghmsg = msgnum;
88 }
89
90 /*
91 * Now increment count, and record message
92 * number in a temporary place for now.
93 */
94 mi[mp->nummsg++] = msgnum;
95
96 } else {
97 switch (dp->d_name[0]) {
98 case '.':
99 case ',':
100 continue;
101
102 default:
103 /* skip any files beginning with backup prefix */
104 if (!strncmp (dp->d_name, BACKUP_PREFIX, prefix_len))
105 continue;
106
107 /* skip the LINK file */
108 if (!strcmp (dp->d_name, LINK))
109 continue;
110
111 /* indicate that there are other files in folder */
112 set_other_files (mp);
113 continue;
114 }
115 }
116 }
117
118 closedir (dd);
119 mp->lowoff = max (mp->lowmsg, 1);
120
121 /* Go ahead and allocate space for 100 additional messages. */
122 mp->hghoff = mp->hghmsg + 100;
123
124 /* for testing, allocate minimal necessary space */
125 /* mp->hghoff = max (mp->hghmsg, 1); */
126
127 /*
128 * If for some reason hghoff < lowoff (like we got an integer overflow)
129 * the complain about this now.
130 */
131
132 if (mp->hghoff < mp->lowoff) {
133 adios(NULL, "Internal failure: high message limit < low message "
134 "limit; possible overflow?");
135 }
136
137 /*
138 * Allocate space for status of each message.
139 */
140 mp->num_msgstats = MSGSTATNUM (mp->lowoff, mp->hghoff);
141 mp->msgstats = mh_xmalloc (MSGSTATSIZE(mp));
142 for (i = 0, v = mp->msgstats; i < mp->num_msgstats; ++i, ++v) {
143 *v = bvector_create (0);
144 }
145
146 mp->msgattrs = svector_create (0);
147
148 /*
149 * Clear all the flag bits for all the message
150 * status entries we just allocated.
151 */
152 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
153 clear_msg_flags (mp, msgnum);
154
155 /*
156 * Scan through the array of messages we've seen and
157 * setup the initial flags for those messages in the
158 * newly allocated mp->msgstats area.
159 */
160 for (msgnum = 0; msgnum < mp->nummsg; msgnum++)
161 set_exists (mp, mi[msgnum]);
162
163 free (mi); /* We don't need this anymore */
164
165 /*
166 * Read and initialize the sequence information.
167 */
168 if (seq_read (mp, lockflag) == NOTOK) {
169 char seqfile[PATH_MAX];
170
171 /* Failed to lock sequence file. */
172 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
173 advise (seqfile, "failed to lock");
174
175 return NULL;
176 }
177
178 return mp;
179 }