Process.pm 5.8 KB

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