]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Simplify the code to create the report directory
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 12 Jul 2015 18:37:47 +0000 (20:37 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 12 Jul 2015 18:37:47 +0000 (20:37 +0200)
There were too many string copying in that code and the code path was
needlessly duplicated depending on the type of index tree to generate.

documentation/util.txt
include/defs.h
report.c
util.c

index 40b49ba6c6d7eb9259583534b4db5f8de9c7c790..755b356620a2f98d89244a2c9f760ecd168be67f 100644 (file)
@@ -440,26 +440,6 @@ whose name is in ::outdir.
 
 
 
-/*! \fn void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
-Create a directory to generate a report for the specified connection data and populate it with the a <tt>sarg-date</tt> file
-containing the current date.
-
-The function also create an <tt>images</tt> directory in \a dir and copy all the files from the <tt>SYSCONFDIR/images</tt> into
-that directory.
-
-\param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
-::DateFormat.
-\param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
-\param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
-\param us The user to whom the report is limited. It is an empty string if all the users are accepted.
-\param form The email address to which the report is sent. It is currently unused.
-
-*/
-
-
-
-
-
 /*! \fn void strip_latin(char *line)
 Remove any HTML entity from the line. A HTML entity starts with an ampersand and end at the next
 semicolon.
index 3c5d658c852d97f22b7239836c04a6419238a8cc..304b224b98a1ef439c0277538e8c355c4ec12c36 100644 (file)
@@ -406,7 +406,7 @@ bool IsTreeFileDirName(const char *Name);
 bool IsTreeYearFileName(const char *Name);
 bool IsTreeMonthFileName(const char *Name);
 bool IsTreeDayFileName(const char *Name);
-int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form);
+int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us);
 int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period);
 void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil);
 void getperiod_merge(struct periodstruct *main,struct periodstruct *candidate);
index 78327720aa03429095e1df561d7ef888099ea581..45414de17a20c932f05415f90b36405937b9a765 100644 (file)
--- a/report.c
+++ b/report.c
@@ -94,7 +94,7 @@ void gerarel(void)
        smartfilter=false;
        memset(&globstat,0,sizeof(globstat));
 
-       if (vrfydir(&period, addr, site, us, email)<0) {
+       if (vrfydir(&period, addr, site, us)<0) {
                debuga(__FILE__,__LINE__,_("Cannot create the output directory name containing the period as part of the name\n"));
                exit(EXIT_FAILURE);
        }
diff --git a/util.c b/util.c
index 8be71f3ffb00a275fa7d32df88e36d7f837ac785..c49824e2971edb87d735080a341b1173218fa752 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1347,12 +1347,24 @@ bool IsTreeDayFileName(const char *Name)
        return(true);
 }
 
-int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form)
+/*!
+ * Create a directory to generate a report for the specified connection data
+ * and populate it with the a <tt>sarg-date</tt> file containing the current
+ * date.
+ *
+ * The function also create an <tt>images</tt> directory in \a dir and copy all
+ * the files from the <tt>SYSCONFDIR/images</tt> into that directory.
+ *
+ * \param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
+ * ::DateFormat.
+ * \param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
+ * \param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
+ * \param us The user to whom the report is limited. It is an empty string if all the users are accepted.
+ */
+int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us)
 {
        FILE *fp_ou;
-       int  num=1, count=0;
        char wdir[MAXLEN];
-       char dirname2[MAXLEN];
        int y1, y2;
        int m1, m2;
        int d1, d2;
@@ -1413,58 +1425,28 @@ int vrfydir(const struct periodstruct *per1, const char *addr, const char *site,
 
        strcpy(outdirname,wdir);
 
-       if(IndexTree != INDEX_TREE_DATE) {
-               if(!OverwriteReport) {
-                       while(num) {
-                               if(access(wdir,R_OK) == 0) {
-                                       sprintf(wdir,"%s.%d",outdirname,num);
-                                       num++;
-                                       count++;
-                               } else
-                                       break;
-                       }
+       // manufacture a new unique name if configured to keep old reports or overwrite old report if configured to do so
+       if (!OverwriteReport) {
+               int num=1;
 
-                       if(count > 0) {
-                               if(debug)
-                                       debuga(__FILE__,__LINE__,_("File %s already exists, moved to %s\n"),outdirname,wdir);
-                               rename(outdirname,wdir);
-                       }
-               } else {
-                       if(access(outdirname,R_OK) == 0) {
-                               unlinkdir(outdirname,1);
-                       }
+               while (access(wdir,R_OK)==0 || errno==EACCES) //file exist or can't be read
+               {
+                       sprintf(wdir,"%s.%d",outdirname,num);
+                       num++;
+               }
+               if (num>1) {
+                       if(debug)
+                               debuga(__FILE__,__LINE__,_("File %s already exists, moved to %s\n"),outdirname,wdir);
+                       rename(outdirname,wdir);
                }
-               my_mkdir(outdirname);
        } else {
-               strcpy(dirname2,wdir);
-               if(!OverwriteReport) {
-                       while(num) {
-                               if(access(wdir,R_OK) == 0) {
-                                       sprintf(wdir,"%s.%d",dirname2,num);
-                                       num++;
-                                       count++;
-                               } else
-                                       break;
-                       }
-
-                       if(count > 0) {
-                               if(debug)
-                                       debuga(__FILE__,__LINE__,_("File %s already exists, moved to %s\n"),dirname2,wdir);
-                               rename(dirname2,wdir);
-                               strcpy(dirname2,wdir);
-                       }
-               } else {
-                       if(access(wdir,R_OK) == 0) {
-                               unlinkdir(wdir,1);
-                       }
+               if(access(outdirname,R_OK) == 0) {
+                       unlinkdir(outdirname,1);
                }
-
-               if(access(wdir, R_OK) != 0)
-                       my_mkdir(wdir);
        }
+       my_mkdir(outdirname);
 
-       strcpy(dirname2,wdir);
-
+       // create sarg-date to keep track of the report creation date
        if (snprintf(wdir,sizeof(wdir),"%s/sarg-date",outdirname)>=sizeof(wdir)) {
                debuga(__FILE__,__LINE__,_("Buffer too small to store "));
                debuga_more("%s/sarg-date",outdirname);