3 # Convert users.v2 or .v3 to JSON .v3j format
5 # It is believed that this can be run at any time...
7 # Copyright (c) 2020 Dirk Koopman G1TLH
12 # make sure that modules are searched in the order local then perl
15 # root of directory tree for this system
17 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
18 unshift @INC, "$root/perl"; # this IS the right way round!
19 unshift @INC, "$root/local";
28 use Data::Structure::Util qw(unbless);
29 use Time::HiRes qw(gettimeofday tv_interval);
40 my $json = JSON->new()->canonical(1);
41 my $ofn = localdata("$fn.v3j");
49 if ( ! -e localdata("$fn.v3") && -e localdata("$fn.v2") ) {
52 LogDbg('',"the module Storable appears to be missing!!");
53 LogDbg('',"trying to continue in compatibility mode (this may fail)");
54 LogDbg('',"please install Storable from CPAN as soon as possible");
57 import Storable qw(nfreeze thaw);
58 $convert = 3 if -e localdata("users.v3") && !-e $ufn;
61 die "need to have a $fn.v2 or (preferably) a $fn.v3 file in /spider/data or /spider/local_data\n" unless $convert;
64 my $nfn = localdata("$fn.v3j.new");
65 say "You appear to have (or are using) $ofn, creating $nfn instead";
69 say "using $ofn for output";
73 # do a conversion if required
75 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
76 my $ta = [gettimeofday];
77 my $ofh = IO::File->new(">$ofn") or die "cannot open $ofn ($!)\n";
82 LogDbg('',"Converting the User from V$convert format to $fn.v3j ");
83 LogDbg('',"This will take a while, maybe as much as 30 secs on very slow disks and/or machines");
84 my $idbm = tie (%oldu, 'DB_File', localdata("users.v$convert"), O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v$convert ($!) [rebuild it from user_asc?]";
85 my $odbm = tie (%newu, 'DB_File', $ofn, O_CREAT|O_RDWR, 0666, $DB_BTREE) or confess "can't open user file: $ofn ($!)";
86 for ($action = R_FIRST; !$idbm->seq($key, $val, $action); $action = R_NEXT) {
89 eval { $ref = storable_decode($val) };
92 eval { $ref = asc_decode($val) };
97 $newu{$ref->{call}} = $json->encode($ref);
105 Log('err', "DXUser: error decoding $@");
112 my $t = _diffms($ta);
113 LogDbg('',"Conversion from users.v$convert to $ofn completed $count records $err errors $t mS");
123 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
126 LogDbg('err', "asc_decode: on '$s' $@");