]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Store the elapsed time and the number of bytes into the structure
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 3 Jul 2012 15:07:10 +0000 (17:07 +0200)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Tue, 3 Jul 2012 15:07:10 +0000 (17:07 +0200)
Two more variables are included in the structure to be shared among
modules.

include/defs.h
include/readlog.h
readlog.c
util.c

index 25838e513d863e59df8fafcf0e6d876b385fb5c7..506781fc53cfe04593272a80e9dccbaae315c780 100755 (executable)
@@ -273,6 +273,8 @@ __attribute__((warn_unused_result)) int getword_multisep(/*@out@*/char *word, in
 __attribute__((warn_unused_result)) int getword_skip(int limit, struct getwordstruct *gwarea, char stop);
 __attribute__((warn_unused_result)) int getword_atoll(/*@out@*/long long int *number, struct getwordstruct *gwarea, char stop);
 __attribute__((warn_unused_result)) int getword_atoi(/*@out@*/int *number, struct getwordstruct *gwarea, char stop);
+__attribute__((warn_unused_result)) int getword_atol(long int *number, struct getwordstruct *gwarea, char stop);
+__attribute__((warn_unused_result)) int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop);
 __attribute__((warn_unused_result)) int getword_ptr(char *orig_line,/*@out@*/char **word, struct getwordstruct *gwarea, char stop);
 long long int my_atoll (const char *nptr);
 int is_absolute(const char *path);
index a6bf38ee4fa34a9be11597680ca223db7a1a87ab..2909a491680fdd0717b72d0e41deaba6269b45a7 100644 (file)
@@ -28,9 +28,9 @@ struct ReadLogStruct
        //! The URL of the visited site.
        char *Url;
        //! Time necessary to process the user's request.
-       unsigned int ElapsedTime;
+       long int ElapsedTime;
        //! Number of transfered bytes.
-       unsigned long int DataSize;
+       long long int DataSize;
        //! HTTP code returned to the user for the entry.
        char *HttpCode;
 };
index 79a3e9381de45e6678c1fe69739680d932eb9e24..4d380e589c8ff545b3c15154f7137d435bbf4d78 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -85,7 +85,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
        char user[MAX_USER_LEN];
        char data[255];
        char ip[60];
-       char tam[255];
        char hora[30];
        char mes[30];
        char tbuf2[128];
@@ -116,8 +115,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
        long int totregsl=0;
        long int totregsg=0;
        long int totregsx=0;
-       long int nbytes;
-       long int elap_time;
        long long int iyear, imonth, iday;
        FILE *fp_in=NULL;
        FILE *fp_log=NULL;
