serialno/File.pm

filter: Statementstmt 91.6% 77 / 84 Branchbran 55.7% 29 / 52 Conditioncond 42.8% 9 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 85.7% 12 / 14 Podpod 100% 2 / 2 totaltotal 69.8% 130 / 186 CC 35 SCAR 32.7
CC
35
Cov
73%
_initialize
48.9
get_serialno
11.0
_full_filename
2.0
CRAP 26.2
↑ Summary serialno.pm »
1 package FTN::Message::serialno::File;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use parent 'FTN::Message::serialno';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 FILE_EXTENSION => 'sn',
12 FILENAME_PATTERN => '^([0-9a-fA-F]{1,8})$',
13 FILENAME_FORMAT => '%x',
14 SERIALNO_FORMAT => '%08x',
15 MAX_TRIES => 5,
16 3 };
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 FTN::Message::serialno::File - handles FTN message serialno via file in dedicated directory
23  
24 =head1 VERSION
25  
26 Version 20141121
27  
28 =cut
29  
30 3 our $VERSION = '20141121';
31  
32 =head1 SYNOPSIS
33  
34 use FTN::Message::serialno::File ();
35  
36 my $serialno = FTN::Message::serialno::File -> new( directory => '/home/user/ftn/_serialno' );
37  
38 my $new_serialno = $serialno -> get_serialno;
39  
40 die 'cannot get new serialno' unless defined $new_serialno;
41  
42 # use $new_serialno value for constructing new message
43  
44 =head1 DESCRIPTION
45  
46 This class is for handling serialno value for new FTN messages. Assigns consecutive unique values.
47  
48 =head1 USAGE
49  
50 This class has the following methods:
51  
52 =head2 new
53  
54 Class constructor. Has the following options:
55  
56 =cut
57  
58 3 ▶sub _initialize {
Subroutine _initialize: called CC=23 SCAR=38.9
59 3 my $self = shift;
60  
61 3 my %param = @_;
62  
63 =over
64  
65 =item * directory
66  
67 The only mandatory option. Specifies directory where files with serialno are being created and removed. It is recommended to have it as a dedicated directory and not to keep any other files in it.
68  
69 FTN::Message::serialno::File -> new( directory => '/some/dir' );
70  
71 =cut
72  
73 # directory
74 die 'directory parameter should be defined!'
75 unless $param{directory}
76 3 ▶ && -d $param{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"directory"} and -d $param{"directory"} 0 3 3
Condition33%1/3
$param{"directory"} and -d $param{"directory"}
!ll&&!rl&&r
003
MC/DC0%0/2
$param{"directory"} and -d $param{"directory"}
$param{"directory"} -d $param{"directory"}
Truth table33%1/3
A and B
A= $param{"directory"}B= -d $param{"directory"}
ABresult
0X0
100
111
77  
78 3 $self -> {directory} = $param{directory};
79  
80  
81 =item * file_extension
82  
83 Serialno files extension. Dot before its value will be added.
84  
85 FTN::Message::serialno::File -> new( directory => '/some/dir',
86 file_extension => 'seq',
87 );
88  
89 Default value is 'sn'.
90  
91 =cut
92  
93 3 ▶ my $extension = exists $param{file_extension}? $param{file_extension} : FILE_EXTENSION;
Branch: 1 branch
BranchTrueFalseTotal
exists $param{'file_extension'} ? : 2 1 3
94  
95 =item * filename_pattern
96  
97 Defines pattern and matching files in the directory are considered as serialno files and hence can be removed/renamed. File extension shouldn't be specified as it's added automatically.
98  
99 =cut
100  
101 3 ▶ my $pattern = $param{filename_pattern} || FILENAME_PATTERN;
Condition50%1/2
$param{'filename_pattern'} || '^([0-9a-fA-F]{1,8})$'
l!l
03
MC/DC0%0/1
$param{'filename_pattern'} || '^([0-9a-fA-F]{1,8})$'
$param{'filename_pattern'}
Truth table50%1/2
A
A= $param{'filename_pattern'}
Aresult
11
00
102  
103 =item * filename_format
104  
105 Defines filename format (as in printf) with encoded serialno value. File extension shouldn't be specified as it's added automatically. Filename created with filename_format should match filename_pattern.
106  
107 Default value is '%x'.
108  
109 =cut
110  
111 3 ▶ $self -> {filename_format} = $param{filename_format} || FILENAME_FORMAT;
Condition50%1/2
$param{'filename_format'} || '%x'
l!l
03
MC/DC0%0/1
$param{'filename_format'} || '%x'
$param{'filename_format'}
Truth table50%1/2
A
A= $param{'filename_format'}
Aresult
11
00
112 3 ▶ if ( defined $extension
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extension and length $extension) 3 0 3
Condition33%1/3
defined $extension and length $extension
!ll&&!rl&&r
003
MC/DC0%0/2
defined $extension and length $extension
defined $extension length $extension
Truth table33%1/3
A and B
A= defined $extensionB= length $extension
ABresult
0X0
100
111
113 && length $extension
114 ) {
115 3 ▶ if ( substr( $pattern, -1 ) eq '$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($pattern, -1) eq '$') { } 3 0 3
116 3 $pattern = substr( $pattern, 0, -1 ) . '\.' . $extension . '$';
117 } else {
118 0 $pattern .= '\.' . $extension;
119 }
120 3 $self -> {filename_format} .= '.' . $extension;
121 }
122 3 $self -> {filename_pattern} = qr/$pattern/;
123  
124  
125 # validate that filename_pattern will match filename_format
126 3 my $t = sprintf $self -> {filename_format}, 1;
127 3 ▶ die 'not matching filename_format ( ' . $self -> {filename_format} . ' ) and filename_pattern ( ' . $self -> {filename_pattern} . ' )'
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /$self->{"filename_pattern"}/ 0 3 3
128 unless $t =~ m/$self->{filename_pattern}/;
129  
130  
131 # decode filename
132 $self -> {decode_filename} = $param{decode_filename} && ref $param{decode_filename} eq 'CODE'?
133 $param{decode_filename}
134 36 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
135 36 ▶ shift =~ m/$self->{filename_pattern}/?
Branch: 1 branch
BranchTrueFalseTotal
(shift()) =~ /$self->{'filename_pattern'}/ ? : 12 24 36
136 ( $1 . '.' . $extension,
137 hex( $1 )
138 )
139 : ();
140 3 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$param{'decode_filename'} && ref $param{'decode_filename'} eq 'CODE' ? : 0 3 3
Condition33%1/3
$param{"decode_filename"} and ref $param{"decode_filename"} eq "CODE"
!ll&&!rl&&r
300
MC/DC0%0/2
$param{"decode_filename"} and ref $param{"decode_filename"} eq "CODE"
$param{"decode_filename"} ref $param{"decode_filename"} eq "CODE"
Truth table33%1/3
A and B
A= $param{"decode_filename"}B= ref $param{"decode_filename"} eq "CODE"
ABresult
0X0
100
111
141  
142 # encode filename
143 $self -> {encode_filename} = $param{encode_filename} && ref $param{encode_filename} eq 'CODE'?
144 $param{encode_filename}
145 15 ▶ : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 15 sprintf $self -> {filename_format}, shift;
147 3 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$param{'encode_filename'} && ref $param{'encode_filename'} eq 'CODE' ? : 0 3 3
Condition33%1/3
$param{"encode_filename"} and ref $param{"encode_filename"} eq "CODE"
!ll&&!rl&&r
300
MC/DC0%0/2
$param{"encode_filename"} and ref $param{"encode_filename"} eq "CODE"
$param{"encode_filename"} ref $param{"encode_filename"} eq "CODE"
Truth table33%1/3
A and B
A= $param{"encode_filename"}B= ref $param{"encode_filename"} eq "CODE"
ABresult
0X0
100
111
148  
149 die 'incorrect decode_filename and/or encode_filename'
150 3 ▶ unless $self -> {decode_filename}( $self -> {encode_filename}( 1 ) ) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"decode_filename"}->($self->{"encode_filename"}->(1)) == 1 0 3 3
151  
152  
153 =item * max_tries
154  
155 Defines how many times renaming of the file is tried before it is considered failed.
156  
157 Default value is 5.
158  
159 =cut
160  
161 # max_tries for renaming
162 3 ▶ $self -> {max_tries} = $param{max_tries} && $param{max_tries} =~ m/^(\d+)$/?
Branch: 1 branch
BranchTrueFalseTotal
$param{'max_tries'} && $param{'max_tries'} =~ /^(\d+)$/ ? : 0 3 3
Condition33%1/3
$param{"max_tries"} and $param{"max_tries"} =~ /^(\d+)$/
!ll&&!rl&&r
300
MC/DC0%0/2
$param{"max_tries"} and $param{"max_tries"} =~ /^(\d+)$/
$param{"max_tries"} $param{"max_tries"} =~ /^(\d+)$/
Truth table33%1/3
A and B
A= $param{"max_tries"}B= $param{"max_tries"} =~ /^(\d+)$/
ABresult
0X0
100
111
163 $1
164 : MAX_TRIES;
165  
166  
167 =item * very_first_init
168  
169 Defines reference to a function for generating very first serialno value in case there are no matching files in the directory.
170  
171 The possible values:
172  
173 =over 6
174  
175 =item * CURRENT_UNIXTIME
176  
177 use current unixtime as a starting value.
178  
179 FTN::Message::serialno::File -> new( directory => '/some/dir',
180 very_first_init => 'CURRENT_UNIXTIME',
181 );
182
183  
184 =item * CURRENT_UNIXTIME_MINUS_3_YEARS
185  
186 use current unixtime minus 3 years as a starting value.
187  
188 =item * user defined function
189  
190 FTN::Message::serialno::File -> new( directory => '/some/dir',
191 very_first_init => sub {
192 42; # voices in my head tell me to use 42
193 },
194 );
195  
196 =back
197  
198 Default value is function returning 1.
199  
200 =cut
201  
202 # very_first_init
203 3 ▶ if ( $param{very_first_init} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"very_first_init"}) 1 2 3
204 1 ▶ if ( ref $param{very_first_init} eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $param{'very_first_init'} eq 'CODE') { } 1 0 1
elsif ($param{'very_first_init'} eq 'CURRENT_UNIXTIME_MINUS_3_YEARS') { } 0 0 0
elsif ($param{'very_first_init'} eq 'CURRENT_UNIXTIME') { } 0 0 0
205 1 $self -> {very_first_init} = $param{very_first_init};
206 } elsif ( $param{very_first_init} eq 'CURRENT_UNIXTIME_MINUS_3_YEARS' ) {
207 0 ▶ $self -> {very_first_init} = sub { time - 3 * 365 * 24 * 60 * 60 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 } elsif ( $param{very_first_init} eq 'CURRENT_UNIXTIME' ) {
209 0 ▶ $self -> {very_first_init} = sub { time; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 }
211 }
212  
213 2 ▶ $self -> {very_first_init} = sub { 1; }
Subroutine __ANON__: called CC=1 SCAR=0.0
214 3 ▶ unless $self -> {very_first_init};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"very_first_init"} 2 1 3
215  
216 =item * serialno_format
217  
218 serialno format (as in printf) for a return value. Can be changed in case you want another casing ('%08X') or no leading zeroes ('%x') for example.
219  
220 Default value is '%08x'.
221  
222 =back
223  
224 =cut
225  
226 3 ▶ $self -> {serialno_format} = $param{serialno_format} || SERIALNO_FORMAT;
Condition100%2/2
$param{'serialno_format'} || '%08x'
l!l
12
MC/DC100%1/1
$param{'serialno_format'} || '%08x'
$param{'serialno_format'}
Truth table100%2/2
A
A= $param{'serialno_format'}
Aresult
11
00
227 }
228  
229  
230 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
231 3 ▶ ref( my $class = shift ) and Carp::croak 'I am only a class method!';
Branch: 1 branch
BranchTrueFalseTotal
ref(my $class = shift()) and Carp::croak("I am only a class method!") 0 3 3
232  
233 3 my $self = $class -> SUPER::new( @_ );
234  
235 3 _initialize( $self, @_ ); # not $self -> _initialize!
236  
237 3 $self;
238 }
239  
240  
241 =head2 get_serialno()
242  
243 Method that does all the work and returns either new valid serialno value or undef.
244  
245 If this is the very first run and no signs of previous serialno values, creates new file with starting value and returns it.
246  
247 If there is just one file with previous serialno value, tries to rename it to a new value (up to defined value (constructor option) times due to parallel execution) and returns that new value on success.
248  
249 If there are more than one file with previous serialno values, sorts them and removes all but last one. Then tries previous approach with last value.
250  
251 If renaming fails more than allowed count, undef is returned.
252  
253 =cut
254  
255 12 ▶sub get_serialno {
Subroutine get_serialno: called CC=10 SCAR=23.9
Pod documented
256 12 ▶ ref( my $self = shift ) or Carp::croak 'I am only an object method!';
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or Carp::croak("I am only an object method!") 12 0 12
257  
258 12 my $try = 1;
259 12 my $new_serialno;
260  
261 {
262 12 opendir my $dh, $self -> {directory}
263 12 ▶ or die "can't opendir $self->{directory}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->{"directory"} or die qq[can't opendir $self->{"directory"}: $!] 12 0 12
264  
265 0 my @found_file = sort { $a -> [ 1 ] <=> $b -> [ 1 ] }
266 map {
267 12 my $filename = $_;
268 33 my @r;
269  
270 33 my @t = $self -> {decode_filename}( $filename );
271  
272 33 ▶ if ( @t ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 9 24 33
273 # my $full_name = $self -> _full_filename( $filename ); tainted on win
274 9 my $full_name = $self -> _full_filename( $t[ 0 ] );
275  
276 9 ▶ push @r,
Branch: 1 branch
BranchTrueFalseTotal
if -f $full_name 9 0 9
277 [ $full_name,
278 $t[ 1 ],
279 ]
280 if -f $full_name;
281 }
282  
283 33 @r;
284 } readdir $dh;
285  
286 12 closedir $dh;
287  
288 12 ▶ if ( @found_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_file) { } 9 3 12
289 # as old numbers can be deleted by other process, we don't care about errors here, so let's do it in one shot
290 9 ▶ unlink map $_ -> [ 0 ],
Branch: 1 branch
BranchTrueFalseTotal
if @found_file > 1 0 9 9
291 splice @found_file, 0, -1
292 if @found_file > 1;
293  
294 # here we try to rename
295 9 my $new_id = ( $found_file[ 0 ][ 1 ] + 1 ) & 0xffffffff;
296 9 my $new_file = $self -> _full_filename( $self -> {encode_filename}( $new_id ) );
297  
298 9 ▶ if ( rename $found_file[ 0 ][ 0 ], $new_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $found_file[0][0], $new_file) { } 9 0 9
299 9 $new_serialno = sprintf $self -> {serialno_format}, $new_id;
300 } else { # might be a parallel request
301 0 ▶ redo if $try++ < $self -> {max_tries};
Branch: 1 branch
BranchTrueFalseTotal
if $try++ < $self->{"max_tries"} 0 0 0
302 }
303 } else { # nothing in found_file. must be the very first run. create new file and return id
304 3 my $id = $self -> {very_first_init}();
305  
306 3 my $fn = $self -> _full_filename( $self -> {encode_filename}( $id ) );
307 3 ▶ open my $fh, '>', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fn or die 'cannot create file ' . $fn . ': ' . $! 3 0 3
308 or die 'cannot create file ' . $fn . ': ' . $!;
309 3 print $fh $id; # save start value there for a history
310 3 close $fh;
311  
312 3 $new_serialno = sprintf $self -> {serialno_format}, $id;
313 }
314 }
315  
316 12 $new_serialno;
317 }
318  
319  
320 21 ▶sub _full_filename {
Subroutine _full_filename: called CC=2 SCAR=7.1
321 21 ▶ ref( my $self = shift ) or Carp::croak 'I am only an object method!';
Branch: 1 branch
BranchTrueFalseTotal
ref(my $self = shift()) or Carp::croak("I am only an object method!") 21 0 21
322  
323 21 my $filename = shift;
324  
325 File::Spec -> catfile( $self -> {directory},
326 21 $filename,
327 );
328 }
329  
330 =head1 AUTHOR
331  
332 Valery Kalesnik, C<< <valkoles at gmail.com> >>
333  
334 =head1 BUGS
335  
336 Please report any bugs or feature requests to C<bug-ftn-message-serialno-file at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FTN-Message-serialno-File>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
337  
338 =head1 SUPPORT
339  
340 You can find documentation for this module with the perldoc command.
341  
342 perldoc FTN::Message::serialno::File
343  
344  
345 You can also look for information at:
346  
347 =over 4
348  
349 =item * RT: CPAN's request tracker (report bugs here)
350  
351 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FTN-Message-serialno-File>
352  
353 =item * AnnoCPAN: Annotated CPAN documentation
354  
355 L<http://annocpan.org/dist/FTN-Message-serialno-File>
356  
357 =item * CPAN Ratings
358  
359 L<http://cpanratings.perl.org/d/FTN-Message-serialno-File>
360  
361 =item * Search CPAN
362  
363 L<http://search.cpan.org/dist/FTN-Message-serialno-File/>
364  
365 =back
366  
367  
368 =head1 ACKNOWLEDGEMENTS
369  
370  
371 =head1 LICENSE AND COPYRIGHT
372  
373 Copyright 2014 Valery Kalesnik.
374  
375 This program is free software; you can redistribute it and/or modify it
376 under the terms of the the Artistic License (2.0). You may obtain a
377 copy of the full license at:
378  
379 L<http://www.perlfoundation.org/artistic_license_2_0>
380  
381 Any use, modification, and distribution of the Standard or Modified
382 Versions is governed by this Artistic License. By using, modifying or
383 distributing the Package, you accept this license. Do not use, modify,
384 or distribute the Package, if you do not accept this license.
385  
386 If your Modified Version has been derived from a Modified Version made
387 by someone other than you, you are nevertheless required to ensure that
388 your Modified Version complies with the requirements of this license.
389  
390 This license does not grant you the right to use any trademark, service
391 mark, tradename, or logo of the Copyright Holder.
392  
393 This license includes the non-exclusive, worldwide, free-of-charge
394 patent license to make, have made, use, offer to sell, sell, import and
395 otherwise transfer the Package with respect to any patent claims
396 licensable by the Copyright Holder that are necessarily infringed by the
397 Package. If you institute patent litigation (including a cross-claim or
398 counterclaim) against any party alleging that the Package constitutes
399 direct or contributory patent infringement, then this Artistic License
400 to you shall terminate on the date that such litigation is filed.
401  
402 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
403 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
404 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
405 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
406 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
407 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
408 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
409 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
410  
411 =cut
412  
413 3 1;
↑ Summary serialno.pm »