]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Add an option to create the per_user_limit files
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 6 Jun 2013 11:05:17 +0000 (13:05 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 6 Jun 2013 11:05:17 +0000 (13:05 +0200)
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
html.c
include/defs.h
sarg.conf

index 20b7a72b5c868c4bc3bb21d826f0e2bf4742085d..4d2c3a3780a5a64621f030408f516dc7f3c5e9dc 100644 (file)
--- 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 && strcasecmp(str,options[i].name) ; i++);
+       if (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 d330b773398d7fd871cf1fb9998c65c61a3d0680..ad5be738e80adcea141bdf66d228aa911fa98070 100644 (file)
--- 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 ; i<PerUserLimitsNumber ; i++) {
-               if (access(PerUserLimits[i].File,R_OK)==0 && unlink(PerUserLimits[i].File)==-1) {
-                       debuga(_("Cannot delete per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
-                                  strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
+       switch (PerUserFileCreation)
+       {
+               case PUFC_Always:
+                       for (i=0 ; i<PerUserLimitsNumber ; i++) {
+                               FILE *fp_usr=fopen(PerUserLimits[i].File,"wt");
+                               if (fp_usr==NULL) {
+                                       debuga(_("Cannot create empty per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
+                                                  strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
+                               fclose(fp_usr);
+                       }
+                       break;
+
+               case PUFC_AsRequired:
+                       for (i=0 ; i<PerUserLimitsNumber ; i++) {
+                               if (access(PerUserLimits[i].File,R_OK)==0 && unlink(PerUserLimits[i].File)==-1) {
+                                       debuga(_("Cannot delete per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
+                                                  strerror(errno));
+                                       exit(EXIT_FAILURE);
+                               }
+                       }
+                       break;
        }
 
        uscan=userinfo_startscan();
@@ -555,7 +574,7 @@ void htmlrel(void)
                                if (limit>PerUserLimits[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);
                                        }
index 58195e3ff7482b6ae449c50d2936b3295060991d..b0802ab87d0105dfde8091391c0025e4abfe6a3b 100755 (executable)
@@ -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.
 */
index f52d1085802e467ac9dce4bf2da70fc931f94c8a..65e23c3e9bdc6354d158d11addd2b75a0bdde987 100644 (file)
--- a/sarg.conf
+++ b/sarg.conf
 #
 #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.