]>
diplodocus.org Git - nmh/blob - sbr/folder_read.c
3 * folder_read.c -- initialize folder structure and read folder
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.
13 /* We allocate the `mi' array 1024 elements at a time */
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.
26 folder_read (char *name
, int lockflag
)
35 name
= m_mailpath (name
);
36 if (!(dd
= opendir (name
))) {
41 /* Allocate the main structure for folder information */
43 clear_folder_flags (mp
);
55 if (access (name
, W_OK
) == -1)
59 * Allocate a temporary place to record the
60 * name of the messages in this folder.
63 mi
= (int *) mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
65 while ((dp
= readdir (dd
))) {
66 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
68 * Check if we need to allocate more
69 * temporary elements for message names.
71 if (mp
->nummsg
>= len
) {
73 mi
= (int *) mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
76 /* Check if this is the first message we've seen */
77 if (mp
->nummsg
== 0) {
81 /* Check if this is it the highest or lowest we've seen? */
82 if (msgnum
< mp
->lowmsg
)
84 if (msgnum
> mp
->hghmsg
)
89 * Now increment count, and record message
90 * number in a temporary place for now.
92 mi
[mp
->nummsg
++] = msgnum
;
95 switch (dp
->d_name
[0]) {
101 /* skip any files beginning with backup prefix */
102 if (HasPrefix(dp
->d_name
, BACKUP_PREFIX
))
105 /* skip the LINK file */
106 if (!strcmp (dp
->d_name
, LINK
))
109 /* indicate that there are other files in folder */
110 set_other_files (mp
);
117 mp
->lowoff
= max (mp
->lowmsg
, 1);
119 /* Go ahead and allocate space for 100 additional messages. */
120 mp
->hghoff
= mp
->hghmsg
+ 100;
122 /* for testing, allocate minimal necessary space */
123 /* mp->hghoff = max (mp->hghmsg, 1); */
126 * If for some reason hghoff < lowoff (like we got an integer overflow)
127 * the complain about this now.
130 if (mp
->hghoff
< mp
->lowoff
) {
131 adios(NULL
, "Internal failure: high message limit < low message "
132 "limit; possible overflow?");
136 * Allocate space for status of each message.
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 *v
= bvector_create (0);
144 mp
->msgattrs
= svector_create (0);
147 * Clear all the flag bits for all the message
148 * status entries we just allocated.
150 for (msgnum
= mp
->lowoff
; msgnum
<= mp
->hghoff
; msgnum
++)
151 clear_msg_flags (mp
, msgnum
);
154 * Scan through the array of messages we've seen and
155 * setup the initial flags for those messages in the
156 * newly allocated mp->msgstats area.
158 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
159 set_exists (mp
, mi
[msgnum
]);
161 free (mi
); /* We don't need this anymore */
164 * Read and initialize the sequence information.
166 if (seq_read (mp
, lockflag
) == NOTOK
) {
167 char seqfile
[PATH_MAX
];
169 /* Failed to lock sequence file. */
170 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
171 advise (seqfile
, "failed to lock");