blib/lib/PAR/Repository/DBM.pm

filter: Statementstmt 97.9% 189 / 193 Branchbran 65.2% 30 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 24 / 24 Podpod 100% 9 / 9 totaltotal 92.6% 252 / 272 CC 25 SCAR 11.0
CC
25
Cov
92%
update_dbm_checksums
6.3
dependencies_dbm
4.1
modules_dbm
4.0
CRAP 3.0
« blib/lib/PAR/Repository/Zip.pm ↑ Summary blib/lib/PAR/dump_db.pl »
1 package PAR::Repository::DBM;
2  
3 22 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use File::Spec::Functions qw/catfile splitpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use DBM::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use File::Copy qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 our $VERSION = '0.20';
14 22 ▶use constant 'MODULES_DBM_FILE' => 'modules_dists.dbm';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use constant 'SYMLINKS_DBM_FILE' => 'symlinks.dbm';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use constant 'SCRIPTS_DBM_FILE' => 'scripts_dists.dbm';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use constant 'DEPENDENCIES_DBM_FILE' => 'dependencies.dbm';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use constant 'DBM_CHECKSUMS_FILE' => 'dbm_checksums.txt';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 PAR::Repository::DBM - DBM tools for PAR::Repository
23  
24 =head1 SYNOPSIS
25  
26 use PAR::Repository;
27  
28 =head1 DESCRIPTION
29  
30 This module is for internal use only.
31 It contains code for accessing the DBM files of a
32 PAR repository.
33  
34 =head2 EXPORT
35  
36 None.
37  
38 =head2 GLOBALS
39  
40 This package has a few constants:
41  
42 C<MODULES_DBM_FILE>, C<SYMLINKS_DBM_FILE>, and C<SCRIPTS_DBM_FILE>,
43 C<DEPENDENCIES_DBM_FILE>, and C<DBM_CHECKSUMS_FILE>.
44 They are accessible
45 as functions via C<PAR::Repository::DBM::...>. They indicate
46 the file names of the DBM databases and the DBM checksums file.
47  
48 =head1 DATABASE STRUCTURE
49  
50 This section outlines the structure of the DBM::Deep
51 database files used by PAR::Repository.
52  
53 If you need to care about this, you should be a
54 PAR::Repository developer.
55  
56 =head2 MODULES-DISTS DBM
57  
58 The DBM file is a hash at top level.
59  
60 It associates namespaces (keys) with a number of file names and
61 versions. The values of the top level hash are hashes again. These
62 contain file names as keys and corresponding versions as values.
63  
64 Example:
65  
66 {
67 'Math::Symbolic::Derivative' => {
68 'Math-Symbolic-0.502-x86_64-linux-gnu-thread-multi-5.8.7.par' => '0.502',
69 'Math-Symbolic-0.200-x86_64-linux-gnu-thread-multi-5.8.6.par' => '0.200',
70 },
71 }
72  
73 This example means that the C<Math::Symbolic::Derivative> module can
74 be found in the two listed distribution files in the repository
75 with the listed versions. Note that the distribution version needs not
76 be the same as the B<module> version. The module version is the one
77 separately indicated.
78  
79 =head2 SYMLINKS DBM
80  
81 The DBM file is a hash at top level.
82  
83 It associates real files in the repository (keys) with a number of
84 symbolic links.
85 The values of the top level hash are arrays of distribution file names
86 which are symlinks.
87  
88 Example: (with some extra linebreaks to keep the text width down)
89  
90 {
91 'Math-Symbolic-0.502-x86_64-linux-gnu-thread-multi-5.8.7.par'
92 => [
93 'Math-Symbolic-0.502-any_arch-5.8.7.par',
94 'Math-Symbolic-0.502-x86_64-linux-gnu-thread-multi-'
95 .'any_version.par',
96 'Math-Symbolic-0.502-any_arch-any_version.par'
97 ],
98 }
99  
100 In the example, the first file is the real file and the paths/file names
101 in the value array are the names of the symbolic links.
102  
103 =head2 SCRIPTS-DISTS DBM
104  
105 This DBM file is a hash at top level. It associates script (executable)
106 names with distributions much like the C<modules_dists.dbm> file.
107  
108 Example:
109  
110 {
111 'parrepo' => {
112 'PAR-Repository-0.03-x86_64--5.8.7.par' => '0.02',
113 'PAR-Repository-0.02-any_arch-any_version.par' => '0.01',
114 },
115 }
116  
117 =head2 DEPENDENCIES DBM
118  
119 This DBM file stores distribution names and associates them with
120 names of modules it depends on and their minimum versions. It does
121 not differentiate between the various types of dependencies that
122 can be found in a CPAN C<META.yml> file.
123  
124 Example:
125  
126 {
127 'Distname-0.03-x86_64-any_version.par' => {
128 'Module::It::Depends::On' => '1.00',
129 },
130 }
131  
132 =head1 METHODS
133  
134 Following is a list of class and instance methods.
135 (Instance methods until otherwise mentioned.)
136  
137 There is no C<PAR::Repository::DBM> object.
138 L<PAR::Repository> inherits from this class.
139  
140 =cut
141  
142 =head2 modules_dbm
143  
144 Opens the modules_dists.dbm.zip file in the repository and
145 returns a tied hash reference to that file. Second return value is the
146 file name.
147  
148 If the file does not exist, it returns the empty list.
149  
150 You should know what you are doing when you use this
151 method.
152  
153 =cut
154  
155 85 ▶sub modules_dbm {
Subroutine modules_dbm: called CC=4 SCAR=13.9
Pod documented
156 85 my $self = shift;
157 85 $self->verbose(2, 'Entering modules_dbm()');
158  
159 85 ▶ if (defined $self->{modules_dbm_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"modules_dbm_hash"}) 31 54 85
160 31 return $self->{modules_dbm_hash};
161 }
162  
163 54 my $old_dir = Cwd::cwd();
164 54 chdir($self->{path});
165 54 my $file = PAR::Repository::DBM::MODULES_DBM_FILE().'.zip';
166 54 ▶ chdir($old_dir), return() if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 54 54
167  
168 54 my ($hash, $tempfile) = $self->_open_dbm($file);
169 54 ▶ chdir($old_dir), return() if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 54 54
170  
171 54 $self->{modules_dbm_hash} = $hash;
172 54 $self->{modules_dbm_temp_file} = $tempfile;
173  
174 54 chdir($old_dir);
175  
176 54 return ($hash, $tempfile);
177 }
178  
179  
180 =head2 symlinks_dbm
181  
182 Opens the symlinks.dbm.zip file in the repository and
183 returns a tied hash reference to that file. Second
184 return value is the file name.
185  
186 If the file does not exist, it returns the empty list.
187  
188 You should know what you are doing when you use this
189 method.
190  
191 =cut
192  
193 60 ▶sub symlinks_dbm {
Subroutine symlinks_dbm: called CC=4 SCAR=13.9
Pod documented
194 60 my $self = shift;
195 60 $self->verbose(2, 'Entering symlinks_dbm()');
196  
197 60 ▶ if (defined $self->{symlinks_dbm_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"symlinks_dbm_hash"}) 12 48 60
198 12 return $self->{symlinks_dbm_hash};
199 }
200  
201 48 my $old_dir = Cwd::cwd();
202 48 chdir($self->{path});
203  
204 48 my $file = PAR::Repository::DBM::SYMLINKS_DBM_FILE().'.zip';
205  
206 48 ▶ chdir($old_dir), return() if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 48 48
207  
208 48 my ($hash, $tempfile) = $self->_open_dbm($file);
209 48 ▶ chdir($old_dir), return() if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 48 48
210  
211 48 $self->{symlinks_dbm_hash} = $hash;
212 48 $self->{symlinks_dbm_temp_file} = $tempfile;
213  
214 48 chdir($old_dir);
215  
216 48 return($hash, $tempfile);
217 }
218  
219 =head2 scripts_dbm
220  
221 Opens the scripts_dists.dbm.zip file in the repository and
222 returns a tied hash reference to that file. Second return value is
223 the file name.
224  
225 If the file does not exist, it returns the empty list.
226  
227 You should know what you are doing when you use this
228 method.
229  
230 =cut
231  
232 31 ▶sub scripts_dbm {
Subroutine scripts_dbm: called CC=4 SCAR=13.9
Pod documented
233 31 my $self = shift;
234 31 $self->verbose(2, 'Entering scripts_dbm()');
235  
236 31 ▶ if (defined $self->{scripts_dbm_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"scripts_dbm_hash"}) 4 27 31
237 4 return $self->{scripts_dbm_hash};
238 }
239  
240 27 my $old_dir = Cwd::cwd();
241 27 chdir($self->{path});
242 27 my $file = PAR::Repository::DBM::SCRIPTS_DBM_FILE().'.zip';
243 27 ▶ chdir($old_dir), return() if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 27 27
244  
245 27 my ($hash, $tempfile) = $self->_open_dbm($file);
246 27 ▶ chdir($old_dir), return() if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 27 27
247  
248 27 $self->{scripts_dbm_hash} = $hash;
249 27 $self->{scripts_dbm_temp_file} = $tempfile;
250  
251 27 chdir($old_dir);
252  
253 27 return($hash, $tempfile);
254 }
255  
256 =head2 dependencies_dbm
257  
258 Opens the dependencies.dbm.zip file in the repository and
259 returns a tied hash reference to that file. Second return value is the
260 file name.
261  
262 If the file does not exist, it returns the empty list.
263  
264 You should know what you are doing when you use this
265 method.
266  
267 =cut
268  
269 53 ▶sub dependencies_dbm {
Subroutine dependencies_dbm: called CC=4 SCAR=14.2
Pod documented
270 53 my $self = shift;
271 53 $self->verbose(2, 'Entering dependencies_dbm()');
272  
273 53 ▶ if (defined $self->{dependencies_dbm_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dependencies_dbm_hash"}) 0 53 53
274 0 return $self->{dependencies_dbm_hash};
275 }
276  
277 53 my $old_dir = Cwd::cwd();
278 53 chdir($self->{path});
279 53 my $file = PAR::Repository::DBM::DEPENDENCIES_DBM_FILE().'.zip';
280 53 ▶ chdir($old_dir), return() if not -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 53 53
281  
282 53 my ($hash, $tempfile) = $self->_open_dbm($file);
283 53 ▶ chdir($old_dir), return() if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 0 53 53
284  
285 53 $self->{dependencies_dbm_hash} = $hash;
286 53 $self->{dependencies_dbm_temp_file} = $tempfile;
287  
288 53 chdir($old_dir);
289  
290 53 return ($hash, $tempfile);
291 }
292  
293  
294 =head2 close_modules_dbm
295  
296 Closes the C<modules_dists.dbm> file committing any
297 changes and then zips it back into
298 C<modules_dists.dbm.zip>.
299  
300 This is called when the object is destroyed.
301  
302 =cut
303  
304 139 ▶sub close_modules_dbm {
Subroutine close_modules_dbm: called CC=2 SCAR=6.9
Pod documented
305 139 my $self = shift;
306 139 $self->verbose(2, 'Entering close_modules_dbm()');
307 139 my $hash = $self->{modules_dbm_hash};
308 139 ▶ return if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 85 54 139
309  
310 54 my $obj = tied($hash);
311 54 $self->{modules_dbm_hash} = undef;
312 54 undef $hash;
313 54 undef $obj;
314  
315 $self->_zip_file(
316 $self->{modules_dbm_temp_file},
317 54 catfile($self->{path}, PAR::Repository::DBM::MODULES_DBM_FILE().'.zip'),
318 PAR::Repository::DBM::MODULES_DBM_FILE(),
319 );
320  
321 54 unlink $self->{modules_dbm_temp_file};
322 54 $self->{modules_dbm_temp_file} = undef;
323  
324 54 return 1;
325 }
326  
327  
328 =head2 close_symlinks_dbm
329  
330 The same as C<close_modules_dbm()> but for the
331 file C<symlinks.dbm.zip>.
332  
333 Also called on object destruction.
334  
335 =cut
336  
337 138 ▶sub close_symlinks_dbm {
Subroutine close_symlinks_dbm: called CC=2 SCAR=6.9
Pod documented
338 138 my $self = shift;
339 138 $self->verbose(2, 'Entering close_symlinks_dbm()');
340 138 my $hash = $self->{symlinks_dbm_hash};
341 138 ▶ return if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 90 48 138
342  
343 48 my $obj = tied($hash);
344 48 $self->{symlinks_dbm_hash} = undef;
345 48 undef $hash;
346 48 undef $obj;
347  
348 $self->_zip_file(
349 $self->{symlinks_dbm_temp_file},
350 48 catfile($self->{path}, PAR::Repository::DBM::SYMLINKS_DBM_FILE().'.zip'),
351 PAR::Repository::DBM::SYMLINKS_DBM_FILE(),
352 );
353  
354 48 unlink $self->{symlinks_dbm_temp_file};
355 48 $self->{symlinks_dbm_temp_file} = undef;
356  
357 48 return 1;
358 }
359  
360 =head2 close_scripts_dbm
361  
362 Closes the C<scripts_dists.dbm> file committing any
363 changes and then zips it back into
364 C<scripts_dists.dbm.zip>.
365  
366 This is called when the object is destroyed.
367  
368 =cut
369  
370 139 ▶sub close_scripts_dbm {
Subroutine close_scripts_dbm: called CC=2 SCAR=6.9
Pod documented
371 139 my $self = shift;
372 139 $self->verbose(2, 'Entering close_scripts_dbm()');
373 139 my $hash = $self->{scripts_dbm_hash};
374 139 ▶ return if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 112 27 139
375  
376 27 my $obj = tied($hash);
377 27 $self->{scripts_dbm_hash} = undef;
378 27 undef $hash;
379 27 undef $obj;
380  
381 $self->_zip_file(
382 $self->{scripts_dbm_temp_file},
383 27 catfile($self->{path}, PAR::Repository::DBM::SCRIPTS_DBM_FILE().'.zip'),
384 PAR::Repository::DBM::SCRIPTS_DBM_FILE()
385 );
386  
387 27 unlink $self->{scripts_dbm_temp_file};
388 27 $self->{scripts_dbm_temp_file} = undef;
389  
390 27 return 1;
391 }
392  
393  
394 =head2 close_dependencies_dbm
395  
396 Closes the C<dependencies.dbm> file committing any
397 changes and then zips it back into
398 C<dependencies.dbm.zip>.
399  
400 This is called when the object is destroyed.
401  
402 =cut
403  
404 138 ▶sub close_dependencies_dbm {
Subroutine close_dependencies_dbm: called CC=2 SCAR=6.9
Pod documented
405 138 my $self = shift;
406 138 $self->verbose(2, 'Entering close_dependencies_dbm()');
407 138 my $hash = $self->{dependencies_dbm_hash};
408 138 ▶ return if not defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 85 53 138
409  
410 53 my $obj = tied($hash);
411 53 $self->{dependencies_dbm_hash} = undef;
412 53 undef $hash;
413 53 undef $obj;
414  
415 $self->_zip_file(
416 $self->{dependencies_dbm_temp_file},
417 53 catfile($self->{path}, PAR::Repository::DBM::DEPENDENCIES_DBM_FILE().'.zip'),
418 PAR::Repository::DBM::DEPENDENCIES_DBM_FILE(),
419 );
420  
421 53 unlink $self->{dependencies_dbm_temp_file};
422 53 $self->{dependencies_dbm_temp_file} = undef;
423  
424 53 return 1;
425 }
426  
427  
428 =head2 update_dbm_checksums
429  
430 Updates the DBM checksums file C<dbm_checksums.txt> with the
431 checksums of the currently existing zipped DBM files.
432  
433 This is called when the L<PAR::Repository> object is destroyed.
434  
435 Maintainer note: Very similar code lives in the
436 C<PAR::Repository::Client::DBM::_calculate_cache_local_checksums>
437 method. Keep in sync or refactor.
438  
439 =cut
440  
441 112 ▶sub update_dbm_checksums {
Subroutine update_dbm_checksums: called CC=6 SCAR=18.4
Pod documented
442 112 my $self = shift;
443 112 $self->verbose(2, 'Entering update_dbm_checksums()');
444  
445 # find a working base64 MD5 implementation
446 112 my $md5_function;
447 112 eval { require Digest::MD5; $md5_function = \&Digest::MD5::md5_base64; };
448 0 ▶ eval { require Digest::Perl::MD5; $md5_function = \&Digest::Perl::MD5::md5_base64; } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 112 112
449 112 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 112 112
450 0 die "Could load neither Digest::MD5 nor Digest::Perl::MD5. Please upgrade your perl or install either of those modules.";
451 }
452
453 # Prepare temporary copy of the checkums file
454 112 my ($tempfh, $tempfile) = File::Temp::tempfile(
455 'temporary_dbm_checksum_XXXXX',
456 UNLINK => 0,
457 DIR => File::Spec->tmpdir(),
458 );
459 112 print $tempfh <<'HERE';
460 # This checksums file has the format
461 # FILENAME BASE64_MD5_HASH
462 # where the file name and the MD5 hash are separated
463 # by a TAB character, not arbitrary whitespace!
464 HERE
465  
466 # calculate hashes and write them to the temp file
467 112 foreach my $dbmfile (
468 PAR::Repository::DBM::MODULES_DBM_FILE(),
469 PAR::Repository::DBM::SCRIPTS_DBM_FILE(),
470 PAR::Repository::DBM::SYMLINKS_DBM_FILE(),
471 PAR::Repository::DBM::DEPENDENCIES_DBM_FILE(),
472 ) {
473 448 my $filepath = catfile($self->{path}, $dbmfile.'.zip');
474 448 ▶ open my $fh, '<', $filepath or die "Could not open DBM file '$filepath' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filepath or die "Could not open DBM file '${filepath}' for reading: $!" 448 0 448
475 448 flock $fh, LOCK_SH;
476 448 local $/ = undef;
477 448 my $hash = $md5_function->(<$fh>);
478 448 close $fh;
479 448 print $tempfh "$dbmfile.zip\t$hash\n";
480 } # end foreach dbm files
481  
482 112 close $tempfh;
483 # move temp file to destination
484 112 my $target_file = catfile($self->{path}, PAR::Repository::DBM::DBM_CHECKSUMS_FILE());
485 112 ▶ File::Copy::move($tempfile, $target_file)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::move($tempfile, $target_file) or die "Could not move checksums file '${tempfile}' to '${target_file}': $!" 112 0 112
486 or die "Could not move checksums file '$tempfile' to '$target_file': $!";
487 # FIXME, could this be done more user friendly? But somehow, the file ended up being 600 by default...
488 112 chmod(0644, $target_file);
489  
490 112 return 1;
491 }
492  
493  
494 =head2 _open_dbm
495  
496 Opens the zipped dbm file given as first argument.
497  
498 This is B<only for internal use>.
499  
500 =cut
501  
502 182 ▶sub _open_dbm {
Subroutine _open_dbm: called CC=2 SCAR=6.9
503 182 my $self = shift;
504 182 $self->verbose(2, 'Entering _open_dbm()');
505 182 my $file = shift;
506 182 my ($tempfh, $tempfile) = File::Temp::tempfile(
507 'temporary_dbm_XXXXX',
508 UNLINK => 0,
509 DIR => File::Spec->tmpdir(),
510 EXLOCK => 0,
511 );
512 182 my ($v, $p, $f) = splitpath($file);
513 182 $f =~ s/\.zip$//;
514 182 ▶ $self->_unzip_file($file, $tempfile, $f) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_unzip_file($file, $tempfile, $f) or return undef 182 0 182
515 182 my %hash;
516 182 my $obj = tie %hash, "DBM::Deep", {
517 file => $tempfile,
518 locking => 1,
519 };
520  
521 182 return (\%hash, $tempfile);
522 }
523  
524 =head2 _create_dbm
525  
526 Creates a zipped dbm file given as first argument.
527  
528 This is B<only for internal use>.
529  
530 =cut
531  
532 45 ▶sub _create_dbm {
Subroutine _create_dbm: called CC=3 SCAR=11.0
533 45 my $self = shift;
534 45 $self->verbose(2, 'Entering _create_dbm()');
535 45 my $file = shift;
536 45 ▶ $file .= '.zip' unless $file =~ /\.zip$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.zip$/i 0 45 45
537  
538 45 my ($tempfh, $tempfile) = File::Temp::tempfile(
539 'temporary_dbm_XXXXX',
540 UNLINK => 0,
541 DIR => File::Spec->tmpdir(),
542 EXLOCK => 0,
543 );
544 {
545 45 my %hash;
546 45 my $obj = tie %hash, "DBM::Deep", {
547 file => $tempfile,
548 locking => 1,
549 };
550 }
551  
552 45 my ($v, $p, $f) = splitpath($file);
553 45 $f =~ s/\.zip$//i;
554 45 ▶ $self->_zip_file($tempfile, $file, $f) or unlink($tempfile), return();
Branch: 1 branch
BranchTrueFalseTotal
$self->_zip_file($tempfile, $file, $f) or unlink($tempfile), (return ()) 45 0 45
555  
556 45 unlink($tempfile);
557  
558 45 return 1;
559 }
560  
561 22 1;
562 __END__
« blib/lib/PAR/Repository/Zip.pm ↑ Summary blib/lib/PAR/dump_db.pl »