]>
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.
11 #include "folder_read.h"
14 #include "m_maildir.h"
16 /* We allocate the `mi' array 1024 elements at a time */
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.
29 folder_read (char *name
, int lockflag
)
38 name
= m_mailpath (name
);
39 if (!(dd
= opendir (name
))) {
44 /* Allocate the main structure for folder information */
46 clear_folder_flags (mp
);
58 if (access (name
, W_OK
) == -1)
62 * Allocate a temporary place to record the
63 * name of the messages in this folder.
66 mi
= mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
68 while ((dp
= readdir (dd
))) {
69 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
71 * Check if we need to allocate more
72 * temporary elements for message names.
74 if (mp
->nummsg
>= len
) {
76 mi
= mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
79 /* Check if this is the first message we've seen */
80 if (mp
->nummsg
== 0) {
84 /* Check if this is it the highest or lowest we've seen? */
85 if (msgnum
< mp
->lowmsg
)
87 if (msgnum
> mp
->hghmsg
)
92 * Now increment count, and record message
93 * number in a temporary place for now.
95 mi
[mp
->nummsg
++] = msgnum
;
98 switch (dp
->d_name
[0]) {
104 /* skip any files beginning with backup prefix */
105 if (has_prefix(dp
->d_name
, BACKUP_PREFIX
))
108 /* skip the LINK file */
109 if (!strcmp (dp
->d_name
, LINK
))
112 /* indicate that there are other files in folder */
113 set_other_files (mp
);
120 mp
->lowoff
= max (mp
->lowmsg
, 1);
122 /* Go ahead and allocate space for 100 additional messages. */
123 mp
->hghoff
= mp
->hghmsg
+ 100;
125 /* for testing, allocate minimal necessary space */
126 /* mp->hghoff = max (mp->hghmsg, 1); */
129 * If for some reason hghoff < lowoff (like we got an integer overflow)
130 * the complain about this now.
133 if (mp
->hghoff
< mp
->lowoff
) {
134 die("Internal failure: high message limit < low message "
135 "limit; possible overflow?");
139 * Allocate space for status of each message.
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
) {
147 mp
->msgattrs
= svector_create (0);
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.
154 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
155 set_exists (mp
, mi
[msgnum
]);
157 free (mi
); /* We don't need this anymore */
160 * Read and initialize the sequence information.
162 if (seq_read (mp
, lockflag
) == NOTOK
) {
163 char seqfile
[PATH_MAX
];
165 /* Failed to lock sequence file. */
166 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
167 advise (seqfile
, "failed to lock");