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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#!perl
use 5.6.0;
use strict;
use warnings FATAL => 'all';
use FindBin;
use File::Spec ();
use lib File::Spec->catdir( $FindBin::Bin, 'lib' );
# This is for the latest version.
use lib File::Spec->catdir( $FindBin::Bin, '..', 'lib' );
use Getopt::Long qw( GetOptions );
use Pod::Usage 1.3;
my %headerver_to_module = (
'0' => 'DBM::Deep::09830',
'2' => 'DBM::Deep::10002',
'3' => 'DBM::Deep',
'4' => 'DBM::Deep',
);
my %is_dev = (
'1' => 1,
);
my %opts = (
man => 0,
help => 0,
version => '2',
autobless => 1,
);
GetOptions( \%opts,
'input=s', 'output=s', 'version:s', 'autobless:i',
'help|?', 'man',
) || pod2man(2);
pod2usage(1) if $opts{help};
pod2usage(-exitstatus => 0, -verbose => 2) if $opts{man};
pod2usage(-msg => "Missing required parameters.", verbose => 1)
unless $opts{input} && $opts{output};
if ( $opts{input} eq $opts{output} ) {
_exit( "Cannot use the same filename for both input and output." );
}
unless ( -f $opts{input} ) {
_exit( "'$opts{input}' is not a file." );
}
my %db;
{
my $ver = _read_file_header( $opts{input} );
if ( $is_dev{ $ver } ) {
_exit( "'$opts{input}' is a dev release and not supported." );
}
my $mod = $headerver_to_module{ $ver };
eval "use $mod;";
if ( $@ ) {
_exit( "Cannot load '$mod' to read header version '$ver':\n\t$@" );
}
$db{input} = $mod->new({
file => $opts{input},
locking => 1,
autobless => $opts{autobless},
});
$db{input}->lock;
}
{
my $ver = $opts{version};
if ( $ver =~ /^2(?:\.|\z)/ ) {
$ver = 4;
}
elsif ( $ver =~ /^1\.001[0-4]/ ) {
$ver = 3;
}
elsif ( $ver =~ /^1\.000[3-9]/ ) {
$ver = 3;
}
elsif ( $ver eq '1.00' || $ver eq '1.000' || $ver =~ /^1\.000[0-2]/ ) {
$ver = 2;
}
elsif ( $ver =~ /^0\.99/ ) {
$ver = 1;
}
elsif ( $ver =~ /^0\.9[1-8]/ ) {
$ver = 0;
}
else {
_exit( "'$ver' is an unrecognized version." );
}
if ( $is_dev{ $ver } ) {
_exit( "-version '$opts{version}' is a dev release and not supported." );
}
# First thing is to destroy the file, in case it's an incompatible version.
unlink $opts{output};
my $mod = $headerver_to_module{ $ver };
eval "use $mod;";
if ( $@ ) {
_exit( "Cannot load '$mod' to read header version '$ver':\n\t$@" );
}
$db{output} = $mod->new({
file => $opts{output},
locking => 1,
autobless => $opts{autobless},
});
$db{output}->lock;
# Hack to write a version 3 file:
if($ver == 3) {
my $engine = $db{output}->_engine;
$engine->{v} = 3;
$engine->storage->print_at( 5, pack('N',3) );
}
}
# Do the actual conversion. This is the code that compress uses.
$db{input}->_copy_node( $db{output} );
undef $db{output};
################################################################################
sub _read_file_header {
my ($file) = @_;
open my $fh, '<', $file
or _exit( "Cannot open '$file' for reading: $!" );
my $buffer = _read_buffer( $fh, 9 );
_exit( "'$file' is not a DBM::Deep file." )
unless length $buffer == 9;
my ($file_sig, $header_sig, $header_ver) = unpack( 'A4 A N', $buffer );
# SIG_FILE == 'DPDB'
_exit( "'$file' is not a DBM::Deep file." )
unless $file_sig eq 'DPDB';
# SIG_HEADER == 'h' - this means that this is a pre-1.0 file
return 0 unless ($header_sig eq 'h');
return $header_ver;
}
sub _read_buffer {
my ($fh, $len) = @_;
my $buffer;
read( $fh, $buffer, $len );
return $buffer;
}
sub _exit {
my ($msg) = @_;
pod2usage( -verbose => 0, -msg => $msg );
}
__END__
=head1 NAME
upgrade_db.pl
=head1 SYNOPSIS
upgrade_db.pl -input <oldfile> -output <newfile>
=head1 DESCRIPTION
This will attempt to upgrade DB files from one version of DBM::Deep to
another. The version of the input file is detected from the file header. The
version of the output file defaults to the version of the distro in this file,
but can be set, if desired.
=head1 OPTIONS
=over 4
=item B<-input> (required)
This is the name of original DB file.
=item B<-output> (required)
This is the name of target output DB file.
=item B<-version>
Optionally, you can specify the version of L<DBM::Deep> for the output file.
This can either be an upgrade or a downgrade. The minimum version supported is
0.91.
If the version is the same as the input file, this acts like a compressed copy
of the database.
=item B<-autobless>
In pre-1.0000 versions, autoblessing was an optional setting defaulting to
false. Autobless in upgrade_db.pl defaults to true.
=item B<-help>
Prints a brief help message, then exits.
=item B<-man>
Prints a much longer message, then exits;
=back
=head1 CAVEATS
The following are known issues with this converter.
=over 4
=item * Diskspace requirements
This will require about twice the diskspace of the input file.
=item * Feature support
Not all versions support the same features. In particular, internal references
were supported in 0.983, removed in 1.000, and re-added in 1.0003. There is no
detection of this by upgrade_db.pl.
=back
=head1 MAINTAINER(S)
Rob Kinyon, L<rkinyon@cpan.org>
Originally written by Rob Kinyon, L<rkinyon@cpan.org>
=head1 LICENSE
Copyright (c) 2007 Rob Kinyon. All Rights Reserved.
This is free software, you may use it and distribute it under the
same terms as Perl itself.
=cut
|