@@ -313,7 +310,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                if((str=(char *) strchr(data, '.')) != (char *) NULL && (str=(char *) strchr(str+1, '.')) != (char *) NULL ) {
                                        strcpy(ip,data);
                                        log_entry.Ip=ip;
-                                       strcpy(elap,"0");
                                        if(squid24) {
                                                if (getword(user,sizeof(user),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0) {
                                                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
@@ -343,7 +339,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                                debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                                exit(EXIT_FAILURE);
                                        }
-                                       if (getword(tam,sizeof(tam),&gwarea,' ')<0) {
+                                       if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0) {
                                                debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                                exit(EXIT_FAILURE);
                                        }
@@ -362,9 +358,6 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                        if ((str = strchr(code, ':')) != NULL)
                                                *str = '/';
 
-                                       if(strcmp(tam,"\0") == 0)
-                                               strcpy(tam,"0");
-
                                        ilf=ILF_Common;
                                        ilf_count[ilf]++;
 
@@ -413,6 +406,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                                        exit(EXIT_FAILURE);
                                                }
                                        if(strlen(elap) < 1) continue;
+                                       log_entry.ElapsedTime=atol(elap);
                                        if (getword(ip,sizeof(ip),&gwarea,' ')<0){
                                                debuga(_("Maybe you have a broken client IP address in your %s file\n"),arq);
                                                exit(EXIT_FAILURE);
@@ -422,7 +416,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                                debuga(_("Maybe you have a broken result code in your %s file\n"),arq);
                                                exit(EXIT_FAILURE);
                                        }
-                                       if (getword(tam,sizeof(tam),&gwarea,' ')<0){
+                                       if (getword_atoll(&log_entry.DataSize,&gwarea,' ')<0){
                                                debuga(_("Maybe you have a broken amount of data in your %s file\n"),arq);
                                                exit(EXIT_FAILURE);
                                        }
@@ -478,7 +472,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                        exit(EXIT_FAILURE);
                                }
-                               if (getword(tam,sizeof(tam),&gwarea,'\t')<0){
+                               if (getword_atoll(&log_entry.DataSize,&gwarea,'\t')<0){
                                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                        exit(EXIT_FAILURE);
                                }
@@ -486,7 +480,7 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                        exit(EXIT_FAILURE);
                                }
-                               if (getword(elap,sizeof(elap),&gwarea,'\t')<0){
+                               if (getword_atol(&log_entry.ElapsedTime,&gwarea,'\t')<0){
                                        debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq);
                                        exit(EXIT_FAILURE);
                                }
@@ -585,17 +579,9 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                                }
                                                strcpy(hora,str);
                                        } else if (x==isa_cols[ISACOL_TimeTaken]) {
-                                               if (strlen(str)>=sizeof(elap)) {
-                                                       debuga(_("Maybe you have a broken download duration in your %s file\n"),arq);
-                                                       exit(EXIT_FAILURE);
-                                               }
-                                               strcpy(elap,str);
+                                               log_entry.ElapsedTime=atol(str);
                                        } else if (x==isa_cols[ISACOL_Bytes]) {
-                                               if (strlen(str)>=sizeof(tam)) {
-                                                       debuga(_("Maybe you have a broken download size in your %s file\n"),arq);
-                                                       exit(EXIT_FAILURE);
-                                               }
-                                               strcpy(tam,str);
+                                               log_entry.DataSize=atoll(str);
                                        } else if (x==isa_cols[ISACOL_Uri]) {
                                                full_url=str;
                                        } else if (x==isa_cols[ISACOL_Status]) {
@@ -777,13 +763,11 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                           strcmp(log_entry.User,"") ==0 || strcmp(log_entry.User,":") ==0)
                                continue;
 
-                       nbytes=atol(tam);
-                       if (nbytes<0) nbytes=0;
+                       if (log_entry.DataSize<0) log_entry.DataSize=0;
                        
-                       elap_time=atol(elap);
-                       if (elap_time<0) elap_time=0;
-                       if (Filter->max_elapsed>0 && elap_time>Filter->max_elapsed) {
-                               elap_time=0;
+                       if (log_entry.ElapsedTime<0) log_entry.ElapsedTime=0;
+                       if (Filter->max_elapsed>0 && log_entry.ElapsedTime>Filter->max_elapsed) {
+                               log_entry.ElapsedTime=0;
                        }
 
                        if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) {
@@ -817,8 +801,8 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                                }
                        }
        #ifdef ENABLE_DOUBLE_CHECK_DATA
-                       ufile->user->nbytes+=nbytes;
-                       ufile->user->elap+=elap_time;
+                       ufile->user->nbytes+=log_entry.DataSize;
+                       ufile->user->elap+=log_entry.ElapsedTime;
        #endif
 
                        if (ufile->file==NULL) {
@@ -850,13 +834,13 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                        strftime(dia, sizeof(dia), "%d/%m/%Y", log_entry.EntryTime);
                        strftime(hora,sizeof(hora),"%H:%M:%S",log_entry.EntryTime);
 
-                       if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,log_entry.Ip,url,nbytes,code,elap_time,smartfilter)<=0) {
+                       if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter)<=0) {
                                debuga(_("Write error in the log file of user %s\n"),log_entry.User);
                                exit(EXIT_FAILURE);
                        }
 
                        if(fp_log && ilf!=ILF_Sarg)
-                               fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%ld\t%s\t%ld\t%s\n",dia,hora,log_entry.User,log_entry.Ip,url,nbytes,code,elap_time,smartfilter);
+                               fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize,code,log_entry.ElapsedTime,smartfilter);
 
                        totregsg++;
 
