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