File: upgrade-1.0-store.pl

package info (click to toggle)
squid 7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,440 kB
  • sloc: cpp: 184,513; ansic: 12,442; sh: 5,688; makefile: 5,247; perl: 2,560; sql: 326; python: 240; awk: 141; sed: 1
file content (148 lines) | stat: -rwxr-xr-x 3,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/local/bin/perl
#
## Copyright (C) 1996-2025 The Squid Software Foundation and contributors
##
## Squid software is distributed under GPLv2+ license and includes
## contributions from numerous individuals and organizations.
## Please see the COPYING and CONTRIBUTORS files for details.
##

select(STDERR); $|=1;
select(STDOUT); $|=1;

$USAGE="Usage: $0 swaplog cachedir1 cachedir2 ...\n";

$dry_run = 0;

$swaplog = shift || die $USAGE;
(@cachedirs = @ARGV) || die $USAGE;
$ncache_dirs = $#cachedirs + 1;

$OLD_SWAP_DIRECTORIES = 100;
$NEW_SWAP_DIRECTORIES_L1 = 16;
$NEW_SWAP_DIRECTORIES_L2 = 256;

$EEXIST = 17;         # check your /usr/include/errno.h

print <<EOF;
This script converts Squid 1.0 cache directories to the Squid 1.1
format.  The first step is to create the new directory structure.
The second step is to link the swapfiles from the old directories
into the new directories.  After this script runs you must manually
remove the old directories.

Filesystem operations are slow, so this script may take a while.
Your cache should NOT be running while this script runs.

Are you ready to proceed?
EOF

$ans = <STDIN>;

exit(1) unless ($ans =~ /^y$/ || $ans =~ /^yes$/);

# make new directories
foreach $c (@cachedirs) {
    $cn = "$c.new";
    &my_mkdir ($cn);
    foreach $d1 (0..($NEW_SWAP_DIRECTORIES_L1-1)) {
        $p1 = sprintf ("$cn/%02X", $d1);
        &my_mkdir ($p1);
        foreach $d2 (0..($NEW_SWAP_DIRECTORIES_L2-1)) {
            $p2 = sprintf ("$p1/%02X", $d2);
            &my_mkdir ($p2);
        }
    }
}

$newlog = "$swaplog.1.1";
open (newlog, ">$newlog") || die "$newlog: $!\n";
select(newlog); $|=1; select(STDOUT);
open (swaplog)    || die "$swaplog: $!\n";
$count = 0;
while (<swaplog>) {
    chop;
    ($file,$url,$expires,$timestamp,$size) = split;
    @F = split('/', $file);
    $oldfileno = pop @F;
    $oldpath = &old_fileno_to_path($oldfileno);
    unless (@S = stat($oldpath)) {
        print "$oldpath: $!\n";
        next;
    }
    unless ($S[7] == $size) {
        print "$oldpath: Wrong Size.\n";
        next;
    }
    $newpath = &new_fileno_to_path($oldfileno);
    next unless &my_link($oldpath,$newpath);
    printf newlog "%08x %08x %08x %08x %9d %s\n",
        $oldfileno,
        $timestamp,
        $expires,
        $timestamp,    # lastmod
        $size,
        $url;
    $count++;
}


print <<EOF;
Done converting.

$count files were linked to the new directories.

At this point you need to manually run these commands:
EOF

foreach $c (@cachedirs) {
    print "    /bin/mv $c $c.old; /bin/mv $c.new $c\n";
}

print <<EOF;
    /bin/mv $swaplog $swaplog.old; /bin/mv $newlog $swaplog\n";

And then start up Squid version 1.1.
EOF
exit(0);





sub old_fileno_to_path {
    local($fn) = @_;
    sprintf ("%s/%02d/%d",
        $cachedirs[$fn % $ncache_dirs],
        ($fn / $ncache_dirs) % $OLD_SWAP_DIRECTORIES,
        $fn);
}

sub new_fileno_to_path {
    local($fn) = @_;
    sprintf ("%s.new/%02X/%02X/%08X",
        $cachedirs[$fn % $ncache_dirs],
        ($fn / $ncache_dirs) % $NEW_SWAP_DIRECTORIES_L1,
        ($fn / $ncache_dirs) / $NEW_SWAP_DIRECTORIES_L1 % $NEW_SWAP_DIRECTORIES_L2,
        $fn);
}

sub my_mkdir {
    local($p) = @_;
    print "Making $p...\n";
    return if ($dry_run);
    unless (mkdir ($p, 0755)) {
        return 1 if ($! == $EEXIST);
        die "$p: $!\n";
    }
}

sub my_link {
    local($f,$t) = @_;
    print "$f --> $t\n";
    return 1 if ($dry_run);
    unlink($t);
    $rc = link ($f,$t);
    warn "$t: $!\n" unless ($rc);
    $rc;
}