]>
Commit | Line | Data |
---|---|---|
d36b569d | 1 | #!/usr/local/bin/perl |
a151895d | 2 | # |
b8ae064d | 3 | ## Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
a151895d AJ |
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 | ## | |
d36b569d | 9 | |
d36b569d | 10 | select(STDERR); $|=1; |
11 | select(STDOUT); $|=1; | |
12 | ||
5e5ff464 | 13 | $USAGE="Usage: $0 swaplog cachedir1 cachedir2 ...\n"; |
d36b569d | 14 | |
15 | $dry_run = 0; | |
16 | ||
17 | $swaplog = shift || die $USAGE; | |
18 | (@cachedirs = @ARGV) || die $USAGE; | |
19 | $ncache_dirs = $#cachedirs + 1; | |
20 | ||
21 | $OLD_SWAP_DIRECTORIES = 100; | |
22 | $NEW_SWAP_DIRECTORIES_L1 = 16; | |
23 | $NEW_SWAP_DIRECTORIES_L2 = 256; | |
24 | ||
47f28373 | 25 | $EEXIST = 17; # check your /usr/include/errno.h |
d36b569d | 26 | |
27 | print <<EOF; | |
28 | This script converts Squid 1.0 cache directories to the Squid 1.1 | |
29 | format. The first step is to create the new directory structure. | |
30 | The second step is to link the swapfiles from the old directories | |
31 | into the new directories. After this script runs you must manually | |
32 | remove the old directories. | |
33 | ||
34 | Filesystem operations are slow, so this script may take a while. | |
35 | Your cache should NOT be running while this script runs. | |
36 | ||
37 | Are you ready to proceed? | |
38 | EOF | |
39 | ||
40 | $ans = <STDIN>; | |
41 | ||
42 | exit(1) unless ($ans =~ /^y$/ || $ans =~ /^yes$/); | |
43 | ||
44 | # make new directories | |
45 | foreach $c (@cachedirs) { | |
47f28373 FC |
46 | $cn = "$c.new"; |
47 | &my_mkdir ($cn); | |
48 | foreach $d1 (0..($NEW_SWAP_DIRECTORIES_L1-1)) { | |
49 | $p1 = sprintf ("$cn/%02X", $d1); | |
50 | &my_mkdir ($p1); | |
51 | foreach $d2 (0..($NEW_SWAP_DIRECTORIES_L2-1)) { | |
52 | $p2 = sprintf ("$p1/%02X", $d2); | |
53 | &my_mkdir ($p2); | |
54 | } | |
55 | } | |
d36b569d | 56 | } |
57 | ||
58 | $newlog = "$swaplog.1.1"; | |
59 | open (newlog, ">$newlog") || die "$newlog: $!\n"; | |
60 | select(newlog); $|=1; select(STDOUT); | |
47f28373 | 61 | open (swaplog) || die "$swaplog: $!\n"; |
d36b569d | 62 | $count = 0; |
63 | while (<swaplog>) { | |
47f28373 FC |
64 | chop; |
65 | ($file,$url,$expires,$timestamp,$size) = split; | |
66 | @F = split('/', $file); | |
67 | $oldfileno = pop @F; | |
68 | $oldpath = &old_fileno_to_path($oldfileno); | |
69 | unless (@S = stat($oldpath)) { | |
70 | print "$oldpath: $!\n"; | |
71 | next; | |
72 | } | |
73 | unless ($S[7] == $size) { | |
74 | print "$oldpath: Wrong Size.\n"; | |
75 | next; | |
76 | } | |
77 | $newpath = &new_fileno_to_path($oldfileno); | |
78 | next unless &my_link($oldpath,$newpath); | |
79 | printf newlog "%08x %08x %08x %08x %9d %s\n", | |
80 | $oldfileno, | |
81 | $timestamp, | |
82 | $expires, | |
83 | $timestamp, # lastmod | |
84 | $size, | |
85 | $url; | |
86 | $count++; | |
d36b569d | 87 | } |
88 | ||
89 | ||
90 | print <<EOF; | |
91 | Done converting. | |
92 | ||
93 | $count files were linked to the new directories. | |
94 | ||
95 | At this point you need to manually run these commands: | |
96 | EOF | |
97 | ||
98 | foreach $c (@cachedirs) { | |
99 | print " /bin/mv $c $c.old; /bin/mv $c.new $c\n"; | |
100 | } | |
101 | ||
102 | print <<EOF; | |
103 | /bin/mv $swaplog $swaplog.old; /bin/mv $newlog $swaplog\n"; | |
104 | ||
105 | And then start up Squid version 1.1. | |
106 | EOF | |
107 | exit(0); | |
108 | ||
109 | ||
110 | ||
111 | ||
112 | ||
113 | sub old_fileno_to_path { | |
47f28373 FC |
114 | local($fn) = @_; |
115 | sprintf ("%s/%02d/%d", | |
116 | $cachedirs[$fn % $ncache_dirs], | |
117 | ($fn / $ncache_dirs) % $OLD_SWAP_DIRECTORIES, | |
118 | $fn); | |
d36b569d | 119 | } |
120 | ||
121 | sub new_fileno_to_path { | |
47f28373 FC |
122 | local($fn) = @_; |
123 | sprintf ("%s.new/%02X/%02X/%08X", | |
124 | $cachedirs[$fn % $ncache_dirs], | |
125 | ($fn / $ncache_dirs) % $NEW_SWAP_DIRECTORIES_L1, | |
126 | ($fn / $ncache_dirs) / $NEW_SWAP_DIRECTORIES_L1 % $NEW_SWAP_DIRECTORIES_L2, | |
127 | $fn); | |
d36b569d | 128 | } |
129 | ||
130 | sub my_mkdir { | |
47f28373 FC |
131 | local($p) = @_; |
132 | print "Making $p...\n"; | |
133 | return if ($dry_run); | |
134 | unless (mkdir ($p, 0755)) { | |
135 | return 1 if ($! == $EEXIST); | |
136 | die "$p: $!\n"; | |
137 | } | |
d36b569d | 138 | } |
139 | ||
140 | sub my_link { | |
47f28373 FC |
141 | local($f,$t) = @_; |
142 | print "$f --> $t\n"; | |
143 | return 1 if ($dry_run); | |
144 | unlink($t); | |
145 | $rc = link ($f,$t); | |
146 | warn "$t: $!\n" unless ($rc); | |
147 | $rc; | |
d36b569d | 148 | } |