CompressedFile.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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::CompressedFile;
  13. use strict;
  14. use warnings;
  15. use Dpkg::Compression;
  16. use Dpkg::Source::Compressor;
  17. use Dpkg::Gettext;
  18. use Dpkg::ErrorHandling;
  19. use POSIX;
  20. # Object methods
  21. sub new {
  22. my ($this, %args) = @_;
  23. my $class = ref($this) || $this;
  24. my $self = {
  25. "compression" => "auto"
  26. };
  27. bless $self, $class;
  28. $self->{"compressor"} = Dpkg::Source::Compressor->new();
  29. $self->{"add_comp_ext"} = $args{"add_compression_extension"} ||
  30. $args{"add_comp_ext"} || 0;
  31. $self->{"allow_sigpipe"} = 0;
  32. if (exists $args{"filename"}) {
  33. $self->set_filename($args{"filename"});
  34. }
  35. if (exists $args{"compression"}) {
  36. $self->set_compression($args{"compression"});
  37. }
  38. if (exists $args{"compression_level"}) {
  39. $self->set_compression_level($args{"compression_level"});
  40. }
  41. return $self;
  42. }
  43. sub reset {
  44. my ($self) = @_;
  45. %{$self} = ();
  46. }
  47. sub set_compression {
  48. my ($self, $method) = @_;
  49. if ($method ne "none" and $method ne "auto") {
  50. $self->{"compressor"}->set_compression($method);
  51. }
  52. $self->{"compression"} = $method;
  53. }
  54. sub set_compression_level {
  55. my ($self, $level) = @_;
  56. $self->{"compressor"}->set_compression_level($level);
  57. }
  58. sub set_filename {
  59. my ($self, $filename, $add_comp_ext) = @_;
  60. $self->{"filename"} = $filename;
  61. # Automatically add compression extension to filename
  62. if (defined($add_comp_ext)) {
  63. $self->{"add_comp_ext"} = $add_comp_ext;
  64. }
  65. if ($self->{"add_comp_ext"} and $filename =~ /\.$comp_regex$/) {
  66. warning("filename %s already has an extension of a compressed file " .
  67. "and add_comp_ext is active", $filename);
  68. }
  69. }
  70. sub get_filename {
  71. my $self = shift;
  72. my $comp = $self->{"compression"};
  73. if ($self->{'add_comp_ext'}) {
  74. if ($comp eq "auto") {
  75. internerr("automatic detection of compression is " .
  76. "incompatible with add_comp_ext");
  77. } elsif ($comp eq "none") {
  78. return $self->{"filename"};
  79. } else {
  80. return $self->{"filename"} . "." . $comp_ext{$comp};
  81. }
  82. } else {
  83. return $self->{"filename"};
  84. }
  85. }
  86. sub use_compression {
  87. my ($self, $update) = @_;
  88. my $comp = $self->{"compression"};
  89. if ($comp eq "none") {
  90. return 0;
  91. } elsif ($comp eq "auto") {
  92. $comp = get_compression_from_filename($self->get_filename());
  93. $self->{"compressor"}->set_compression($comp) if $comp;
  94. }
  95. return $comp;
  96. }
  97. sub open_for_write {
  98. my ($self) = @_;
  99. my $handle;
  100. if ($self->use_compression()) {
  101. $self->{'compressor'}->compress(from_pipe => \$handle,
  102. to_file => $self->get_filename());
  103. } else {
  104. open($handle, '>', $self->get_filename()) ||
  105. syserr(_g("cannot write %s"), $self->get_filename());
  106. }
  107. return $handle;
  108. }
  109. sub open_for_read {
  110. my ($self) = @_;
  111. my $handle;
  112. if ($self->use_compression()) {
  113. $self->{'compressor'}->uncompress(to_pipe => \$handle,
  114. from_file => $self->get_filename());
  115. $self->{'allow_sigpipe'} = 1;
  116. } else {
  117. open($handle, '<', $self->get_filename()) ||
  118. syserr(_g("cannot read %s"), $self->get_filename());
  119. }
  120. return $handle;
  121. }
  122. sub cleanup_after_open {
  123. my ($self) = @_;
  124. my $cmdline = $self->{"compressor"}{"cmdline"} || "";
  125. $self->{"compressor"}->wait_end_process(nocheck => $self->{'allow_sigpipe'});
  126. if ($self->{'allow_sigpipe'}) {
  127. unless (($? == 0) || (WIFSIGNALED($?) && (WTERMSIG($?) == SIGPIPE))) {
  128. subprocerr($cmdline);
  129. }
  130. }
  131. }
  132. 1;