| 1 | package PAR::WebStart::Util; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Archive::Zip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use ExtUtils::Manifest qw(mkmanifest maniread); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Module::Signature qw(sign verify SIGNATURE_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.20'; | |||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = qw(make_par verifyMD5); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant WIN32 => ($^O eq 'MSWin32'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $no_sign; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub make_par { | ||||||||||||||||||||||||||
|
Subroutine
make_par: called CC=28 SCAR=43.1
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 23 | 1 | my ($src_dir, $dst_dir, $name) = @args{qw(src_dir dst_dir name)}; | |||||||||||||||||||||||||||
| 24 | 1 | ▶ | $no_sign = 1 if $args{no_sign}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | if ($src_dir and -d $src_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$src_dir and -d $src_dir
MC/DC0%0/2
$src_dir and -d $src_dir
$src_dir
-d $src_dir
Truth table33%1/3
A and B
A= $src_dirB= -d $src_dir
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | chdir($src_dir) or die qq{Cannot chdir to "$src_dir": $!}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | if (-d 'blib') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | chdir('blib') or die qq{Cannot chdir to "blib": $!}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | my $cwd = getcwd; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | $dst_dir ||= $cwd; | ||||||||||||||||||||||||||
|
Condition33%1/3
$dst_dir ||= $cwd
MC/DC0%0/2
$dst_dir ||= $cwd
$dst_dir
$cwd
Truth table33%1/3
A ||= B
A= $dst_dirB= $cwd
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | $src_dir ||= $cwd; | ||||||||||||||||||||||||||
|
Condition33%1/3
$src_dir ||= $cwd
MC/DC0%0/2
$src_dir ||= $cwd
$src_dir
$cwd
Truth table33%1/3
A ||= B
A= $src_dirB= $cwd
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless ($name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my @d = File::Spec->splitdir($cwd); | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | $name = ($d[$#d] eq 'blib') ? $d[$#d-1] : $d[$#d]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 1 | ▶ | $name .= '.par' unless ($name =~ /\.par$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | my $dst_par = File::Spec->catfile($dst_dir, $name); | |||||||||||||||||||||||||||
| 43 | 1 | my $dst_cs = $dst_par . '.md5'; | |||||||||||||||||||||||||||
| 44 | 1 | my $src_par = File::Spec->catfile($src_dir, $name); | |||||||||||||||||||||||||||
| 45 | 1 | my $src_cs = $src_par . '.md5'; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | my @dirs = qw(arch lib script bin); | |||||||||||||||||||||||||||
| 48 | 1 | my $test = 0; | |||||||||||||||||||||||||||
| 49 | 1 | my %has; | |||||||||||||||||||||||||||
| 50 | 1 | foreach my $d (@dirs) { | |||||||||||||||||||||||||||
| 51 | 4 | ▶ | if (-d $d) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | $test++; | |||||||||||||||||||||||||||
| 53 | 2 | $has{$d}++; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | die qq{Cannot find any of "@dirs"} unless $test; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | for my $file ( $dst_par, $dst_cs, $src_par, $src_cs, qw(SIGNATURE MANIFEST) ) { | |||||||||||||||||||||||||||
| 59 | 6 | ▶ | next unless -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | warn "Removing $file ...\n"; | |||||||||||||||||||||||||||
| 61 | 0 | unlink($file); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | mkmanifest(); | |||||||||||||||||||||||||||
| 65 | 1 | my $maniread = maniread(); | |||||||||||||||||||||||||||
| 66 | 1 | ▶ | if ($no_sign) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $maniread->{SIGNATURE} = undef if $maniread->{SIGNATURE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | open(my $fh, '>>', 'MANIFEST') or die "Cannot open MANIFEST: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | print $fh "SIGNATURE\n"; | |||||||||||||||||||||||||||
| 72 | 0 | close $fh; | |||||||||||||||||||||||||||
| 73 | 0 | $maniread->{SIGNATURE}++; | |||||||||||||||||||||||||||
| 74 | 0 | sign(overwrite => 1); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | (verify() == SIGNATURE_OK) or die qq{Signature verification failed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | my $arc = Archive::Zip->new(); | |||||||||||||||||||||||||||
| 79 | 1 | print qq{\nAdding files to zip archive...\n}; | |||||||||||||||||||||||||||
| 80 | 1 | foreach my $f(keys %$maniread) { | |||||||||||||||||||||||||||
| 81 | 3 | ▶ | die qq{zip of "$f" failed} unless $arc->addFile($f, $f); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 3 | print "\t$f\n"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | die qq{Writing to "$dst_par" failed} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | unless $arc->writeToFileNamed($dst_par) == Archive::Zip::AZ_OK(); | ||||||||||||||||||||||||||||
| 87 | 1 | print qq{Done!\n}; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | open(my $par_fh, $dst_par) or die qq{Cannot open "$dst_par": $!}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | binmode($par_fh); | |||||||||||||||||||||||||||
| 91 | 1 | my $md5; | |||||||||||||||||||||||||||
| 92 | 1 | ▶ | unless ($md5 = Digest::MD5->new->addfile($par_fh)->hexdigest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | close $par_fh; | |||||||||||||||||||||||||||
| 94 | 0 | die qq{Computing md5 checksum of "$dst_par" failed}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 1 | close $par_fh; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | open(my $md5_fh, '>', $dst_cs) or die qq{Cannot open "$dst_cs": $!}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | print $md5_fh $md5; | |||||||||||||||||||||||||||
| 100 | 1 | close $md5_fh; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | my $check = verifyMD5(file => $dst_par, md5 => $dst_cs); | |||||||||||||||||||||||||||
| 103 | 1 | ▶ | if ($check == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 1 | print "Checksum for $dst_par OK.\n"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | die qq{Checksum for $dst_par failed: $check}; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 1 | return ($dst_par, $dst_cs); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | sub verifyMD5 { | ||||||||||||||||||||||||||
|
Subroutine
verifyMD5: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | my ($md5_file, $file) = @args{qw(md5 file)}; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | my ($should, $is); | |||||||||||||||||||||||||||
| 118 | 2 | ▶ | open(my $cs, $md5_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | or return qq{Cannot open "$md5_file" to verify md5: $!}; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | chomp($should = <$cs>); | |||||||||||||||||||||||||||
| 122 | 2 | $should =~ s{\r}{}; | |||||||||||||||||||||||||||
| 123 | 2 | close $cs; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | open (my $par, $file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | or return qq{Cannot open "$file": $!}; | ||||||||||||||||||||||||||||
| 127 | 2 | binmode($par); | |||||||||||||||||||||||||||
| 128 | 2 | ▶ | unless ($is = Digest::MD5->new->addfile($par)->hexdigest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | close $par; | |||||||||||||||||||||||||||
| 130 | 0 | return qq{Could not compute checksum for "$file": $!}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 2 | close $par; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | return ($should eq $is) ? 1 : qq{Checksum for "$file" failed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||