blib/lib/EBook/Ishmael/Unzip.pm

filter: Statementstmt 87.8% 36 / 41 Branchbran 41.6% 5 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 63.8% 53 / 83 CC 12 SCAR 30.3
CC
12
Cov
63%
safe_tmp_unzip
50.1
unzip
5.4
BEGIN
1.0
CRAP 20.7
« blib/lib/EBook/Ishmael/CharDet/HZ.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Mobi.pm »
1 package EBook::Ishmael::Unzip;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.03';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 our @EXPORT_OK = qw(safe_tmp_unzip unzip);
9  
10 17 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub safe_tmp_unzip {
17  
18 # Archive::Zip does not support unzipping to symlinked directory. This is a
19 # problem on platforms like Darwin, as /tmp is symlinked.
20 31 ▶ if (not -l File::Spec->tmpdir) {
Subroutine safe_tmp_unzip: called CC=8 SCAR=39.1
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
if (not -l 'File::Spec'->tmpdir) { } 31 0 31
elsif (not -l cwd() and -w cwd()) { } 0 0 0
elsif (exists $ENV{'HOME'} and -d $ENV{'HOME'} and not -l $ENV{'HOME'} and -w $ENV{'HOME'}) { } 0 0 0
21 31 return tempdir(CLEANUP => 1);
22 # Try working directory...
23 ▶ } elsif (! -l cwd and -w cwd) {
Condition0%0/3
not -l cwd() and -w cwd()
!ll&&!rl&&r
000
MC/DC0%0/2
not -l cwd() and -w cwd()
not -l cwd() -w cwd()
Truth table0%0/3
not(A) and B
A= not -l cwd()B= -w cwd()
ABresult
0X0
100
111
24 0 return tempdir(DIR => cwd, CLEANUP => 1);
25 # Try HOME...
26 ▶ } elsif (
Condition0%0/9
exists $ENV{"HOME"} and -d $ENV{"HOME"} and not -l $ENV{"HOME"} and -w $ENV{"HOME"}
!ll&&!rl&&r
000
exists $ENV{"HOME"} and -d $ENV{"HOME"} and not -l $ENV{"HOME"}
!ll&&!rl&&r
000
exists $ENV{"HOME"} and -d $ENV{"HOME"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $ENV{"HOME"} and -d $ENV{"HOME"} and not -l $ENV{"HOME"} and -w $ENV{"HOME"}
exists $ENV{"HOME"} -d $ENV{"HOME"} not -l $ENV{"HOME"} -w $ENV{"HOME"}
Truth table0%0/5
A and B and not(C) and D
A= exists $ENV{"HOME"}B= -d $ENV{"HOME"}C= not -l $ENV{"HOME"}D= -w $ENV{"HOME"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
27 exists $ENV{HOME} and
28 -d $ENV{HOME} and
29 ! -l $ENV{HOME} and
30 -w $ENV{HOME}
31 ) {
32 0 return tempdir(DIR => $ENV{HOME}, CLEANUP => 1);
33 # Give up and die :-(
34 } else {
35 0 die "Could not find a suitable unzip directory\n";
36 }
37  
38 }
39  
40 sub unzip {
41  
42 31 ▶ my $zip = shift;
Subroutine unzip: called CC=5 SCAR=16.8
Pod documented
43 31 my $out = shift;
44  
45 31 my $obj = Archive::Zip->new;
46  
47 31 ▶ unless ($obj->read($zip) == AZ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->read($zip) == 0) 0 31 31
48 0 die "Could not read $zip as a zip archive\n";
49 }
50  
51 31 for my $m ($obj->members) {
52 443 ▶ $m->unixFileAttributes($m->isDirectory ? 0755 : 0644);
Branch: 1 branch
BranchTrueFalseTotal
$m->isDirectory ? : 20 423 443
53 }
54  
55 31 ▶ unless ($obj->extractTree('', $out) == AZ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->extractTree("", $out) == 0) 0 31 31
56 0 die "Could not unzip $zip to $out\n";
57 }
58  
59 31 return 1;
60  
61 }
62  
63 17 1;
64  
65 =head1 NAME
66  
67 EBook::Ishmael::Unzip - Unzip Zip archives
68  
69 =head1 SYNOPSIS
70  
71 use EBook::Ishmael::Unzip qw(unzip);
72  
73 unzip($zip, $out);
74  
75 =head1 DESCRIPTION
76  
77 B<EBook::Ishmael::Unzip> is a module that provides the C<unzip()> subroutine,
78 which unzips a given Zip file to a specified directory. This is developer
79 documentation, for L<ishmael> user documentation you should consult its manual.
80  
81 =head1 SUBROUTINES
82  
83 =over 4
84  
85 =item $tmpdir = safe_tmp_unzip()
86  
87 Creates and returns a suitable temporary unzip directory. This function exists
88 because Archive::Zip cannot unzip to some kinds of directories, like symlinked
89 ones, which can be problematic on platforms such as Darwin where their F</tmp>
90 directory is symlinked by default.
91  
92 =item unzip($zip, $out)
93  
94 Unzips C<$zip> to the C<$out> directory. Returns C<1> if successful.
95  
96 =back
97  
98 =head1 AUTHOR
99  
100 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
101  
102 This project's source can be found on its
103 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
104 requests are welcome!
105  
106 =head1 COPYRIGHT
107  
108 Copyright (C) 2025-2026 Samuel Young
109  
110 This program is free software: you can redistribute it and/or modify
111 it under the terms of the GNU General Public License as published by
112 the Free Software Foundation, either version 3 of the License, or
113 (at your option) any later version.
114  
115 =cut
« blib/lib/EBook/Ishmael/CharDet/HZ.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Mobi.pm »