File Coverage

blib/lib/Business/BancaSella/Ric/FileFast.pm
Criterion Covered Total %
statement 53 70 75.7
branch 18 40 45.0
condition n/a
subroutine 5 7 71.4
pod 3 6 50.0
total 79 123 64.2


line stmt bran cond sub pod time code
1             #
2             # Business::BancaSella::Ric::FileFast
3             #
4             # author : Marco Gazerro
5             # initial date : 06/02/2001 ( originally in Open2b, www.open2b.com )
6             #
7             # version : 0.11
8             # date : 11/01/2002
9             #
10             # Copyright (c) 2001-2002 Marco Gazerro, Mauro Fedele
11             #
12             # This library is free software; you can redistribute it and/or
13             # modify it under the same terms as Perl itself.
14             #
15              
16             package Business::BancaSella::Ric::FileFast;
17              
18             $VERSION = '0.11';
19 0     0 0 0 sub Version { $VERSION }
20              
21             require 5.004;
22              
23 1     1   1810 use strict;
  1         2  
  1         3072  
24              
25             my $_DEBUG = 0;
26              
27             sub new {
28 1     1 0 17 my $class = shift;
29 1         4 my $self = bless { }, $class;
30 1         7 return $self->init(@_);
31             }
32              
33             sub init {
34 1     1 0 5 my ($self,%options) = @_;
35 1 50       5 if ( $options{'file'} eq '' ) {
36 0         0 die "You must declare file in " . ref($self) . "::new";
37             }
38 1         7 $self->{'file'} = $options{file};
39 1         4 return $self;
40             }
41              
42             sub file {
43 0     0 1 0 my ($self,$value) = @_;
44 0 0       0 $self->{'file'} = $value if defined $value;
45 0         0 return $self->{'file'};
46             }
47              
48             #
49             # extract a password from the ric file
50             #
51             # return the password extracted
52             # raise an exception 'SYSTEM. description' on I/O error
53             # raise an exception 'CORRUPT. description' if the file is corrupted
54             #
55             sub extract {
56 2     2 1 1627 my $self = shift;
57              
58 2         3 my $password;
59              
60             # open the file
61 2 50       74 open(REQUEST,"+<$self->{'file'}")
62             || die "SYSTEM. opening $self->{'file'} : $!\n";
63              
64 2         5 eval {
65              
66             # lock the file
67 2         3 my $has_lock = eval { flock(REQUEST,2) };
  2         13  
68 2 50       10 if ( $@ ) {
    50          
69 0         0 warn "WARNING. this platform don't implements 'flock'\n";
70             } elsif ( ! $has_lock ) {
71 0         0 die "SYSTEM. locking $self->{'file'} : $!\n";
72             }
73              
74             # length of a row of password
75 2         3 my $row_length = 33;
76              
77 2         3 my $size_bytes;
78 2 50       24 unless ( $size_bytes = (stat(REQUEST))[7] ) {
79 0 0       0 die (( $! ) ? $! : "EMPTY : the file $self->{'file'} is empty\n" );
80             }
81 2 50       10 if ( $size_bytes % $row_length != 0 ) {
82 0         0 die "CORRUPT. dimension of $self->{'file'} is wrong\n";
83             }
84              
85             # number of passwords in the file
86 2         5 my $size = $size_bytes / $row_length;
87              
88             # read the last password
89 2         3 my $row;
90 2 50       14 seek(REQUEST,($size-1)*$row_length,0)
91             || die "SYSTEM. while seek in $self->{'file'} : $!\n";
92              
93 2 50       40 read(REQUEST,$row,$row_length) || die "SYSTEM. reading $self->{'file'} : $!\n";
94              
95 2 50       13 unless ( $row =~ /^([a-zA-Z0-9]{32})\n$/ ) {
96 0         0 die "CORRUPT. file $self->{'file'} corrupted at last line\n";
97             }
98 2         6 $password = $1;
99              
100             # delete the last password
101 2         4 my $is_truncate = eval { truncate(REQUEST,($size-1)*$row_length) };
  2         79  
102 2 50       6 if ( $@ ) {
103 0         0 die "SYSTEM. the 'truncate' function is not implemented on this platform!\n";
104             }
105 2 50       9 unless ( $is_truncate ) {
106 0         0 die "SYSTEM. while truncate $self->{'file'} : $!\n";
107             }
108              
109             }; # end eval
110              
111 2         4 my $error = $@;
112              
113             # close the file
114 2         23 close(REQUEST);
115              
116             # die on error
117 2 50       7 die $error if $error;
118              
119             # return the password
120 2         7 return $password;
121             }
122              
123             #
124             # create the work copy of a ric file
125             #
126             # return nothing
127             # raise an exception on error
128             #
129             sub prepare {
130 1     1 1 356 my ($self,$source_file) = @_;
131              
132             # read the passwords
133 1 50       52 open(SOURCE,"<$source_file") || die "SYSTEM. opening $source_file : $!\n";
134 1         390 my @rows = ;
135 1 50       23 if ( $! ) {
136 0         0 die "SYSTEM. reading $source_file : $!\n";
137             }
138 1 50       14 close(SOURCE) || die "SYSTEM. closing $source_file : $!\n";
139              
140             # verify the passwords
141 1         3 my @passwords = ();
142 1         2 my $line = 1;
143 1         3 foreach my $row ( @rows ) {
144 468 50       1468 unless ( $row =~ /^([a-zA-Z0-9]{32})\n+$/ ) {
145 0         0 die "CORRUPT. file $source_file corrupted at line $line\n";
146             }
147 468         1060 push @passwords, ($1);
148             }
149              
150             # write the passwords
151 1 50       126 open(TARGET,"+>$self->{'file'}") || die "SYSTEM. opening $self->{'file'} : $!\n";
152 1         5 binmode(TARGET);
153 1         3 $line = 1;
154 1         4 foreach my $password ( @passwords ) {
155 468 50       1036 unless ( print TARGET "$password\n" ) {
156 0         0 close(TARGET);
157 0         0 unlink($self->{'file'});
158 0         0 die "SYSTEM. writing file $self->{'file'} at line $line: $!\n";
159             }
160 468         575 $line++;
161             }
162 1         48 close(TARGET);
163              
164 1         80 return;
165             }
166              
167             1;