diff options
author | Raphaël Hertzog <hertzog@debian.org> | 2009-11-01 18:56:08 +0100 |
---|---|---|
committer | Raphaël Hertzog <hertzog@debian.org> | 2010-01-22 01:50:03 +0100 |
commit | 31bde76e009e2a18c8813fe61816ee9719f2228d (patch) | |
tree | dcee227d7c1c033f2b4be52548bba554ad93cf04 /scripts/Dpkg/Compression/CompressedFile.pm | |
parent | 42a195698c55c7382f9b1e07378ad7c8cc00c7d9 (diff) | |
download | dpkg-31bde76e009e2a18c8813fe61816ee9719f2228d.tar.gz |
Rename Dpkg::Source::CompressedFile into Dpkg::Compression::CompressedFile
Diffstat (limited to 'scripts/Dpkg/Compression/CompressedFile.pm')
-rw-r--r-- | scripts/Dpkg/Compression/CompressedFile.pm | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/scripts/Dpkg/Compression/CompressedFile.pm b/scripts/Dpkg/Compression/CompressedFile.pm new file mode 100644 index 000000000..c7eae1352 --- /dev/null +++ b/scripts/Dpkg/Compression/CompressedFile.pm @@ -0,0 +1,149 @@ +# Copyright © 2008 Raphaël Hertzog <hertzog@debian.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +package Dpkg::Compression::CompressedFile; + +use strict; +use warnings; + +use Dpkg::Compression; +use Dpkg::Compression::Compressor; +use Dpkg::Gettext; +use Dpkg::ErrorHandling; +use POSIX; + +# Object methods +sub new { + my ($this, %args) = @_; + my $class = ref($this) || $this; + my $self = { + "compression" => "auto" + }; + bless $self, $class; + $self->{"compressor"} = Dpkg::Compression::Compressor->new(); + $self->{"add_comp_ext"} = $args{"add_compression_extension"} || + $args{"add_comp_ext"} || 0; + $self->{"allow_sigpipe"} = 0; + if (exists $args{"filename"}) { + $self->set_filename($args{"filename"}); + } + if (exists $args{"compression"}) { + $self->set_compression($args{"compression"}); + } + if (exists $args{"compression_level"}) { + $self->set_compression_level($args{"compression_level"}); + } + return $self; +} + +sub reset { + my ($self) = @_; + %{$self} = (); +} + +sub set_compression { + my ($self, $method) = @_; + if ($method ne "none" and $method ne "auto") { + $self->{"compressor"}->set_compression($method); + } + $self->{"compression"} = $method; +} + +sub set_compression_level { + my ($self, $level) = @_; + $self->{"compressor"}->set_compression_level($level); +} + +sub set_filename { + my ($self, $filename, $add_comp_ext) = @_; + $self->{"filename"} = $filename; + # Automatically add compression extension to filename + if (defined($add_comp_ext)) { + $self->{"add_comp_ext"} = $add_comp_ext; + } + if ($self->{"add_comp_ext"} and $filename =~ /\.$comp_regex$/) { + warning("filename %s already has an extension of a compressed file " . + "and add_comp_ext is active", $filename); + } +} + +sub get_filename { + my $self = shift; + my $comp = $self->{"compression"}; + if ($self->{'add_comp_ext'}) { + if ($comp eq "auto") { + internerr("automatic detection of compression is " . + "incompatible with add_comp_ext"); + } elsif ($comp eq "none") { + return $self->{"filename"}; + } else { + return $self->{"filename"} . "." . $comp_ext{$comp}; + } + } else { + return $self->{"filename"}; + } +} + +sub use_compression { + my ($self, $update) = @_; + my $comp = $self->{"compression"}; + if ($comp eq "none") { + return 0; + } elsif ($comp eq "auto") { + $comp = get_compression_from_filename($self->get_filename()); + $self->{"compressor"}->set_compression($comp) if $comp; + } + return $comp; +} + +sub open_for_write { + my ($self) = @_; + my $handle; + if ($self->use_compression()) { + $self->{'compressor'}->compress(from_pipe => \$handle, + to_file => $self->get_filename()); + } else { + open($handle, '>', $self->get_filename()) || + syserr(_g("cannot write %s"), $self->get_filename()); + } + return $handle; +} + +sub open_for_read { + my ($self) = @_; + my $handle; + if ($self->use_compression()) { + $self->{'compressor'}->uncompress(to_pipe => \$handle, + from_file => $self->get_filename()); + $self->{'allow_sigpipe'} = 1; + } else { + open($handle, '<', $self->get_filename()) || + syserr(_g("cannot read %s"), $self->get_filename()); + } + return $handle; +} + +sub cleanup_after_open { + my ($self) = @_; + my $cmdline = $self->{"compressor"}{"cmdline"} || ""; + $self->{"compressor"}->wait_end_process(nocheck => $self->{'allow_sigpipe'}); + if ($self->{'allow_sigpipe'}) { + unless (($? == 0) || (WIFSIGNALED($?) && (WTERMSIG($?) == SIGPIPE))) { + subprocerr($cmdline); + } + } +} + +1; |