s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
maildir.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include "prioq.h"
5#include "env.h"
6#include "stralloc.h"
7#include "direntry.h"
8#include "datetime.h"
9#include "now.h"
10#include "str.h"
11#include "maildir.h"
12#include "logmsg.h"
13
14#define WHO "maildir"
15
17{
18 char *maildir;
19 maildir = env_get("MAILDIR");
20 if (!maildir)
21 logmsg(WHO,111,ERROR,"MAILDIR not set");
22 if (chdir(maildir) == -1)
23 logmsg(WHO,110,FATAL,B("unable to chdir to: ",maildir));
24 return 0;
25}
26
27void maildir_clean(stralloc *tmpname)
28{
29 DIR *dir;
30 direntry *d;
31 datetime_sec time;
32 struct stat st;
33
34 time = now();
35
36 dir = opendir("tmp");
37 if (!dir) return;
38
39 while ((d = readdir(dir))) {
40 if (d->d_name[0] == '.') continue;
41 if (!stralloc_copys(tmpname,"tmp/")) break;
42 if (!stralloc_cats(tmpname,d->d_name)) break;
43 if (!stralloc_0(tmpname)) break;
44 if (stat(tmpname->s,&st) == 0)
45 if (time > st.st_atime + 129600)
46 unlink(tmpname->s);
47 }
48 closedir(dir);
49}
50
51static int append(prioq *pq, stralloc *filenames, char *subdir, datetime_sec time)
52{
53 DIR *dir;
54 direntry *d;
55 struct prioq_elt pe;
56 unsigned int pos;
57 struct stat st;
58
59 dir = opendir(subdir);
60 if (!dir)
61 logmsg(WHO,112,FATAL,B("unable to scan $MAILDIR/:",subdir));
62
63 while ((d = readdir(dir))) {
64 if (d->d_name[0] == '.') continue;
65 pos = filenames->len;
66 if (!stralloc_cats(filenames,subdir)) break;
67 if (!stralloc_cats(filenames,"/")) break;
68 if (!stralloc_cats(filenames,d->d_name)) break;
69 if (!stralloc_0(filenames)) break;
70 if (stat(filenames->s + pos,&st) == 0)
71 if (st.st_mtime < time) { /* don't want to mix up the order */
72 pe.dt = st.st_mtime;
73 pe.id = pos;
74 if (!prioq_insert(pq,&pe)) break;
75 }
76 }
77
78 closedir(dir);
79 if (d) logmsg(WHO,112,FATAL,B("unable to read $MAILDIR/:",subdir));
80 return 0;
81}
82
83int maildir_scan(prioq *pq, stralloc *filenames, int flagnew, int flagcur)
84{
85 struct prioq_elt pe;
86 datetime_sec time;
87
88 if (!stralloc_copys(filenames,"")) return 0;
89 while (prioq_min(pq,&pe))
91
92 time = now();
93
94 if (flagnew) if (append(pq,filenames,"new",time) == -1) return -1;
95 if (flagcur) if (append(pq,filenames,"cur",time) == -1) return -1;
96 return 0;
97}
long datetime_sec
Definition: datetime.h:15
int stralloc_copys(stralloc *, char const *)
stralloc filenames
Definition: maildir2mbox.c:23
prioq pq
Definition: maildir2mbox.c:24
int maildir_scan(prioq *pq, stralloc *filenames, int flagnew, int flagcur)
Definition: maildir.c:83
int maildir_chdir()
Definition: maildir.c:16
#define WHO
Definition: maildir.c:14
void maildir_clean(stralloc *tmpname)
Definition: maildir.c:27
datetime_sec now()
Definition: now.c:5
void prioq_delmin()
int prioq_min()
void maildir(char *fn)
Definition: qmail-local.c:176
struct del * d[CHANNELS]
Definition: qmail-send.c:720
Definition: prioq.h:7