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