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
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Dpkg::Source::Compressor;
  13. use strict;
  14. use warnings;
  15. use Dpkg::Compression;
  16. use Dpkg::Gettext;
  17. use Dpkg::IPC;
  18. use Dpkg::ErrorHandling;
  19. use POSIX;
  20. our $default_compression = "gzip";
  21. our $default_compression_level = 9;
  22. # Class methods
  23. sub set_default_compression {
  24. my ($self, $method) = @_;
  25. error(_g("%s is not a supported compression"), $method)
  26. unless $comp_supported{$method};
  27. $default_compression = $method;
  28. }
  29. sub set_default_compression_level {
  30. my ($self, $level) = @_;
  31. error(_g("%s is not a compression level"), $level)
  32. unless $level =~ /^([1-9]|fast|best)$/;
  33. $default_compression_level = $level;
  34. }
  35. # Object methods
  36. sub new {
  37. my ($this, %args) = @_;
  38. my $class = ref($this) || $this;
  39. my $self = {};
  40. bless $self, $class;
  41. $self->set_compression($args{"compression"} || $default_compression);
  42. $self->set_compression_level($args{"compression_level"} ||
  43. $default_compression_level);
  44. return $self;
  45. }
  46. sub set_compression {
  47. my ($self, $method) = @_;
  48. error(_g("%s is not a supported compression method"), $method)
  49. unless $comp_supported{$method};
  50. $self->{"compression"} = $method;
  51. }
  52. sub set_compression_level {
  53. my ($self, $level) = @_;
  54. error(_g("%s is not a compression level"), $level)
  55. unless $level =~ /^([1-9]|fast|best)$/;
  56. $self->{"compression_level"} = $level;
  57. }
  58. sub get_compress_cmdline {
  59. my ($self) = @_;
  60. my @prog = ($comp_prog{$self->{"compression"}});
  61. my $level = "-" . $self->{"compression_level"};
  62. $level = "--" . $self->{"compression_level"}
  63. if $self->{"compression_level"} =~ m/best|fast/;
  64. push @prog, $level;
  65. return @prog;
  66. }
  67. sub get_uncompress_cmdline {
  68. my ($self) = @_;
  69. return ($comp_decomp_prog{$self->{"compression"}});
  70. }
  71. sub _sanity_check {
  72. my ($self, %opts) = @_;
  73. # Check for proper cleaning before new start
  74. error(_g("Dpkg::Source::Compressor can only start one subprocess at a time"))
  75. if $self->{"pid"};
  76. # Check options
  77. my $to = my $from = 0;
  78. foreach (qw(file handle string pipe)) {
  79. $to++ if $opts{"to_$_"};
  80. $from++ if $opts{"from_$_"};
  81. }
  82. internerr("exactly one to_* parameter is needed") if $to != 1;
  83. internerr("exactly one from_* parameter is needed") if $from != 1;
  84. return %opts;
  85. }
  86. sub compress {
  87. my $self = shift;
  88. my %opts = $self->_sanity_check(@_);
  89. my @prog = $self->get_compress_cmdline();
  90. $opts{"exec"} = \@prog;
  91. $self->{"cmdline"} = "@prog";
  92. $self->{"pid"} = fork_and_exec(%opts);
  93. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  94. }
  95. sub uncompress {
  96. my $self = shift;
  97. my %opts = $self->_sanity_check(@_);
  98. my @prog = $self->get_uncompress_cmdline();
  99. $opts{"exec"} = \@prog;
  100. $self->{"cmdline"} = "@prog";
  101. $self->{"pid"} = fork_and_exec(%opts);
  102. delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
  103. }
  104. sub wait_end_process {
  105. my ($self, %opts) = @_;
  106. $opts{"cmdline"} ||= $self->{"cmdline"};
  107. wait_child($self->{"pid"}, %opts) if $self->{'pid'};
  108. delete $self->{"pid"};
  109. delete $self->{"cmdline"};
  110. }
  111. 1;