Compressor.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright © 2008 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::Compressor;
  16. use strict;
  17. use warnings;
  18. use Dpkg::Compression;
  19. use Dpkg::Gettext;
  20. use Dpkg::IPC;
  21. use Dpkg::ErrorHandling;
  22. use POSIX;
  23. # Object methods
  24. sub new {
  25. my ($this, %args) = @_;
  26. my $class = ref($this) || $this;
  27. my $self = {};
  28. bless $self, $class;
  29. $self->set_compression($args{"compression"} || compression_get_default());
  30. $self->set_compression_level($args{"compression_level"} ||
  31. compression_get_default_level());
  32. return $self;
  33. }
  34. sub set_compression {
  35. my ($self, $method) = @_;
  36. error(_g("%s is not a supported compression method"), $method)
  37. unless compression_is_supported($method);
  38. $self->{"compression"} = $method;
  39. }
  40. sub set_compression_level {
  41. my ($self, $level) = @_;
  42. error(_g("%s is not a compression level"), $level)
  43. unless compression_is_valid_level($level);
  44. $self->{"compression_level"} = $level;
  45. }
  46. sub get_compress_cmdline {
  47. my ($self) = @_;
  48. my @prog = (compression_get_property($self->{"compression"}, "comp_prog"));
  49. my $level = "-" . $self->{"compression_level"};
  50. $level = "--" . $self->{"compression_level"}
  51. if $self->{"compression_level"} =~ m/best|fast/;
  52. push @prog, $level;
  53. return @prog;
  54. }
  55. sub get_uncompress_cmdline {
  56. my ($self) = @_;
  57. return (compression_get_property($self->{"compression"}, "decomp_prog"));
  58. }
  59. sub _sanity_check {
  60. my ($self, %opts) = @_;
  61. # Check for proper cleaning before new start
  62. error(_g("Dpkg::Compression::Compressor can only start one subprocess at a time"))
  63. if $self->{"pid"};
  64. # Check options
  65. my $to = my $from = 0;
  66. foreach (qw(file handle string pipe)) {
  67. $to++ if $opts{"to_$_"};
  68. $from++ if $opts{"from_$_"};
  69. }
  70. internerr("exactly one to_* parameter is needed") if $to != 1;
  71. internerr("exactly one from_* parameter is needed") if $from != 1;
  72. return %opts;
  73. }
  74. sub compress {
  75. my $self = shift;
  76. my %opts = $self->_sanity_check(@_);
  77. my @prog = $self->get_compress_cmdline();
  78. $opts{"exec"} = \@prog;
  79. $self->{"cmdline"} = "@prog";
  80. $self->{"pid"} = fork_and_exec(%opts);
  81. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  82. }
  83. sub uncompress {
  84. my $self = shift;
  85. my %opts = $self->_sanity_check(@_);
  86. my @prog = $self->get_uncompress_cmdline();
  87. $opts{"exec"} = \@prog;
  88. $self->{"cmdline"} = "@prog";
  89. $self->{"pid"} = fork_and_exec(%opts);
  90. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  91. }
  92. sub wait_end_process {
  93. my ($self, %opts) = @_;
  94. $opts{"cmdline"} ||= $self->{"cmdline"};
  95. wait_child($self->{"pid"}, %opts) if $self->{'pid'};
  96. delete $self->{"pid"};
  97. delete $self->{"cmdline"};
  98. }
  99. 1;