| 1 | # Copyright © 2008-2010 Raphaël Hertzog <hertzog@debian.org> | ||||||||||||||||||||
| 2 | # Copyright © 2008-2022 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||
| 7 | # (at your option) any later version. | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||||||||||||
| 13 | # | ||||||||||||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | Dpkg::Compression::Process - run compression/decompression processes | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | This module provides an object oriented interface to run and manage | ||||||||||||||||||||
| 26 | compression/decompression processes. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | package Dpkg::Compression::Process 1.00; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 23 | ▶ | use v5.36; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 23 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 23 | ▶ | use Dpkg::Compression; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 23 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 23 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 23 | ▶ | use Dpkg::IPC; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head1 METHODS | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =item $proc = Dpkg::Compression::Process->new(%opts) | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Create a new instance of the object. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | Options: | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =over | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =item B<compression> | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | See $proc->set_compression(). | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =item B<compression_level> | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | See $proc->set_compression_level(). | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 183 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.8
Pod
documented
| |||||||||||||||||||||
| 66 | 183 | my ($this, %opts) = @_; | |||||||||||||||||||
| 67 | 183 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 68 | 183 | my $self = {}; | |||||||||||||||||||
| 69 | 183 | bless $self, $class; | |||||||||||||||||||
| 70 | 183 | ▶ | $self->set_compression($opts{compression} || compression_get_default()); | ||||||||||||||||||
|
Condition33%1/3
$opts{'compression'} || compression_get_default()
MC/DC0%0/2
$opts{'compression'} || compression_get_default()
$opts{'compression'}
compression_get_default()
Truth table33%1/3
A || B
A= $opts{'compression'}B= compression_get_default()
| |||||||||||||||||||||
| 71 | $self->set_compression_level($opts{compression_level} || | ||||||||||||||||||||
| 72 | 183 | ▶ | compression_get_default_level()); | ||||||||||||||||||
|
Condition33%1/3
$opts{'compression_level'} || compression_get_default_level()
MC/DC0%0/2
$opts{'compression_level'} || compression_get_default_level()
$opts{'compression_level'}
compression_get_default_level()
Truth table33%1/3
A || B
A= $opts{'compression_level'}B= compression_get_default_level()
| |||||||||||||||||||||
| 73 | 183 | return $self; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =item $proc->set_compression($comp) | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Select the compression method to use. It errors out if the method is not | ||||||||||||||||||||
| 79 | supported according to compression_is_supported() (of | ||||||||||||||||||||
| 80 | L<Dpkg::Compression>). | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 197 | ▶ | sub set_compression { | ||||||||||||||||||
|
Subroutine
set_compression: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 85 | 197 | my ($self, $method) = @_; | |||||||||||||||||||
| 86 | 197 | ▶ | error(g_('%s is not a supported compression method'), $method) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | unless compression_is_supported($method); | ||||||||||||||||||||
| 88 | 197 | $self->{compression} = $method; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =item $proc->set_compression_level($level) | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | Select the compression level to use. It errors out if the level is not | ||||||||||||||||||||
| 94 | valid according to compression_is_valid_level() (of | ||||||||||||||||||||
| 95 | L<Dpkg::Compression>). | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 183 | ▶ | sub set_compression_level { | ||||||||||||||||||
|
Subroutine
set_compression_level: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 100 | 183 | my ($self, $level) = @_; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 183 | compression_set_level($self->{compression}, $level); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =item @exec = $proc->get_compress_cmdline() | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =item @exec = $proc->get_uncompress_cmdline() | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Returns a list ready to be passed to exec(), its first element is the | ||||||||||||||||||||
| 110 | program name (either for compression or decompression) and the following | ||||||||||||||||||||
| 111 | elements are parameters for the program. | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | When executed the program acts as a filter between its standard input | ||||||||||||||||||||
| 114 | and its standard output. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 9 | ▶ | sub get_compress_cmdline { | ||||||||||||||||||
|
Subroutine
get_compress_cmdline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 119 | 9 | my $self = shift; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 9 | return compression_get_cmdline_compress($self->{compression}); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 5 | ▶ | sub get_uncompress_cmdline { | ||||||||||||||||||
|
Subroutine
get_uncompress_cmdline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 125 | 5 | my $self = shift; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 5 | return compression_get_cmdline_decompress($self->{compression}); | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 14 | ▶ | sub _check_opts { | ||||||||||||||||||
|
Subroutine
_check_opts: called CC=7 SCAR=19.7
| |||||||||||||||||||||
| 131 | 14 | my ($self, %opts) = @_; | |||||||||||||||||||
| 132 | # Check for proper cleaning before new start. | ||||||||||||||||||||
| 133 | error(g_('Dpkg::Compression::Process can only start one subprocess at a time')) | ||||||||||||||||||||
| 134 | 14 | ▶ | if $self->{pid}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | # Check options. | ||||||||||||||||||||
| 136 | 14 | my $to = my $from = 0; | |||||||||||||||||||
| 137 | 14 | foreach my $thing (qw(file handle string pipe)) { | |||||||||||||||||||
| 138 | 56 | ▶ | $to++ if $opts{"to_$thing"}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 56 | ▶ | $from++ if $opts{"from_$thing"}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 14 | ▶ | croak 'exactly one to_* parameter is needed' if $to != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 14 | ▶ | croak 'exactly one from_* parameter is needed' if $from != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 14 | return %opts; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =item $proc->compress(%opts) | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | Starts a compressor program. You must indicate where it will read its | ||||||||||||||||||||
| 149 | uncompressed data from and where it will write its compressed data to. | ||||||||||||||||||||
| 150 | This is accomplished by passing one parameter C<to_*> and one parameter | ||||||||||||||||||||
| 151 | C<from_*> as accepted by Dpkg::IPC::spawn(). | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | You must call wait_end_process() after having called this method to | ||||||||||||||||||||
| 154 | properly close the sub-process (and verify that it exited without error). | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Options: | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | See Dpkg::IPC::spawn(). | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 9 | ▶ | sub compress { | ||||||||||||||||||
|
Subroutine
compress: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 163 | 9 | my ($self, %opts) = @_; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 9 | $self->_check_opts(%opts); | |||||||||||||||||||
| 166 | 9 | my @prog = $self->get_compress_cmdline(); | |||||||||||||||||||
| 167 | 9 | $opts{exec} = \@prog; | |||||||||||||||||||
| 168 | 9 | $self->{cmdline} = "@prog"; | |||||||||||||||||||
| 169 | 9 | $self->{pid} = spawn(%opts); | |||||||||||||||||||
| 170 | # We already did wait_child(). | ||||||||||||||||||||
| 171 | 7 | ▶ | delete $self->{pid} if $opts{to_string}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =item $proc->uncompress(%opts) | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Starts a decompressor program. You must indicate where it will read its | ||||||||||||||||||||
| 177 | compressed data from and where it will write its uncompressed data to. | ||||||||||||||||||||
| 178 | This is accomplished by passing one parameter C<to_*> and one parameter | ||||||||||||||||||||
| 179 | C<from_*> as accepted by Dpkg::IPC::spawn(). | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | You must call wait_end_process() after having called this method to | ||||||||||||||||||||
| 182 | properly close the sub-process (and verify that it exited without error). | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Options: | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | See Dpkg::IPC::spawn(). | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 5 | ▶ | sub uncompress { | ||||||||||||||||||
|
Subroutine
uncompress: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 191 | 5 | my ($self, %opts) = @_; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 5 | $self->_check_opts(%opts); | |||||||||||||||||||
| 194 | 5 | my @prog = $self->get_uncompress_cmdline(); | |||||||||||||||||||
| 195 | 5 | $opts{exec} = \@prog; | |||||||||||||||||||
| 196 | 5 | $self->{cmdline} = "@prog"; | |||||||||||||||||||
| 197 | 5 | $self->{pid} = spawn(%opts); | |||||||||||||||||||
| 198 | # We already did wait_child(). | ||||||||||||||||||||
| 199 | 3 | ▶ | delete $self->{pid} if $opts{to_string}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =item $proc->wait_end_process(%opts) | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | Call Dpkg::IPC::wait_child() to wait until the sub-process has exited | ||||||||||||||||||||
| 205 | and verify its return code. Any given option will be forwarded to | ||||||||||||||||||||
| 206 | the wait_child() function. Most notably you can use the "no_check" option | ||||||||||||||||||||
| 207 | to verify the return code yourself instead of letting wait_child() do | ||||||||||||||||||||
| 208 | it for you. | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Options: | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | See Dpkg::IPC::wait_child(). | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 146 | ▶ | sub wait_end_process { | ||||||||||||||||||
|
Subroutine
wait_end_process: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 217 | 146 | my ($self, %opts) = @_; | |||||||||||||||||||
| 218 | 146 | ▶ | $opts{cmdline} //= $self->{cmdline}; | ||||||||||||||||||
|
Condition66%2/3
$opts{"cmdline"} //= $self->{"cmdline"}
MC/DC50%1/2
$opts{"cmdline"} //= $self->{"cmdline"}
$opts{"cmdline"}
$self->{"cmdline"}
Truth table66%2/3
A //= B
A= $opts{"cmdline"}B= $self->{"cmdline"}
| |||||||||||||||||||||
| 219 | 146 | ▶ | wait_child($self->{pid}, %opts) if $self->{pid}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | 146 | delete $self->{pid}; | |||||||||||||||||||
| 221 | 146 | delete $self->{cmdline}; | |||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =back | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =head1 CHANGES | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =head2 Version 1.00 (dpkg 1.15.6) | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | Mark the module as public. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 21 | 1; | |||||||||||||||||||