]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/check_cache.pl
SourceFormat Enforcement
[thirdparty/squid.git] / scripts / check_cache.pl
CommitLineData
090089c4 1#!/usr/local/bin/perl
a151895d 2#
4ac4a490 3## Copyright (C) 1996-2017 The Squid Software Foundation and contributors
a151895d
AJ
4##
5## Squid software is distributed under GPLv2+ license and includes
6## contributions from numerous individuals and organizations.
7## Please see the COPYING and CONTRIBUTORS files for details.
8##
090089c4 9
b2d4b0ff 10# check_cache.pl
090089c4 11#
b2d4b0ff 12# Squid-1.0 version by martin hamilton <m.t.hamilton@lut.ac.uk>
13# Squid-1.1 version by Bertold Kolics <bertold@tohotom.vein.hu>
14#
15# Check the Squid-1.1.x cache directory for stale objects - i.e. those
090089c4 16# which exist on disk but aren't listed in cached's log file.
090089c4 17
090089c4 18require "getopts.pl";
b2d4b0ff 19&Getopts("c:drt:vh");
20# -c : the full path to squid.conf
21# -d : turn on debugging
22# -r : actually remove stale files
23# -t tmpdir : temporary directory
24# -v : list stale files
25# -h : print the help
090089c4 26
b2d4b0ff 27if ($opt_h) {
28 print "Usage: check_cache.pl -drvh -c squid.conf\n";
29 print "\t-c the full path to squid.conf\n";
30 print "\t-d turn on debugging\n";
31 print "\t-r actually remove stale files\n";
32 print "\t-t temporary directory\n";
33 print "\t-v list stale files\n";
34 print "\t-h print the help\n";
35 exit;
36}
37
38$squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
39open (squidconf) || die "$squidconf: $!\n";
40$no_cachedir = 0;
41$swaplog = '';
42$level1dirno = 16;
43$level2dirno = 256;
44while (<squidconf>) {
45 chop;
46 if (/^cache_dir\s+(.*)/) {
47 push (@cachedir, $1);
48 } elsif (/cache_swap_log\s+(.*)/) {
49 $swaplog = $1;
50 } elsif (/swap_level1_dirs/) {
51 $level1dirno = $1;
52 } elsif (/swap_level21_dirs/) {
53 $level2dirno = $1;
54 }
55}
56close (squidconf);
57push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
58$swaplog = $cachedir[0] . '/log' unless ($swaplog);
59$no_cachedir = $#cachedir + 1;
60print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
61print "SWAP LOG: $swaplog\n" if ($opt_d);
090089c4 62
b2d4b0ff 63$tmpdir = $opt_t || $ENV{TMPDIR} || "/var/tmp";
090089c4 64chdir($tmpdir);
65
b2d4b0ff 66# snarf file numbers from Squid log & sort em
67system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
090089c4 68system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
69
70# get list of files in cache & sort em
b2d4b0ff 71for ($i = 0 ; $i < $no_cachedir; $i++) {
72 chdir($cachedir[i]);
73 system("find ./ -print -type f > $tmpdir/fp$$");
74 chdir($tmpdir);
75# this cut prints only the lines with 4 fields so unnecessary lines
76# are supressed
77 system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
78}
090089c4 79system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
80
b2d4b0ff 81# get list of objects on disk (scd$$) but not in the log (spl$$)
090089c4 82system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
83
b2d4b0ff 84chdir($tmpdir);
090089c4 85# iterate through it
86open(IN, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
b2d4b0ff 87unlink("comm$$");
090089c4 88while(<IN>) {
89 chop;
b2d4b0ff 90 $filename = $_;
91
92# calculate the full path of the current filename
93 $fileno = hex($filename);
94 $dirno = $fileno % $no_cachedir;
95 $a = $fileno / $no_cachedir;
96 $level1 = sprintf("%02X", $a % $level1dirno);
97 $level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
98 $filename = "$cachedir[dirno]/$level1/$level2/$filename";
090089c4 99
b2d4b0ff 100 next if -d "$filename"; # don't want directories
101
102 print "$filename\n" if $opt_v; # print filename if asked
090089c4 103
104 # skip if cached file appeared since script started running
b2d4b0ff 105 if (-M $filename < 0) {
106 print STDERR "skipping $filename\n" if $opt_d;
090089c4 107 next;
108 }
b2d4b0ff 109 print "Orphan: $filename\n";
110 unlink($filename) if $opt_r; # only remove if asked!
090089c4 111}
112close(IN);