<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/local/bin/perl
#
## Copyright (C) 1996-2023 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 &lt;&lt;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 = &lt;STDIN&gt;;

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

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

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


print &lt;&lt;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 &lt;&lt;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 --&gt; $t\n";
    return 1 if ($dry_run);
    unlink($t);
    $rc = link ($f,$t);
    warn "$t: $!\n" unless ($rc);
    $rc;
}
</pre></body></html>