From: Frédéric Marchal Date: Fri, 7 May 2010 09:18:28 +0000 (+0000) Subject: Test some more write errors. X-Git-Tag: v2.3-pre2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=13a19c1a7fc6ea44b25530850ec00b2a0d94a8ca;p=thirdparty%2Fsarg.git Test some more write errors. Avoid a couple of close/open on the same file. Purge sarg-general when the program exits. --- diff --git a/email.c b/email.c index 8386e72..2bb3940 100644 --- a/email.c +++ b/email.c @@ -29,7 +29,6 @@ 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; @@ -38,7 +37,7 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *ema 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; @@ -49,27 +48,19 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *ema 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); @@ -152,6 +143,7 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *ema 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); diff --git a/report.c b/report.c index 1b663d1..d030204 100644 --- a/report.c +++ b/report.c @@ -401,12 +401,7 @@ void gerarel(void) 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; } diff --git a/topuser.c b/topuser.c index e09ba3a..960393a 100644 --- a/topuser.c +++ b/topuser.c @@ -63,23 +63,14 @@ void topuser(void) 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); @@ -151,6 +142,7 @@ void topuser(void) 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)) { @@ -166,6 +158,7 @@ void topuser(void) 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); @@ -392,12 +385,14 @@ void topuser(void) 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; } diff --git a/totger.c b/totger.c index 2489051..7691072 100644 --- a/totger.c +++ b/totger.c @@ -29,7 +29,7 @@ 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; @@ -40,8 +40,7 @@ int totalger(const char *dirname, int debug, const char *outdir) 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); } @@ -61,16 +60,21 @@ int totalger(const char *dirname, int debug, const char *outdir) 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); } diff --git a/util.c b/util.c index a2ac345..ad250da 100644 --- a/util.c +++ b/util.c @@ -1338,22 +1338,22 @@ char *strup(char *string) 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); } @@ -1361,16 +1361,20 @@ void removetmp(const char *outdir) 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);