]>
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 /* We allocate the `mi' array 1024 elements at a time */
15 * 1) Create the folder/message structure
16 * 2) Read the directory (folder) and temporarily
17 * record the numbers of the messages we have seen.
18 * 3) Then allocate the array for message attributes and
19 * set the initial flags for all messages we've seen.
20 * 4) Read and initialize the sequence information.
24 folder_read (char *name
, int lockflag
)
33 name
= m_mailpath (name
);
34 if (!(dd
= opendir (name
))) {
39 /* Allocate the main structure for folder information */
41 clear_folder_flags (mp
);
53 if (access (name
, W_OK
) == -1)
57 * Allocate a temporary place to record the
58 * name of the messages in this folder.
61 mi
= (int *) mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
63 while ((dp
= readdir (dd
))) {
64 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
66 * Check if we need to allocate more
67 * temporary elements for message names.
69 if (mp
->nummsg
>= len
) {
71 mi
= (int *) mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
74 /* Check if this is the first message we've seen */
75 if (mp
->nummsg
== 0) {
79 /* Check if this is it the highest or lowest we've seen? */
80 if (msgnum
< mp
->lowmsg
)
82 if (msgnum
> mp
->hghmsg
)
87 * Now increment count, and record message
88 * number in a temporary place for now.
90 mi
[mp
->nummsg
++] = msgnum
;
93 switch (dp
->d_name
[0]) {
99 /* skip any files beginning with backup prefix */
100 if (has_prefix(dp
->d_name
, BACKUP_PREFIX
))
103 /* skip the LINK file */
104 if (!strcmp (dp
->d_name
, LINK
))
107 /* indicate that there are other files in folder */
108 set_other_files (mp
);
115 mp
->lowoff
= max (mp
->lowmsg
, 1);
117 /* Go ahead and allocate space for 100 additional messages. */
118 mp
->hghoff
= mp
->hghmsg
+ 100;
120 /* for testing, allocate minimal necessary space */
121 /* mp->hghoff = max (mp->hghmsg, 1); */
124 * If for some reason hghoff < lowoff (like we got an integer overflow)
125 * the complain about this now.
128 if (mp
->hghoff
< mp
->lowoff
) {
129 adios(NULL
, "Internal failure: high message limit < low message "
130 "limit; possible overflow?");
134 * Allocate space for status of each message.
136 mp
->num_msgstats
= MSGSTATNUM (mp
->lowoff
, mp
->hghoff
);
137 mp
->msgstats
= mh_xmalloc (MSGSTATSIZE(mp
));
138 for (i
= 0, v
= mp
->msgstats
; i
< mp
->num_msgstats
; ++i
, ++v
) {
142 mp
->msgattrs
= svector_create (0);
145 * Scan through the array of messages we've seen and
146 * setup the initial flags for those messages in the
147 * newly allocated mp->msgstats area.
149 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
150 set_exists (mp
, mi
[msgnum
]);
152 free (mi
); /* We don't need this anymore */
155 * Read and initialize the sequence information.
157 if (seq_read (mp
, lockflag
) == NOTOK
) {
158 char seqfile
[PATH_MAX
];
160 /* Failed to lock sequence file. */
161 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
162 advise (seqfile
, "failed to lock");