]>
diplodocus.org Git - nmh/blob - sbr/folder_read.c
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 "m_maildir.h"
12 /* We allocate the `mi' array 1024 elements at a time */
16 * 1) Create the folder/message structure
17 * 2) Read the directory (folder) and temporarily
18 * record the numbers of the messages we have seen.
19 * 3) Then allocate the array for message attributes and
20 * set the initial flags for all messages we've seen.
21 * 4) Read and initialize the sequence information.
25 folder_read (char *name
, int lockflag
)
34 name
= m_mailpath (name
);
35 if (!(dd
= opendir (name
))) {
40 /* Allocate the main structure for folder information */
42 clear_folder_flags (mp
);
54 if (access (name
, W_OK
) == -1)
58 * Allocate a temporary place to record the
59 * name of the messages in this folder.
62 mi
= mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
64 while ((dp
= readdir (dd
))) {
65 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
67 * Check if we need to allocate more
68 * temporary elements for message names.
70 if (mp
->nummsg
>= len
) {
72 mi
= mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
75 /* Check if this is the first message we've seen */
76 if (mp
->nummsg
== 0) {
80 /* Check if this is it the highest or lowest we've seen? */
81 if (msgnum
< mp
->lowmsg
)
83 if (msgnum
> mp
->hghmsg
)
88 * Now increment count, and record message
89 * number in a temporary place for now.
91 mi
[mp
->nummsg
++] = msgnum
;
94 switch (dp
->d_name
[0]) {
100 /* skip any files beginning with backup prefix */
101 if (has_prefix(dp
->d_name
, BACKUP_PREFIX
))
104 /* skip the LINK file */
105 if (!strcmp (dp
->d_name
, LINK
))
108 /* indicate that there are other files in folder */
109 set_other_files (mp
);
116 mp
->lowoff
= max (mp
->lowmsg
, 1);
118 /* Go ahead and allocate space for 100 additional messages. */
119 mp
->hghoff
= mp
->hghmsg
+ 100;
121 /* for testing, allocate minimal necessary space */
122 /* mp->hghoff = max (mp->hghmsg, 1); */
125 * If for some reason hghoff < lowoff (like we got an integer overflow)
126 * the complain about this now.
129 if (mp
->hghoff
< mp
->lowoff
) {
130 adios(NULL
, "Internal failure: high message limit < low message "
131 "limit; possible overflow?");
135 * Allocate space for status of each message.
137 mp
->num_msgstats
= MSGSTATNUM (mp
->lowoff
, mp
->hghoff
);
138 mp
->msgstats
= mh_xmalloc (MSGSTATSIZE(mp
));
139 for (i
= 0, v
= mp
->msgstats
; i
< mp
->num_msgstats
; ++i
, ++v
) {
143 mp
->msgattrs
= svector_create (0);
146 * Scan through the array of messages we've seen and
147 * setup the initial flags for those messages in the
148 * newly allocated mp->msgstats area.
150 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
151 set_exists (mp
, mi
[msgnum
]);
153 free (mi
); /* We don't need this anymore */
156 * Read and initialize the sequence information.
158 if (seq_read (mp
, lockflag
) == NOTOK
) {
159 char seqfile
[PATH_MAX
];
161 /* Failed to lock sequence file. */
162 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
163 advise (seqfile
, "failed to lock");