man: Add dpkg-build-api behavior for Rules-Requires-Root field defaults
[dpkg.git] / scripts / Dpkg / Compression / Process.pm
blobcf4c504034e16e06fa222824cbcd4c1f2de27000
1 # Copyright © 2008-2010 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008-2022 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
17 =encoding utf8
19 =head1 NAME
21 Dpkg::Compression::Process - run compression/decompression processes
23 =head1 DESCRIPTION
25 This module provides an object oriented interface to run and manage
26 compression/decompression processes.
28 =cut
30 package Dpkg::Compression::Process 1.00;
32 use strict;
33 use warnings;
35 use Carp;
37 use Dpkg::Compression;
38 use Dpkg::ErrorHandling;
39 use Dpkg::Gettext;
40 use Dpkg::IPC;
42 =head1 METHODS
44 =over 4
46 =item $proc = Dpkg::Compression::Process->new(%opts)
48 Create a new instance of the object. Supported options are "compression"
49 and "compression_level" (see corresponding set_* functions).
51 =cut
53 sub new {
54 my ($this, %args) = @_;
55 my $class = ref($this) || $this;
56 my $self = {};
57 bless $self, $class;
58 $self->set_compression($args{compression} || compression_get_default());
59 $self->set_compression_level($args{compression_level} ||
60 compression_get_default_level());
61 return $self;
64 =item $proc->set_compression($comp)
66 Select the compression method to use. It errors out if the method is not
67 supported according to compression_is_supported() (of
68 L<Dpkg::Compression>).
70 =cut
72 sub set_compression {
73 my ($self, $method) = @_;
74 error(g_('%s is not a supported compression method'), $method)
75 unless compression_is_supported($method);
76 $self->{compression} = $method;
79 =item $proc->set_compression_level($level)
81 Select the compression level to use. It errors out if the level is not
82 valid according to compression_is_valid_level() (of
83 L<Dpkg::Compression>).
85 =cut
87 sub set_compression_level {
88 my ($self, $level) = @_;
90 compression_set_level($self->{compression}, $level);
93 =item @exec = $proc->get_compress_cmdline()
95 =item @exec = $proc->get_uncompress_cmdline()
97 Returns a list ready to be passed to exec(), its first element is the
98 program name (either for compression or decompression) and the following
99 elements are parameters for the program.
101 When executed the program acts as a filter between its standard input
102 and its standard output.
104 =cut
106 sub get_compress_cmdline {
107 my $self = shift;
109 return compression_get_cmdline_compress($self->{compression});
112 sub get_uncompress_cmdline {
113 my $self = shift;
115 return compression_get_cmdline_decompress($self->{compression});
118 sub _check_opts {
119 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 if $self->{pid};
123 # Check options
124 my $to = my $from = 0;
125 foreach my $thing (qw(file handle string pipe)) {
126 $to++ if $opts{"to_$thing"};
127 $from++ if $opts{"from_$thing"};
129 croak 'exactly one to_* parameter is needed' if $to != 1;
130 croak 'exactly one from_* parameter is needed' if $from != 1;
131 return %opts;
134 =item $proc->compress(%opts)
136 Starts a compressor program. You must indicate where it will read its
137 uncompressed data from and where it will write its compressed data to.
138 This is accomplished by passing one parameter C<to_*> and one parameter
139 C<from_*> as accepted by Dpkg::IPC::spawn().
141 You must call wait_end_process() after having called this method to
142 properly close the sub-process (and verify that it exited without error).
144 =cut
146 sub compress {
147 my ($self, %opts) = @_;
149 $self->_check_opts(%opts);
150 my @prog = $self->get_compress_cmdline();
151 $opts{exec} = \@prog;
152 $self->{cmdline} = "@prog";
153 $self->{pid} = spawn(%opts);
154 delete $self->{pid} if $opts{to_string}; # wait_child already done
157 =item $proc->uncompress(%opts)
159 Starts a decompressor program. You must indicate where it will read its
160 compressed data from and where it will write its uncompressed data to.
161 This is accomplished by passing one parameter C<to_*> and one parameter
162 C<from_*> as accepted by Dpkg::IPC::spawn().
164 You must call wait_end_process() after having called this method to
165 properly close the sub-process (and verify that it exited without error).
167 =cut
169 sub uncompress {
170 my ($self, %opts) = @_;
172 $self->_check_opts(%opts);
173 my @prog = $self->get_uncompress_cmdline();
174 $opts{exec} = \@prog;
175 $self->{cmdline} = "@prog";
176 $self->{pid} = spawn(%opts);
177 delete $self->{pid} if $opts{to_string}; # wait_child already done
180 =item $proc->wait_end_process(%opts)
182 Call Dpkg::IPC::wait_child() to wait until the sub-process has exited
183 and verify its return code. Any given option will be forwarded to
184 the wait_child() function. Most notably you can use the "nocheck" option
185 to verify the return code yourself instead of letting wait_child() do
186 it for you.
188 =cut
190 sub wait_end_process {
191 my ($self, %opts) = @_;
192 $opts{cmdline} //= $self->{cmdline};
193 wait_child($self->{pid}, %opts) if $self->{pid};
194 delete $self->{pid};
195 delete $self->{cmdline};
198 =back
200 =head1 CHANGES
202 =head2 Version 1.00 (dpkg 1.15.6)
204 Mark the module as public.
206 =cut