blib/lib/File/VMSVersions.pm

filter: Statementstmt 88.8% 120 / 135 Branchbran 52.5% 42 / 80 Conditioncond 42.3% 11 / 26 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 16 / 16 Podpod 80.0% 4 / 5 totaltotal 70.6% 195 / 276 CC 52 SCAR 29.5
CC
52
Cov
72%
open
53.3
purge
14.1
_config
10.3
CRAP 19.0
↑ Summary
1 package File::VMSVersions;
2
3 2 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 2 our $VERSION = '0.1';
15
16 2 my $vcfilename = '.vcntl';
17
18 =head1 NAME
19
20 File::VMSVersions - Perl extension for opening files in a directory with
21 VMS like versioning
22
23 =head1 SYNOPSIS
24
25 use File::VMSVersions;
26
27 my $vdir = File::VMSVersions->new(
28 -name => "./mydir",
29 -mode => 'versions',
30 -limit => 3,
31 );
32
33 foreach my $i (1..6) {
34 my($fh, $fn) = $vdir->open('bla.dat', '>');
35 die $fn unless $fh;
36 print $fh "file number $i\n";
37 print "created $fn\n";
38 $fh->close;
39 }
40
41 Now you should have the following files in ./mydir:
42
43 .vcntl
44 bla.dat;lck
45 bla.dat;4
46 bla.dat;5
47 bla.dat;6
48
49 =head1 DESCRIPTION
50
51 The B<File::VMSVersions> module was developed for maintaining automatic
52 versioning of files in a directory. When you are using the module's routines for
53 opening files, it will keep a configurable amount of old versions. The versions
54 will be identified by a number that is added at the end of the filename after a
55 semicolon (i. e. F<'myfile.dat;7'>).
56
57 The configured options for a directory are saved in the file F<'.vcntl'>. They
58 are read each time the B<open> method is called and written when the B<new>
59 constructor or the B<config> method are called with according options.
60
61 F<'.vcntl'> consists of only one line with limit and mode separated by an '#'.
62 For example:
63
64 20#days
65
66 10#versions
67
68 It is ok to edit F<'.vcntl'> manually
69
70 =cut
71
72 =head1 CONSTRUCTOR
73
74 To create a new B<File::VMSVersions> call the B<new> contructor
75
76 $obj = File::VMSVersions->new(
77 -name => <directory name>,
78 [ -mode => <'versions'|'days'>,
79 -limit => <version limit>, ]
80 );
81
82 You have to specify both B<-limit> and B<-mode> or none of them. If both
83 evaluate to false the file F<.vcntl> is read. Otherwise it will be replaced with
84 the new values. If the file doesn't exist when the configuration is read, there
85 is no version limit at all.
86
87 =cut
88
89 2 ▶sub new {
Subroutine new: called CC=6 SCAR=22.9
Pod undocumented
90 2 my($caller) = shift;
91 2 ▶ my($class) = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
92
93 2 my %cfg = @_;
94
95 $cfg{-name} or
96 2 ▶ croak << ' END';
Branch: 1 branch
BranchTrueFalseTotal
$cfg{"-name"} or croak(" usage: File::VMSVersions->new(\n -name => <dirname>,\n [-mode => <\"days\"|\"versions\">,\n -limit => <versionlimit>,]\n );\n") 2 0 2
97 usage: File::VMSVersions->new(
98 -name => <dirname>,
99 [-mode => <"days"|"versions">,
100 -limit => <versionlimit>,]
101 );
102 END
103
104 2 ▶ if ($cfg{-mode} xor $cfg{-limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"-mode"} xor $cfg{"-limit"}) 0 2 2
Condition25%1/4
$cfg{"-mode"} xor $cfg{"-limit"}
l&&rl&&!r!l&&r!l&&!r
2000
MC/DC0%0/2
$cfg{"-mode"} xor $cfg{"-limit"}
$cfg{"-mode"} $cfg{"-limit"}
Truth table25%1/4
A xor B
A= $cfg{"-mode"}B= $cfg{"-limit"}
ABresult
110
101
011
000
105 $cfg{-mode} ?
106 0 ▶ croak("-limit not specified") :
Branch: 1 branch
BranchTrueFalseTotal
$cfg{'-mode'} ? : 0 0 0
107 croak("-mode not specified");
108 }
109
110 2 %cfg = _config(%cfg);
111
112 2 return(bless(\%cfg, $class));
113 }
114
115
116 =head1 METHODS
117
118 =over 4
119
120 =item B<<< $obj->open(<filename> [, <mode:'<|>|>>']> [, <version>]) >>>
121
122 Opens a version of a file. The default mode is '<' (read).
123
124 If version is not specified when reading, the last version will be opened.
125
126 If mode equals '>' (write) or '>>' (append), the specified version of the
127 desired file will be created or appended (append will create a new file if the
128 version doesn't exist).
129
130 If there is no version specified, the highest existing version will be
131 incremented by 1.
132
133 If the specified version is negative the nth last version will be opened.
134
135 B<open> returns a list with an indirect filehandle and the filename. On errors
136 the filehandle is undefined and the filename contains an error message.
137
138 =cut
139
140 11 ▶sub open {
Subroutine open: called CC=16 SCAR=39.8
Pod documented
141 11 my($self) = shift;
142 11 my($fn, $mode, $ver) = @_;
143
144 11 ▶ $mode ||= '<';
Condition50%1/2
$mode ||= "<"
l!l
110
MC/DC0%0/1
$mode ||= "<"
$mode
Truth table50%1/2
A
A= $mode
Aresult
11
00
145 11 ▶ croak("illegal mode '$mode'") unless $mode =~ /^<|>|>>$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^<|>|>>$/ 0 11 11
146
147 11 ▶ $ver ||= 0;
Condition50%1/2
$ver ||= 0
l!l
011
MC/DC0%0/1
$ver ||= 0
$ver
Truth table50%1/2
A
A= $ver
Aresult
11
00
148
149 11 my $fullfn = catfile($self->{-name}, $fn);
150
151 # waiting for lock on lockfile in write mode
152 11 my $lck;
153 11 ▶ if ($mode =~ />/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ />/) 11 0 11
154 11 $lck = _getlock("$fullfn;lck");
155 }
156
157 # get version info
158 11 my $info = $self->info($fn);
159
160 11 my $purge = 0;
161
162 11 ▶ if ($mode eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq '>') { } 11 0 11
163
164 # negative versions in write mode make no sense
165 # increase version anyway (like VMS)
166 11 ▶ $ver = $info->{max} + 1 if $ver <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ver <= 0 11 0 11
167 11 $purge = 1;
168
169 } else {
170 0 ▶ if ($ver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver) { } 0 0 0
171 0 ▶ if ($ver > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver > 0) { } 0 0 0
172 # ver too small -> set to minimum
173 0 ▶ $ver = $info->{min} if $ver < $info->{min};
Branch: 1 branch
BranchTrueFalseTotal
if $ver < $info->{"min"} 0 0 0
174 } else {
175 # get the desired version with negative array index
176 0 $ver = $info->{$self->{-mode}}->[$ver-1];
177 0 ▶ return(undef, "version >>$ver<< not found") unless defined($ver);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ver 0 0 0
178 }
179 0 ▶ if ( !exists($info->{$ver}) and $mode eq '>>') {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $info->{$ver} and $mode eq ">>") 0 0 0
Condition0%0/3
not exists $info->{$ver} and $mode eq ">>"
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $info->{$ver} and $mode eq ">>"
not exists $info->{$ver} $mode eq ">>"
Truth table0%0/3
not(A) and B
A= not exists $info->{$ver}B= $mode eq ">>"
ABresult
0X0
100
111
180 0 $purge = 1;
181 }
182 } else {
183 0 $ver = $info->{max};
184 }
185 }
186
187 11 ▶ CORE::open(my $fh, $mode, "$fullfn;$ver") or
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, $mode, "$fullfn;$ver" or return undef, "error opening $fullfn version $ver in mode $mode, $!" 11 0 11
188 return(undef, "error opening $fullfn version $ver in mode $mode, $!");
189
190 11 ▶ $self->purge($fn, $self->config()) if $purge;
Branch: 1 branch
BranchTrueFalseTotal
if $purge 11 0 11
191
192 # releasing lock
193 11 ▶ $lck->close if $lck;
Branch: 1 branch
BranchTrueFalseTotal
if $lck 11 0 11
194
195 11 return($fh, "$fullfn;$ver");
196 }
197
198
199 =item B<<< $obj->purge(<filename>, [-mode => <mode>, -limit => <limit>] >>>
200
201 purges the versions of a file to the specified limit. When limit and mode are
202 not specified all but the last versions are purged. There is no need to call
203 B<purge> for normal versioning.
204
205 =cut
206
207 12 ▶sub purge {
Subroutine purge: called CC=12 SCAR=26.5
Pod documented
208 12 my($self) = shift;
209 12 my($fn, %cfg) = @_;
210
211 12 ▶ croak("purge: no filename specified") unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 12 12
212
213 12 my $fullfn = catfile($self->{-name}, $fn);
214
215 12 ▶ if ($cfg{-mode} xor $cfg{-limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"-mode"} xor $cfg{"-limit"}) 0 12 12
Condition50%2/4
$cfg{"-mode"} xor $cfg{"-limit"}
l&&rl&&!r!l&&r!l&&!r
11001
MC/DC0%0/2
$cfg{"-mode"} xor $cfg{"-limit"}
$cfg{"-mode"} $cfg{"-limit"}
Truth table50%2/4
A xor B
A= $cfg{"-mode"}B= $cfg{"-limit"}
ABresult
110
101
011
000
216 $cfg{-mode} ?
217 0 ▶ croak("-limit not specified") :
Branch: 1 branch
BranchTrueFalseTotal
$cfg{'-mode'} ? : 0 0 0
218 croak("-mode not specified");
219 }
220
221 12 ▶ ($cfg{-limit}, $cfg{-mode}) = (1, 'versions') unless $cfg{-mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg{"-mode"} 1 11 12
222
223 12 my $info = $self->info($fn);
224
225 12 print Dumper($info);
226 12 print Dumper($self);
227 12 print Dumper(\%cfg);
228
229 12 foreach my $v ( @{$info->{$self->{-mode}}} ) {
230
231 31 ▶ if ($cfg{-mode} eq 'versions') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{'-mode'} eq 'versions') { } 10 21 31
232
233 10 ▶ last if ( $info->{count} <= $cfg{-limit} );
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"count"} <= $cfg{"-limit"} 6 4 10
234
235 4 ▶ if ( unlink("$fullfn;$v") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink "$fullfn;$v") { } 4 0 4
236 4 delete($info->{$v});
237 } else {
238 0 carp("couldn't purge $fullfn;$v");
239 }
240
241 4 $info->{count}--;
242
243 } else {
244
245 21 ▶ if ( $info->{$v} - $info->{d_max} > $cfg{-limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{$v} - $info->{"d_max"} > $cfg{"-limit"}) 2 19 21
246 2 ▶ if ( unlink("$fullfn;$v") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink "$fullfn;$v") { } 2 0 2
247 2 delete($info->{$v});
248 } else {
249 0 carp("couldn't purge $fullfn;$v");
250 }
251 }
252
253 }
254 }
255 }
256
257
258 =item B<<< $obj->config([-mode => <mode>, -limit => <limit>]) >>>
259
260 Sets and/or returns limit and mode of the directory
261
262 =cut
263
264 11 ▶sub config {
Subroutine config: called CC=3 SCAR=13.0
Pod documented
265 11 my $self = shift;
266
267 11 my %cfg = @_;
268
269 11 ▶ if ($cfg{-limit} xor $cfg{-mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{"-limit"} xor $cfg{"-mode"}) 0 11 11
Condition25%1/4
$cfg{"-limit"} xor $cfg{"-mode"}
l&&rl&&!r!l&&r!l&&!r
00011
MC/DC0%0/2
$cfg{"-limit"} xor $cfg{"-mode"}
$cfg{"-limit"} $cfg{"-mode"}
Truth table25%1/4
A xor B
A= $cfg{"-limit"}B= $cfg{"-mode"}
ABresult
110
101
011
000
270 0 croak('please specify both -limit and -mode or none of them!');
271 }
272
273 11 $cfg{-name} = $self->{-name};
274
275 11 return(_config(%cfg));
276 }
277
278
279 13 ▶sub _config {
Subroutine _config: called CC=9 SCAR=23.3
280 13 my(%cfg) = @_;
281
282 13 my $vfn = catfile($cfg{-name}, $vcfilename);
283
284 13 ▶ if ( $cfg{-limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg{'-limit'}) { } 2 11 13
285
286 2 ▶ croak("illegal mode >>$cfg{-mode}<<") unless $cfg{-mode} =~ /^days|versions$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg{"-mode"} =~ /^days|versions$/ 0 2 2
287 2 ▶ croak("illegal limit >>$cfg{-limit}<<") unless $cfg{-limit} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg{"-limit"} =~ /^\d+$/ 0 2 2
288
289 2 ▶ CORE::open(my $vfh, ">", $vfn) or croak("could not write $vfn, $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $vfh, ">", $vfn or croak("could not write $vfn, $!") 2 0 2
290 2 print $vfh join('#', $cfg{-limit}, $cfg{-mode});
291 2 $vfh->close;
292
293 } else {
294
295 11 ▶ if ( -f $vfn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $vfn) { } 11 0 11
296 11 ▶ CORE::open(my $vfh, "<", $vfn) or croak("couldn't read $vfn, $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $vfh, "<", $vfn or croak("couldn't read $vfn, $!") 11 0 11
297 11 ( $cfg{-limit}, $cfg{-mode} ) = split(/#/, <$vfh>);
298 11 $vfh->close;
299
300 croak("illegal mode >>$cfg{-mode}<< from $vfn")
301 11 ▶ unless $cfg{-mode} =~ /^days|versions$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg{"-mode"} =~ /^days|versions$/ 0 11 11
302 croak("illegal limit >>$cfg{-limit}<< from $vfn")
303 11 ▶ unless $cfg{-limit} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg{"-limit"} =~ /^\d+$/ 0 11 11
304
305 } else {
306 0 ( $cfg{-limit}, $cfg{-mode} ) = (999999999999999, 'versions');
307 }
308
309 }
310
311 13 return(%cfg);
312 }
313
314
315 =item B<<< $obj->info(<filename>) >>>
316
317 returns a hashref with version information for <filename>
318
319 =cut
320
321 26 ▶sub info {
Subroutine info: called CC=8 SCAR=20.8
Pod documented
322 26 my($self) = shift;
323 26 my($fn) = @_;
324
325 26 ▶ $fn or croak "usage: info(<filename>)";
Branch: 1 branch
BranchTrueFalseTotal
$fn or croak("usage: info(<filename>)") 26 0 26
326
327 26 my $fullfn = catfile($self->{-name}, $fn);
328
329 26 my(%info, @tmp, $ver);
330
331 26 foreach my $f (glob("$fullfn;*")) {
332 96 $ver = (split(/;/, $f))[-1];
333 96 ▶ next unless $ver =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ver =~ /^\d+$/ 26 70 96
334 70 $info{$ver} = -M $f;
335 }
336
337 26 @tmp = sort {$a <=> $b} keys(%info);
338 26 $info{versions} = [@tmp];
339 26 $info{count} = @tmp;
340 26 ▶ $info{min} = $tmp[0] || 0;
Condition100%2/2
$tmp[0] || 0
l!l
242
MC/DC100%1/1
$tmp[0] || 0
$tmp[0]
Truth table100%2/2
A
A= $tmp[0]
Aresult
11
00
341 26 ▶ $info{max} = $tmp[-1] || 0;
Condition100%2/2
$tmp[-1] || 0
l!l
242
MC/DC100%1/1
$tmp[-1] || 0
$tmp[-1]
Truth table100%2/2
A
A= $tmp[-1]
Aresult
11
00
342 26 @tmp = sort {$info{$b} <=> $info{$a}} grep {/^\d+$/} keys(%info);
343 26 $info{days} = [@tmp];
344 26 ▶ $info{d_min} = $tmp[0] ? $info{$tmp[0]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tmp[0] ? : 24 2 26
345 26 ▶ $info{d_max} = $tmp[-1] ? $info{$tmp[-1]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tmp[-1] ? : 24 2 26
346
347 26 return(\%info);
348 }
349
350
351 11 ▶sub _getlock {
Subroutine _getlock: called CC=4 SCAR=14.5
352 11 my($fn) = @_;
353
354 11 ▶ my $mode = -e $fn ? '<' : '>';
Branch: 1 branch
BranchTrueFalseTotal
-e $fn ? : 9 2 11
355 11 ▶ CORE::open(my $lck, $mode, $fn) or croak "couldn't open lock file $fn, $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $lck, $mode, $fn or croak("couldn't open lock file $fn, $!") 11 0 11
356
357 11 ▶ unless (flock($lck, LOCK_EX | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $lck, 6) 0 11 11
358 0 flock($lck, LOCK_EX);
359 }
360
361 11 return($lck);
362 }
363
364
365 =head1 AUTHOR
366
367 Thomas Kratz, E<lt>ThomasKratz@web.deE<gt>
368
369 =head1 COPYRIGHT AND LICENSE
370
371 Copyright 2003 by Thomas Kratz
372
373 This library is free software; you can redistribute it and/or modify
374 it under the same terms as Perl itself.
375
376 =cut
↑ Summary