]>
Commit | Line | Data |
---|---|---|
2b380a88 AM |
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 | |
5 | ||
6 | Does not use any extra perl modules. | |
7 | Attempts hard coded types first, so no extra code for the standard | |
8 | case. | |
9 | Easy to add more compression types. | |
10 | --- | |
11 | src/src/exigrep.src | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ | |
12 | 1 file changed, 53 insertions(+) | |
13 | ||
2634bfff | 14 | diff --git a/src/src/exigrep.src b/src/src/exigrep.src |
2b380a88 | 15 | index 0950b58..d22d362 100644 |
2634bfff AM |
16 | --- a/src/src/exigrep.src |
17 | +++ b/src/src/exigrep.src | |
2b380a88 | 18 | @@ -124,6 +124,54 @@ elsif ( ($invert && (($insensitive && !/$pattern/io) || !/$pattern/o)) || |
2634bfff AM |
19 | { print "$_\n"; } |
20 | } | |
21 | ||
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. | |
26 | +# | |
27 | +# List of known compression extensions and their associated commands: | |
28 | +my $compressors = { | |
29 | + gz => { cmd => 'zcat', args => '' }, | |
30 | + bz2 => { cmd => 'bzcat', args => '' }, | |
31 | + xz => { cmd => 'xzcat', args => '' }, | |
32 | + lzma => { cmd => 'lzma', args => '-dc' } | |
33 | +}; | |
34 | +my $csearch = 0; | |
35 | + | |
36 | +sub detect_compressor_bin | |
37 | + { | |
38 | + my $ext = shift(); | |
39 | + my $c = $compressors->{$ext}->{cmd}; | |
cc8360fe | 40 | + $compressors->{$ext}->{bin} = `which $c 2>/dev/null`; |
2634bfff AM |
41 | + chomp($compressors->{$ext}->{bin}); |
42 | + } | |
43 | + | |
44 | +sub detect_compressor_capable | |
45 | + { | |
46 | + my $filename = shift(); | |
47 | + map { &detect_compressor_bin($_) } keys %$compressors | |
48 | + if (!$csearch); | |
49 | + $csearch = 1; | |
50 | + return undef | |
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) | |
56 | + { | |
57 | + if ($filename =~ /\.(?:$ext)$/) | |
58 | + { | |
2b380a88 AM |
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} ." ". | |
cc8360fe | 64 | + $compressors->{$ext}->{args}; |
2634bfff AM |
65 | + last; |
66 | + } | |
67 | + } | |
68 | + return $cmdline; | |
69 | + } | |
70 | ||
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 | |
2b380a88 | 73 | @@ -154,6 +202,11 @@ if (@ARGV) |
2634bfff AM |
74 | open(LOG, "ZCAT_COMMAND $filename |") || |
75 | die "Unable to zcat $filename: $!\n"; | |
76 | } | |
77 | + elsif (my $cmdline = &detect_compressor_capable($filename)) | |
78 | + { | |
79 | + open(LOG, "$cmdline $filename |") || | |
80 | + die "Unable to decompress $filename: $!\n"; | |
81 | + } | |
82 | else | |
83 | { | |
84 | open(LOG, "<$filename") || die "Unable to open $filename: $!\n"; | |
2b380a88 AM |
85 | -- |
86 | 1.7.9.5 | |
87 |