1 From 83befcbcee0756af0c43f2a5f7dbed3bb5a4cd6e Mon Sep 17 00:00:00 2001
2 From: Todd Lyons <tlyons@exim.org>
3 Date: Sat, 12 Oct 2013 09:42:31 -0700
4 Subject: [PATCH] Bug 1334: AutoDetect compression type in exigrep
6 Does not use any extra perl modules.
7 Attempts hard coded types first, so no extra code for the standard
9 Easy to add more compression types.
11 src/src/exigrep.src | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
12 1 file changed, 53 insertions(+)
14 diff --git a/src/src/exigrep.src b/src/src/exigrep.src
15 index 0950b58..d22d362 100644
16 --- a/src/src/exigrep.src
17 +++ b/src/src/exigrep.src
18 @@ -124,6 +124,54 @@ elsif ( ($invert && (($insensitive && !/$pattern/io) || !/$pattern/o)) ||
22 +# Rotated log files are frequently compressed and there are a variety of
23 +# formats it could be compressed with. Rather than use just one that is
24 +# detected and hardcoded at Exim compile time, detect and use what the
25 +# logfile is compressed with on the fly.
27 +# List of known compression extensions and their associated commands:
29 + gz => { cmd => 'zcat', args => '' },
30 + bz2 => { cmd => 'bzcat', args => '' },
31 + xz => { cmd => 'xzcat', args => '' },
32 + lzma => { cmd => 'lzma', args => '-dc' }
36 +sub detect_compressor_bin
39 + my $c = $compressors->{$ext}->{cmd};
40 + $compressors->{$ext}->{bin} = `which $c 2>/dev/null`;
41 + chomp($compressors->{$ext}->{bin});
44 +sub detect_compressor_capable
46 + my $filename = shift();
47 + map { &detect_compressor_bin($_) } keys %$compressors
51 + unless (grep {$filename =~ /\.(?:$_)$/} keys %$compressors);
52 + # Loop through them, figure out which one it detected,
53 + # and build the commandline.
54 + my $cmdline = undef;
55 + foreach my $ext (keys %$compressors)
57 + if ($filename =~ /\.(?:$ext)$/)
59 + # Just die if compressor not found; if this occurrs in the middle of
60 + # two valid files with a lot of matches, error could easily be missed.
61 + die("Didn't find $ext decompressor for $filename\n")
62 + if ($compressors->{$ext}->{bin} eq '');
63 + $cmdline = $compressors->{$ext}->{bin} ." ".
64 + $compressors->{$ext}->{args};
71 # The main program. Extract the pattern and make sure any relevant characters
72 # are quoted if the -l flag is given. The -t flag gives a time-on-queue value
73 @@ -154,6 +202,11 @@ if (@ARGV)
74 open(LOG, "ZCAT_COMMAND $filename |") ||
75 die "Unable to zcat $filename: $!\n";
77 + elsif (my $cmdline = &detect_compressor_capable($filename))
79 + open(LOG, "$cmdline $filename |") ||
80 + die "Unable to decompress $filename: $!\n";
84 open(LOG, "<$filename") || die "Unable to open $filename: $!\n";