Process.pm 5.8 KB

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