File Coverage

File:Dpkg/Compression/Process.pm
Coverage:89.1%

linestmtbrancondsubpodtimecode
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 - 28
=head1 NAME

Dpkg::Compression::Process - run compression/decompression processes

=head1 DESCRIPTION

This module provides an object oriented interface to run and manage
compression/decompression processes.

=cut
29
30package Dpkg::Compression::Process 1.00;
31
32
75
75
75
242
57
1399
use strict;
33
75
75
75
139
56
1683
use warnings;
34
35
75
75
75
147
66
2014
use Carp;
36
37
75
75
75
147
68
3289
use Dpkg::Compression;
38
75
75
75
168
63
2737
use Dpkg::ErrorHandling;
39
75
75
75
145
81
1771
use Dpkg::Gettext;
40
75
75
75
10787
101
23969
use Dpkg::IPC;
41
42 - 51
=head1 METHODS

=over 4

=item $proc = Dpkg::Compression::Process->new(%opts)

Create a new instance of the object. Supported options are "compression"
and "compression_level" (see corresponding set_* functions).

=cut
52
53sub new {
54
528
1
821
    my ($this, %args) = @_;
55
528
1625
    my $class = ref($this) || $this;
56
528
493
    my $self = {};
57
528
532
    bless $self, $class;
58
528
1877
    $self->set_compression($args{compression} || compression_get_default());
59    $self->set_compression_level($args{compression_level} ||
60
528
1501
        compression_get_default_level());
61
528
1084
    return $self;
62}
63
64 - 70
=item $proc->set_compression($comp)

Select the compression method to use. It errors out if the method is not
supported according to compression_is_supported() (of
L<Dpkg::Compression>).

=cut
71
72sub set_compression {
73
570
1
709
    my ($self, $method) = @_;
74
570
969
    error(g_('%s is not a supported compression method'), $method)
75        unless compression_is_supported($method);
76
570
961
    $self->{compression} = $method;
77}
78
79 - 85
=item $proc->set_compression_level($level)

Select the compression level to use. It errors out if the level is not
valid according to compression_is_valid_level() (of
L<Dpkg::Compression>).

=cut
86
87sub set_compression_level {
88
528
1
586
    my ($self, $level) = @_;
89
90
528
955
    compression_set_level($self->{compression}, $level);
91}
92
93 - 104
=item @exec = $proc->get_compress_cmdline()

=item @exec = $proc->get_uncompress_cmdline()

Returns a list ready to be passed to exec(), its first element is the
program name (either for compression or decompression) and the following
elements are parameters for the program.

When executed the program acts as a filter between its standard input
and its standard output.

=cut
105
106sub get_compress_cmdline {
107
27
1
24
    my $self = shift;
108
109
27
78
    return compression_get_cmdline_compress($self->{compression});
110}
111
112sub get_uncompress_cmdline {
113
15
1
14
    my $self = shift;
114
115
15
40
    return compression_get_cmdline_decompress($self->{compression});
116}
117
118sub _check_opts {
119
42
68
    my ($self, %opts) = @_;
120    # Check for proper cleaning before new start
121    error(g_('Dpkg::Compression::Process can only start one subprocess at a time'))
122
42
58
        if $self->{pid};
123    # Check options
124
42
45
    my $to = my $from = 0;
125
42
57
    foreach my $thing (qw(file handle string pipe)) {
126
168
161
        $to++ if $opts{"to_$thing"};
127
168
156
        $from++ if $opts{"from_$thing"};
128    }
129
42
59
    croak 'exactly one to_* parameter is needed' if $to != 1;
130
42
64
    croak 'exactly one from_* parameter is needed' if $from != 1;
131
42
51
    return %opts;
132}
133
134 - 144
=item $proc->compress(%opts)

Starts a compressor program. You must indicate where it will read its
uncompressed data from and where it will write its compressed data to.
This is accomplished by passing one parameter C<to_*> and one parameter
C<from_*> as accepted by Dpkg::IPC::spawn().

You must call wait_end_process() after having called this method to
properly close the sub-process (and verify that it exited without error).

=cut
145
146sub compress {
147
27
1
119
    my ($self, %opts) = @_;
148
149
27
98
    $self->_check_opts(%opts);
150
27
61
    my @prog = $self->get_compress_cmdline();
151
27
48
    $opts{exec} = \@prog;
152
27
50
    $self->{cmdline} = "@prog";
153
27
62
    $self->{pid} = spawn(%opts);
154
21
489
    delete $self->{pid} if $opts{to_string}; # wait_child already done
155}
156
157 - 167
=item $proc->uncompress(%opts)

Starts a decompressor program. You must indicate where it will read its
compressed data from and where it will write its uncompressed data to.
This is accomplished by passing one parameter C<to_*> and one parameter
C<from_*> as accepted by Dpkg::IPC::spawn().

You must call wait_end_process() after having called this method to
properly close the sub-process (and verify that it exited without error).

=cut
168
169sub uncompress {
170
15
1
45
    my ($self, %opts) = @_;
171
172
15
36
    $self->_check_opts(%opts);
173
15
23
    my @prog = $self->get_uncompress_cmdline();
174
15
27
    $opts{exec} = \@prog;
175
15
26
    $self->{cmdline} = "@prog";
176
15
51
    $self->{pid} = spawn(%opts);
177
9
98
    delete $self->{pid} if $opts{to_string}; # wait_child already done
178}
179
180 - 188
=item $proc->wait_end_process(%opts)

Call Dpkg::IPC::wait_child() to wait until the sub-process has exited
and verify its return code. Any given option will be forwarded to
the wait_child() function. Most notably you can use the "nocheck" option
to verify the return code yourself instead of letting wait_child() do
it for you.

=cut
189
190sub wait_end_process {
191
450
1
867
    my ($self, %opts) = @_;
192
450
1825
    $opts{cmdline} //= $self->{cmdline};
193
450
716
    wait_child($self->{pid}, %opts) if $self->{pid};
194
450
439
    delete $self->{pid};
195
450
684
    delete $self->{cmdline};
196}
197
198=back
199
200 - 206
=head1 CHANGES

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut
207
2081;