ze-filter  (ze-filter-0.8.0-develop-180218)
ze-cleanspool.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 #include <ze-sys.h>
26 
27 #include "ze-filter.h"
28 
29 
30 
31 /* ****************************************************************************
32  * *
33  * *
34  ******************************************************************************/
35 void
36 cleanup_spool(dirname, maxage)
37  char *dirname;
38  unsigned long maxage;
39 {
40  DIR *dir;
41  struct dirent *p;
42  struct stat st;
43  char fname[1024];
44  time_t now = time(NULL);
45  int nb = 0;
46 
47  if (dirname == NULL)
48  return;
49 
50  ZE_MessageInfo(9, "*** Cleaning up spool dir : %s", dirname);
51 
52  if (maxage == 0) {
53  ZE_LogMsgInfo(9, "MAX_AGE == 0 : no spool cleaning up");
54  return;
55  }
56 
57  if ((dir = opendir(dirname)) != NULL) {
58  while ((p = readdir(dir)) != NULL) {
59  if (p->d_name[0] == '.')
60  continue;
61  snprintf(fname, sizeof (fname), "%s/%s", dirname, p->d_name);
62 #if HAVE_LSTAT
63  if (lstat(fname, &st) == 0) {
64 #else
65  if (stat(fname, &st) == 0) {
66 #endif
67  if (S_ISREG(st.st_mode)) {
68  if (st.st_mtime + maxage < now) {
69  nb++;
70  ZE_MessageInfo(11, "* Deleting file : %s", fname);
71  unlink(fname);
72  }
73  }
74  } else {
75  ZE_LogMsgWarning(0, "lstat(%s) error", STRNULL(fname, ""));
76  }
77  }
78  closedir(dir);
79  } else {
80  ZE_LogMsgWarning(0, "opendir(%s) error", STRNULL(dirname, ""));
81  }
82 
83  ZE_MessageInfo(9, "*** %d files deleted", nb);
84 }
#define STRNULL(x, r)
Definition: macros.h:81
#define ZE_LogMsgInfo(level,...)
Definition: zeSyslog.h:110
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
int nb
Definition: ze-connopen.c:61
#define ZE_LogMsgWarning(level,...)
Definition: zeSyslog.h:112
void cleanup_spool(char *dirname, unsigned long maxage)
Definition: ze-cleanspool.c:36