]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Avoid rereading the user_limit file each time a user is added
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 30 May 2013 08:41:01 +0000 (10:41 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 30 May 2013 08:41:01 +0000 (10:41 +0200)
Each time a user reach the download limit, he/she is added to the
user_limit file only once. Instead of rereading the whole file to see if
the user is already there, a flag is kept in memory to track already
limited users.

html.c
include/conf.h
include/defs.h
readlog.c
redirector.c
userinfo.c

diff --git a/html.c b/html.c
index b23ce108e972539c4e47645bf3990f40f05984c2..54d953f25970196b9975198254947bec6b574440 100644 (file)
--- a/html.c
+++ b/html.c
@@ -31,7 +31,7 @@ extern struct globalstatstruct globstat;
 
 void htmlrel(void)
 {
-       FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2, *fp_usr;
+       FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2;
 
        long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0;
        long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0;
@@ -57,7 +57,7 @@ void htmlrel(void)
        char siteind[MAX_TRUNCATED_URL];
        struct getwordstruct gwarea;
        longline line,line1;
-       const struct userinfostruct *uinfo;
+       struct userinfostruct *uinfo;
        userscan uscan;
 
        if (snprintf(tmp2,sizeof(tmp2),"%s/sargtmp.int_unsort",tmp)>=sizeof(tmp2)) {
@@ -527,39 +527,22 @@ void htmlrel(void)
                        fputs("</tr>\n",fp_ou);
                }
 
-               if(PerUserLimit > 0) {
-                       if(tnbytes > (PerUserLimit*1000000)) {
-                               limit_flag=0;
-                               if(access(PerUserLimitFile, R_OK) == 0) {
-                                       if((fp_usr = fopen(PerUserLimitFile, "r")) == 0) {
-                                               debuga(_("(html9) Cannot open file %s: %s\n"),PerUserLimitFile,strerror(errno));
-                                               exit(EXIT_FAILURE);
-                                       }
-                                       while(fgets(tmp6,sizeof(tmp6),fp_usr)!=NULL) {
-                                               fixendofline(tmp6);
-                                               if(strcmp(tmp6,uinfo->label) == 0) {
-                                                       limit_flag=1;
-                                                       break;
-                                               }
-                                       }
-                                       fclose(fp_usr);
-                               }
+               if (PerUserLimit > 0 && (int)(tnbytes/1000000LLU) > PerUserLimit && !uinfo->user_limit) {
+                       FILE *fp_usr;
 
-                               if(!limit_flag) {
-                                       if((fp_usr = fopen(PerUserLimitFile, "a")) == 0) {
-                                               debuga(_("(html10) Cannot open file %s: %s\n"),PerUserLimitFile,strerror(errno));
-                                               exit(EXIT_FAILURE);
-                                       }
-                                       fprintf(fp_usr,"%s\n",uinfo->label);
-                                       if (fclose(fp_usr)==EOF) {
-                                               debuga(_("Write error in %s: %s\n"),PerUserLimitFile,strerror(errno));
-                                               exit(EXIT_FAILURE);
-                                       }
-
-                                       if(debug)
-                                               debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,PerUserLimit,PerUserLimitFile);
-                               }
+                       if((fp_usr = fopen(PerUserLimitFile, "a")) == 0) {
+                               debuga(_("(html10) Cannot open file %s: %s\n"),PerUserLimitFile,strerror(errno));
+                               exit(EXIT_FAILURE);
                        }
+                       fprintf(fp_usr,"%s\n",uinfo->ip);
+                       if (fclose(fp_usr)==EOF) {
+                               debuga(_("Write error in %s: %s\n"),PerUserLimitFile,strerror(errno));
+                               exit(EXIT_FAILURE);
+                       }
+                       uinfo->user_limit=1;
+
+                       if(debug)
+                               debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,PerUserLimit,PerUserLimitFile);
                }
 
                if ((ReportType & REPORT_TYPE_TOPUSERS) != 0 && (UserReportFields & USERREPORTFIELDS_AVERAGE) != 0) {
index 3de0631ca5bc1b6e9ddfe9409a236a929ee78604..4d847188d5641e73ddfe47d7ac3933f8d0688ea3 100755 (executable)
@@ -462,7 +462,6 @@ int  idate;
 int  dansguardian_count;
 int  redirector_count;
 int  useragent_count;
-int  limit_flag;
 int  z1, z2, z3;
 int  ttopen;
 int  sarglog;
index 6a14051619e9eb464070f4bdbb267bb451542395..e7e2f18f252fa0d4b9e215d53f2850a5f33e6743 100755 (executable)
@@ -45,6 +45,8 @@ struct userinfostruct
 {
        //! The ID of the user as found in the input file.
        const char *id;
+       //! The user's IP address.
+       const char *ip;
        //! \c True if the ID is in fact the IP address from which the user connected.
        bool id_is_ip;
        //! \c True if the user doesn't have a report file.
@@ -55,6 +57,8 @@ struct userinfostruct
        const char *filename;
        //! \c True if this user is in the topuser list.
        int topuser;
+       //! \c True if the user's limit has been reached.
+       int user_limit;
        //! A general purpose flag that can be set when scanning the user's list.
        int flag;
 #ifdef ENABLE_DOUBLE_CHECK_DATA
@@ -270,7 +274,7 @@ void usage(const char *prog);
 void useragent(void);
 
 // userinfo.c
-/*@shared@*/struct userinfostruct *userinfo_create(const char *userid);
+/*@shared@*/struct userinfostruct *userinfo_create(const char *userid, const char *ip);
 void userinfo_free(void);
 void userinfo_label(struct userinfostruct *uinfo,const char *label);
 /*@shared@*/struct userinfostruct *userinfo_find_from_file(const char *filename);
index dc6c510818c07f9801d68f7292333117e360f26d..d3930255001017edd990cdc4f5d8c160fa40d8f6 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -561,9 +561,8 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                        memset(ufile,0,sizeof(*ufile));
                        ufile->next=first_user_file;
                        first_user_file=ufile;
-                       uinfo=userinfo_create(log_entry.User);
+                       uinfo=userinfo_create(log_entry.User,(id_is_ip) ? NULL : log_entry.Ip);
                        ufile->user=uinfo;
-                       uinfo->id_is_ip=id_is_ip;
                        nusers++;
                } else {
                        if (prev_ufile) {
index 3b3e324107c8456d6ade5d69169da2445f27393e..a0678ee2912bc5b1488f7111edfb358afe84477f 100644 (file)
@@ -204,8 +204,7 @@ static void parse_log(FILE *fp_ou,char *buf)
        }
        uinfo=userinfo_find_from_id(user);
        if (!uinfo) {
-               uinfo=userinfo_create(user);
-               uinfo->id_is_ip=id_is_ip;
+               uinfo=userinfo_create(user,(id_is_ip) ? NULL : ip);
                uinfo->no_report=true;
                if(Ip2Name && id_is_ip) ip2name(user,sizeof(user));
                user_find(userlabel,MAX_USER_LEN, user);
index c1b5dc0c04a939719b5e8ab70c553eeaa369ea0c..37451b07b130f7afe2f4724907a2d39e6c945d9d 100644 (file)
@@ -65,7 +65,7 @@ static StringBufferObject UserStrings=NULL;
 //! User aliases.
 static AliasObject UserAliases=NULL;
 
-struct userinfostruct *userinfo_create(const char *userid)
+struct userinfostruct *userinfo_create(const char *userid,const char *ip)
 {
        struct usergroupstruct *group, *last;
        struct userinfostruct *user;
@@ -110,6 +110,14 @@ struct userinfostruct *userinfo_create(const char *userid)
                exit(EXIT_FAILURE);
        }
        user->label=user->id; //assign a label to avoid a NULL pointer in case none is provided
+       if (ip) {
+               user->id_is_ip=0;
+               user->ip=StringBuffer_Store(UserStrings,ip);
+       } else {
+               user->id_is_ip=1;
+               user->ip=user->id;
+       }
+       user->user_limit=0;
 
        if (AnonymousOutputFiles) {
                snprintf(filename,sizeof(filename),"%d",AnonymousCounter++);