]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/upgrade-1.0-store.pl
SourceFormat Enforcement
[thirdparty/squid.git] / scripts / upgrade-1.0-store.pl
CommitLineData
d36b569d 1#!/usr/local/bin/perl
a151895d 2#
4ac4a490 3## Copyright (C) 1996-2017 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 10select(STDERR); $|=1;
11select(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
25$EEXIST = 17; # check your /usr/include/errno.h
26
27print <<EOF;
28This script converts Squid 1.0 cache directories to the Squid 1.1
29format. The first step is to create the new directory structure.
30The second step is to link the swapfiles from the old directories
31into the new directories. After this script runs you must manually
32remove the old directories.
33
34Filesystem operations are slow, so this script may take a while.
35Your cache should NOT be running while this script runs.
36
37Are you ready to proceed?
38EOF
39
40$ans = <STDIN>;
41
42exit(1) unless ($ans =~ /^y$/ || $ans =~ /^yes$/);
43
44# make new directories
45foreach $c (@cachedirs) {
46 $cn = "$c.new";
47 &my_mkdir ($cn);
48 foreach $d1 (0..($NEW_SWAP_DIRECTORIES_L1-1)) {
5e5ff464 49 $p1 = sprintf ("$cn/%02X", $d1);
d36b569d 50 &my_mkdir ($p1);
51 foreach $d2 (0..($NEW_SWAP_DIRECTORIES_L2-1)) {
5e5ff464 52 $p2 = sprintf ("$p1/%02X", $d2);
d36b569d 53 &my_mkdir ($p2);
54 }
55 }
56}
57
58$newlog = "$swaplog.1.1";
59open (newlog, ">$newlog") || die "$newlog: $!\n";
60select(newlog); $|=1; select(STDOUT);
61open (swaplog) || die "$swaplog: $!\n";
62$count = 0;
63while (<swaplog>) {
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++;
87}
88
89
90print <<EOF;
91Done converting.
92
93$count files were linked to the new directories.
94
95At this point you need to manually run these commands:
96EOF
97
98foreach $c (@cachedirs) {
99 print " /bin/mv $c $c.old; /bin/mv $c.new $c\n";
100}
101
102print <<EOF;
103 /bin/mv $swaplog $swaplog.old; /bin/mv $newlog $swaplog\n";
104
105And then start up Squid version 1.1.
106EOF
107exit(0);
108
109
110
111
112
113sub old_fileno_to_path {
114 local($fn) = @_;
5e5ff464 115 sprintf ("%s/%02d/%d",
d36b569d 116 $cachedirs[$fn % $ncache_dirs],
117 ($fn / $ncache_dirs) % $OLD_SWAP_DIRECTORIES,
5e5ff464 118 $fn);
d36b569d 119}
120
121sub new_fileno_to_path {
122 local($fn) = @_;
5e5ff464 123 sprintf ("%s.new/%02X/%02X/%08X",
d36b569d 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,
5e5ff464 127 $fn);
d36b569d 128}
129
130sub my_mkdir {
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 }
138}
139
140sub my_link {
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;
148}