]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/check_cache.pl
Import of fix-ranges branch
[thirdparty/squid.git] / scripts / check_cache.pl
1 #!/usr/local/bin/perl
2
3 # check_cache.pl
4 #
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>
7 #
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.
10
11 # $Id: check_cache.pl,v 1.5 2003/01/23 00:37:06 robertc Exp $
12
13 require "getopts.pl";
14 &Getopts("c:drt:vh");
15 # -c : the full path to squid.conf
16 # -d : turn on debugging
17 # -r : actually remove stale files
18 # -t tmpdir : temporary directory
19 # -v : list stale files
20 # -h : print the help
21
22 if ($opt_h) {
23 print "Usage: check_cache.pl -drvh -c squid.conf\n";
24 print "\t-c the full path to squid.conf\n";
25 print "\t-d turn on debugging\n";
26 print "\t-r actually remove stale files\n";
27 print "\t-t temporary directory\n";
28 print "\t-v list stale files\n";
29 print "\t-h print the help\n";
30 exit;
31 }
32
33 $squidconf = $opt_c || "/usr/local/squid/etc/squid.conf";
34 open (squidconf) || die "$squidconf: $!\n";
35 $no_cachedir = 0;
36 $swaplog = '';
37 $level1dirno = 16;
38 $level2dirno = 256;
39 while (<squidconf>) {
40 chop;
41 if (/^cache_dir\s+(.*)/) {
42 push (@cachedir, $1);
43 } elsif (/cache_swap_log\s+(.*)/) {
44 $swaplog = $1;
45 } elsif (/swap_level1_dirs/) {
46 $level1dirno = $1;
47 } elsif (/swap_level21_dirs/) {
48 $level2dirno = $1;
49 }
50 }
51 close (squidconf);
52 push (@cachedir, '/usr/local/squid/cache') unless ($#cachedir > $[-1);
53 $swaplog = $cachedir[0] . '/log' unless ($swaplog);
54 $no_cachedir = $#cachedir + 1;
55 print "$no_cachedir CACHE DIRS: ", join(' ', @cachedir), "\n" if ($opt_d);
56 print "SWAP LOG: $swaplog\n" if ($opt_d);
57
58 $tmpdir = $opt_t || $ENV{TMPDIR} || "/var/tmp";
59 chdir($tmpdir);
60
61 # snarf file numbers from Squid log & sort em
62 system("cut -f1 -d' ' $swaplog |tr [a-z] [A-Z] >pl$$");
63 system("sort -T $tmpdir pl$$ >spl$$; rm pl$$");
64
65 # get list of files in cache & sort em
66 for ($i = 0 ; $i < $no_cachedir; $i++) {
67 chdir($cachedir[i]);
68 system("find ./ -print -type f > $tmpdir/fp$$");
69 chdir($tmpdir);
70 # this cut prints only the lines with 4 fields so unnecessary lines
71 # are supressed
72 system("cut -d'/' -f4 -s fp$$ >> cd$$ ; rm fp$$")
73 }
74 system("sort -T $tmpdir cd$$ >scd$$; rm cd$$");
75
76 # get list of objects on disk (scd$$) but not in the log (spl$$)
77 system("comm -13 spl$$ scd$$ >comm$$; rm spl$$ scd$$");
78
79 chdir($tmpdir);
80 # iterate through it
81 open(IN, "comm$$") || die "Can't open temporary file $tmpdir/comm$$: $!";
82 unlink("comm$$");
83 while(<IN>) {
84 chop;
85 $filename = $_;
86
87 # calculate the full path of the current filename
88 $fileno = hex($filename);
89 $dirno = $fileno % $no_cachedir;
90 $a = $fileno / $no_cachedir;
91 $level1 = sprintf("%02X", $a % $level1dirno);
92 $level2 = sprintf("%02X", $a / $level1dirno % $level2dirno);
93 $filename = "$cachedir[dirno]/$level1/$level2/$filename";
94
95 next if -d "$filename"; # don't want directories
96
97 print "$filename\n" if $opt_v; # print filename if asked
98
99 # skip if cached file appeared since script started running
100 if (-M $filename < 0) {
101 print STDERR "skipping $filename\n" if $opt_d;
102 next;
103 }
104 print "Orphan: $filename\n";
105 unlink($filename) if $opt_r; # only remove if asked!
106 }
107 close(IN);