/*
- * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
- * 1998, 2008
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
+ * 1998, 2013
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ * http://sourceforge.net/projects/sarg/forums/forum/363374
* ---------------------------------------------------------------------
*
* This program is free software; you can redistribute it and/or modify
*/
#include "include/conf.h"
+#include "include/defs.h"
-void decomp(char *arq, char *zip, char *tmp)
-{
-
- char cmd[1024];
+/*!
+Open the log file. If it is compressed, uncompress it through a pipe.
- if(access(arq, R_OK) != 0) {
- sprintf(cmd,"%s: %s",text[64],arq);
- debuga(cmd);
- exit(1);
- }
+Log files compressed with gzip, bzip2 or compress are uncompressed with zcat or bzcat.
- if(strstr(arq,".gz") !=0) {
- sprintf(cmd,"%s: %s > %s/sarg-file.in (zcat)",text[62],arq,tmp);
- debuga(cmd);
- sprintf(cmd,"zcat %s > %s/sarg-file.in",arq,tmp);
- system(cmd);
- strcpy(zip,"zcat");
- sprintf(arq,"%s/sarg-file.in",tmp);
- return;
- }
+If the log file does not exist, the process terminates with an error message.
- if(strstr(arq,".bz2") != 0) {
- sprintf(cmd,"%s: %s > %s/sarg-file.in (bzcat)",text[62],arq,tmp);
- debuga(cmd);
- sprintf(cmd,"bzcat %s > %s/sarg-file.in",arq,tmp);
- system(cmd);
- strcpy(zip,"zcat");
- sprintf(arq,"%s/sarg-file.in",tmp);
- return;
- }
+\param arq The log file to process.
+\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.
- if(strstr(arq,".Z")) {
- sprintf(cmd,"%s: %s (uncompress)",text[62],arq);
- debuga(cmd);
- sprintf(cmd,"uncompress %s",arq);
- system(cmd);
- arq[strlen(arq)-2]='\0';
- strcpy(zip,"compress");
- }
+\date 2009-09-24 - F Marchal\n This function used to uncompress .Z files in
+place using uncompress but that required a write access to the log directory,
+could conflict with logrotate and could leave the log file uncompressed if sarg
+crashed. According to the documentation, zcat is capable of uncompressing .Z
+files so it is now used.
- return;
-
-}
-
-
-void recomp(char *arq, char *zip)
+\date 2010-05-10 - F Marchal\n
+The function doesn't use a temporary file any more and read the compressed file through a pipe.
+*/
+FILE *decomp(const char *arq, bool *pipe)
{
-
- char cmd[1024];
-
- if(access(arq, R_OK) != 0) {
- sprintf(cmd,"%s: %s",text[64],arq);
- debuga(cmd);
- exit(1);
- }
-
- sprintf(cmd,"%s: %s",text[63],arq);
- debuga(cmd);
-
- if(strcmp(zip,"gzip") == 0)
- sprintf(cmd,"%s %s",zip,arq);
-
- if(strcmp(zip,"compress") == 0)
- sprintf(cmd,"%s %s",zip,arq);
-
- system(cmd);
- return;
-
+ FILE *fi;
+ char cmd[1024];
+ int arqlen;
+
+ arqlen=strlen(arq);
+ if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) {
+ /*
+ TRANSLATORS: The last %s is the command used to uncompress
+ the log file such as zcat ou bzip2.
+ */
+ debuga(_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
+ if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
+ debuga(_("decompression command too long for log file %s\n"),arq);
+ exit(EXIT_FAILURE);
+ }
+ *pipe=true;
+ fi=popen(cmd,"r");
+ }
+ else if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) {
+ debuga(_("Decompressing log file \"%s\" with %s\n"),arq,"bzcat");
+ if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) {
+ debuga(_("decompression command too long for log file %s\n"),arq);
+ exit(EXIT_FAILURE);
+ }
+ *pipe=true;
+ fi=popen(cmd,"r");
+ }
+ else if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) {
+ debuga(_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
+ if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
+ debuga(_("decompression command too long for log file %s\n"),arq);
+ exit(EXIT_FAILURE);
+ }
+ *pipe=true;
+ fi=popen(cmd,"r");
+ }
+ else {
+ *pipe=false;
+ fi=MY_FOPEN(arq,"r");
+ }
+ return(fi);
}