]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Test some more write errors.
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 7 May 2010 09:18:28 +0000 (09:18 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Fri, 7 May 2010 09:18:28 +0000 (09:18 +0000)
Avoid a couple of close/open on the same file.
Purge sarg-general when the program exits.

email.c
report.c
topuser.c
totger.c
util.c

diff --git a/email.c b/email.c
index 8386e72f5d62ff2f08d09015e561db577b10cad3..2bb39400db749bd1a6b223df9b269ecb4df29c70 100644 (file)
--- 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);
index 1b663d12206425cc10d705c60ccd272db285531d..d03020429176f14253c2453bf238963ce96d6483 100644 (file)
--- 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;
 }
 
index e09ba3af91ef16d7ded8e74c562bfe94b17a70c3..960393a70b3667805982b7cd74745e8fafdee9f7 100644 (file)
--- 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;
 }
index 24890515abb2bfb8237eead07a11aaaa2622f022..7691072a1ca24b9f0f4ff2a693d7128a4952a2ba 100644 (file)
--- 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 a2ac345e93ca689fb9330df06a8dcd5f6432d90f..ad250da8d957c8e6ab8425776b366a0a536153c9 100644 (file)
--- 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);