@@ -899,13 +883,13 @@ int ReadLogFile(struct ReadLogDataStruct *Filter)
                        if(debugm){
                                printf("IP=\t%s\n",log_entry.Ip);
                                printf("USER=\t%s\n",log_entry.User);
-                               printf("ELAP=\t%ld\n",elap_time);
+                               printf("ELAP=\t%ld\n",log_entry.ElapsedTime);
                                printf("DATE=\t%s\n",dia);
                                printf("TIME=\t%s\n",hora);
                                printf("FUNC=\t%s\n",fun);
                                printf("URL=\t%s\n",url);
                                printf("CODE=\t%s\n",code);
-                               printf("LEN=\t%ld\n",nbytes);
+                               printf("LEN=\t%"PRIu64"\n",(uint64_t)log_entry.DataSize);
                        }
                }
 
diff --git a/util.c b/util.c
index 419c3e68529c2b367e5847c119ac9375d8e3eb45..59056ca9fa7158e484bfbc453ba437918f66713b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -246,6 +246,85 @@ int getword_atoi(int *number, struct getwordstruct *gwarea, char stop)
        return(0);
 }
 
+int getword_atol(long int *number, struct getwordstruct *gwarea, char stop)
+{
+       long int x;
+       long int sign=+1;
+       int digit;
+
+       if (gwarea->current[0] == '-') {
+               gwarea->current++;
+               sign=-1;
+       } else if (gwarea->current[0] == '+') {
+               gwarea->current++;
+       }
+       *number=0;
+       for(x=0;isdigit(gwarea->current[x]);x++) {
+               digit=gwarea->current[x]-'0';
+               if (*number > (LONG_MAX-digit)/10) {
+                       debuga(_("Integer overflow detected in getword_atol in line %s\n"),gwarea->beginning);
+                       return(-1);
+               }
+               *number=(*number * 10) + digit;
+       }
+       if(gwarea->current[x] && gwarea->current[x]!=stop) {
+               printf("SARG: getword_atol loop detected after %ld bytes.\n",x);
+               printf("SARG: Line=\"%s\"\n",gwarea->beginning);
+               printf("SARG: Record=\"%s\"\n",gwarea->current);
+               printf("SARG: searching for \'x%x\'\n",stop);
+               //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
+#if USE_GETWORD_BACKTRACE
+               getword_backtrace();
+#endif
+               return(-1);
+       }
+       *number*=sign;
+
+       if (gwarea->current[x]) ++x;
+       gwarea->current+=x;
+       return(0);
+}
+
+int getword_atolu(unsigned long int *number, struct getwordstruct *gwarea, char stop)
+{
+       unsigned long int x;
+       int digit;
+
+       if (gwarea->current[0] == '-') {
+               printf("SARG: getword_atolu got a negative number.\n");
+               printf("SARG: Line=\"%s\"\n",gwarea->beginning);
+               printf("SARG: Record=\"%s\"\n",gwarea->current);
+               return(-1);
+       }
+       if (gwarea->current[0] == '+') {
+               gwarea->current++;
+       }
+       *number=0;
+       for(x=0;isdigit(gwarea->current[x]);x++) {
+               digit=gwarea->current[x]-'0';
+               if (*number > (ULONG_MAX-digit)/10) {
+                       debuga(_("Integer overflow detected in getword_atolu in line %s\n"),gwarea->beginning);
+                       return(-1);
+               }
+               *number=(*number * 10) + digit;
+       }
+       if(gwarea->current[x] && gwarea->current[x]!=stop) {
+               printf("SARG: getword_atolu loop detected after %ld bytes.\n",x);
+               printf("SARG: Line=\"%s\"\n",gwarea->beginning);
+               printf("SARG: Record=\"%s\"\n",gwarea->current);
+               printf("SARG: searching for \'x%x\'\n",stop);
+               //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
+#if USE_GETWORD_BACKTRACE
+               getword_backtrace();
+#endif
+               return(-1);
+       }
+
+       if (gwarea->current[x]) ++x;
+       gwarea->current+=x;
+       return(0);
+}
+
 
 int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop)
 {