]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - topsites.c
Add support to decompress xz files
[thirdparty/sarg.git] / topsites.c
index d599744134a8b99b38aa45c7ac1b068434a2a32f..5992cef520e38c3b672b299095ffddff01137106 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SARG Squid Analysis Report Generator      http://sarg.sourceforge.net
- *                                                            1998, 2013
+ *                                                            1998, 2015
  *
  * SARG donations:
  *      please look at http://sarg.sourceforge.net/donations.php
@@ -33,12 +33,13 @@ extern struct globalstatstruct globstat;
 
 void topsites(void)
 {
-       FILE *fp_in, *fp_ou;
+       FileObject *fp_in;
+       FILE *fp_ou;
 
        char *buf;
        char *url;
        char *ourl=NULL;
-       char csort[255];
+       char csort[4096];
        char general[MAXLEN];
        char general2[MAXLEN];
        char general3[MAXLEN];
@@ -69,9 +70,11 @@ void topsites(void)
        struct generalitemstruct item;
 
        if(Privacy) {
-               if (debugz>=LogLevel_Process) debugaz(_("Top sites report not produced because privacy option is on\n"));
+               if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Top sites report not produced because privacy option is on\n"));
                return;
        }
+       if (debugz>=LogLevel_Process)
+               debuga(__FILE__,__LINE__,_("Creating top sites report...\n"));
 
        sprintf(general,"%s/sarg-general",outdirname);
        sprintf(sites,"%s/sarg-sites",outdirname);
@@ -81,29 +84,29 @@ void topsites(void)
        sprintf(report,"%s/topsites.html",outdirname);
 
        if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
-               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
                exit(EXIT_FAILURE);
        }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-               debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-               debuga(_("sort command: %s\n"),csort);
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
-       if((fp_in=fopen(general2,"r"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s: %s\n"),general2,strerror(errno));
-               debuga(_("sort command: %s\n"),csort);
+       if((fp_in=FileObject_Open(general2))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,FileObject_GetLastOpenError());
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(general3,"w"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s: %s\n"),general3,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read file %s\n"),general2);
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),general2);
                exit(EXIT_FAILURE);
        }
 
@@ -117,7 +120,7 @@ void topsites(void)
                                ourl_size=url_len+1;
                                ourl=realloc(ourl,ourl_size);
                                if (!ourl) {
-                                       debuga(_("Not enough memory to store the url\n"));
+                                       debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
                                        exit(EXIT_FAILURE);
                                }
                        }
@@ -136,7 +139,7 @@ void topsites(void)
                                ourl_size=url_len+1;
                                ourl=realloc(ourl,ourl_size);
                                if (!ourl) {
-                                       debuga(_("Not enough memory to store the url\n"));
+                                       debuga(__FILE__,__LINE__,_("Not enough memory to store the url\n"));
                                        exit(EXIT_FAILURE);
                                }
                        }
@@ -160,7 +163,10 @@ void topsites(void)
                ttntime+=item.nelap;
 #endif
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        if (ourl) {
@@ -173,19 +179,19 @@ void topsites(void)
        }
 
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Write error in %s: %s\n"),general3,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),general3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
 #ifdef ENABLE_DOUBLE_CHECK_DATA
        if (ttnacc!=globstat.nacc || ttnbytes!=globstat.nbytes || ttntime!=globstat.elap) {
-               debuga(_("Total statistics mismatch when reading %s to produce the top sites\n"),general2);
+               debuga(__FILE__,__LINE__,_("Total statistics mismatch when reading \"%s\" to produce the top sites\n"),general2);
                exit(EXIT_FAILURE);
        }
 #endif
 
        if (!KeepTempLog && unlink(general2)) {
-               debuga(_("Cannot delete \"%s\": %s\n"),general2,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),general2,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -207,28 +213,28 @@ void topsites(void)
        }
 
        if (snprintf(csort,sizeof(csort),"sort -t \"\t\" %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3)>=sizeof(csort)) {
-               debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites);
+               debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general3,sites);
                exit(EXIT_FAILURE);
        }
        cstatus=system(csort);
        if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
-               debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
-               debuga(_("sort command: %s\n"),csort);
+               debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
-       if((fp_in=fopen(sites,"r"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s: %s\n"),sites,strerror(errno));
-               debuga(_("sort command: %s\n"),csort);
+       if((fp_in=FileObject_Open(sites))==NULL) {
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),sites,FileObject_GetLastOpenError());
+               debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
                exit(EXIT_FAILURE);
        }
 
        if (!KeepTempLog && unlink(general3)) {
-               debuga(_("Cannot delete \"%s\": %s\n"),general3,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),general3,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
        if((fp_ou=fopen(report,"w"))==NULL) {
-               debuga(_("(topsites) Cannot open log file %s: %s\n"),report,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }
 
@@ -264,27 +270,27 @@ void topsites(void)
        ntopsites = 0;
 
        if ((line=longline_create())==NULL) {
-               debuga(_("Not enough memory to read file %s\n"),sites);
+               debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),sites);
                exit(EXIT_FAILURE);
        }
 
        while(regs<TopSitesNum && (buf=longline_read(fp_in,line))!=NULL) {
                getword_start(&gwarea,buf);
                if (getword_atoll(&nacc,&gwarea,'\t')<0) {
-                       debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),sites);
                        exit(EXIT_FAILURE);
                }
                if (nacc == 0) continue;
                if (getword_atoll(&nbytes,&gwarea,'\t')<0 || getword_atoll(&ntime,&gwarea,'\t')<0) {
-                       debuga(_("Maybe you have a broken record or garbage in your %s file\n"),sites);
+                       debuga(__FILE__,__LINE__,_("Invalid record in file \"%s\"\n"),sites);
                        exit(EXIT_FAILURE);
                }
                if (getword_atoi(&nusers,&gwarea,'\t')<0) {
-                       debuga(_("The number of users is invalid in file %s\n"),sites);
+                       debuga(__FILE__,__LINE__,_("Invalid number of users in file \"%s\"\n"),sites);
                        exit(EXIT_FAILURE);
                }
                if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
-                       debuga(_("The url is invalid in file %s\n"),sites);
+                       debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),sites);
                        exit(EXIT_FAILURE);
                }
 
@@ -314,14 +320,16 @@ void topsites(void)
                if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",nusers);
                fprintf(fp_ou,">%s</td></tr>\n",fixnum(nusers,1));
        }
-       fclose(fp_in);
+       if (FileObject_Close(fp_in)) {
+               debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),sites,FileObject_GetLastCloseError());
+               exit(EXIT_FAILURE);
+       }
        longline_destroy(&line);
 
        fputs("</table></div>\n",fp_ou);
-       if (write_html_trailer(fp_ou)<0)
-               debuga(_("Write error in file %s\n"),report);
+       write_html_trailer(fp_ou);
        if (fclose(fp_ou)==EOF) {
-               debuga(_("Write error in %s: %s\n"),report,strerror(errno));
+               debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
                exit(EXIT_FAILURE);
        }