]>
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
)
28 int msgnum
, prefix_len
, len
, *mi
;
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)
57 prefix_len
= strlen(BACKUP_PREFIX
);
60 * Allocate a temporary place to record the
61 * name of the messages in this folder.
64 mi
= (int *) mh_xmalloc ((size_t) (len
* sizeof(*mi
)));
66 while ((dp
= readdir (dd
))) {
67 if ((msgnum
= m_atoi (dp
->d_name
)) && msgnum
> 0) {
69 * Check if we need to allocate more
70 * temporary elements for message names.
72 if (mp
->nummsg
>= len
) {
74 mi
= (int *) mh_xrealloc (mi
, (size_t) (len
* sizeof(*mi
)));
77 /* Check if this is the first message we've seen */
78 if (mp
->nummsg
== 0) {
82 /* Check if this is it the highest or lowest we've seen? */
83 if (msgnum
< mp
->lowmsg
)
85 if (msgnum
> mp
->hghmsg
)
90 * Now increment count, and record message
91 * number in a temporary place for now.
93 mi
[mp
->nummsg
++] = msgnum
;
96 switch (dp
->d_name
[0]) {
102 /* skip any files beginning with backup prefix */
103 if (!strncmp (dp
->d_name
, BACKUP_PREFIX
, prefix_len
))
106 /* skip the LINK file */
107 if (!strcmp (dp
->d_name
, LINK
))
110 /* indicate that there are other files in folder */
111 set_other_files (mp
);
118 mp
->lowoff
= max (mp
->lowmsg
, 1);
120 /* Go ahead and allocate space for 100 additional messages. */
121 mp
->hghoff
= mp
->hghmsg
+ 100;
123 /* for testing, allocate minimal necessary space */
124 /* mp->hghoff = max (mp->hghmsg, 1); */
127 * If for some reason hghoff < lowoff (like we got an integer overflow)
128 * the complain about this now.
131 if (mp
->hghoff
< mp
->lowoff
) {
132 adios(NULL
, "Internal failure: high message limit < low message "
133 "limit; possible overflow?");
137 * Allocate space for status of each message.
139 mp
->num_msgstats
= MSGSTATNUM (mp
->lowoff
, mp
->hghoff
);
140 mp
->msgstats
= mh_xmalloc (MSGSTATSIZE(mp
));
141 for (i
= 0, v
= mp
->msgstats
; i
< mp
->num_msgstats
; ++i
, ++v
) {
142 *v
= bvector_create (0);
145 mp
->msgattrs
= svector_create (0);
148 * Clear all the flag bits for all the message
149 * status entries we just allocated.
151 for (msgnum
= mp
->lowoff
; msgnum
<= mp
->hghoff
; msgnum
++)
152 clear_msg_flags (mp
, msgnum
);
155 * Scan through the array of messages we've seen and
156 * setup the initial flags for those messages in the
157 * newly allocated mp->msgstats area.
159 for (msgnum
= 0; msgnum
< mp
->nummsg
; msgnum
++)
160 set_exists (mp
, mi
[msgnum
]);
162 free (mi
); /* We don't need this anymore */
165 * Read and initialize the sequence information.
167 if (seq_read (mp
, lockflag
) == NOTOK
) {
168 char seqfile
[PATH_MAX
];
170 /* Failed to lock sequence file. */
171 snprintf (seqfile
, sizeof(seqfile
), "%s/%s", mp
->foldpath
, mh_seq
);
172 advise (seqfile
, "failed to lock");