From: Frederic Marchal Date: Sun, 12 Jul 2015 18:37:47 +0000 (+0200) Subject: Simplify the code to create the report directory X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b2c1a7b4e9fb48630ad906a69e567c3d120c8925;p=thirdparty%2Fsarg.git Simplify the code to create the report directory 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. --- diff --git a/documentation/util.txt b/documentation/util.txt index 40b49ba..755b356 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -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 sarg-date file -containing the current date. - -The function also create an images directory in \a dir and copy all the files from the SYSCONFDIR/images 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. diff --git a/include/defs.h b/include/defs.h index 3c5d658..304b224 100644 --- a/include/defs.h +++ b/include/defs.h @@ -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); diff --git a/report.c b/report.c index 7832772..45414de 100644 --- 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 8be71f3..c49824e 100644 --- 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 sarg-date file containing the current + * date. + * + * The function also create an images directory in \a dir and copy all + * the files from the SYSCONFDIR/images 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);