842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * http://www.apache.org/licenses/LICENSE-2.0
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Unless required by applicable law or agreed to in writing, software
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * distributed under the License is distributed on an "AS IS" BASIS,
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * See the License for the specific language governing permissions and
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * limitations under the License.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * htcacheclean.c: simple program for cleaning of
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * the disk cache of the Apache HTTP server
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz *
2d728410bcf9e6e53698bb035f516e18efd76d8bnd * Contributed by Andreas Steinmetz <ast domdv.de>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * 8 Oct 2004
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_lib.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_strings.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_file_io.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_file_info.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_pools.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_hash.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_thread_proc.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_signal.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_getopt.h"
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin#include "apr_md5.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_ring.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include "apr_date.h"
806b50b4d37ade77f1c767a1593fac5a2d7786c8minfrin#include "apr_buckets.h"
c07866e582be4daac7f6a0a8754fa12fca044663fuankg
07ed2f9227b98d2cac21a7121ef7ea5328fa3f98fuankg#include "../modules/cache/cache_common.h"
15ff8c621815e8337abc10638f2b2853ee6fd076minfrin#include "../modules/cache/cache_disk_common.h"
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAVE_UNISTD_H
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <unistd.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#if APR_HAVE_STDLIB_H
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#include <stdlib.h>
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/* define the following for debugging */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#undef DEBUG
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * Note: on Linux delays <= 2ms are busy waits without
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * scheduling, so never use a delay <= 2ms below
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define NICE_DELAY 10000 /* usecs */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define DELETE_NICE 10 /* be nice after this amount of delete ops */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define STAT_ATTEMPTS 10 /* maximum stat attempts for a file */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define STAT_DELAY 5000 /* usecs */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define HEADER 1 /* headers file */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define DATA 2 /* body file */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define TEMP 4 /* temporary file */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define HEADERDATA (HEADER|DATA)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define MAXDEVIATION 3600 /* secs */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define SECS_PER_MIN 60
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define KBYTE 1024
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define MBYTE 1048576
15351ab1a34130c1f880daef4e5dc15407a75969colm#define GBYTE 1073741824
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define DIRINFO (APR_FINFO_MTIME|APR_FINFO_SIZE|APR_FINFO_TYPE|APR_FINFO_LINK)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndtypedef struct _direntry {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_ENTRY(_direntry) link;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int type; /* type of file/fileset: TEMP, HEADER, DATA, HEADERDATA */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t htime; /* headers file modification time */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t dtime; /* body file modification time */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_off_t hsize; /* headers file size */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_off_t dsize; /* body or temporary file size */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd char *basename; /* file/fileset base name */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz} DIRENTRY;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndtypedef struct _entry {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_ENTRY(_entry) link;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t expire; /* cache entry exiration time */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t response_time; /* cache entry time of last response to client */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t htime; /* headers file modification time */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t dtime; /* body file modification time */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_off_t hsize; /* headers file size */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_off_t dsize; /* body or temporary file size */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd char *basename; /* fileset base name */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz} ENTRY;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int delcount; /* file deletion count for nice mode */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int interrupted; /* flag: true if SIGINT or SIGTERM occurred */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int realclean; /* flag: true means user said apache is not running */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int verbose; /* flag: true means print statistics */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int benice; /* flag: true means nice mode is activated */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int dryrun; /* flag: true means dry run, don't actually delete
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd anything */
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantzstatic int deldirs; /* flag: true means directories should be deleted */
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic int listurls; /* flag: true means list cached urls */
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic int listextended;/* flag: true means list cached urls */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic int baselen; /* string length of the path to the proxy directory */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic apr_time_t now; /* start time of this processing run */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic apr_file_t *errfile; /* stderr file handle */
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic apr_file_t *outfile; /* stdout file handle */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic apr_off_t unsolicited; /* file size summary for deleted unsolicited
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd files */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9ndstatic APR_RING_ENTRY(_entry) root; /* ENTRY ring anchor */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
2d728410bcf9e6e53698bb035f516e18efd76d8bnd/* short program name as called */
2d728410bcf9e6e53698bb035f516e18efd76d8bndstatic const char *shortname = "htcacheclean";
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin/* what did we clean? */
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrinstruct stats {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t total;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t sum;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t max;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t ntotal;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t nodes;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t inodes;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t etotal;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t entries;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t dfuture;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t dexpired;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_off_t dfresh;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin};
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd#ifdef DEBUG
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * fake delete for debug purposes
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#define apr_file_remove fake_file_remove
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic void fake_file_remove(char *pathname, apr_pool_t *p)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_finfo_t info;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* stat and printing to simulate some deletion system load and to
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz display what would actually have happened */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_stat(&info, pathname, DIRINFO, p);
a51acf58d9d82f52e0ee56106cd9282030f3f3bend apr_file_printf(errfile, "would delete %s" APR_EOL_STR, pathname);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * called on SIGINT or SIGTERM
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic void setterm(int unused)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#ifdef DEBUG
a51acf58d9d82f52e0ee56106cd9282030f3f3bend apr_file_printf(errfile, "interrupt" APR_EOL_STR);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#endif
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz interrupted = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * called in out of memory condition
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzstatic int oom(int unused)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz static int called = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* be careful to call exit() only once */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!called) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz called = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz exit(1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return APR_ENOMEM;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * print purge statistics
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrinstatic void printstats(char *path, struct stats *s)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd char ttype, stype, mtype, utype;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_off_t tfrag, sfrag, ufrag;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!verbose) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ttype = 'K';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin tfrag = ((s->total * 10) / KBYTE) % 10;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->total /= KBYTE;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (s->total >= KBYTE) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ttype = 'M';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin tfrag = ((s->total * 10) / KBYTE) % 10;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->total /= KBYTE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz stype = 'K';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin sfrag = ((s->sum * 10) / KBYTE) % 10;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->sum /= KBYTE;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (s->sum >= KBYTE) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz stype = 'M';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin sfrag = ((s->sum * 10) / KBYTE) % 10;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->sum /= KBYTE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz mtype = 'K';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->max /= KBYTE;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin if (s->max >= KBYTE) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz mtype = 'M';
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s->max /= KBYTE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin apr_file_printf(errfile, "Cleaned %s. Statistics:" APR_EOL_STR, path);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (unsolicited) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz utype = 'K';
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ufrag = ((unsolicited * 10) / KBYTE) % 10;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited /= KBYTE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (unsolicited >= KBYTE) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz utype = 'M';
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ufrag = ((unsolicited * 10) / KBYTE) % 10;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited /= KBYTE;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!unsolicited && !ufrag) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz ufrag = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
a51acf58d9d82f52e0ee56106cd9282030f3f3bend apr_file_printf(errfile, "unsolicited size %d.%d%c" APR_EOL_STR,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd (int)(unsolicited), (int)(ufrag), utype);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(errfile, "size limit %" APR_OFF_T_FMT ".0%c" APR_EOL_STR,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin s->max, mtype);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(errfile, "inodes limit %" APR_OFF_T_FMT APR_EOL_STR,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin s->inodes);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin errfile,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "total size was %" APR_OFF_T_FMT ".%" APR_OFF_T_FMT "%c, total size now "
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "%" APR_OFF_T_FMT ".%" APR_OFF_T_FMT "%c" APR_EOL_STR, s->total,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin tfrag, ttype, s->sum, sfrag, stype);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(errfile, "total inodes was %" APR_OFF_T_FMT
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ", total %sinodes now "
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "%" APR_OFF_T_FMT APR_EOL_STR, s->ntotal, dryrun && deldirs ? "estimated "
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin : "", s->nodes);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin errfile,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "total entries was %" APR_OFF_T_FMT ", total entries now %" APR_OFF_T_FMT
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin APR_EOL_STR, s->etotal, s->entries);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_file_printf(
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin errfile,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "%" APR_OFF_T_FMT " entries deleted (%" APR_OFF_T_FMT " from future, %"
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin APR_OFF_T_FMT " expired, %" APR_OFF_T_FMT " fresh)" APR_EOL_STR,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (s->etotal - s->entries), s->dfuture, s->dexpired, s->dfresh);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin/**
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin * Round the value up to the given threshold.
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin */
fac37c9794a18c24d187f4e0f97a9476c4344118minfrinstatic apr_size_t round_up(apr_size_t val, apr_off_t round) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (round > 1) {
289a94d4157967ac04dc5c865b94e361400eae0afuankg return (apr_size_t)(((val + round - 1) / round) * round);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin return val;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin}
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * delete parent directories
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic void delete_parent(const char *path, const char *basename,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t *nodes, apr_pool_t *pool)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin char *nextpath, *name;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *p;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* temp pool, otherwise lots of memory could be allocated */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_create(&p, pool);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin name = apr_pstrdup(p, basename);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* If asked to delete dirs, do so now. We don't care if it fails.
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * If it fails, it likely means there was something else there.
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (deldirs && !dryrun) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin const char *vary;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin char *end = strrchr(name, '/');
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin while (end) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin *end = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* remove the directory */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin nextpath = apr_pstrcat(p, path, "/", name, NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!apr_dir_remove(nextpath, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin /* vary directory found? */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin vary = strstr(name, CACHE_VDIR_SUFFIX);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (vary && !vary[sizeof(CACHE_VDIR_SUFFIX) - 1]) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin nextpath = apr_pstrcat(p, path, "/", apr_pstrndup(p, name, vary
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin - name), NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!apr_file_remove(nextpath, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin break;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin end = strrchr(name, '/');
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_destroy(p);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (benice) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (++delcount >= DELETE_NICE) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_sleep(NICE_DELAY);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delcount = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin}
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin/*
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin * delete a single file
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic void delete_file(char *path, char *basename, apr_off_t *nodes,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_t *pool)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin char *nextpath;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_t *p;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* temp pool, otherwise lots of memory could be allocated */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_create(&p, pool);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd nextpath = apr_pstrcat(p, path, "/", basename, NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (dryrun) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_finfo_t finfo;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!apr_stat(&finfo, nextpath, APR_FINFO_NLINK, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (!apr_file_remove(nextpath, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_destroy(p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (benice) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (++delcount >= DELETE_NICE) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_sleep(NICE_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd delcount = 0;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_parent(path, basename, nodes, pool);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * delete cache file set
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic void delete_entry(char *path, char *basename, apr_off_t *nodes,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_pool_t *pool)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd char *nextpath;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *p;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* temp pool, otherwise lots of memory could be allocated */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_create(&p, pool);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd nextpath = apr_pstrcat(p, path, "/", basename, CACHE_HEADER_SUFFIX, NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (dryrun) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_finfo_t finfo;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!apr_stat(&finfo, nextpath, APR_FINFO_NLINK, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (!apr_file_remove(nextpath, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd nextpath = apr_pstrcat(p, path, "/", basename, CACHE_DATA_SUFFIX, NULL);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (dryrun) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_finfo_t finfo;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!apr_stat(&finfo, nextpath, APR_FINFO_NLINK, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (!apr_file_remove(nextpath, p)) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)--;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_destroy(p);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (benice) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd delcount += 2;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (delcount >= DELETE_NICE) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_sleep(NICE_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd delcount = 0;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_parent(path, basename, nodes, pool);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
5876f43a746f688a32b7201bced8591ddf19bd43minfrin/*
5876f43a746f688a32b7201bced8591ddf19bd43minfrin * list the cache directory tree
5876f43a746f688a32b7201bced8591ddf19bd43minfrin */
5876f43a746f688a32b7201bced8591ddf19bd43minfrinstatic int list_urls(char *path, apr_pool_t *pool, apr_off_t round)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin{
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_dir_t *dir;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_finfo_t info;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_size_t len;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pool_t *p;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_t *fd;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin const char *ext, *nextpath;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin char *url;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_uint32_t format;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_cache_info_t disk_info;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pool_create(&p, pool);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (apr_dir_open(&dir, path, p) != APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
717473b4288c54ef043f016a4ee801aba477303erjung while (apr_dir_read(&info, APR_FINFO_TYPE, dir) == APR_SUCCESS && !interrupted) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (info.filetype == APR_DIR) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (!strcmp(info.name, ".") || !strcmp(info.name, "..")) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin continue;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (list_urls(apr_pstrcat(p, path, "/", info.name, NULL), pool, round)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else if (info.filetype == APR_REG) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ext = strchr(info.name, '.');
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
c840604b10dc1d368a2da321ec51362373d502a3minfrin if (ext && !strcasecmp(ext, CACHE_HEADER_SUFFIX)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin nextpath = apr_pstrcat(p, path, "/", info.name, NULL);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (apr_file_open(&fd, nextpath, APR_FOPEN_READ
5876f43a746f688a32b7201bced8591ddf19bd43minfrin | APR_FOPEN_BINARY, APR_OS_DEFAULT, p) == APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin len = sizeof(format);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (apr_file_read_full(fd, &format, len, &len)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin == APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (format == DISK_FORMAT_VERSION) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_off_t offset = 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_seek(fd, APR_SET, &offset);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin len = sizeof(disk_cache_info_t);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (apr_file_read_full(fd, &disk_info, len, &len)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin == APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin len = disk_info.name_len;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin url = apr_palloc(p, len + 1);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin url[len] = 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (apr_file_read_full(fd, url, len, &len)
5876f43a746f688a32b7201bced8591ddf19bd43minfrin == APR_SUCCESS) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (listextended) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_finfo_t hinfo, dinfo;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* stat the header file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (APR_SUCCESS != apr_file_info_get(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin &hinfo, APR_FINFO_SIZE, fd)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* ignore the file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else if (disk_info.has_body && APR_SUCCESS
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != apr_stat(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin &dinfo,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pstrcat(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin path,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin "/",
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pstrndup(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin info.name,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ext
5876f43a746f688a32b7201bced8591ddf19bd43minfrin - info.name),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin CACHE_DATA_SUFFIX,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin NULL),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_FINFO_SIZE
5876f43a746f688a32b7201bced8591ddf19bd43minfrin | APR_FINFO_IDENT,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* ignore the file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else if (disk_info.has_body && (dinfo.device
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != disk_info.device
5876f43a746f688a32b7201bced8591ddf19bd43minfrin || dinfo.inode
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != disk_info.inode)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* ignore the file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_printf(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin outfile,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin "%s %" APR_SIZE_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %" APR_SIZE_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %d %" APR_SIZE_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %" APR_TIME_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %" APR_TIME_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %" APR_TIME_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %" APR_TIME_T_FMT
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " %d %d\n",
5876f43a746f688a32b7201bced8591ddf19bd43minfrin url,
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg round_up((apr_size_t)hinfo.size, round),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin round_up(
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg disk_info.has_body ? (apr_size_t)dinfo.size
5876f43a746f688a32b7201bced8591ddf19bd43minfrin : 0, round),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.status,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.entity_version,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.date,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.expire,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.request_time,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.response_time,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.has_body,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin disk_info.header_only);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_finfo_t dinfo;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* stat the data file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (disk_info.has_body && APR_SUCCESS
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != apr_stat(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin &dinfo,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pstrcat(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin path,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin "/",
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pstrndup(
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin info.name,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin ext
5876f43a746f688a32b7201bced8591ddf19bd43minfrin - info.name),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin CACHE_DATA_SUFFIX,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin NULL),
5876f43a746f688a32b7201bced8591ddf19bd43minfrin APR_FINFO_SIZE
5876f43a746f688a32b7201bced8591ddf19bd43minfrin | APR_FINFO_IDENT,
5876f43a746f688a32b7201bced8591ddf19bd43minfrin p)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* ignore the file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else if (disk_info.has_body && (dinfo.device
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != disk_info.device
5876f43a746f688a32b7201bced8591ddf19bd43minfrin || dinfo.inode
5876f43a746f688a32b7201bced8591ddf19bd43minfrin != disk_info.inode)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin /* ignore the file */
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin else {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_printf(outfile, "%s\n",
5876f43a746f688a32b7201bced8591ddf19bd43minfrin url);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin break;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_close(fd);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_dir_close(dir);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (interrupted) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_pool_destroy(p);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (benice) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_sleep(NICE_DELAY);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (interrupted) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return 0;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin}
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * walk the cache directory tree
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic int process_dir(char *path, apr_pool_t *pool, apr_off_t *nodes)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_dir_t *dir;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *p;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_t *h;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_index_t *i;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_file_t *fd;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_status_t status;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_finfo_t info;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_size_t len;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t current, deviation;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin char *nextpath, *base, *ext;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_RING_ENTRY(_direntry) anchor;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd DIRENTRY *d, *t, *n;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ENTRY *e;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd int skip, retries;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd disk_cache_info_t disk_info;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_RING_INIT(&anchor, _direntry, link);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_create(&p, pool);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd h = apr_hash_make(p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd fd = NULL;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd deviation = MAXDEVIATION * APR_USEC_PER_SEC;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (apr_dir_open(&dir, path, p) != APR_SUCCESS) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd while (apr_dir_read(&info, 0, dir) == APR_SUCCESS && !interrupted) {
0ecc05d02d9ee3d110cb9e263080aaa2b77a12f9jim if (!strcmp(info.name, ".") || !strcmp(info.name, "..")) {
07d000c26f08bb7d590da25057748b28b8b59741jim continue;
07d000c26f08bb7d590da25057748b28b8b59741jim }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d = apr_pcalloc(p, sizeof(DIRENTRY));
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->basename = apr_pstrcat(p, path, "/", info.name, NULL);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd APR_RING_INSERT_TAIL(&anchor, d, _direntry, link);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin (*nodes)++;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_dir_close(dir);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (interrupted) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd skip = baselen + 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd for (d = APR_RING_FIRST(&anchor);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz !interrupted && d != APR_RING_SENTINEL(&anchor, _direntry, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz d=n) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd n = APR_RING_NEXT(d, link);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd base = strrchr(d->basename, '/');
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!base++) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd base = d->basename;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ext = strchr(base, '.');
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* there may be temporary files which may be gone before
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * processing, always skip these if not in realclean mode
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!ext && !realclean) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd && strlen(base) == AP_TEMPFILE_NAMELEN) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
6850c836b8c7c688ef8a8ec280e798e1d0bfbf01trawick /* this may look strange but apr_stat() may return an error which
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * is system dependent and there may be transient failures,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * so just blindly retry for a short while
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd retries = STAT_ATTEMPTS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd status = APR_SUCCESS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd do {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status != APR_SUCCESS) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_sleep(STAT_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd status = apr_stat(&info, d->basename, DIRINFO, p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd } while (status != APR_SUCCESS && !interrupted && --retries);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* what may happen here is that apache did create a file which
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * we did detect but then does delete the file before we can
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * get file information, so if we don't get any file information
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * we will ignore the file in this case
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status != APR_SUCCESS) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!realclean && !interrupted) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (info.filetype == APR_DIR) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (process_dir(d->basename, pool, nodes)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (info.filetype != APR_REG) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!ext) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!strncasecmp(base, AP_TEMPFILE_BASE, AP_TEMPFILE_BASELEN)
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd && strlen(base) == AP_TEMPFILE_NAMELEN) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->basename += skip;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->type = TEMP;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->dsize = info.size;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!strcasecmp(ext, CACHE_HEADER_SUFFIX)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd *ext = '\0';
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->basename += skip;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* if a user manually creates a '.header' file */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (d->basename[0] == '\0') {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (t) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d = t;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->type |= HEADER;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->htime = info.mtime;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->hsize = info.size;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!strcasecmp(ext, CACHE_DATA_SUFFIX)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd *ext = '\0';
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->basename += skip;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd /* if a user manually creates a '.data' file */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (d->basename[0] == '\0') {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd continue;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd t = apr_hash_get(h, d->basename, APR_HASH_KEY_STRING);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (t) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d = t;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->type |= DATA;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->dtime = info.mtime;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd d->dsize = info.size;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_hash_set(h, d->basename, APR_HASH_KEY_STRING, d);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (interrupted) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz path[baselen] = '\0';
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (i = apr_hash_first(p, h); i && !interrupted; i = apr_hash_next(i)) {
7585f3892ce3057554a5bef740302dd479b223b2nd void *hvalue;
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_uint32_t format;
7585f3892ce3057554a5bef740302dd479b223b2nd
7585f3892ce3057554a5bef740302dd479b223b2nd apr_hash_this(i, NULL, NULL, &hvalue);
7585f3892ce3057554a5bef740302dd479b223b2nd d = hvalue;
7585f3892ce3057554a5bef740302dd479b223b2nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch(d->type) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case HEADERDATA:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz nextpath = apr_pstrcat(p, path, "/", d->basename,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd CACHE_HEADER_SUFFIX, NULL);
e8f95a682820a599fe41b22977010636be5c2717jim if (apr_file_open(&fd, nextpath, APR_FOPEN_READ | APR_FOPEN_BINARY,
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm APR_OS_DEFAULT, p) == APR_SUCCESS) {
13be69a729c912bea7d0aa22596b92717df92551pquerna len = sizeof(format);
e8f95a682820a599fe41b22977010636be5c2717jim if (apr_file_read_full(fd, &format, len,
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd &len) == APR_SUCCESS) {
13be69a729c912bea7d0aa22596b92717df92551pquerna if (format == DISK_FORMAT_VERSION) {
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_off_t offset = 0;
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_file_seek(fd, APR_SET, &offset);
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna len = sizeof(disk_cache_info_t);
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna if (apr_file_read_full(fd, &disk_info, len,
13be69a729c912bea7d0aa22596b92717df92551pquerna &len) == APR_SUCCESS) {
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_file_close(fd);
13be69a729c912bea7d0aa22596b92717df92551pquerna e = apr_palloc(pool, sizeof(ENTRY));
13be69a729c912bea7d0aa22596b92717df92551pquerna APR_RING_INSERT_TAIL(&root, e, _entry, link);
13be69a729c912bea7d0aa22596b92717df92551pquerna e->expire = disk_info.expire;
13be69a729c912bea7d0aa22596b92717df92551pquerna e->response_time = disk_info.response_time;
13be69a729c912bea7d0aa22596b92717df92551pquerna e->htime = d->htime;
13be69a729c912bea7d0aa22596b92717df92551pquerna e->dtime = d->dtime;
13be69a729c912bea7d0aa22596b92717df92551pquerna e->hsize = d->hsize;
13be69a729c912bea7d0aa22596b92717df92551pquerna e->dsize = d->dsize;
0696197a54f186a65abacba1037f6fbe0cb975a1niq e->basename = apr_pstrdup(pool, d->basename);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin if (!disk_info.has_body) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_file(path, apr_pstrcat(p, path, "/",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin d->basename, CACHE_DATA_SUFFIX, NULL),
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin nodes, p);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
13be69a729c912bea7d0aa22596b92717df92551pquerna break;
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna else {
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_file_close(fd);
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna else if (format == VARY_FORMAT_VERSION) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_finfo_t finfo;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
e8f95a682820a599fe41b22977010636be5c2717jim /* This must be a URL that added Vary headers later,
13be69a729c912bea7d0aa22596b92717df92551pquerna * so kill the orphaned .data file
13be69a729c912bea7d0aa22596b92717df92551pquerna */
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_file_close(fd);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (apr_stat(&finfo, apr_pstrcat(p, nextpath,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin CACHE_VDIR_SUFFIX, NULL), APR_FINFO_TYPE, p)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin || finfo.filetype != APR_DIR) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_file(path, apr_pstrcat(p, path, "/",
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin d->basename, CACHE_DATA_SUFFIX, NULL),
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin nodes, p);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
9de73d09e50d2daa2b0ba263d01cf6bf972a27fdrpluem break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin else {
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin /* We didn't recognise the format, kill the files */
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin apr_file_close(fd);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin break;
af91703d7657e52921bb36c44ff093b2d2077d0cminfrin }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_file_close(fd);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
13be69a729c912bea7d0aa22596b92717df92551pquerna
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* we have a somehow unreadable headers file which is associated
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * with a data file. this may be caused by apache currently
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * rewriting the headers file. thus we may delete the file set
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * either in realclean mode or if the headers file modification
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * timestamp is not within a specified positive or negative offset
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * to the current time.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz current = apr_time_now();
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (realclean || d->htime < current - deviation
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd || d->htime > current + deviation) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited += d->hsize;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited += d->dsize;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* single data and header files may be deleted either in realclean
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * mode or if their modification timestamp is not within a
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * specified positive or negative offset to the current time.
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * this handling is necessary due to possible race conditions
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * between apache and this process
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case HEADER:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz current = apr_time_now();
13be69a729c912bea7d0aa22596b92717df92551pquerna nextpath = apr_pstrcat(p, path, "/", d->basename,
13be69a729c912bea7d0aa22596b92717df92551pquerna CACHE_HEADER_SUFFIX, NULL);
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm if (apr_file_open(&fd, nextpath, APR_FOPEN_READ | APR_FOPEN_BINARY,
85fa7868b2734d79c4e27b74d54b6846a1f9176fcolm APR_OS_DEFAULT, p) == APR_SUCCESS) {
13be69a729c912bea7d0aa22596b92717df92551pquerna len = sizeof(format);
e8f95a682820a599fe41b22977010636be5c2717jim if (apr_file_read_full(fd, &format, len,
13be69a729c912bea7d0aa22596b92717df92551pquerna &len) == APR_SUCCESS) {
13be69a729c912bea7d0aa22596b92717df92551pquerna if (format == VARY_FORMAT_VERSION) {
13be69a729c912bea7d0aa22596b92717df92551pquerna apr_time_t expires;
13be69a729c912bea7d0aa22596b92717df92551pquerna
13be69a729c912bea7d0aa22596b92717df92551pquerna len = sizeof(expires);
13be69a729c912bea7d0aa22596b92717df92551pquerna
a059fab04e8584e8bfdcaf1103e2ec6f53f97a14pquerna if (apr_file_read_full(fd, &expires, len,
a059fab04e8584e8bfdcaf1103e2ec6f53f97a14pquerna &len) == APR_SUCCESS) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_finfo_t finfo;
13be69a729c912bea7d0aa22596b92717df92551pquerna
be60e23cacdadb070a83d40086a6d689410acc38rpluem apr_file_close(fd);
13be69a729c912bea7d0aa22596b92717df92551pquerna
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (apr_stat(&finfo, apr_pstrcat(p, nextpath,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin CACHE_VDIR_SUFFIX, NULL), APR_FINFO_TYPE, p)
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin || finfo.filetype != APR_DIR) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (expires < current) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
be60e23cacdadb070a83d40086a6d689410acc38rpluem }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
be60e23cacdadb070a83d40086a6d689410acc38rpluem break;
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin else if (format == DISK_FORMAT_VERSION) {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_off_t offset = 0;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_file_seek(fd, APR_SET, &offset);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin len = sizeof(disk_cache_info_t);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin if (apr_file_read_full(fd, &disk_info, len,
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin &len) == APR_SUCCESS) {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_file_close(fd);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e = apr_palloc(pool, sizeof(ENTRY));
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin APR_RING_INSERT_TAIL(&root, e, _entry, link);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->expire = disk_info.expire;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->response_time = disk_info.response_time;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->htime = d->htime;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->dtime = d->dtime;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->hsize = d->hsize;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->dsize = d->dsize;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin e->basename = apr_pstrdup(pool, d->basename);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin break;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin else {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_file_close(fd);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin else {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_file_close(fd);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin break;
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin }
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin else {
9ee5ae3a993134d40e9fb156b44b610ce261875cminfrin apr_file_close(fd);
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna }
13be69a729c912bea7d0aa22596b92717df92551pquerna
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (realclean || d->htime < current - deviation
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd || d->htime > current + deviation) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd unsolicited += d->hsize;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case DATA:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz current = apr_time_now();
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (realclean || d->dtime < current - deviation
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd || d->dtime > current + deviation) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, d->basename, nodes, p);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd unsolicited += d->dsize;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* temp files may only be deleted in realclean mode which
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * is asserted above if a tempfile is in the hash array
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case TEMP:
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_file(path, d->basename, nodes, p);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited += d->dsize;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (interrupted) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_destroy(p);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (benice) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_sleep(NICE_DELAY);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (interrupted) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * purge cache entries
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrinstatic void purge(char *path, apr_pool_t *pool, apr_off_t max,
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t inodes, apr_off_t nodes, apr_off_t round)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd ENTRY *e, *n, *oldest;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin struct stats s;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.sum = 0;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.entries = 0;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dfuture = 0;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dexpired = 0;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dfresh = 0;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.max = max;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin s.nodes = nodes;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin s.inodes = inodes;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin s.ntotal = nodes;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (e = APR_RING_FIRST(&root);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz e != APR_RING_SENTINEL(&root, _entry, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz e = APR_RING_NEXT(e, link)) {
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum += round_up((apr_size_t)e->hsize, round);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum += round_up((apr_size_t)e->dsize, round);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.entries++;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.total = s.sum;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.etotal = s.entries;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if ((!s.max || s.sum <= s.max) && (!s.inodes || s.nodes <= s.inodes)) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin printstats(path, &s);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* process all entries with a timestamp in the future, this may
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * happen if a wrong system time is corrected
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (e = APR_RING_FIRST(&root);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz n = APR_RING_NEXT(e, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (e->response_time > now || e->htime > now || e->dtime > now) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, e->basename, &s.nodes, pool);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)e->hsize, round);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)e->dsize, round);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.entries--;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dfuture++;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_REMOVE(e, link);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if ((!s.max || s.sum <= s.max) && (!s.inodes || s.nodes <= s.inodes)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!interrupted) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin printstats(path, &s);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz e = n;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (interrupted) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* process all entries with are expired */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz for (e = APR_RING_FIRST(&root);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd e != APR_RING_SENTINEL(&root, _entry, link) && !interrupted;) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz n = APR_RING_NEXT(e, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (e->expire != APR_DATE_BAD && e->expire < now) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, e->basename, &s.nodes, pool);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)e->hsize, round);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)e->dsize, round);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.entries--;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dexpired++;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_REMOVE(e, link);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if ((!s.max || s.sum <= s.max) && (!s.inodes || s.nodes <= s.inodes)) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (!interrupted) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin printstats(path, &s);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz e = n;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (interrupted) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* process remaining entries oldest to newest, the check for an emtpy
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * ring actually isn't necessary except when the compiler does
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * corrupt 64bit arithmetics which happend to me once, so better safe
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * than sorry
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin while (!((!s.max || s.sum <= s.max) && (!s.inodes || s.nodes <= s.inodes))
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin && !interrupted && !APR_RING_EMPTY(&root, _entry, link)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz oldest = APR_RING_FIRST(&root);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd for (e = APR_RING_NEXT(oldest, link);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd e != APR_RING_SENTINEL(&root, _entry, link);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd e = APR_RING_NEXT(e, link)) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (e->dtime < oldest->dtime) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz oldest = e;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin delete_entry(path, oldest->basename, &s.nodes, pool);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)oldest->hsize, round);
51c6a215cedf19fe46a20b5cbb57f01ec4d5e8c4fuankg s.sum -= round_up((apr_size_t)oldest->dsize, round);
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.entries--;
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin s.dfresh++;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_REMOVE(oldest, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!interrupted) {
9fe23388f983cb652b5d68e2bd92aa9f0568c574minfrin printstats(path, &s);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic apr_status_t remove_directory(apr_pool_t *pool, const char *dir)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_status_t rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_dir_t *dirp;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_finfo_t dirent;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = apr_dir_open(&dirp, dir, pool);
2c2ced9d18f0b065f620b0ead09ba25e955c3cc7sf if (APR_STATUS_IS_ENOENT(rv)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (rv != APR_SUCCESS) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc apr_file_printf(errfile, "Could not open directory %s: %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc dir, &rv);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin while (apr_dir_read(&dirent, APR_FINFO_DIRENT | APR_FINFO_TYPE, dirp)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin == APR_SUCCESS) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (dirent.filetype == APR_DIR) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (strcmp(dirent.name, ".") && strcmp(dirent.name, "..")) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = remove_directory(pool, apr_pstrcat(pool, dir, "/",
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin dirent.name, NULL));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* tolerate the directory not being empty, the cache may have
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * attempted to recreate the directory in the mean time.
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (APR_SUCCESS != rv && APR_ENOTEMPTY != rv) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin break;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin } else {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *file = apr_pstrcat(pool, dir, "/", dirent.name, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = apr_file_remove(file, pool);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (APR_SUCCESS != rv) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_file_printf(errfile,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc "Could not remove file '%s': %pm" APR_EOL_STR, file,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc &rv);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin break;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_dir_close(dirp);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (rv == APR_SUCCESS) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = apr_dir_remove(dir, pool);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (APR_ENOTEMPTY == rv) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = APR_SUCCESS;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (rv != APR_SUCCESS) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc apr_file_printf(errfile, "Could not remove directory %s: %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc dir, &rv);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic apr_status_t find_directory(apr_pool_t *pool, const char *base,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *rest)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_status_t rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_dir_t *dirp;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_finfo_t dirent;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int found = 0, files = 0;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *header = apr_pstrcat(pool, rest, CACHE_HEADER_SUFFIX, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *data = apr_pstrcat(pool, rest, CACHE_DATA_SUFFIX, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *vdir = apr_pstrcat(pool, rest, CACHE_HEADER_SUFFIX,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin CACHE_VDIR_SUFFIX, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *dirname = NULL;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = apr_dir_open(&dirp, base, pool);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (rv != APR_SUCCESS) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc apr_file_printf(errfile, "Could not open directory %s: %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc base, &rv);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = APR_ENOENT;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin while (apr_dir_read(&dirent, APR_FINFO_DIRENT | APR_FINFO_TYPE, dirp)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin == APR_SUCCESS) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int len = strlen(dirent.name);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int restlen = strlen(rest);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (dirent.filetype == APR_DIR && !strncmp(rest, dirent.name, len)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin dirname = apr_pstrcat(pool, base, "/", dirent.name, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = find_directory(pool, dirname, rest + (len < restlen ? len
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin : restlen));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (APR_SUCCESS == rv) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin found = 1;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (dirent.filetype == APR_DIR) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (!strcmp(dirent.name, vdir)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin files = 1;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (dirent.filetype == APR_REG) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (!strcmp(dirent.name, header) || !strcmp(dirent.name, data)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin files = 1;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_dir_close(dirp);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (files) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = APR_SUCCESS;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (!dryrun) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin const char *remove;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_status_t status;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin remove = apr_pstrcat(pool, base, "/", header, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin status = apr_file_remove(remove, pool);
2c2ced9d18f0b065f620b0ead09ba25e955c3cc7sf if (status != APR_SUCCESS && !APR_STATUS_IS_ENOENT(status)) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc apr_file_printf(errfile, "Could not remove file %s: %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc remove, &status);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = status;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin remove = apr_pstrcat(pool, base, "/", data, NULL);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin status = apr_file_remove(remove, pool);
2c2ced9d18f0b065f620b0ead09ba25e955c3cc7sf if (status != APR_SUCCESS && !APR_STATUS_IS_ENOENT(status)) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc apr_file_printf(errfile, "Could not remove file %s: %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc remove, &status);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = status;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin status = remove_directory(pool, apr_pstrcat(pool, base, "/", vdir, NULL));
2c2ced9d18f0b065f620b0ead09ba25e955c3cc7sf if (status != APR_SUCCESS && !APR_STATUS_IS_ENOENT(status)) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin rv = status;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* If asked to delete dirs, do so now. We don't care if it fails.
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * If it fails, it likely means there was something else there.
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (dirname && deldirs && !dryrun) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_dir_remove(dirname, pool);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (found) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return APR_SUCCESS;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return rv;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin/**
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * Delete a specific URL from the cache.
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrinstatic apr_status_t delete_url(apr_pool_t *pool, const char *proxypath, const char *url)
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin{
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_md5_ctx_t context;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin unsigned char digest[16];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin char tmp[23];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int i, k;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin unsigned int x;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin static const char enc_table[64] =
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_md5_init(&context);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_md5_update(&context, (const unsigned char *) url, strlen(url));
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_md5_final(digest, &context);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* encode 128 bits as 22 characters, using a modified uuencoding
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * the encoding is 3 bytes -> 4 characters* i.e. 128 bits is
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin * 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin for (i = 0, k = 0; i < 15; i += 3) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[x >> 18];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[(x >> 12) & 0x3f];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[(x >> 6) & 0x3f];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[x & 0x3f];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* one byte left */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin x = digest[15];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[x >> 2]; /* use up 6 bits */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k++] = enc_table[(x << 4) & 0x3f];
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin tmp[k] = 0;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin /* automatically find the directory levels */
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return find_directory(pool, proxypath, tmp);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin}
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * usage info
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
2d728410bcf9e6e53698bb035f516e18efd76d8bnd#define NL APR_EOL_STR
1018201f5223624476334c6e23aead02db7c4040minfrinstatic void usage(const char *error)
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
1018201f5223624476334c6e23aead02db7c4040minfrin if (error) {
9333012aa2af9e669db651f0ade3e044ccc3ec84rpluem apr_file_printf(errfile, "%s error: %s\n", shortname, error);
1018201f5223624476334c6e23aead02db7c4040minfrin }
9333012aa2af9e669db651f0ade3e044ccc3ec84rpluem apr_file_printf(errfile,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd "%s -- program for cleaning the disk cache." NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin "Usage: %s [-Dvtrn] -pPATH [-lLIMIT|-LLIMIT] [-PPIDFILE]" NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " %s [-nti] -dINTERVAL -pPATH [-lLIMIT|-LLIMIT] [-PPIDFILE]" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin " %s [-Dvt] -pPATH URL ..." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd "Options:" NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -d Daemonize and repeat cache cleaning every INTERVAL minutes." NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " This option is mutually exclusive with the -D, -v and -r" NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " options." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -D Do a dry run and don't delete anything. This option is mutually" NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " exclusive with the -d option. When doing a dry run and deleting" NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " directories with -t, the inodes reported deleted in the stats" NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " cannot take into account the directories deleted, and will be" NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " marked as an estimate." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -v Be verbose and print statistics. This option is mutually" NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " exclusive with the -d option." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -r Clean thoroughly. This assumes that the Apache web server is " NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " not running. This option is mutually exclusive with the -d" NL
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz " option and implies -t." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -n Be nice. This causes slower processing in favour of other" NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " processes." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz " -t Delete all empty directories. By default only cache files are" NL
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz " removed, however with some configurations the large number of" NL
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz " directories created may require attention." NL
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -p Specify PATH as the root directory of the disk cache." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
4dd97ffc9c529494e0ec820fe9de80b39b589d42minfrin " -P Specify PIDFILE as the file to write the pid to." NL
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin NL
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin " -R Specify amount to round sizes up to." NL
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin NL
487d8b4e063696dd9698792a13c8e426635f1072nd " -l Specify LIMIT as the total disk cache size limit. Attach 'K'" NL
487d8b4e063696dd9698792a13c8e426635f1072nd " or 'M' to the number for specifying KBytes or MBytes." NL
487d8b4e063696dd9698792a13c8e426635f1072nd NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin " -L Specify LIMIT as the total disk cache inode limit." NL
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " -i Be intelligent and run only when there was a modification of" NL
2d728410bcf9e6e53698bb035f516e18efd76d8bnd " the disk cache. This option is only possible together with the" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin " -d option." NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " -a List the URLs currently stored in the cache. Variants of the" NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " same URL will be listed once for each variant." NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " -A List the URLs currently stored in the cache, along with their" NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " attributes in the following order: url, header size, body size," NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " status, entity version, date, expiry, request time," NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin " response time, body present, head request." NL
5876f43a746f688a32b7201bced8591ddf19bd43minfrin NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "Should an URL be provided on the command line, the URL will be" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "deleted from the cache. A reverse proxied URL is made up as follows:" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "http://<hostname>:<port><path>?[query]. So, for the path \"/\" on the" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "host \"localhost\" and port 80, the URL to delete becomes" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "\"http://localhost:80/?\". Note the '?' in the URL must always be" NL
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin "specified explicitly, whether a query string is present or not." NL,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin shortname,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd shortname,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd shortname,
2d728410bcf9e6e53698bb035f516e18efd76d8bnd shortname
2d728410bcf9e6e53698bb035f516e18efd76d8bnd );
2d728410bcf9e6e53698bb035f516e18efd76d8bnd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz exit(1);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}
2d728410bcf9e6e53698bb035f516e18efd76d8bnd#undef NL
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawickstatic void usage_repeated_arg(apr_pool_t *pool, char option) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim usage(apr_psprintf(pool,
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick "The option '%c' cannot be specified more than once",
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick option));
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick}
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrinstatic void log_pid(apr_pool_t *pool, const char *pidfilename, apr_file_t **pidfile)
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin{
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_status_t status;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin pid_t mypid = getpid();
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin
5c345baea74f2eb2d74548bed271370b972499e5minfrin if (APR_SUCCESS == (status = apr_file_open(pidfile, pidfilename,
5c345baea74f2eb2d74548bed271370b972499e5minfrin APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE |
5c345baea74f2eb2d74548bed271370b972499e5minfrin APR_FOPEN_DELONCLOSE, APR_FPROT_UREAD | APR_FPROT_UWRITE |
5c345baea74f2eb2d74548bed271370b972499e5minfrin APR_FPROT_GREAD | APR_FPROT_WREAD, pool))) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_file_printf(*pidfile, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin else {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin if (errfile) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_file_printf(errfile,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc "Could not write the pid file '%s': %pm" APR_EOL_STR,
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc pidfilename, &status);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin exit(1);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin}
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz/*
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * main
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantzint main(int argc, const char * const argv[])
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz{
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t max, inodes, round;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_time_t current, repeat, delay, previous;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_status_t status;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd apr_pool_t *pool, *instance;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_getopt_t *o;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_finfo_t info;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_file_t *pidfile;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin int retries, isdaemon, limit_found, inodes_found, intelligent, dowork;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz char opt;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz const char *arg;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin char *proxypath, *path, *pidfilename;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz interrupted = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz repeat = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz isdaemon = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz dryrun = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz limit_found = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes_found = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz max = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes = 0;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin round = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz verbose = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz realclean = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz benice = 0;
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz deldirs = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 0;
f81f966693feefb53f11dc3363b864779873a608nd previous = 0; /* avoid compiler warning */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz proxypath = NULL;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin pidfilename = NULL;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz atexit(apr_terminate);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
2d728410bcf9e6e53698bb035f516e18efd76d8bnd if (argc) {
2d728410bcf9e6e53698bb035f516e18efd76d8bnd shortname = apr_filepath_name_get(argv[0]);
2d728410bcf9e6e53698bb035f516e18efd76d8bnd }
2d728410bcf9e6e53698bb035f516e18efd76d8bnd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_abort_set(oom, pool);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_file_open_stderr(&errfile, pool);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin apr_file_open_stdout(&outfile, pool);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_signal(SIGINT, setterm);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_signal(SIGTERM, setterm);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_getopt_init(&o, pool, argc, argv);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz while (1) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin status = apr_getopt(o, "iDnvrtd:l:L:p:P:R:aA", &opt, &arg);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status == APR_EOF) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (status != APR_SUCCESS) {
1018201f5223624476334c6e23aead02db7c4040minfrin usage(NULL);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin char *end;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin apr_status_t rv;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd switch (opt) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 'i':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (intelligent) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 'D':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (dryrun) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd dryrun = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 'n':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (benice) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz benice = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
e8f95a682820a599fe41b22977010636be5c2717jim
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz case 't':
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz if (deldirs) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz }
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz deldirs = 1;
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd case 'v':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (verbose) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz verbose = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd case 'r':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (realclean) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz realclean = 1;
663c339c8a4663cf1977f890481f8b7e251d3221jerenkrantz deldirs = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 'd':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (isdaemon) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz isdaemon = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz repeat = apr_atoi64(arg);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz repeat *= SECS_PER_MIN;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz repeat *= APR_USEC_PER_SEC;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 'l':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (limit_found) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz limit_found = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
487d8b4e063696dd9698792a13c8e426635f1072nd do {
487d8b4e063696dd9698792a13c8e426635f1072nd rv = apr_strtoff(&max, arg, &end, 10);
487d8b4e063696dd9698792a13c8e426635f1072nd if (rv == APR_SUCCESS) {
487d8b4e063696dd9698792a13c8e426635f1072nd if ((*end == 'K' || *end == 'k') && !end[1]) {
487d8b4e063696dd9698792a13c8e426635f1072nd max *= KBYTE;
487d8b4e063696dd9698792a13c8e426635f1072nd }
487d8b4e063696dd9698792a13c8e426635f1072nd else if ((*end == 'M' || *end == 'm') && !end[1]) {
487d8b4e063696dd9698792a13c8e426635f1072nd max *= MBYTE;
487d8b4e063696dd9698792a13c8e426635f1072nd }
15351ab1a34130c1f880daef4e5dc15407a75969colm else if ((*end == 'G' || *end == 'g') && !end[1]) {
15351ab1a34130c1f880daef4e5dc15407a75969colm max *= GBYTE;
15351ab1a34130c1f880daef4e5dc15407a75969colm }
487d8b4e063696dd9698792a13c8e426635f1072nd else if (*end && /* neither empty nor [Bb] */
487d8b4e063696dd9698792a13c8e426635f1072nd ((*end != 'B' && *end != 'b') || end[1])) {
487d8b4e063696dd9698792a13c8e426635f1072nd rv = APR_EGENERAL;
487d8b4e063696dd9698792a13c8e426635f1072nd }
487d8b4e063696dd9698792a13c8e426635f1072nd }
487d8b4e063696dd9698792a13c8e426635f1072nd if (rv != APR_SUCCESS) {
1018201f5223624476334c6e23aead02db7c4040minfrin usage(apr_psprintf(pool, "Invalid limit: %s"
1018201f5223624476334c6e23aead02db7c4040minfrin APR_EOL_STR APR_EOL_STR, arg));
487d8b4e063696dd9698792a13c8e426635f1072nd }
487d8b4e063696dd9698792a13c8e426635f1072nd } while(0);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd break;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin case 'L':
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (inodes_found) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin usage_repeated_arg(pool, opt);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes_found = 1;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin do {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin rv = apr_strtoff(&inodes, arg, &end, 10);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (rv == APR_SUCCESS) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if ((*end == 'K' || *end == 'k') && !end[1]) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes *= KBYTE;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if ((*end == 'M' || *end == 'm') && !end[1]) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes *= MBYTE;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if ((*end == 'G' || *end == 'g') && !end[1]) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin inodes *= GBYTE;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin else if (*end && /* neither empty nor [Bb] */
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin ((*end != 'B' && *end != 'b') || end[1])) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin rv = APR_EGENERAL;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (rv != APR_SUCCESS) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin usage(apr_psprintf(pool, "Invalid limit: %s"
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin APR_EOL_STR APR_EOL_STR, arg));
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin }
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin } while(0);
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin break;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin case 'a':
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (listurls) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin usage_repeated_arg(pool, opt);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin listurls = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin break;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin case 'A':
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (listurls) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin usage_repeated_arg(pool, opt);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin listurls = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin listextended = 1;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin break;
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd case 'p':
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (proxypath) {
4e1b035de81cbd18cfaea3604d40209bc6919bd9trawick usage_repeated_arg(pool, opt);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd proxypath = apr_pstrdup(pool, arg);
1018201f5223624476334c6e23aead02db7c4040minfrin if ((status = apr_filepath_set(proxypath, pool)) != APR_SUCCESS) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc usage(apr_psprintf(pool, "Could not set filepath to '%s': %pm",
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc proxypath, &status));
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin case 'P':
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin if (pidfilename) {
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin usage_repeated_arg(pool, opt);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin pidfilename = apr_pstrdup(pool, arg);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin break;
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin case 'R':
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (round) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin usage_repeated_arg(pool, opt);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin rv = apr_strtoff(&round, arg, &end, 10);
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (rv == APR_SUCCESS) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (*end) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin usage(apr_psprintf(pool, "Invalid round value: %s"
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin APR_EOL_STR APR_EOL_STR, arg));
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin else if (round < 0) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin usage(apr_psprintf(pool, "Round value must be positive: %s"
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin APR_EOL_STR APR_EOL_STR, arg));
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin if (rv != APR_SUCCESS) {
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin usage(apr_psprintf(pool, "Invalid round value: %s"
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin APR_EOL_STR APR_EOL_STR, arg));
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin }
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin break;
fac37c9794a18c24d187f4e0f97a9476c4344118minfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd } /* switch */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd } /* else */
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd } /* while */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
1018201f5223624476334c6e23aead02db7c4040minfrin if (argc <= 1) {
c42b94a839cae93073d33b0d21f0ff1118d958e8poirier usage(NULL);
1018201f5223624476334c6e23aead02db7c4040minfrin }
1018201f5223624476334c6e23aead02db7c4040minfrin
e87ef600cfe7150babdf56efb1c26aba66ef1522jorton if (!proxypath) {
e87ef600cfe7150babdf56efb1c26aba66ef1522jorton usage("Option -p must be specified");
e87ef600cfe7150babdf56efb1c26aba66ef1522jorton }
e87ef600cfe7150babdf56efb1c26aba66ef1522jorton
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (o->ind < argc) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int deleted = 0;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin int error = 0;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (isdaemon) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin usage("Option -d cannot be used with URL arguments, aborting");
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (intelligent) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin usage("Option -i cannot be used with URL arguments, aborting");
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (limit_found) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin usage("Option -l cannot be used with URL arguments, aborting");
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin while (o->ind < argc) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin status = delete_url(pool, proxypath, argv[o->ind]);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (APR_SUCCESS == status) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (verbose) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_file_printf(errfile, "Removed: %s" APR_EOL_STR,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin argv[o->ind]);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin deleted = 1;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin else if (APR_ENOENT == status) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (verbose) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_file_printf(errfile, "Not cached: %s" APR_EOL_STR,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin argv[o->ind]);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin else {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin if (verbose) {
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin apr_file_printf(errfile, "Error while removed: %s" APR_EOL_STR,
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin argv[o->ind]);
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin error = 1;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin o->ind++;
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin }
6fee4e2faa2e45fe2636d01e35d03c2cf0c9d431minfrin return error ? 1 : deleted ? 0 : 2;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
1018201f5223624476334c6e23aead02db7c4040minfrin if (isdaemon && repeat <= 0) {
1018201f5223624476334c6e23aead02db7c4040minfrin usage("Option -d must be greater than zero");
1018201f5223624476334c6e23aead02db7c4040minfrin }
1018201f5223624476334c6e23aead02db7c4040minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (isdaemon && (verbose || realclean || dryrun || listurls)) {
5876f43a746f688a32b7201bced8591ddf19bd43minfrin usage("Option -d cannot be used with -v, -r, -L or -D");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!isdaemon && intelligent) {
1018201f5223624476334c6e23aead02db7c4040minfrin usage("Option -i cannot be used without -d");
1018201f5223624476334c6e23aead02db7c4040minfrin }
1018201f5223624476334c6e23aead02db7c4040minfrin
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!listurls && max <= 0 && inodes <= 0) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin usage("At least one of option -l or -L must be greater than zero");
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (apr_filepath_get(&path, 0, pool) != APR_SUCCESS) {
46a4dc3f1dfeb9c4cb76727c56ed0a2230acb7cdjailletc usage(apr_psprintf(pool, "Could not get the filepath: %pm", &status));
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz baselen = strlen(path);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin if (pidfilename) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin log_pid(pool, pidfilename, &pidfile); /* before daemonizing, so we
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin * can report errors
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin */
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin
5876f43a746f688a32b7201bced8591ddf19bd43minfrin if (listurls) {
03ee592beedc99301c3c79838af79a333af47361minfrin list_urls(path, pool, round);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin return (interrupted != 0);
5876f43a746f688a32b7201bced8591ddf19bd43minfrin }
5876f43a746f688a32b7201bced8591ddf19bd43minfrin
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz#ifndef DEBUG
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (isdaemon) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_file_close(errfile);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin errfile = NULL;
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin if (pidfilename) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_file_close(pidfile); /* delete original pidfile only in parent */
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin if (pidfilename) {
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin log_pid(pool, pidfilename, &pidfile);
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin }
fc1206972499ab73c4dfded43cef279d8bf01ad9minfrin#endif
5dc4220fc22561537ce1421a03e11846a5b719ebminfrin
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd do {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_create(&instance, pool);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz now = apr_time_now();
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz APR_RING_INIT(&root, _entry, link);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delcount = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz unsolicited = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz dowork = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz switch (intelligent) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 0:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz dowork = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 1:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz retries = STAT_ATTEMPTS;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz status = APR_SUCCESS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd do {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_sleep(STAT_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz } while (status != APR_SUCCESS && !interrupted && --retries);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (status == APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz previous = info.mtime;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 2;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz dowork = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz case 2:
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz retries = STAT_ATTEMPTS;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz status = APR_SUCCESS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd do {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_sleep(STAT_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd } while (status != APR_SUCCESS && !interrupted && --retries);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (status == APR_SUCCESS) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (previous != info.mtime) {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd dowork = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd previous = info.mtime;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz dowork = 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz break;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (dowork && !interrupted) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin apr_off_t nodes = 0;
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin if (!process_dir(path, instance, &nodes) && !interrupted) {
f58bb3da705eb7ec926f4883597fc2eb1336a360minfrin purge(path, instance, max, inodes, nodes, round);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (!isdaemon && !interrupted) {
a51acf58d9d82f52e0ee56106cd9282030f3f3bend apr_file_printf(errfile, "An error occurred, cache cleaning "
a51acf58d9d82f52e0ee56106cd9282030f3f3bend "aborted." APR_EOL_STR);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (intelligent && !interrupted) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz retries = STAT_ATTEMPTS;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz status = APR_SUCCESS;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd do {
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd if (status != APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_sleep(STAT_DELAY);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz status = apr_stat(&info, path, APR_FINFO_MTIME, instance);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz } while (status != APR_SUCCESS && !interrupted && --retries);
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (status == APR_SUCCESS) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz previous = info.mtime;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 2;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz intelligent = 1;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_pool_destroy(instance);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz current = apr_time_now();
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (current < now) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delay = repeat;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else if (current - now >= repeat) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delay = repeat;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delay = now + repeat - current;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz /* we can't sleep the whole delay time here apiece as this is racy
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * with respect to interrupt delivery - think about what happens
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz * if we have tested for an interrupt, then get scheduled
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * before the apr_sleep() call and while waiting for the cpu
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd * we do get an interrupt
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz */
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (isdaemon) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz while (delay && !interrupted) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (delay > APR_USEC_PER_SEC) {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_sleep(APR_USEC_PER_SEC);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delay -= APR_USEC_PER_SEC;
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd }
03bdb4fb430b0d4e502ddfc75f7e9dbd91db72e9nd else {
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz apr_sleep(delay);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz delay = 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz } while (isdaemon && !interrupted);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz if (!isdaemon && interrupted) {
a51acf58d9d82f52e0ee56106cd9282030f3f3bend apr_file_printf(errfile, "Cache cleaning aborted due to user "
a51acf58d9d82f52e0ee56106cd9282030f3f3bend "request." APR_EOL_STR);
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 1;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz }
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz return 0;
9fe74ffcdea85800f04a7222f716f78ae60cce51jerenkrantz}