1
/* $Id: prune.cpp,v 1.13 2004/08/24 16:21:12 terpstra Exp $
3
* prune.cpp - Prune obsolete / stale cache files
5
* Copyright (C) 2002 - Wesley W. Terpstra
9
* Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; version 2.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#define _XOPEN_SOURCE 500
26
#define _FILE_OFFSET_BITS 64
28
#include <ConfigFile.h>
31
#include <sys/types.h>
49
void help(const char* name)
51
cerr << "Lurker-prune (v" << VERSION << ") prunes the web-server cache.\n";
53
cerr << "Usage: " << name << " -c <config-file> -d <docroot> [-m <d> -a <d> -p -v]\n";
55
cerr << "\t-c <config-file> Use this config file for lurker settings\n";
56
cerr << "\t-d <docroot> The base directory of the lurker cache\n";
57
cerr << "\t-m <days> Keep cached files for at most this many days [7]\n";
58
cerr << "\t-a <days> Kill cached files not accessed for this many days [1]\n";
59
cerr << "\t-p Purge everything even if it appears to not be expired\n";
60
cerr << "\t-v Verbose operation\n";
62
cerr << "Prune obsolete or stale html/xml from the web-server accessible cache.\n";
63
cerr << "This command should be run at regular intervals from cron for each site.\n";
67
int main(int argc, char** argv)
71
const char* config = 0;
72
const char* docroot = 0;
75
time_t modifyTime = 60*60*24*7;
76
time_t accessTime = 60*60*24*1;
80
while ((c = getopt(argc, (char*const*)argv, "c:d:m:a:vp?")) != -1)
91
modifyTime = atol(optarg)*60*60*24;
99
accessTime = atol(optarg)*60*60*24;
118
if (!config || !docroot || optind < argc)
125
if (cfg.load(config) != 0)
127
cerr << cfg.getError() << flush;
131
string docfile = string(docroot) + "/lurker.docroot";
132
int fd = open(docfile.c_str(), O_RDWR | O_CREAT, 0666);
135
cerr << "open()ing " << docfile << ": " << strerror(errno) << endl;
140
if (fstat(fd, &dbuf) < 0)
142
cerr << "stat()ing " << docfile << ": " << strerror(errno) << endl;
146
enum LockState { GOT, FAIL, USED } state = GOT;
149
if (flock(fd, LOCK_EX|LOCK_NB) != 0)
151
if (errno == EWOULDBLOCK)
158
memset(&lock, 0, sizeof(lock));
159
lock.l_type = F_WRLCK;
160
lock.l_whence = SEEK_SET;
161
if (fcntl(fd, F_SETLK, &lock) != 0)
163
if (errno == EACCES || errno == EAGAIN)
172
if (verbose) cout << "Already pruning this docroot" << endl;
178
cerr << "Locking " << docfile << " failed: " << strerror(errno) << endl;
182
std::auto_ptr<ESort::Reader> db(ESort::Reader::opendb(cfg.dbdir + "/db"));
185
cerr << "Opening database: " << strerror(errno) << endl;
189
time_t beginfix = time(0);
191
if (chdir(docroot) != 0)
193
cerr << "chdir: " << docroot << ": " << strerror(errno) << endl;
197
PTable ptable(cfg, db.get(), dbuf.st_mtime, purge, verbose, modifyTime, accessTime);
200
if ((ok = ptable.load()) != "")
202
cerr << "load: " << ok << endl;
206
if ((ok = ptable.calc()) != "")
208
cerr << "calc: " << ok << endl;
212
if ((ok = ptable.kill()) != "")
214
cerr << "kill: " << ok << endl;
218
// set the mtime stamp to beginfix
219
struct utimbuf touch;
220
touch.actime = touch.modtime = beginfix;
221
if (utime("lurker.docroot", &touch) < 0)
223
cerr << "touching " << docfile << ": " << strerror(errno) << endl;