Process.pm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Copyright © 2008-2010 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Compression::Process;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. use Carp;
  20. use Dpkg::Compression;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Gettext;
  23. use Dpkg::IPC;
  24. =encoding utf8
  25. =head1 NAME
  26. Dpkg::Compression::Process - run compression/decompression processes
  27. =head1 DESCRIPTION
  28. This module provides an object oriented interface to run and manage
  29. compression/decompression processes.
  30. =head1 METHODS
  31. =over 4
  32. =item $proc = Dpkg::Compression::Process->new(%opts)
  33. Create a new instance of the object. Supported options are "compression"
  34. and "compression_level" (see corresponding set_* functions).
  35. =cut
  36. sub new {
  37. my ($this, %args) = @_;
  38. my $class = ref($this) || $this;
  39. my $self = {};
  40. bless $self, $class;
  41. $self->set_compression($args{compression} || compression_get_default());
  42. $self->set_compression_level($args{compression_level} ||
  43. compression_get_default_level());
  44. return $self;
  45. }
  46. =item $proc->set_compression($comp)
  47. Select the compression method to use. It errors out if the method is not
  48. supported according to C<compression_is_supported> (of
  49. B<Dpkg::Compression>).
  50. =cut
  51. sub set_compression {
  52. my ($self, $method) = @_;
  53. error(g_('%s is not a supported compression method'), $method)
  54. unless compression_is_supported($method);
  55. $self->{compression} = $method;
  56. }
  57. =item $proc->set_compression_level($level)
  58. Select the compression level to use. It errors out if the level is not
  59. valid according to C<compression_is_valid_level> (of
  60. B<Dpkg::Compression>).
  61. =cut
  62. sub set_compression_level {
  63. my ($self, $level) = @_;
  64. error(g_('%s is not a compression level'), $level)
  65. unless compression_is_valid_level($level);
  66. $self->{compression_level} = $level;
  67. }
  68. =item @exec = $proc->get_compress_cmdline()
  69. =item @exec = $proc->get_uncompress_cmdline()
  70. Returns a list ready to be passed to C<exec>, its first element is the
  71. program name (either for compression or decompression) and the following
  72. elements are parameters for the program.
  73. When executed the program acts as a filter between its standard input
  74. and its standard output.
  75. =cut
  76. sub get_compress_cmdline {
  77. my $self = shift;
  78. my @prog = (@{compression_get_property($self->{compression}, 'comp_prog')});
  79. my $level = '-' . $self->{compression_level};
  80. $level = '--' . $self->{compression_level}
  81. if $self->{compression_level} !~ m/^[1-9]$/;
  82. push @prog, $level;
  83. return @prog;
  84. }
  85. sub get_uncompress_cmdline {
  86. my $self = shift;
  87. return (@{compression_get_property($self->{compression}, 'decomp_prog')});
  88. }
  89. sub _sanity_check {
  90. my ($self, %opts) = @_;
  91. # Check for proper cleaning before new start
  92. error(g_('Dpkg::Compression::Process can only start one subprocess at a time'))
  93. if $self->{pid};
  94. # Check options
  95. my $to = my $from = 0;
  96. foreach my $thing (qw(file handle string pipe)) {
  97. $to++ if $opts{"to_$thing"};
  98. $from++ if $opts{"from_$thing"};
  99. }
  100. croak 'exactly one to_* parameter is needed' if $to != 1;
  101. croak 'exactly one from_* parameter is needed' if $from != 1;
  102. return %opts;
  103. }
  104. =item $proc->compress(%opts)
  105. Starts a compressor program. You must indicate where it will read its
  106. uncompressed data from and where it will write its compressed data to.
  107. This is accomplished by passing one parameter C<to_*> and one parameter
  108. C<from_*> as accepted by B<Dpkg::IPC::spawn>.
  109. You must call C<wait_end_process> after having called this method to
  110. properly close the sub-process (and verify that it exited without error).
  111. =cut
  112. sub compress {
  113. my ($self, %opts) = @_;
  114. $self->_sanity_check(%opts);
  115. my @prog = $self->get_compress_cmdline();
  116. $opts{exec} = \@prog;
  117. $self->{cmdline} = "@prog";
  118. $self->{pid} = spawn(%opts);
  119. delete $self->{pid} if $opts{to_string}; # wait_child already done
  120. }
  121. =item $proc->uncompress(%opts)
  122. Starts a decompressor program. You must indicate where it will read its
  123. compressed data from and where it will write its uncompressed data to.
  124. This is accomplished by passing one parameter C<to_*> and one parameter
  125. C<from_*> as accepted by B<Dpkg::IPC::spawn>.
  126. You must call C<wait_end_process> after having called this method to
  127. properly close the sub-process (and verify that it exited without error).
  128. =cut
  129. sub uncompress {
  130. my ($self, %opts) = @_;
  131. $self->_sanity_check(%opts);
  132. my @prog = $self->get_uncompress_cmdline();
  133. $opts{exec} = \@prog;
  134. $self->{cmdline} = "@prog";
  135. $self->{pid} = spawn(%opts);
  136. delete $self->{pid} if $opts{to_string}; # wait_child already done
  137. }
  138. =item $proc->wait_end_process(%opts)
  139. Call B<Dpkg::IPC::wait_child> to wait until the sub-process has exited
  140. and verify its return code. Any given option will be forwarded to
  141. the C<wait_child> function. Most notably you can use the "nocheck" option
  142. to verify the return code yourself instead of letting C<wait_child> do
  143. it for you.
  144. =cut
  145. sub wait_end_process {
  146. my ($self, %opts) = @_;
  147. $opts{cmdline} //= $self->{cmdline};
  148. wait_child($self->{pid}, %opts) if $self->{pid};
  149. delete $self->{pid};
  150. delete $self->{cmdline};
  151. }
  152. =back
  153. =head1 CHANGES
  154. =head2 Version 1.00 (dpkg 1.15.6)
  155. Mark the module as public.
  156. =cut
  157. 1;