From: Frederic Marchal Date: Thu, 30 May 2013 08:41:01 +0000 (+0200) Subject: Avoid rereading the user_limit file each time a user is added X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aa6ac9f26e8828c789f8105e38b4569130ffb6d7;p=thirdparty%2Fsarg.git Avoid rereading the user_limit file each time a user is added 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. --- diff --git a/html.c b/html.c index b23ce10..54d953f 100644 --- 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("\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) { diff --git a/include/conf.h b/include/conf.h index 3de0631..4d84718 100755 --- a/include/conf.h +++ b/include/conf.h @@ -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; diff --git a/include/defs.h b/include/defs.h index 6a14051..e7e2f18 100755 --- a/include/defs.h +++ b/include/defs.h @@ -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); diff --git a/readlog.c b/readlog.c index dc6c510..d393025 100644 --- 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) { diff --git a/redirector.c b/redirector.c index 3b3e324..a0678ee 100644 --- a/redirector.c +++ b/redirector.c @@ -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); diff --git a/userinfo.c b/userinfo.c index c1b5dc0..37451b0 100644 --- a/userinfo.c +++ b/userinfo.c @@ -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++);