No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / popper / maildir.c
blobb2068d3ef5c20add4292fab89bb5b42764c61674
1 /*
2 * Copyright (c) 1998 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include <popper.h>
35 #include <dirent.h>
36 __RCSID("$Heimdal: maildir.c 10678 2001-09-10 11:56:53Z joda $"
37 "$NetBSD$");
39 static void
40 make_path(POP *p, MsgInfoList *mp, int new, char *buf, size_t len)
42 snprintf(buf, len, "%s/%s%s%s", p->drop_name,
43 new ? "new" : "cur", mp ? "/" : "", mp ? mp->name : "");
46 static int
47 scan_file(POP *p, MsgInfoList *mp)
49 char path[MAXPATHLEN];
50 FILE *f;
51 char buf[1024];
52 int eoh = 0;
54 make_path(p, mp, mp->flags & NEW_FLAG, path, sizeof(path));
55 f = fopen(path, "r");
57 if(f == NULL) {
58 #ifdef DEBUG
59 if(p->debug)
60 pop_log(p, POP_DEBUG,
61 "Failed to open message file `%s': %s",
62 path, strerror(errno));
63 #endif
64 return pop_msg (p, POP_FAILURE,
65 "Failed to open message file `%s'", path);
67 while(fgets(buf, sizeof(buf), f)) {
68 if(buf[strlen(buf) - 1] == '\n')
69 mp->lines++;
70 mp->length += strlen(buf);
71 if(eoh)
72 continue;
73 if(strcmp(buf, "\n") == 0)
74 eoh = 1;
75 parse_header(mp, buf);
77 fclose(f);
78 return add_missing_headers(p, mp);
81 static int
82 scan_dir(POP *p, int new)
84 char tmp[MAXPATHLEN];
85 DIR *dir;
86 struct dirent *dent;
87 MsgInfoList *mp = p->mlp;
88 int n_mp = p->msg_count;
89 int e;
91 make_path(p, NULL, new, tmp, sizeof(tmp));
92 mkdir(tmp, 0700);
93 dir = opendir(tmp);
94 while((dent = readdir(dir)) != NULL) {
95 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
96 continue;
97 mp = realloc(mp, (n_mp + 1) * sizeof(*mp));
98 if(mp == NULL) {
99 p->msg_count = 0;
100 return pop_msg (p, POP_FAILURE,
101 "Can't build message list for '%s': Out of memory",
102 p->user);
104 memset(mp + n_mp, 0, sizeof(*mp));
105 mp[n_mp].name = strdup(dent->d_name);
106 if(mp[n_mp].name == NULL) {
107 p->msg_count = 0;
108 return pop_msg (p, POP_FAILURE,
109 "Can't build message list for '%s': Out of memory",
110 p->user);
112 mp[n_mp].number = n_mp + 1;
113 mp[n_mp].flags = 0;
114 if(new)
115 mp[n_mp].flags |= NEW_FLAG;
116 e = scan_file(p, &mp[n_mp]);
117 if(e != POP_SUCCESS)
118 return e;
119 p->drop_size += mp[n_mp].length;
120 n_mp++;
122 closedir(dir);
123 p->mlp = mp;
124 p->msg_count = n_mp;
125 return POP_SUCCESS;
129 pop_maildir_info(POP *p)
131 int e;
133 p->temp_drop[0] = '\0';
134 p->mlp = NULL;
135 p->msg_count = 0;
137 e = scan_dir(p, 0);
138 if(e != POP_SUCCESS) return e;
140 e = scan_dir(p, 1);
141 if(e != POP_SUCCESS) return e;
142 return POP_SUCCESS;
146 pop_maildir_update(POP *p)
148 int i;
149 char tmp1[MAXPATHLEN], tmp2[MAXPATHLEN];
150 for(i = 0; i < p->msg_count; i++) {
151 make_path(p, &p->mlp[i], p->mlp[i].flags & NEW_FLAG,
152 tmp1, sizeof(tmp1));
153 if(p->mlp[i].flags & DEL_FLAG) {
154 #ifdef DEBUG
155 if(p->debug)
156 pop_log(p, POP_DEBUG, "Removing `%s'", tmp1);
157 #endif
158 if(unlink(tmp1) < 0) {
159 #ifdef DEBUG
160 if(p->debug)
161 pop_log(p, POP_DEBUG, "Failed to remove `%s': %s",
162 tmp1, strerror(errno));
163 #endif
164 /* return failure? */
166 } else if((p->mlp[i].flags & NEW_FLAG) &&
167 (p->mlp[i].flags & RETR_FLAG)) {
168 make_path(p, &p->mlp[i], 0, tmp2, sizeof(tmp2));
169 #ifdef DEBUG
170 if(p->debug)
171 pop_log(p, POP_DEBUG, "Linking `%s' to `%s'", tmp1, tmp2);
172 #endif
173 if(link(tmp1, tmp2) == 0) {
174 #ifdef DEBUG
175 if(p->debug)
176 pop_log(p, POP_DEBUG, "Removing `%s'", tmp1);
177 #endif
178 if(unlink(tmp1) < 0) {
179 #ifdef DEBUG
180 if(p->debug)
181 pop_log(p, POP_DEBUG, "Failed to remove `%s'", tmp1);
182 #endif
183 /* return failure? */
185 } else {
186 if(errno == EXDEV) {
187 #ifdef DEBUG
188 if(p->debug)
189 pop_log(p, POP_DEBUG, "Trying to rename `%s' to `%s'",
190 tmp1, tmp2);
191 #endif
192 if(rename(tmp1, tmp2) < 0) {
193 #ifdef DEBUG
194 if(p->debug)
195 pop_log(p, POP_DEBUG, "Failed to rename `%s' to `%s'",
196 tmp1, tmp2);
197 #endif
203 return(pop_quit(p));
207 pop_maildir_open(POP *p, MsgInfoList *mp)
209 char tmp[MAXPATHLEN];
210 make_path(p, mp, mp->flags & NEW_FLAG, tmp, sizeof(tmp));
211 if(p->drop)
212 fclose(p->drop);
213 p->drop = fopen(tmp, "r");
214 if(p->drop == NULL)
215 return pop_msg(p, POP_FAILURE, "Failed to open message file");
216 return POP_SUCCESS;