2 * Copyright (c) 1998 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: maildir.c,v 1.6 2001/09/10 11:56:53 joda Exp $");
39 make_path(POP *p, MsgInfoList *mp, int new, char *buf, size_t len)
41 snprintf(buf, len, "%s/%s%s%s", p->drop_name,
42 new ? "new" : "cur", mp ? "/" : "", mp ? mp->name : "");
46 scan_file(POP *p, MsgInfoList *mp)
48 char path[MAXPATHLEN];
53 make_path(p, mp, mp->flags & NEW_FLAG, path, sizeof(path));
60 "Failed to open message file `%s': %s",
61 path, strerror(errno));
63 return pop_msg (p, POP_FAILURE,
64 "Failed to open message file `%s'", path);
66 while(fgets(buf, sizeof(buf), f)) {
67 if(buf[strlen(buf) - 1] == '\n')
69 mp->length += strlen(buf);
72 if(strcmp(buf, "\n") == 0)
74 parse_header(mp, buf);
77 return add_missing_headers(p, mp);
81 scan_dir(POP *p, int new)
86 MsgInfoList *mp = p->mlp;
87 int n_mp = p->msg_count;
90 make_path(p, NULL, new, tmp, sizeof(tmp));
93 while((dent = readdir(dir)) != NULL) {
94 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
96 mp = realloc(mp, (n_mp + 1) * sizeof(*mp));
99 return pop_msg (p, POP_FAILURE,
100 "Can't build message list for '%s': Out of memory",
103 memset(mp + n_mp, 0, sizeof(*mp));
104 mp[n_mp].name = strdup(dent->d_name);
105 if(mp[n_mp].name == NULL) {
107 return pop_msg (p, POP_FAILURE,
108 "Can't build message list for '%s': Out of memory",
111 mp[n_mp].number = n_mp + 1;
114 mp[n_mp].flags |= NEW_FLAG;
115 e = scan_file(p, &mp[n_mp]);
118 p->drop_size += mp[n_mp].length;
128 pop_maildir_info(POP *p)
132 p->temp_drop[0] = '\0';
137 if(e != POP_SUCCESS) return e;
140 if(e != POP_SUCCESS) return e;
145 pop_maildir_update(POP *p)
148 char tmp1[MAXPATHLEN], tmp2[MAXPATHLEN];
149 for(i = 0; i < p->msg_count; i++) {
150 make_path(p, &p->mlp[i], p->mlp[i].flags & NEW_FLAG,
152 if(p->mlp[i].flags & DEL_FLAG) {
155 pop_log(p, POP_DEBUG, "Removing `%s'", tmp1);
157 if(unlink(tmp1) < 0) {
160 pop_log(p, POP_DEBUG, "Failed to remove `%s': %s",
161 tmp1, strerror(errno));
163 /* return failure? */
165 } else if((p->mlp[i].flags & NEW_FLAG) &&
166 (p->mlp[i].flags & RETR_FLAG)) {
167 make_path(p, &p->mlp[i], 0, tmp2, sizeof(tmp2));
170 pop_log(p, POP_DEBUG, "Linking `%s' to `%s'", tmp1, tmp2);
172 if(link(tmp1, tmp2) == 0) {
175 pop_log(p, POP_DEBUG, "Removing `%s'", tmp1);
177 if(unlink(tmp1) < 0) {
180 pop_log(p, POP_DEBUG, "Failed to remove `%s'", tmp1);
182 /* return failure? */
188 pop_log(p, POP_DEBUG, "Trying to rename `%s' to `%s'",
191 if(rename(tmp1, tmp2) < 0) {
194 pop_log(p, POP_DEBUG, "Failed to rename `%s' to `%s'",
206 pop_maildir_open(POP *p, MsgInfoList *mp)
208 char tmp[MAXPATHLEN];
209 make_path(p, mp, mp->flags & NEW_FLAG, tmp, sizeof(tmp));
212 p->drop = fopen(tmp, "r");
214 return pop_msg(p, POP_FAILURE, "Failed to open message file");