CompressedFile.pm 4.1 KB

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