]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/fileno-to-pathname.pl
ad924baeac7e9261184816ed4fd99e61215b39c3
[thirdparty/squid.git] / scripts / fileno-to-pathname.pl
1 #!/usr/bin/perl -w
2 #
3 ## Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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 ##
9
10 # Convert hexadecimal cache file numbers (from swap log) into full pathnames.
11 # Duane Wessels 6/30/97
12
13 # 2001-12-18 Adapted for squid-2.x Alain Thivillon <at@rominet.net>
14 # -w and use strict;
15 # Getopt::Std
16
17 use strict;
18 use vars qw($opt_c);
19 use Getopt::Std;
20
21 &getopts('c:');
22
23 my @L1 = ();
24 my @L2 = ();
25 my @CD = ();
26
27 my $SWAP_DIR_SHIFT=24;
28 my $SWAP_FILE_MASK=0x00FFFFFF;
29
30 my $CF = $opt_c || '/usr/local/squid/etc/squid.conf';
31 &usage unless (open (CF,"<$CF"));
32
33 my $ncache_dirs = 0;
34
35 while (<CF>) {
36 # Squid 2.3 ===>
37 # cache_dir ufs path size L1 L2
38 if (/^cache_dir\s+(\S+)\s+(\S+)\s+\d+\s+(\S+)\s+(\S+)/i) {
39 $CD[$ncache_dirs] = $2;
40 $L1[$ncache_dirs] = $3;
41 $L2[$ncache_dirs++] = $4;
42 }
43 }
44 close(CF);
45
46 if ($ncache_dirs == 0) {
47 print STDERR "No proper cache_dir line found\n";
48 exit 2;
49 }
50
51 while (<>) {
52 chop;
53 print &storeSwapFullPath(hex($_)), "\n";
54 }
55
56 sub storeSwapFullPath {
57 my($fn) = @_;
58
59 my $dirn = ($fn >> $SWAP_DIR_SHIFT) % $ncache_dirs;
60 my $filn = $fn & $SWAP_FILE_MASK;
61
62 sprintf "%s/%02X/%02X/%08X",
63 $CD[$dirn],
64 (($fn / $L2[$dirn]) / $L2[$dirn]) % $L1[$dirn],
65 ($fn / $L2[$dirn]) % $L2[$dirn],
66 $fn;
67 }
68
69 sub usage {
70 print STDERR "usage: $0 -c config\n";
71 print STDERR "hexadecimal file numbers are read from stdin\n";
72 exit 1;
73 }