Avoid a couple of close/open on the same file.
Purge sarg-general when the program exits.
int geramail(const char *dirname, int debug, const char *outdir, const char *email, const char *TempDir)
{
-
FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
long long int ttnbytes=0, ttnacc=0, tnacc=0;
long long int tnbytes=0, ttnelap=0, tnelap=0;
double perc2=0.00;
int posicao=0;
char olduser[MAX_USER_LEN], csort[MAXLEN];
- char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN], tusr[MAXLEN];
+ char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN];
char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
char buf[MAXLEN];
int totuser=0;
struct getwordstruct gwarea;
struct generalitemstruct item;
- strcpy(wger,dirname);
- strcpy(top1,dirname);
- strcpy(top2,dirname);
- strcpy(top3,dirname);
- strcpy(tusr,dirname);
- strcat(wger,"/sarg-general");
- strcat(top1,"/top");
- strcat(top2,"/top.tmp");
- strcat(top3,"/report");
- strcat(tusr,"/sarg-users");
-
+ snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
if((fp_in=fopen(wger,"r"))==NULL) {
debuga(_("(email) Cannot open file %s\n"),wger);
exit(EXIT_FAILURE);
}
+ snprintf(top1,sizeof(top1),"%s/top",dirname);
if((fp_top1=fopen(top1,"w"))==NULL) {
debuga(_("(email) Cannot open file %s\n"),top1);
exit(EXIT_FAILURE);
}
+ snprintf(top2,sizeof(top2),"%s/top.tmp",dirname);
if((fp_top2=fopen(top2,"w"))==NULL) {
debuga(_("(email) Cannot open file %s\n"),top2);
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
+ snprintf(top3,sizeof(top3),"%s/report",dirname);
if((fp_top3=fopen(top3,"w"))==NULL) {
debuga(_("(email) Cannot open file %s\n"),top3);
exit(EXIT_FAILURE);
index_only(outdirname, debug);
}
- /*
- 2009-10-13(Frederic) This piece of code is never called so it is commented out for good.
- if(strlen(email) < 0)
- removetmp(dirname);
- */
-
+ removetmp(outdirname);
return;
}
ipantes[0]='\0';
nameantes[0]='\0';
- strcpy(wger,outdirname);
- strcpy(top1,outdirname);
- strcpy(top2,outdirname);
- strcpy(top3,outdirname);
- strcpy(tusr,outdirname);
- strcat(wger,"/sarg-general");
- strcat(top1,"/top");
- strcat(top2,"/top.tmp");
- strcat(tusr,"/sarg-users");
- strcat(top3,"/index.html");
-
ntopuser = 0;
+ snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
if((fp_in=fopen(wger,"r"))==NULL) {
debuga(_("(topuser) Cannot open file %s\n"),wger);
exit(EXIT_FAILURE);
}
+ snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname);
if((fp_top2=fopen(top2,"w"))==NULL) {
debuga(_("(topuser) Cannot open file %s\n"),top2);
exit(EXIT_FAILURE);
if(strcmp(TopuserSortOrder,"normal") == 0)
order="";
+ snprintf(top1,sizeof(top1),"%s/top",outdirname);
sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", TempDir, order, sfield, top1, top2);
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
unlink(top2);
+ snprintf(top3,sizeof(top3),"%s/index.html",outdirname);
if((fp_top3=fopen(top3,"w"))==NULL) {
debuga(_("(topuser) Cannot open file %s\n"),top3);
exit(EXIT_FAILURE);
if (fclose(fp_top3)==EOF)
debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno));
+ snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname);
if((fp_ou=fopen(tusr,"w"))==NULL) {
debuga(_("(topuser) Cannot open file %s\n"),tusr);
exit(EXIT_FAILURE);
}
fprintf(fp_ou,"%d\n",totuser);
- fclose(fp_ou);
+ if (fclose(fp_ou)==EOF)
+ debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno));
return;
}
int totalger(const char *dirname, int debug, const char *outdir)
{
- FILE *fp_in, *fp_ou;
+ FILE *fp_in;
long long int tnacc=0;
long long int tnbytes=0;
long long int telap=0;
struct generalitemstruct item;
snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
-
- if((fp_in=fopen(wger,"r"))==NULL) {
+ if((fp_in=fopen(wger,"r+"))==NULL) {
debuga(_("(totger) Cannot open file %s\n"),wger);
exit(EXIT_FAILURE);
}
toucache+=item.oucache;
}
- fclose(fp_in);
longline_destroy(&line);
- if((fp_ou=fopen(wger,"a"))==NULL) {
- debuga(_("(totger) Cannot open file %s\n"),wger);
- exit(EXIT_FAILURE);
+ if (fseek(fp_in,0,SEEK_END)==-1) {
+ debuga(_("Failed to move to the end of %s - %s\n"),wger,strerror(errno));
+ exit(EXIT_FAILURE);
}
- fprintf(fp_ou,"TOTAL\t%lld\t%lld\t%lld\t%lld\t%lld\n",tnacc,tnbytes,telap,tincache,toucache);
- fclose(fp_ou);
+ if (fprintf(fp_in,"TOTAL\t%lld\t%lld\t%lld\t%lld\t%lld\n",tnacc,tnbytes,telap,tincache,toucache)<0) {
+ debuga(_("Failed to write the total line in %s\n"),wger);
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(fp_in)==EOF) {
+ debuga(_("Failed to close file %s - %s\n"),wger,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
return (0);
}
void removetmp(const char *outdir)
{
-
FILE *fp_in;
char warea[256];
char buf[MAXLEN];
+ long pos;
if(!RemoveTempFiles)
return;
if(debug) {
- debuga(_("Removing temporary files sarg-general, sarg-period\n"));
+ debuga(_("Purging temporary file sarg-general\n"));
}
if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) {
debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir);
exit(EXIT_FAILURE);
}
- if((fp_in=fopen(warea,"r"))==NULL){
+ if((fp_in=fopen(warea,"r+"))==NULL){
debuga(_("(removetmp) Cannot open file %s\n"),warea);
exit(EXIT_FAILURE);
}
if(strncmp(buf,"TOTAL",5) == 0 && (buf[6]=='\t' || buf[6]==' '))
break;
}
- fclose(fp_in);
-
- if((fp_in=fopen(warea,"w"))==NULL){
- debuga(_("(removetmp) Cannot open file %s\n"),warea);
+ if (fseek(fp_in,0,SEEK_SET)==-1) {
+ debuga(_("Failed to rewind to the beginning of the file %s: %s\n"),warea,strerror(errno));
exit(EXIT_FAILURE);
}
+
if (fputs(buf,fp_in)==EOF) {
debuga(_("Failed to write the total line in %s - %s\n"),warea,strerror(errno));
exit(EXIT_FAILURE);
}
+ pos=ftell(fp_in);
+ if (pos>0 && ftruncate(fileno(fp_in),pos)==-1) {
+ debuga(_("Failed to truncate %s: %s\n"),warea,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
if (fclose(fp_in)==EOF) {
debuga(_("Failed to close %s after writing the total line - %s\n"),warea,strerror(errno));
exit(EXIT_FAILURE);