htcacheclean.c revision a059fab04e8584e8bfdcaf1103e2ec6f53f97a14
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* Licensed to the Apache Software Foundation (ASF) under one or more
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * contributor license agreements. See the NOTICE file distributed with
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * this work for additional information regarding copyright ownership.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * The ASF licenses this file to You under the Apache License, Version 2.0
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * (the "License"); you may not use this file except in compliance with
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * the License. You may obtain a copy of the License at
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * http://www.apache.org/licenses/LICENSE-2.0
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Unless required by applicable law or agreed to in writing, software
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * distributed under the License is distributed on an "AS IS" BASIS,
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek * See the License for the specific language governing permissions and
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * limitations under the License.
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * htcacheclean.c: simple program for cleaning of
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the disk cache of the Apache HTTP server
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek *
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * Contributed by Andreas Steinmetz <ast domdv.de>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * 8 Oct 2004
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr.h"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include "apr_lib.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_strings.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_file_io.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_file_info.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_pools.h"
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "apr_hash.h"
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek#include "apr_thread_proc.h"
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek#include "apr_signal.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_getopt.h"
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#include "apr_ring.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_date.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "apr_buckets.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "../modules/cache/mod_disk_cache.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#if APR_HAVE_UNISTD_H
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <unistd.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if APR_HAVE_STDLIB_H
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include <stdlib.h>
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#endif
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek/* define the following for debugging */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek#undef DEBUG
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Note: on Linux delays <= 2ms are busy waits without
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * scheduling, so never use a delay <= 2ms below
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#define NICE_DELAY 10000 /* usecs */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define DELETE_NICE 10 /* be nice after this amount of delete ops */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define STAT_ATTEMPTS 10 /* maximum stat attempts for a file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define STAT_DELAY 5000 /* usecs */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define HEADER 1 /* headers file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define DATA 2 /* body file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define TEMP 4 /* temporary file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define HEADERDATA (HEADER|DATA)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define MAXDEVIATION 3600 /* secs */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#define SECS_PER_MIN 60
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#define KBYTE 1024
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#define MBYTE 1048576
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define GBYTE 1073741824
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#define DIRINFO (APR_FINFO_MTIME|APR_FINFO_SIZE|APR_FINFO_TYPE|APR_FINFO_LINK)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozektypedef struct _direntry {
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek APR_RING_ENTRY(_direntry) link;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek int type; /* type of file/fileset: TEMP, HEADER, DATA, HEADERDATA */
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek apr_time_t htime; /* headers file modification time */
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek apr_time_t dtime; /* body file modification time */
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek apr_off_t hsize; /* headers file size */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_off_t dsize; /* body or temporary file size */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *basename; /* file/fileset base name */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher} DIRENTRY;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghertypedef struct _entry {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_RING_ENTRY(_entry) link;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_time_t expire; /* cache entry exiration time */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_time_t response_time; /* cache entry time of last response to client */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_time_t htime; /* headers file modification time */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_time_t dtime; /* body file modification time */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_off_t hsize; /* headers file size */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_off_t dsize; /* body or temporary file size */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *basename; /* fileset base name */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher} ENTRY;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int delcount; /* file deletion count for nice mode */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozekstatic int interrupted; /* flag: true if SIGINT or SIGTERM occurred */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic int realclean; /* flag: true means user said apache is not running */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int verbose; /* flag: true means print statistics */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherstatic int benice; /* flag: true means nice mode is activated */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int dryrun; /* flag: true means dry run, don't actually delete
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher anything */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int deldirs; /* flag: true means directories should be deleted */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int baselen; /* string length of the path to the proxy directory */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic apr_time_t now; /* start time of this processing run */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic apr_file_t *errfile; /* stderr file handle */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherstatic apr_off_t unsolicited; /* file size summary for deleted unsolicited
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher files */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic APR_RING_ENTRY(_entry) root; /* ENTRY ring anchor */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* short program name as called */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic const char *shortname = "htcacheclean";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#ifdef DEBUG
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * fake delete for debug purposes
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#define apr_file_remove fake_file_remove
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherstatic void fake_file_remove(char *pathname, apr_pool_t *p)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_finfo_t info;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* stat and printing to simulate some deletion system load and to
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher display what would actually have happened */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_stat(&info, pathname, DIRINFO, p);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_printf(errfile, "would delete %s" APR_EOL_STR, pathname);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher}
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * called on SIGINT or SIGTERM
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void setterm(int unused)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#ifdef DEBUG
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_file_printf(errfile, "interrupt" APR_EOL_STR);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher interrupted = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher * called in out of memory condition
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int oom(int unused)
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek{
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek static int called = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* be careful to call exit() only once */
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher if (!called) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher called = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(1);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return APR_ENOMEM;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * print purge statistics
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekstatic void printstats(apr_off_t total, apr_off_t sum, apr_off_t max,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_off_t etotal, apr_off_t entries)
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char ttype, stype, mtype, utype;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_off_t tfrag, sfrag, ufrag;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!verbose) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ttype = 'K';
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek tfrag = ((total * 10) / KBYTE) % 10;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek total /= KBYTE;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek if (total >= KBYTE) {
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek ttype = 'M';
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek tfrag = ((total * 10) / KBYTE) % 10;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher total /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher stype = 'K';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sfrag = ((sum * 10) / KBYTE) % 10;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sum /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (sum >= KBYTE) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher stype = 'M';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sfrag = ((sum * 10) / KBYTE) % 10;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sum /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mtype = 'K';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher max /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (max >= KBYTE) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mtype = 'M';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher max /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_file_printf(errfile, "Statistics:" APR_EOL_STR);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (unsolicited) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher utype = 'K';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ufrag = ((unsolicited * 10) / KBYTE) % 10;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsolicited /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (unsolicited >= KBYTE) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher utype = 'M';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ufrag = ((unsolicited * 10) / KBYTE) % 10;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsolicited /= KBYTE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (!unsolicited && !ufrag) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ufrag = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_printf(errfile, "unsolicited size %d.%d%c" APR_EOL_STR,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (int)(unsolicited), (int)(ufrag), utype);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_file_printf(errfile, "size limit %d.0%c" APR_EOL_STR,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (int)(max), mtype);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_printf(errfile, "total size was %d.%d%c, total size now "
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher "%d.%d%c" APR_EOL_STR,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (int)(total), (int)(tfrag), ttype, (int)(sum),
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (int)(sfrag), stype);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_printf(errfile, "total entries was %d, total entries now %d"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_EOL_STR, (int)(etotal), (int)(entries));
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher}
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * delete a single file
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic void delete_file(char *path, char *basename, apr_pool_t *pool)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek{
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek char *nextpath;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_t *p;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (dryrun) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* temp pool, otherwise lots of memory could be allocated */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_create(&p, pool);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek nextpath = apr_pstrcat(p, path, "/", basename, NULL);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_remove(nextpath, p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_destroy(p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (benice) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (++delcount >= DELETE_NICE) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_sleep(NICE_DELAY);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek delcount = 0;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek}
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek/*
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * delete cache file set
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekstatic void delete_entry(char *path, char *basename, apr_pool_t *pool)
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek{
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek char *nextpath;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_t *p;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (dryrun) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek /* temp pool, otherwise lots of memory could be allocated */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_create(&p, pool);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek nextpath = apr_pstrcat(p, path, "/", basename, CACHE_HEADER_SUFFIX, NULL);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_remove(nextpath, p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek nextpath = apr_pstrcat(p, path, "/", basename, CACHE_DATA_SUFFIX, NULL);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_remove(nextpath, p);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek apr_pool_destroy(p);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (benice) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek delcount += 2;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (delcount >= DELETE_NICE) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_sleep(NICE_DELAY);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek delcount = 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek}
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek/*
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * walk the cache directory tree
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozekstatic int process_dir(char *path, apr_pool_t *pool)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek{
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_dir_t *dir;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_pool_t *p;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_hash_t *h;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_hash_index_t *i;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_t *fd;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_status_t status;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_finfo_t info;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_size_t len;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_time_t current, deviation;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek char *nextpath, *base, *ext, *orig_basename;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek APR_RING_ENTRY(_direntry) anchor;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek DIRENTRY *d, *t, *n;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek ENTRY *e;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek int skip, retries;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek disk_cache_info_t disk_info;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek APR_RING_INIT(&anchor, _direntry, link);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_pool_create(&p, pool);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek h = apr_hash_make(p);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek fd = NULL;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek skip = 0;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek deviation = MAXDEVIATION * APR_USEC_PER_SEC;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (apr_dir_open(&dir, path, p) != APR_SUCCESS) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek return 1;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek while (apr_dir_read(&info, 0, dir) == APR_SUCCESS && !interrupted) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (!strcmp(info.name, ".") || !strcmp(info.name, "..")) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek continue;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek d = apr_pcalloc(p, sizeof(DIRENTRY));
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek d->basename = apr_pstrcat(p, path, "/", info.name, NULL);
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek APR_RING_INSERT_TAIL(&anchor, d, _direntry, link);
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_dir_close(dir);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (interrupted) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return 1;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek skip = baselen + 1;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek for (d = APR_RING_FIRST(&anchor);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek !interrupted && d != APR_RING_SENTINEL(&anchor, _direntry, link);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek d=n) {
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek n = APR_RING_NEXT(d, link);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek base = strrchr(d->basename, '/');
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (!base++) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek base = d->basename;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek ext = strchr(base, '.');
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek /* there may be temporary files which may be gone before
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek * processing, always skip these if not in realclean mode
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (!ext && !realclean) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek && strlen(base) == AP_TEMPFILE_NAMELEN) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek continue;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* this may look strange but apr_stat() may return errno which
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * is system dependent and there may be transient failures,
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * so just blindly retry for a short while
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek */
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek retries = STAT_ATTEMPTS;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek status = APR_SUCCESS;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek do {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (status != APR_SUCCESS) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_sleep(STAT_DELAY);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek status = apr_stat(&info, d->basename, DIRINFO, p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek } while (status != APR_SUCCESS && !interrupted && --retries);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek /* what may happen here is that apache did create a file which
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * we did detect but then does delete the file before we can
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * get file information, so if we don't get any file information
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek * we will ignore the file in this case
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (status != APR_SUCCESS) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (!realclean && !interrupted) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek continue;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek return 1;
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (info.filetype == APR_DIR) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher /* Make a copy of the basename, as process_dir modifies it */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher orig_basename = apr_pstrdup(pool, d->basename);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (process_dir(d->basename, pool)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* If asked to delete dirs, do so now. We don't care if it fails.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * If it fails, it likely means there was something else there.
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (deldirs && !dryrun) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher apr_dir_remove(orig_basename, pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher if (info.filetype != APR_REG) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!ext) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher && strlen(base) == AP_TEMPFILE_NAMELEN) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d->basename += skip;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher d->type = TEMP;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher d->dsize = info.size;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek continue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!strcasecmp(ext, CACHE_HEADER_SUFFIX)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *ext = '\0';
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek d->basename += skip;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* if a user manually creates a '.header' file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (d->basename[0] == '\0') {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (t) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d = t;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher d->type |= HEADER;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher d->htime = info.mtime;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher d->hsize = info.size;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek continue;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!strcasecmp(ext, CACHE_DATA_SUFFIX)) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek *ext = '\0';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d->basename += skip;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher /* if a user manually creates a '.data' file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (d->basename[0] == '\0') {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (t) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d = t;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher d->type |= DATA;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher d->dtime = info.mtime;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d->dsize = info.size;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (interrupted) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher path[baselen] = '\0';
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = apr_hash_first(p, h); i && !interrupted; i = apr_hash_next(i)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek void *hvalue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_uint32_t format;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_hash_this(i, NULL, NULL, &hvalue);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher d = hvalue;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek switch(d->type) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case HEADERDATA:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher nextpath = apr_pstrcat(p, path, "/", d->basename,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher CACHE_HEADER_SUFFIX, NULL);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (apr_file_open(&fd, nextpath, APR_FOPEN_READ | APR_FOPEN_BINARY,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_OS_DEFAULT, p) == APR_SUCCESS) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher len = sizeof(format);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (apr_file_read_full(fd, &format, len,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher &len) == APR_SUCCESS) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (format == DISK_FORMAT_VERSION) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_off_t offset = 0;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher apr_file_seek(fd, APR_SET, &offset);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = sizeof(disk_cache_info_t);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (apr_file_read_full(fd, &disk_info, len,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher &len) == APR_SUCCESS) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek apr_file_close(fd);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek e = apr_palloc(pool, sizeof(ENTRY));
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek APR_RING_INSERT_TAIL(&root, e, _entry, link);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher e->expire = disk_info.expire;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher e->response_time = disk_info.response_time;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher e->htime = d->htime;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek e->dtime = d->dtime;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher e->hsize = d->hsize;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher e->dsize = d->dsize;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher e->basename = apr_pstrdup(pool, d->basename);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek else {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_file_close(fd);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek else if (format == VARY_FORMAT_VERSION) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* This must be a URL that added Vary headers later,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * so kill the orphaned .data file
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_file_close(fd);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_file_remove(apr_pstrcat(p, path, "/", d->basename,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek CACHE_DATA_SUFFIX, NULL),
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek p);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek else {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek apr_file_close(fd);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* we have a somehow unreadable headers file which is associated
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * with a data file. this may be caused by apache currently
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * rewriting the headers file. thus we may delete the file set
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * either in realclean mode or if the headers file modification
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * timestamp is not within a specified positive or negative offset
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * to the current time.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek current = apr_time_now();
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (realclean || d->htime < current - deviation
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek || d->htime > current + deviation) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek delete_entry(path, d->basename, p);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek unsolicited += d->hsize;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek unsolicited += d->dsize;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek break;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek /* single data and header files may be deleted either in realclean
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * mode or if their modification timestamp is not within a
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * specified positive or negative offset to the current time.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * this handling is necessary due to possible race conditions
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * between apache and this process
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek case HEADER:
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek current = apr_time_now();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher nextpath = apr_pstrcat(p, path, "/", d->basename,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CACHE_HEADER_SUFFIX, NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (apr_file_open(&fd, nextpath, APR_FOPEN_READ | APR_FOPEN_BINARY,
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek APR_OS_DEFAULT, p) == APR_SUCCESS) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher len = sizeof(format);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (apr_file_read_full(fd, &format, len,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher &len) == APR_SUCCESS) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (format == VARY_FORMAT_VERSION) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_time_t expires;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek len = sizeof(expires);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher if (apr_file_read_full(fd, &expires, len,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher &len) == APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_close(fd);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (expires < current) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delete_entry(path, d->basename, p);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_file_close(fd);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (realclean || d->htime < current - deviation
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher || d->htime > current + deviation) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delete_entry(path, d->basename, p);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsolicited += d->hsize;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case DATA:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher current = apr_time_now();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (realclean || d->dtime < current - deviation
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher || d->dtime > current + deviation) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delete_entry(path, d->basename, p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek unsolicited += d->dsize;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* temp files may only be deleted in realclean mode which
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * is asserted above if a tempfile is in the hash array
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek case TEMP:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delete_file(path, d->basename, p);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher unsolicited += d->dsize;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (interrupted) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_pool_destroy(p);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (benice) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_sleep(NICE_DELAY);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (interrupted) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher * purge cache entries
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozekstatic void purge(char *path, apr_pool_t *pool, apr_off_t max)
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek{
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek apr_off_t sum, total, entries, etotal;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek ENTRY *e, *n, *oldest;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek sum = 0;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek entries = 0;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek for (e = APR_RING_FIRST(&root);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek e != APR_RING_SENTINEL(&root, _entry, link);
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek e = APR_RING_NEXT(e, link)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek sum += e->hsize;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek sum += e->dsize;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek entries++;
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek }
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek total = sum;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek etotal = entries;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (sum <= max) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek printstats(total, sum, max, etotal, entries);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek /* process all entries with a timestamp in the future, this may
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek * happen if a wrong system time is corrected
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek */
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek for (e = APR_RING_FIRST(&root);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek n = APR_RING_NEXT(e, link);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (e->response_time > now || e->htime > now || e->dtime > now) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek delete_entry(path, e->basename, pool);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek sum -= e->hsize;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek sum -= e->dsize;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek entries--;
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek APR_RING_REMOVE(e, link);
be5cc3c013ece0c957f2f8c28a217052227dfd07Jakub Hrozek if (sum <= max) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (!interrupted) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek printstats(total, sum, max, etotal, entries);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek e = n;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (interrupted) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek /* process all entries with are expired */
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek for (e = APR_RING_FIRST(&root);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek n = APR_RING_NEXT(e, link);
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (e->expire != APR_DATE_BAD && e->expire < now) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek delete_entry(path, e->basename, pool);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek sum -= e->hsize;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek sum -= e->dsize;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek entries--;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek APR_RING_REMOVE(e, link);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (sum <= max) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (!interrupted) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek printstats(total, sum, max, etotal, entries);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek return;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek e = n;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (interrupted) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek return;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek /* process remaining entries oldest to newest, the check for an emtpy
481ec0e1eb0058195732cb320845b41f6f4d43ebJakub Hrozek * ring actually isn't necessary except when the compiler does
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek * corrupt 64bit arithmetics which happend to me once, so better safe
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * than sorry
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek while (sum > max && !interrupted && !APR_RING_EMPTY(&root, _entry, link)) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek oldest = APR_RING_FIRST(&root);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek for (e = APR_RING_NEXT(oldest, link);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek e != APR_RING_SENTINEL(&root, _entry, link);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek e = APR_RING_NEXT(e, link)) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek if (e->dtime < oldest->dtime) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek oldest = e;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek delete_entry(path, oldest->basename, pool);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek sum -= oldest->hsize;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek sum -= oldest->dsize;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek entries--;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek APR_RING_REMOVE(oldest, link);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (!interrupted) {
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek printstats(total, sum, max, etotal, entries);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek/*
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek * usage info
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek */
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#define NL APR_EOL_STR
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozekstatic void usage(void)
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_printf(errfile,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "%s -- program for cleaning the disk cache." NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek "Usage: %s [-Dvtrn] -pPATH -lLIMIT" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " %s [-nti] -dINTERVAL -pPATH -lLIMIT" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "Options:" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -d Daemonize and repeat cache cleaning every INTERVAL minutes." NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek " This option is mutually exclusive with the -D, -v and -r" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " options." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -D Do a dry run and don't delete anything. This option is mutually" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " exclusive with the -d option." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -v Be verbose and print statistics. This option is mutually" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " exclusive with the -d option." NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -r Clean thoroughly. This assumes that the Apache web server is " NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " not running. This option is mutually exclusive with the -d" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " option and implies -t." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -n Be nice. This causes slower processing in favour of other" NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek " processes." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -t Delete all empty directories. By default only cache files are" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " removed, however with some configurations the large number of" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " directories created may require attention." NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -p Specify PATH as the root directory of the disk cache." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -l Specify LIMIT as the total disk cache size limit. Attach 'K'" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " or 'M' to the number for specifying KBytes or MBytes." NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " -i Be intelligent and run only when there was a modification of" NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek " the disk cache. This option is only possible together with the" NL
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek " -d option." NL,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek shortname,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek shortname,
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek shortname
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek );
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek exit(1);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek}
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek#undef NL
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek/*
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek * main
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek */
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozekint main(int argc, const char * const argv[])
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek{
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_off_t max;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_time_t current, repeat, delay, previous;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_status_t status;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek apr_pool_t *pool, *instance;
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek apr_getopt_t *o;
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek apr_finfo_t info;
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek int retries, isdaemon, limit_found, intelligent, dowork;
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek char opt;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek const char *arg;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek char *proxypath, *path;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek interrupted = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek repeat = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek isdaemon = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek dryrun = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek limit_found = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek max = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek verbose = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek realclean = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek benice = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek deldirs = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek intelligent = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek previous = 0; /* avoid compiler warning */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek proxypath = NULL;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek return 1;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek atexit(apr_terminate);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (argc) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek shortname = apr_filepath_name_get(argv[0]);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_pool_abort_set(oom, pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_open_stderr(&errfile, pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_signal(SIGINT, setterm);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_signal(SIGTERM, setterm);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_getopt_init(&o, pool, argc, argv);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = apr_getopt(o, "iDnvrtd:l:L:p:", &opt, &arg);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (status == APR_EOF) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (status != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher switch (opt) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 'i':
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (intelligent) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher intelligent = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek case 'D':
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (dryrun) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dryrun = 1;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 'n':
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (benice) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher benice = 1;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher break;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher case 't':
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (deldirs) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher usage();
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher deldirs = 1;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher break;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher case 'v':
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (verbose) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek verbose = 1;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 'r':
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (realclean) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek usage();
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher realclean = 1;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher deldirs = 1;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher case 'd':
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (isdaemon) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher usage();
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher isdaemon = 1;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher repeat = apr_atoi64(arg);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek repeat *= SECS_PER_MIN;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek repeat *= APR_USEC_PER_SEC;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek break;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek case 'l':
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (limit_found) {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek usage();
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek limit_found = 1;
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek do {
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek apr_status_t rv;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek char *end;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek rv = apr_strtoff(&max, arg, &end, 10);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (rv == APR_SUCCESS) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if ((*end == 'K' || *end == 'k') && !end[1]) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek max *= KBYTE;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek else if ((*end == 'M' || *end == 'm') && !end[1]) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek max *= MBYTE;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek else if ((*end == 'G' || *end == 'g') && !end[1]) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek max *= GBYTE;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else if (*end && /* neither empty nor [Bb] */
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek ((*end != 'B' && *end != 'b') || end[1])) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek rv = APR_EGENERAL;
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (rv != APR_SUCCESS) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek apr_file_printf(errfile, "Invalid limit: %s"
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek APR_EOL_STR APR_EOL_STR, arg);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek usage();
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek } while(0);
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek case 'p':
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek if (proxypath) {
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek usage();
5ee3fba0bd812242a1ffe189f5ddf2689e6e6811Jakub Hrozek }
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek proxypath = apr_pstrdup(pool, arg);
a23014d69b56cbdf48ad05229c334648b5309d8fJakub Hrozek if (apr_filepath_set(proxypath, pool) != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } /* switch */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } /* else */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } /* while */
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (o->ind != argc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (isdaemon && (repeat <= 0 || verbose || realclean || dryrun)) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!isdaemon && intelligent) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!proxypath || max <= 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (apr_filepath_get(&path, 0, pool) != APR_SUCCESS) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek usage();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher baselen = strlen(path);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#ifndef DEBUG
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (isdaemon) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_file_close(errfile);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_pool_create(&instance, pool);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher now = apr_time_now();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher APR_RING_INIT(&root, _entry, link);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delcount = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsolicited = 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek dowork = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher switch (intelligent) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 0:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dowork = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 1:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher retries = STAT_ATTEMPTS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = APR_SUCCESS;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (status != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_sleep(STAT_DELAY);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } while (status != APR_SUCCESS && !interrupted && --retries);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (status == APR_SUCCESS) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek previous = info.mtime;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher intelligent = 2;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dowork = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case 2:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher retries = STAT_ATTEMPTS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = APR_SUCCESS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (status != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_sleep(STAT_DELAY);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } while (status != APR_SUCCESS && !interrupted && --retries);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (status == APR_SUCCESS) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (previous != info.mtime) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dowork = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher previous = info.mtime;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek intelligent = 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek dowork = 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek break;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (dowork && !interrupted) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek if (!process_dir(path, instance) && !interrupted) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek purge(path, instance, max);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek else if (!isdaemon && !interrupted) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_file_printf(errfile, "An error occurred, cache cleaning "
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek "aborted." APR_EOL_STR);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return 1;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (intelligent && !interrupted) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek retries = STAT_ATTEMPTS;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek status = APR_SUCCESS;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (status != APR_SUCCESS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher apr_sleep(STAT_DELAY);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek } while (status != APR_SUCCESS && !interrupted && --retries);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek if (status == APR_SUCCESS) {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek previous = info.mtime;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek intelligent = 2;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek else {
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek intelligent = 1;
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek apr_pool_destroy(instance);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek current = apr_time_now();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (current < now) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delay = repeat;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek else if (current - now >= repeat) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delay = repeat;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher delay = now + repeat - current;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* we can't sleep the whole delay time here apiece as this is racy
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * with respect to interrupt delivery - think about what happens
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * if we have tested for an interrupt, then get scheduled
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek * before the apr_sleep() call and while waiting for the cpu
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * we do get an interrupt
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (isdaemon) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (delay && !interrupted) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (delay > APR_USEC_PER_SEC) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_sleep(APR_USEC_PER_SEC);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek delay -= APR_USEC_PER_SEC;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek else {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek apr_sleep(delay);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek delay = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } while (isdaemon && !interrupted);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (!isdaemon && interrupted) {
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek apr_file_printf(errfile, "Cache cleaning aborted due to user "
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek "request." APR_EOL_STR);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return 0;
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek}
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek