Process.pm 5.8 KB

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