]>
Commit | Line | Data |
---|---|---|
090089c4 | 1 | #!/usr/local/bin/perl |
2 | ||
3 | # flag_truncs.pl - martin hamilton <m.t.hamilton@lut.ac.uk> | |
4 | # | |
5 | # Check the CERN/Harvest/Netscape cache for truncated objects | |
6 | # - i.e. those for which there is a "Content-length:" HTTP header, | |
7 | # and this does not match the size of the cached object | |
8 | ||
090089c4 | 9 | require "getopts.pl"; |
10 | require "stat.pl"; | |
11 | &Getopts("cd"); | |
12 | # -c -> just count the number of objects with a Content-length header | |
13 | # -d -> turn on debugging output | |
14 | ||
15 | # pass filenames on command line or via STDIN | |
16 | @things = $#ARGV >= 0 ? @ARGV : <STDIN>; | |
17 | ||
18 | $total_objects = 0, $content_length = 0; | |
19 | ||
20 | # iterate through them | |
21 | foreach $thing (@things) { | |
22 | chop $thing; | |
23 | ||
24 | $opt_d && (print STDERR ">> inspecting: $thing\n"); | |
25 | next if -d "$thing"; # don't want directories | |
26 | ||
27 | $size = (stat($thing))[$ST_SIZE]||next; | |
28 | $opt_d && (print STDERR ">> stat: $size\n"); | |
29 | print "$thing\n", next if ($size == 0); | |
30 | ||
31 | $total_objects++; | |
32 | ||
33 | $count = 0, $expected = 0; | |
34 | open(IN, "$thing") || die "Can't open cached object $thing: $!"; | |
35 | while(<IN>) { | |
36 | $count += length($_); | |
37 | chop; | |
38 | print STDERR ">> inspecting $_\n" if $opt_d; | |
39 | last if /^(\s+|)$/; # drop out after the end of the HTTP headers | |
40 | ||
41 | # skip if cached file appeared since script started running | |
42 | if (-M $_ < 0) { | |
43 | print STDERR ">> skipping $_\n" if $opt_d; | |
44 | next; | |
45 | } | |
46 | ||
47 | if (/^Content-length:\s+(\d+)/i) { | |
48 | $expected = $1; | |
49 | $content_length++; | |
50 | } | |
51 | } | |
52 | close(IN); | |
53 | ||
54 | next if $opt_c; | |
55 | next if $expected == 0; # no Content-length header | |
56 | ||
57 | # looked at the headers now | |
58 | $difference = $size - $count; | |
59 | $opt_d && print STDERR ">> real: ", $difference, ", expected: $expected\n"; | |
60 | if ($difference != $expected) { | |
61 | print "$thing (expected: $expected, got: $difference)\n"; | |
62 | } | |
63 | } | |
64 | ||
65 | print "$content_length out of $total_objects had Content-length: header\n" | |
66 | if $opt_c; |