]>
git.ipfire.org Git - thirdparty/squid.git/blob - scripts/check_cache.pl
5 # Squid-1.0 version by martin hamilton <m.t.hamilton@lut.ac.uk>
6 # Squid-1.1 version by Bertold Kolics <bertold@tohotom.vein.hu>
8 # Check the Squid-1.1.x cache directory for stale objects - i.e. those
9 # which exist on disk but aren't listed in cached's log file.
13 # -c : the full path to squid.conf
14 # -d : turn on debugging
15 # -r : actually remove stale files
16 # -t tmpdir : temporary directory
17 # -v : list stale files
21 print "Usage: check_cache.pl -drvh -c squid.conf\n";
22 print "\t-c the full path to squid.conf\n";
23 print "\t-d turn on debugging\n";
24 print "\t-r actually remove stale files\n";
25 print "\t-t temporary directory\n";
26 print "\t-v list stale files\n";
27 print "\t-h print the help\n";
31 $squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
32 open (squidconf
) || die "$squidconf: $!\n";
39 if (/^cache_dir\s+(.*)/) {
41 } elsif (/cache_swap_log\s+(.*)/) {
43 } elsif (/swap_level1_dirs/) {
45 } elsif (/swap_level21_dirs/) {
50 push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
51 $swaplog = $cachedir[0] . '/log' unless ($swaplog);
52 $no_cachedir = $#cachedir + 1;
53 print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
54 print "SWAP LOG: $swaplog\n" if ($opt_d);
56 $tmpdir = $opt_t || $ENV{TMPDIR
} || "/var/tmp";
59 # snarf file numbers from Squid log & sort em
60 system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
61 system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
63 # get list of files in cache & sort em
64 for ($i = 0 ; $i < $no_cachedir; $i++) {
66 system("find ./ -print -type f > $tmpdir/fp$$");
68 # this cut prints only the lines with 4 fields so unnecessary lines
70 system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
72 system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
74 # get list of objects on disk (scd$$) but not in the log (spl$$)
75 system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
79 open(IN
, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
85 # calculate the full path of the current filename
86 $fileno = hex($filename);
87 $dirno = $fileno % $no_cachedir;
88 $a = $fileno / $no_cachedir;
89 $level1 = sprintf("%02X", $a % $level1dirno);
90 $level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
91 $filename = "$cachedir[dirno]/$level1/$level2/$filename";
93 next if -d
"$filename"; # don't want directories
95 print "$filename\n" if $opt_v; # print filename if asked
97 # skip if cached file appeared since script started running
98 if (-M
$filename < 0) {
99 print STDERR
"skipping $filename\n" if $opt_d;
102 print "Orphan: $filename\n";
103 unlink($filename) if $opt_r; # only remove if asked!