]>
diplodocus.org Git - nmh/blob - sbr/folder_read.c
044059fbb0351c4e4f7fcfa672686abd7c038dfe
1 /* folder_read.c -- initialize folder structure and read folder
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.
10 #include "folder_read.h"
13 #include "m_maildir.h"
15 /* We allocate the `mi' array 1024 elements at a time */
19 * 1) Create the folder/message structure
20 * 2) Read the directory (folder) and temporarily
21 * record the numbers of the messages we have seen.
22 * 3) Then allocate the array for message attributes and
23 * set the initial flags for all messages we've seen.
24 * 4) Read and initialize the sequence information.
28 folder_read (char *name
, int lockflag
)
37 name
= m_mailpath (name
);
38 if (!(dd
= opendir (name
))) {
43 /* Allocate the main structure for folder information */
45 clear_folder_flags (mp
);
57 if (access (name
, W_OK
) == -1)
61 * Allocate a temporary place to record the
62 * name of the messages in this folder.
65 mi
= mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
67 while ((dp
= readdir (dd
))) {
68 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
70 * Check if we need to allocate more
71 * temporary elements for message names.
73 if (mp
->nummsg
>= len
) {
75 mi
= mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
78 /* Check if this is the first message we've seen */
79 if (mp
->nummsg
== 0) {
83 /* Check if this is it the highest or lowest we've seen? */
84 if (msgnum
< mp
->lowmsg
)
86 if (msgnum
> mp
->hghmsg
)
91 * Now increment count, and record message
92 * number in a temporary place for now.
94 mi
[mp
->nummsg
++] = msgnum
;
97 switch (dp
->d_name
[0]) {
103 /* skip any files beginning with backup prefix */
104 if (has_prefix(dp
->d_name
, BACKUP_PREFIX
))
107 /* skip the LINK file */
108 if (!strcmp (dp
->d_name
, LINK
))
111 /* indicate that there are other files in folder */
112 set_other_files (mp
);
119 mp
->lowoff
= max (mp
->lowmsg
, 1);
121 /* Go ahead and allocate space for 100 additional messages. */
122 mp
->hghoff
= mp
->hghmsg
+ 100;
124 /* for testing, allocate minimal necessary space */
125 /* mp->hghoff = max (mp->hghmsg, 1); */
128 * If for some reason hghoff < lowoff (like we got an integer overflow)
129 * the complain about this now.
132 if (mp
->hghoff
< mp
->lowoff
) {
133 die("Internal failure: high message limit < low message "
134 "limit; possible overflow?");
138 * Allocate space for status of each message.
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
) {
146 mp
->msgattrs
= svector_create (0);
149 * Scan through the array of messages we've seen and
150 * setup the initial flags for those messages in the
151 * newly allocated mp->msgstats area.
153 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
154 set_exists (mp
, mi
[msgnum
]);
156 free (mi
); /* We don't need this anymore */
159 * Read and initialize the sequence information.
161 if (seq_read (mp
, lockflag
) == NOTOK
) {
162 char seqfile
[PATH_MAX
];
164 /* Failed to lock sequence file. */
165 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
166 advise (seqfile
, "failed to lock");