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