<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2008 RaphaÃ«l Hertzog &lt;hertzog@debian.org&gt;

# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

package Dpkg::Source::CompressedFile;

use strict;
use warnings;

use Dpkg::Compression;
use Dpkg::Source::Compressor;
use Dpkg::Gettext;
use Dpkg::ErrorHandling qw(error syserr warning);

# Object methods
sub new {
    my ($this, %args) = @_;
    my $class = ref($this) || $this;
    my $self = {
	"compression" =&gt; "auto"
    };
    bless $self, $class;
    $self-&gt;{"compressor"} = Dpkg::Source::Compressor-&gt;new();
    $self-&gt;{"add_comp_ext"} = $args{"add_compression_extension"} ||
	    $args{"add_comp_ext"} || 0;
    if (exists $args{"filename"}) {
	$self-&gt;set_filename($args{"filename"});
    }
    if (exists $args{"compression"}) {
	$self-&gt;set_compression($args{"compression"});
    }
    if (exists $args{"compression_level"}) {
	$self-&gt;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-&gt;{"compressor"}-&gt;set_compression($method);
    }
    $self-&gt;{"compression"} = $method;
}

sub set_compression_level {
    my ($self, $level) = @_;
    $self-&gt;{"compressor"}-&gt;set_compression_level($level);
}

sub set_filename {
    my ($self, $filename, $add_comp_ext) = @_;
    $self-&gt;{"filename"} = $filename;
    # Automatically add compression extension to filename
    if (defined($add_comp_ext)) {
	$self-&gt;{"add_comp_ext"} = $add_comp_ext;
    }
    if ($self-&gt;{"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-&gt;{"compression"};
    if ($self-&gt;{'add_comp_ext'}) {
	if ($comp eq "auto") {
	    error("automatic detection of compression is " .
	          "incompatible with add_comp_ext");
	} elsif ($comp eq "none") {
	    return $self-&gt;{"filename"};
	} else {
	    return $self-&gt;{"filename"} . "." . $comp_ext{$comp};
	}
    } else {
	return $self-&gt;{"filename"};
    }
}

sub use_compression {
    my ($self, $update) = @_;
    my $comp = $self-&gt;{"compression"};
    if ($comp eq "none") {
	return 0;
    } elsif ($comp eq "auto") {
	$comp = get_compression_from_filename($self-&gt;get_filename());
	$self-&gt;{"compressor"}-&gt;set_compression($comp) if $comp;
    }
    return $comp;
}

sub open_for_write {
    my ($self) = @_;
    my $handle;
    if ($self-&gt;use_compression()) {
	$self-&gt;{'compressor'}-&gt;compress(from_pipe =&gt; \$handle,
		to_file =&gt; $self-&gt;get_filename());
    } else {
	open($handle, '&gt;', $self-&gt;get_filename()) ||
		syserr(_g("cannot write %s"), $self-&gt;get_filename());
    }
    return $handle;
}

sub open_for_read {
    my ($self) = @_;
    my $handle;
    if ($self-&gt;use_compression()) {
	$self-&gt;{'compressor'}-&gt;uncompress(to_pipe =&gt; \$handle,
		from_file =&gt; $self-&gt;get_filename());
    } else {
	open($handle, '&lt;', $self-&gt;get_filename()) ||
		syserr(_g("cannot read %s"), $self-&gt;get_filename());
    }
    return $handle;
}

sub cleanup_after_open {
    my ($self) = @_;
    $self-&gt;{"compressor"}-&gt;wait_end_process();
}

1;
</pre></body></html>