]>
git.ipfire.org Git - thirdparty/squid.git/blob - scripts/check_cache.pl
3 ## Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
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>
15 # Check the Squid-1.1.x cache directory for stale objects - i.e. those
16 # which exist on disk but aren't listed in cached's log file.
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
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";
38 $squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
39 open (squidconf
) || die "$squidconf: $!\n";
46 if (/^cache_dir\s+(.*)/) {
48 } elsif (/cache_swap_log\s+(.*)/) {
50 } elsif (/swap_level1_dirs/) {
52 } elsif (/swap_level21_dirs/) {
57 push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
58 $swaplog = $cachedir[0] . '/log' unless ($swaplog);
59 $no_cachedir = $#cachedir + 1;
60 print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
61 print "SWAP LOG: $swaplog\n" if ($opt_d);
63 $tmpdir = $opt_t || $ENV{TMPDIR
} || "/var/tmp";
66 # snarf file numbers from Squid log & sort em
67 system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
68 system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
70 # get list of files in cache & sort em
71 for ($i = 0 ; $i < $no_cachedir; $i++) {
73 system("find ./ -print -type f > $tmpdir/fp$$");
75 # this cut prints only the lines with 4 fields so unnecessary lines
77 system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
79 system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
81 # get list of objects on disk (scd$$) but not in the log (spl$$)
82 system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
86 open(IN
, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
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";
100 next if -d
"$filename"; # don't want directories
102 print "$filename\n" if $opt_v; # print filename if asked
104 # skip if cached file appeared since script started running
105 if (-M
$filename < 0) {
106 print STDERR
"skipping $filename\n" if $opt_d;
109 print "Orphan: $filename\n";
110 unlink($filename) if $opt_r; # only remove if asked!