1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include "maildir-bulletin.h"
#include "mb_util.h"
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string>
using namespace std;
int expire_file(CPCCHAR file, mb_log &log)
{
struct stat buf;
if(stat(file, &buf))
{
log.error("Can't stat file \"%s\"", file);
return 1;
}
ino_t inode = buf.st_ino;
setpwent();
passwd *pw;
int count = 0;
while( (pw = getpwent()) )
{
if(pw->pw_uid > MIN_USER_ID)
{
string newDir = pw->pw_dir;
newDir += "/Maildir/new";
DIR *d = opendir(newDir.c_str());
if(!d)
{
log.error("Can't open directory \"%s\"", newDir.c_str());
}
else
{
dirent *dent;
while( (dent = readdir(d)) )
{
string fileName(newDir);
fileName += "/";
fileName += dent->d_name;
if(stat(fileName.c_str(), &buf))
{
log.error("Can't stat file \"%s\"", fileName.c_str());
return 1;
}
if(buf.st_ino == inode)
{
if(unlink(fileName.c_str()))
{
log.error("Can't unlink file \"%s\"", fileName.c_str());
return 1;
}
count++;
}
}
closedir(d);
}
}
}
endpwent();
if(count)
{
char bCount[9];
sprintf(bCount, "%d", count);
log.info("Deleted %s links to bulletin \"%s\"", bCount, file);
}
return 0;
}
int main(int argc, char **argv)
{
time_t t = time(NULL);
struct tm *loc = NULL;
loc = localtime(&t);
mb_log log(LOG_FILE, ERROR_FILE, loc);
int i;
if(argc > 1)
{
log.interactive(true);
for(i = 1; i < argc; i++)
{
expire_file(argv[i], log);
}
return 0;
}
DIR *d = opendir(BULLETINSDIR);
if(!d)
{
log.error("Can't open directory \"" BULLETINSDIR "\"", NULL, NULL, true);
return 1;
}
dirent *dent;
while( (dent = readdir(d)) )
{
int sepCount = 0;
for(i = 0; dent->d_name[i] && sepCount < 5; i++)
{
if(dent->d_name[i] == ':')
sepCount++;
}
int year, mon, day;
if(3 == sscanf(&(dent->d_name[i]), "%d-%d-%d", &year, &mon, &day) )
{
year -= 1900;
mon--;
if(loc->tm_year > year
|| (loc->tm_year == year && (loc->tm_mon > mon
|| (loc->tm_mon == mon && loc->tm_mday > day)
) ) )
{
string name(BULLETINSDIR);
name += "/";
name += dent->d_name;
expire_file(name.c_str(), log);
string newName(BULLETINSDIR);
newName += "/removed/";
newName += dent->d_name;
if(rename(name.c_str(), newName.c_str()) )
{
log.error("Can't rename \"%s\" to \"%s\".", name.c_str()
, newName.c_str(), true);
return 1;
}
}
}
}
return 0;
}
|