CompressedFile.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 qw(error syserr warning);
  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. if (exists $args{"filename"}) {
  32. $self->set_filename($args{"filename"});
  33. }
  34. if (exists $args{"compression"}) {
  35. $self->set_compression($args{"compression"});
  36. }
  37. if (exists $args{"compression_level"}) {
  38. $self->set_compression_level($args{"compression_level"});
  39. }
  40. return $self;
  41. }
  42. sub reset {
  43. my ($self) = @_;
  44. %{$self} = ();
  45. }
  46. sub set_compression {
  47. my ($self, $method) = @_;
  48. if ($method ne "none" and $method ne "auto") {
  49. $self->{"compressor"}->set_compression($method);
  50. }
  51. $self->{"compression"} = $method;
  52. }
  53. sub set_compression_level {
  54. my ($self, $level) = @_;
  55. $self->{"compressor"}->set_compression_level($level);
  56. }
  57. sub set_filename {
  58. my ($self, $filename, $add_comp_ext) = @_;
  59. $self->{"filename"} = $filename;
  60. # Automatically add compression extension to filename
  61. if (defined($add_comp_ext)) {
  62. $self->{"add_comp_ext"} = $add_comp_ext;
  63. }
  64. if ($self->{"add_comp_ext"} and $filename =~ /\.$comp_regex$/) {
  65. warning("filename %s already has an extension of a compressed file " .
  66. "and add_comp_ext is active", $filename);
  67. }
  68. }
  69. sub get_filename {
  70. my $self = shift;
  71. my $comp = $self->{"compression"};
  72. if ($self->{'add_comp_ext'}) {
  73. if ($comp eq "auto") {
  74. error("automatic detection of compression is " .
  75. "incompatible with add_comp_ext");
  76. } elsif ($comp eq "none") {
  77. return $self->{"filename"};
  78. } else {
  79. return $self->{"filename"} . "." . $comp_ext{$comp};
  80. }
  81. } else {
  82. return $self->{"filename"};
  83. }
  84. }
  85. sub use_compression {
  86. my ($self, $update) = @_;
  87. my $comp = $self->{"compression"};
  88. if ($comp eq "none") {
  89. return 0;
  90. } elsif ($comp eq "auto") {
  91. $comp = get_compression_from_filename($self->get_filename());
  92. $self->{"compressor"}->set_compression($comp) if $comp;
  93. }
  94. return $comp;
  95. }
  96. sub open_for_write {
  97. my ($self) = @_;
  98. my $handle;
  99. if ($self->use_compression()) {
  100. $self->{'compressor'}->compress(from_pipe => \$handle,
  101. to_file => $self->get_filename());
  102. } else {
  103. open($handle, '>', $self->get_filename()) ||
  104. syserr(_g("cannot write %s"), $self->get_filename());
  105. }
  106. return $handle;
  107. }
  108. sub open_for_read {
  109. my ($self) = @_;
  110. my $handle;
  111. if ($self->use_compression()) {
  112. $self->{'compressor'}->uncompress(to_pipe => \$handle,
  113. from_file => $self->get_filename());
  114. } else {
  115. open($handle, '<', $self->get_filename()) ||
  116. syserr(_g("cannot read %s"), $self->get_filename());
  117. }
  118. return $handle;
  119. }
  120. sub cleanup_after_open {
  121. my ($self) = @_;
  122. $self->{"compressor"}->wait_end_process();
  123. }
  124. 1;