Back to index

courier  0.68.2
maildirpurgetmp.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000-2003 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if HAVE_CONFIG_H
00007 #include "config.h"
00008 #endif
00009 #include "maildirquota.h"
00010 
00011 #include <sys/types.h>
00012 #if HAVE_DIRENT_H
00013 #include <dirent.h>
00014 #define NAMLEN(dirent) strlen((dirent)->d_name)
00015 #else
00016 #define dirent direct
00017 #define NAMLEN(dirent) (dirent)->d_namlen
00018 #if HAVE_SYS_NDIR_H
00019 #include <sys/ndir.h>
00020 #endif
00021 #if HAVE_SYS_DIR_H
00022 #include <sys/dir.h>
00023 #endif
00024 #if HAVE_NDIR_H
00025 #include <ndir.h>
00026 #endif
00027 #endif
00028 #include      <sys/types.h>
00029 #include      <sys/stat.h>
00030 #include      <string.h>
00031 #include      <stdlib.h>
00032 #include      <time.h>
00033 #if    HAVE_UNISTD_H
00034 #include      <unistd.h>
00035 #endif
00036 
00037 #include      "maildirmisc.h"
00038 
00039 static void dopurge(const char *m, unsigned nage,
00040                   int *nfiles,
00041                   long *nbytes)
00042 {
00043 time_t current_time;
00044 DIR    *dirp;
00045 struct dirent *de;
00046 
00047        time (&current_time);
00048        dirp=opendir(m);
00049        *nfiles=0;
00050        *nbytes=0;
00051 
00052        while (dirp && (de=readdir(dirp)) != 0)
00053        {
00054        char   *z;
00055        struct stat   stat_buf;
00056 
00057               if (de->d_name[0] == '.')   continue;
00058               z=malloc(strlen(m)+strlen(de->d_name)+2);
00059               if (!z)       continue;
00060 
00061               strcat(strcat(strcpy(z, m), "/"), de->d_name);
00062 
00063               if (stat(z, &stat_buf) == 0
00064                      && stat_buf.st_ctime < current_time - nage)
00065               {
00066                      if (maildirquota_countfile(de->d_name))
00067                      {
00068                             ++ *nfiles;
00069                             *nbytes += stat_buf.st_size;
00070                      }
00071                      unlink(z);
00072               }
00073               free(z);
00074        }
00075        if (dirp)     closedir(dirp);
00076 }
00077 
00078 void maildir_purgetmp(const char *maildir)
00079 {
00080        char   *m=malloc(strlen(maildir)+sizeof("/tmp"));
00081        int nfiles;
00082        long nbytes;
00083 
00084        if (!m)       return;
00085        strcat(strcpy(m, maildir), "/tmp");
00086        dopurge(m, 60 * 60 * 36, &nfiles, &nbytes);
00087        free(m);
00088 }
00089 
00090 void maildir_purge(const char *maildir, unsigned nage)
00091 {
00092        char   *m=malloc(strlen(maildir)+sizeof("/cur"));
00093        char   *p;
00094        int adjustquota;
00095        int nfiles;
00096        long nbytes;
00097 
00098        int nfiles2;
00099        long nbytes2;
00100 
00101        p=strrchr(maildir, '/');
00102        if (p)
00103               ++p;
00104        else
00105               p=".";
00106 
00107        adjustquota=maildirquota_countfolder(p);
00108 
00109        if (!m)       return;
00110        strcat(strcpy(m, maildir), "/cur");
00111        dopurge(m, nage, &nfiles, &nbytes);
00112        strcat(strcpy(m, maildir), "/new");
00113        dopurge(m, nage, &nfiles2, &nbytes2);
00114        free(m);
00115 
00116        nfiles += nfiles2;
00117        nbytes += nbytes2;
00118 
00119        if (adjustquota && nfiles > 0)
00120               maildir_quota_deleted(maildir, -nbytes, -nfiles);
00121 }