From 0b5356bbf147989f8aaed3ecdcb3ccc381443df0 Mon Sep 17 00:00:00 2001 From: Frederic Marchal Date: Thu, 6 Jun 2013 13:05:17 +0200 Subject: [PATCH] Add an option to create the per_user_limit files Sarg.conf allows the user to either always create a per_user_limit file even if it is empty or only create it if some user crosses the limit. --- getconf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ html.c | 33 ++++++++++++++++++++++++++------- include/defs.h | 11 +++++++++++ sarg.conf | 11 +++++++++++ 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/getconf.c b/getconf.c index 20b7a72..4d2c3a3 100644 --- a/getconf.c +++ b/getconf.c @@ -34,6 +34,7 @@ extern numlist hours, weekdays; extern FileListObject AccessLog; extern int PerUserLimitsNumber; extern struct PerUserLimitStruct PerUserLimits[MAX_USER_LIMITS]; +extern enum PerUserFileCreationEnum PerUserFileCreation; struct param_list { @@ -53,6 +54,14 @@ struct sort_list unsigned long int value; }; +struct select_list +{ + //! The name of the value of the parameter. + const char *name; + //! The value to assign when the name is selected. + int value; +}; + static struct param_list report_type_values[]= { {"users_sites",REPORT_TYPE_USERS_SITES,0}, @@ -189,6 +198,12 @@ struct sort_list user_sort[]= {"TIME",USER_SORT_TIME}, }; +static struct select_list per_user_limit_create_file[]= +{ + {"always",PUFC_Always}, //always create an empty file + {"as_required",PUFC_AsRequired}, //create the file if necessary (no empty file is created) +}; + static int is_param(const char *param,const char *buf) { int plen; @@ -437,6 +452,30 @@ static int getparam_sort(const char *param,struct sort_list *options,int noption return(1); } +static int getparam_select(const char *param,struct select_list *options,int noptions,char *buf,int *value) +{ + int plen; + char *str; + int i; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + str=buf; + while (*buf && (unsigned char)*buf>' ' && *buf!=';') buf++; + *buf='\0'; + for (i=0 ; i=noptions) { + debuga(_("Unknown value \"%s\" for parameter \"%s\"\n"),str,param); + exit(EXIT_FAILURE); + } + *value=options[i].value; + return(1); +} + static int getparam_userlimit(const char *param,char *buf) { int plen; @@ -501,6 +540,7 @@ static void parmtest(char *buf) { char wbuf[2048]; struct getwordstruct gwarea; + int iVal; while (*buf && (unsigned char)*buf<=' ') buf++; @@ -631,6 +671,11 @@ static void parmtest(char *buf) if (getparam_userlimit("per_user_limit",buf)>0) return; + if (getparam_select("per_user_limit_file_create",SET_LIST(per_user_limit_create_file),buf,&iVal)>0) { + PerUserFileCreation=(enum PerUserFileCreationEnum)iVal; + return; + } + if (getparam_int("lastlog",buf,&LastLog)>0) return; if (getparam_bool("remove_temp_files",buf,&RemoveTempFiles)>0) return; diff --git a/html.c b/html.c index d330b77..ad5be73 100644 --- a/html.c +++ b/html.c @@ -31,6 +31,8 @@ int PerUserLimitsNumber=0; //! Log user's who downloaded more than the limit. struct PerUserLimitStruct PerUserLimits[MAX_USER_LIMITS]; +//! How to create a per user file. +enum PerUserFileCreationEnum PerUserFileCreation=PUFC_Always; extern struct globalstatstruct globstat; @@ -84,12 +86,29 @@ void htmlrel(void) sort_labels(&sort_field,&sort_order); - for (i=0 ; iPerUserLimits[i].Limit && (user_limit[maskid] & mask)==0) { FILE *fp_usr; - if((fp_usr = fopen(PerUserLimits[i].File, "a")) == 0) { + if((fp_usr = fopen(PerUserLimits[i].File, "at")) == 0) { debuga(_("(html10) Cannot open per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno)); exit(EXIT_FAILURE); } diff --git a/include/defs.h b/include/defs.h index 58195e3..b0802ab 100755 --- a/include/defs.h +++ b/include/defs.h @@ -116,6 +116,17 @@ struct ReadLogDataStruct int EndTime; }; +/*! +\brief How to handle the per_user_limit file creation. +*/ +enum PerUserFileCreationEnum +{ + //! Purge the file if it exists or create an empty file. + PUFC_Always, + //! Delete old files and don't create a new file unless necessary. + PUFC_AsRequired +}; + /*! \brief How to log every user crossing the download limit. */ diff --git a/sarg.conf b/sarg.conf index f52d108..65e23c3 100644 --- a/sarg.conf +++ b/sarg.conf @@ -232,6 +232,17 @@ # #per_user_limit none +# TAG: per_user_limit_file_create always/as_required +# When to create a per_user_limit file. +# +# Use 'always' to always create the file requested by per_user_limit +# even if it is empty. +# +# Use 'as_required' to create a per_user_limit file only if at least +# one user crosses the limit. +# +#per_user_limit_file_create purge + # TAG: lastlog n # How many reports files must be keept in reports directory. # The oldest report file will be automatically removed. -- 2.47.2