Compressor.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Compressor;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Compression;
  17. use Dpkg::Gettext;
  18. use Dpkg::IPC;
  19. use Dpkg::ErrorHandling;
  20. use POSIX;
  21. our $default_compression = "gzip";
  22. our $default_compression_level = 9;
  23. # Class methods
  24. sub set_default_compression {
  25. my ($self, $method) = @_;
  26. error(_g("%s is not a supported compression"), $method)
  27. unless $comp_supported{$method};
  28. $default_compression = $method;
  29. }
  30. sub set_default_compression_level {
  31. my ($self, $level) = @_;
  32. error(_g("%s is not a compression level"), $level)
  33. unless $level =~ /^([1-9]|fast|best)$/;
  34. $default_compression_level = $level;
  35. }
  36. # Object methods
  37. sub new {
  38. my ($this, %args) = @_;
  39. my $class = ref($this) || $this;
  40. my $self = {};
  41. bless $self, $class;
  42. $self->set_compression($args{"compression"} || $default_compression);
  43. $self->set_compression_level($args{"compression_level"} ||
  44. $default_compression_level);
  45. return $self;
  46. }
  47. sub set_compression {
  48. my ($self, $method) = @_;
  49. error(_g("%s is not a supported compression method"), $method)
  50. unless $comp_supported{$method};
  51. $self->{"compression"} = $method;
  52. }
  53. sub set_compression_level {
  54. my ($self, $level) = @_;
  55. error(_g("%s is not a compression level"), $level)
  56. unless $level =~ /^([1-9]|fast|best)$/;
  57. $self->{"compression_level"} = $level;
  58. }
  59. sub get_compress_cmdline {
  60. my ($self) = @_;
  61. my @prog = ($comp_prog{$self->{"compression"}});
  62. my $level = "-" . $self->{"compression_level"};
  63. $level = "--" . $self->{"compression_level"}
  64. if $self->{"compression_level"} =~ m/best|fast/;
  65. push @prog, $level;
  66. return @prog;
  67. }
  68. sub get_uncompress_cmdline {
  69. my ($self) = @_;
  70. return ($comp_decomp_prog{$self->{"compression"}});
  71. }
  72. sub _sanity_check {
  73. my ($self, %opts) = @_;
  74. # Check for proper cleaning before new start
  75. error(_g("Dpkg::Source::Compressor can only start one subprocess at a time"))
  76. if $self->{"pid"};
  77. # Check options
  78. my $to = my $from = 0;
  79. foreach (qw(file handle string pipe)) {
  80. $to++ if $opts{"to_$_"};
  81. $from++ if $opts{"from_$_"};
  82. }
  83. error("exactly one to_* parameter is needed") if $to != 1;
  84. error("exactly one from_* parameter is needed") if $from != 1;
  85. return %opts;
  86. }
  87. sub compress {
  88. my $self = shift;
  89. my %opts = $self->_sanity_check(@_);
  90. my @prog = $self->get_compress_cmdline();
  91. $opts{"exec"} = \@prog;
  92. $self->{"cmdline"} = "@prog";
  93. $self->{"pid"} = fork_and_exec(%opts);
  94. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  95. }
  96. sub uncompress {
  97. my $self = shift;
  98. my %opts = $self->_sanity_check(@_);
  99. my @prog = $self->get_uncompress_cmdline();
  100. $opts{"exec"} = \@prog;
  101. $self->{"cmdline"} = "@prog";
  102. $self->{"pid"} = fork_and_exec(%opts);
  103. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  104. }
  105. sub wait_end_process {
  106. my ($self) = @_;
  107. wait_child($self->{"pid"}, cmdline => $self->{"cmdline"}) if $self->{'pid'};
  108. delete $self->{"pid"};
  109. delete $self->{"cmdline"};
  110. }
  111. 1;