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