]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
67302a9e | 3 | * 1998, 2013 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
9 | * --------------------------------------------------------------------- |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "include/conf.h" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
d2cd218e FM |
30 | /*! |
31 | Open the log file. If it is compressed, uncompress it through a pipe. | |
32 | ||
33 | Log files compressed with gzip, bzip2 or compress are uncompressed with zcat or bzcat. | |
34 | ||
35 | If the log file does not exist, the process terminates with an error message. | |
36 | ||
37 | \param arq The log file to process. | |
38 | \param pipe A variable set to \c true if the log file is opened through a pipe or set to \c false if the file is open directly. | |
39 | ||
40 | \date 2009-09-24 - F Marchal\n This function used to uncompress .Z files in | |
41 | place using uncompress but that required a write access to the log directory, | |
42 | could conflict with logrotate and could leave the log file uncompressed if sarg | |
43 | crashed. According to the documentation, zcat is capable of uncompressing .Z | |
44 | files so it is now used. | |
45 | ||
46 | \date 2010-05-10 - F Marchal\n | |
47 | The function doesn't use a temporary file any more and read the compressed file through a pipe. | |
48 | */ | |
d2855b39 | 49 | FILE *decomp(const char *arq, bool *pipe) |
25697a35 | 50 | { |
d2cd218e | 51 | FILE *fi; |
9bd92830 FM |
52 | char cmd[1024]; |
53 | int arqlen; | |
25697a35 | 54 | |
9bd92830 FM |
55 | arqlen=strlen(arq); |
56 | if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) { | |
57 | debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); | |
58 | if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { | |
59 | debuga(_("decompression command too long for log file %s\n"),arq); | |
60 | exit(EXIT_FAILURE); | |
61 | } | |
62 | *pipe=true; | |
d2cd218e | 63 | fi=popen(cmd,"r"); |
9bd92830 | 64 | } |
d2cd218e | 65 | else if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) { |
9bd92830 FM |
66 | debuga(_("Decompressing log file \"%s\" with bzcat\n"),arq); |
67 | if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) { | |
68 | debuga(_("decompression command too long for log file %s\n"),arq); | |
69 | exit(EXIT_FAILURE); | |
70 | } | |
71 | *pipe=true; | |
d2cd218e | 72 | fi=popen(cmd,"r"); |
9bd92830 | 73 | } |
d2cd218e | 74 | else if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) { |
9bd92830 FM |
75 | debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); |
76 | if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { | |
77 | debuga(_("decompression command too long for log file %s\n"),arq); | |
78 | exit(EXIT_FAILURE); | |
79 | } | |
80 | *pipe=true; | |
d2cd218e | 81 | fi=popen(cmd,"r"); |
9bd92830 | 82 | } |
d2cd218e FM |
83 | else { |
84 | *pipe=false; | |
85 | fi=MY_FOPEN(arq,"r"); | |
86 | } | |
87 | return(fi); | |
25697a35 | 88 | } |