From 753e47cdf4265b429dc8c800321162949ab65abb Mon Sep 17 00:00:00 2001 From: Raphael Hertzog Date: Sat, 16 Feb 2008 18:52:00 +0100 Subject: [PATCH] Dpkg::Compression: add get_compression_from_filename() * scripts/Dpkg/Compression.pm: Add function get_compression_from_filename() to identify the compression type from the file extension. Returns undef in case of failure. * scripts/Dpkg/Source/Compressor.pm: Simplify some code by using the new get_compression_from_filename(). --- scripts/Dpkg/Compression.pm | 13 ++++++++++++- scripts/Dpkg/Source/Compressor.pm | 16 ++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/scripts/Dpkg/Compression.pm b/scripts/Dpkg/Compression.pm index 7d7c1738..414dcfe8 100644 --- a/scripts/Dpkg/Compression.pm +++ b/scripts/Dpkg/Compression.pm @@ -5,7 +5,8 @@ use warnings; use base qw(Exporter); our @EXPORT = qw(@comp_supported %comp_supported %comp_ext $comp_regex - %comp_prog %comp_decomp_prog); + %comp_prog %comp_decomp_prog + get_compression_from_filename); our @comp_supported = qw(gzip bzip2 lzma); our %comp_supported = map { $_ => 1 } @comp_supported; @@ -14,4 +15,14 @@ our $comp_regex = '(?:gz|bz2|lzma)'; our %comp_prog = (gzip => 'gzip', bzip2 => 'bzip2', lzma => 'lzma'); our %comp_decomp_prog = (gzip => 'gunzip', bzip2 => 'bunzip2', lzma => 'unlzma'); +sub get_compression_from_filename { + my $filename = shift; + foreach my $comp (@comp_supported) { + if ($filename =~ /^(.*)\.\Q$comp_ext{$comp}\E$/) { + return $comp; + } + } + return undef; +} + 1; diff --git a/scripts/Dpkg/Source/Compressor.pm b/scripts/Dpkg/Source/Compressor.pm index 461c1acb..06ad5841 100644 --- a/scripts/Dpkg/Source/Compressor.pm +++ b/scripts/Dpkg/Source/Compressor.pm @@ -55,17 +55,13 @@ sub set_compression_level { sub set_filename { my ($self, $filename) = @_; - # Identify compression from filename - my $found = 0; - foreach my $comp (@comp_supported) { - if ($filename =~ /^(.*)\.\Q$comp_ext{$comp}\E$/) { - $found = 1; - $self->set_compression($comp); - $self->set_uncompressed_filename($1); - last; - } + my $comp = get_compression_from_filename($filename); + if ($comp) { + $self->set_compression($comp); + $self->set_compressed_filename($filename); + } else { + error(_g("unknown compression type on file %s"), $filename); } - error(_g("unknown compression type on file %s"), $filename) unless $found; } sub set_compressed_filename { -- 2.39.5