From: Frédéric Marchal Date: Sun, 26 Dec 2010 18:29:38 +0000 (+0000) Subject: Check for the return code when deleting a file X-Git-Tag: v2.3.2~106 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08f9b029f8a65d292b7e97cdaec14c145edbdb20;p=thirdparty%2Fsarg.git Check for the return code when deleting a file Most of the calls to unlink are checked to report an error if the file cannot be deleted. The calls that are not checked are call where the file may not exists and it might not be desirable to abort the process for such an error. --- diff --git a/authfail.c b/authfail.c index 434bb47..6e41169 100644 --- a/authfail.c +++ b/authfail.c @@ -82,7 +82,10 @@ void authfail_report(void) debuga(_("sort command: %s\n"),csort); exit(EXIT_FAILURE); } - unlink(tmp4); + if (unlink(tmp4)) { + debuga(_("Cannot delete %s - %s\n"),tmp4,strerror(errno)); + exit(EXIT_FAILURE); + } if((fp_ou=MY_FOPEN(report,"w"))==NULL) { debuga(_("(authfail) Cannot open file %s\n"),report); @@ -190,7 +193,10 @@ void authfail_report(void) if (fclose(fp_ou)==EOF) debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - unlink(authfail_in); + if (unlink(authfail_in)) { + debuga(_("Cannot delete %s - %s\n"),authfail_in,strerror(errno)); + exit(EXIT_FAILURE); + } return; } diff --git a/dansguardian_log.c b/dansguardian_log.c index a90367e..9f5ba8c 100644 --- a/dansguardian_log.c +++ b/dansguardian_log.c @@ -139,5 +139,8 @@ void dansguardian_log(void) debuga(_("sort command: %s\n"),tmp6); exit(EXIT_FAILURE); } - unlink(guard_in); + if (unlink(guard_in)) { + debuga(_("Cannot delete %s - %s\n"),guard_in,strerror(errno)); + exit(EXIT_FAILURE); + } } diff --git a/dansguardian_report.c b/dansguardian_report.c index 0ff685f..2d8aa37 100644 --- a/dansguardian_report.c +++ b/dansguardian_report.c @@ -157,7 +157,10 @@ void dansguardian_report(void) if (fclose(fp_ou)==EOF) debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - unlink(dansguardian_in); + if (unlink(dansguardian_in)) { + debuga(_("Cannot delete %s - %s\n"),dansguardian_in,strerror(errno)); + exit(EXIT_FAILURE); + } return; } diff --git a/denied.c b/denied.c index bf6bf56..30e41a1 100644 --- a/denied.c +++ b/denied.c @@ -63,7 +63,7 @@ void gen_denied_report(void) ouser2[0]='\0'; sprintf(denied_in,"%s/denied.log",tmp); - if(!denied_count) { + if (!denied_count) { unlink(denied_in); return; } diff --git a/download.c b/download.c index a490952..be4665b 100644 --- a/download.c +++ b/download.c @@ -166,7 +166,10 @@ void download_report(void) if (fclose(fp_ou)==EOF) debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - unlink(report_in); + if (unlink(report_in)) { + debuga(_("Cannot delete %s - %s\n"),report_in,strerror(errno)); + exit(EXIT_FAILURE); + } return; } diff --git a/email.c b/email.c index 704d4e1..a0dbe61 100644 --- a/email.c +++ b/email.c @@ -127,7 +127,10 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *ema exit(EXIT_FAILURE); } - unlink(top2); + if (unlink(top2)) { + debuga(_("Cannot delete %s - %s\n"),top2,strerror(errno)); + exit(EXIT_FAILURE); + } if((fp_top1=fopen(top1,"r"))==NULL) { debuga(_("(email) Cannot open file %s\n"),top1); @@ -235,7 +238,10 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *ema #endif fclose(fp_top1); - unlink(top1); + if (unlink(top1)) { + debuga(_("Cannot delete %s - %s\n"),top1,strerror(errno)); + exit(EXIT_FAILURE); + } t = time(NULL); local = localtime(&t); diff --git a/grepday.c b/grepday.c index 3be390b..ed3de6f 100644 --- a/grepday.c +++ b/grepday.c @@ -713,7 +713,10 @@ void greport_day(const struct userinfostruct *uinfo) } } fclose(fp_in); - unlink(wdirname); + if (unlink(wdirname)) { + debuga(_("Cannot delete %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } if (snprintf(wdirname,sizeof(wdirname),"%s/%s/graph.html",outdirname,uinfo->filename)>=sizeof(wdirname)) { debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,"graph.html"); diff --git a/html.c b/html.c index 7570264..5918c4b 100644 --- a/html.c +++ b/html.c @@ -480,8 +480,14 @@ void htmlrel(void) fclose(fp_ip); longline_destroy(&line1); - unlink(tmp2); - unlink(tmp3); + if (unlink(tmp2)) { + debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno)); + exit(EXIT_FAILURE); + } + if (unlink(tmp3)) { + debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } if (olduserip[0]!='\0') { fprintf(fp_ou,"%s",olduserip); @@ -509,9 +515,16 @@ void htmlrel(void) fclose(fp_in); longline_destroy(&line); - if(iprel) - unlink(arqip); - unlink(arqin); + if (iprel) { + if (unlink(arqip)) { + debuga(_("Cannot delete %s - %s\n"),arqip,strerror(errno)); + exit(EXIT_FAILURE); + } + } + if (unlink(arqin)) { + debuga(_("Cannot delete %s - %s\n"),arqin,strerror(errno)); + exit(EXIT_FAILURE); + } if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) fputs("",fp_ou); diff --git a/index.c b/index.c index 9350422..3420369 100644 --- a/index.c +++ b/index.c @@ -42,7 +42,12 @@ void make_index(void) if(Index == INDEX_NO) { sprintf(wdir,"%sindex.html",outdir); - if(access(wdir, R_OK) == 0) unlink(wdir); + if(access(wdir, R_OK) == 0) { + if (unlink(wdir)) { + debuga(_("Cannot delete %s - %s\n"),wdir,strerror(errno)); + exit(EXIT_FAILURE); + } + } return; } diff --git a/lastlog.c b/lastlog.c index 8b71d05..83ff3fc 100644 --- a/lastlog.c +++ b/lastlog.c @@ -83,12 +83,19 @@ void mklastlog(const char *outdir) exit(EXIT_FAILURE); } - unlink(temp); + if (unlink(temp)) { + debuga(_("Cannot delete %s - %s\n"),temp,strerror(errno)); + exit(EXIT_FAILURE); + } if(ftot<=LastLog) { snprintf(temp,sizeof(temp),"%slastlog",outdir); - if(access(temp, R_OK) == 0) - unlink(temp); + if(access(temp, R_OK) == 0) { + if (unlink(temp)) { + debuga(_("Cannot delete %s - %s\n"),temp,strerror(errno)); + exit(EXIT_FAILURE); + } + } return; } diff --git a/log.c b/log.c index 2f115f2..c93f795 100644 --- a/log.c +++ b/log.c @@ -1654,7 +1654,10 @@ int main(int argc,char *argv[]) debuga(_("sort command: %s\n"),csort); exit(EXIT_FAILURE); } - unlink(denied_unsort); + if (unlink(denied_unsort)) { + debuga(_("Cannot delete %s - %s\n"),denied_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } } sort_users_log(tmp, debug); @@ -1664,8 +1667,9 @@ int main(int argc,char *argv[]) else gerarel(); - if((ReportType & REPORT_TYPE_DENIED) != 0) + if((ReportType & REPORT_TYPE_DENIED) != 0) { unlink(denied_sort); + } if(strcmp(tmp,"/tmp") != 0) { unlinkdir(tmp,0); diff --git a/realtime.c b/realtime.c index 6dc382c..a38375e 100755 --- a/realtime.c +++ b/realtime.c @@ -86,7 +86,10 @@ static void getlog(void) debuga(_("sort command: %s\n"),cmd); exit(EXIT_FAILURE); } - unlink(template1); + if (unlink(template1)) { + debuga(_("Cannot delete %s - %s\n"),template1,strerror(errno)); + exit(EXIT_FAILURE); + } datashow(template2); } @@ -268,7 +271,10 @@ static void datashow(const char *tmp) puts("\n\n\n\n"); fclose(fin); - unlink(tmp); + if (unlink(tmp)) { + debuga(_("Cannot delete %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } fflush(NULL); } diff --git a/report.c b/report.c index 8b90ed3..01b1dbc 100644 --- a/report.c +++ b/report.c @@ -323,7 +323,10 @@ void gerarel(void) fclose(fp_in); longline_destroy(&line); if (oldurltt) free(oldurltt); - unlink(tmp3); + if (unlink(tmp3)) { + debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } } userinfo_stopscan(uscan); diff --git a/siteuser.c b/siteuser.c index a22f14f..1becea9 100644 --- a/siteuser.c +++ b/siteuser.c @@ -213,7 +213,10 @@ void siteuser(void) } if (ourl) free(ourl); - unlink(general2); + if (unlink(general2)) { + debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno)); + exit(EXIT_FAILURE); + } fputs("\n",fp_ou); if (write_html_trailer(fp_ou)<0) diff --git a/smartfilter.c b/smartfilter.c index a34b797..71c8321 100644 --- a/smartfilter.c +++ b/smartfilter.c @@ -77,7 +77,10 @@ void smartfilter_report(void) debuga(_("sort command: %s\n"),csort); exit(EXIT_FAILURE); } - unlink(smart_in); + if (unlink(smart_in)) { + debuga(_("Cannot delete %s - %s\n"),smart_in,strerror(errno)); + exit(EXIT_FAILURE); + } if((fp_ou=fopen(report,"w"))==NULL) { debuga(_("(smartfilter) Cannot open log file %s\n"),report); diff --git a/sort.c b/sort.c index 0b229a5..e41f9a2 100644 --- a/sort.c +++ b/sort.c @@ -87,7 +87,10 @@ void tmpsort(void) debuga(_("sort command: %s\n"),csort); exit(EXIT_FAILURE); } - unlink(arqin); + if (unlink(arqin)) { + debuga(_("Cannot delete %s - %s\n"),arqin,strerror(errno)); + exit(EXIT_FAILURE); + } } userinfo_stopscan(uscan); @@ -150,7 +153,10 @@ void sort_users_log(const char *tmp, int debug) debuga(_("user name too long for %s/%s.unsort\n"),tmp,user); exit(EXIT_FAILURE); } - unlink(wdname); + if (unlink(wdname)) { + debuga(_("Cannot delete %s - %s\n"),wdname,strerror(errno)); + exit(EXIT_FAILURE); + } } (void)closedir( dirp ); diff --git a/squidguard_log.c b/squidguard_log.c index 4c0d7a5..c13ad8a 100644 --- a/squidguard_log.c +++ b/squidguard_log.c @@ -383,6 +383,9 @@ void squidguard_log(void) exit(EXIT_FAILURE); } - unlink(guard_in); + if (unlink(guard_in)) { + debuga(_("Cannot delete %s - %s\n"),guard_in,strerror(errno)); + exit(EXIT_FAILURE); + } return; } diff --git a/squidguard_report.c b/squidguard_report.c index 8f3dc7e..8e2b861 100644 --- a/squidguard_report.c +++ b/squidguard_report.c @@ -176,7 +176,10 @@ void squidguard_report(void) if (fclose(fp_ou)==EOF) debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - unlink(squidguard_in); + if (unlink(squidguard_in)) { + debuga(_("Cannot delete %s - %s\n"),squidguard_in,strerror(errno)); + exit(EXIT_FAILURE); + } return; } diff --git a/topsites.c b/topsites.c index 744cbd7..950efdb 100644 --- a/topsites.c +++ b/topsites.c @@ -138,7 +138,10 @@ void topsites(void) tntime+=item.nelap; } fclose(fp_in); - unlink(general2); + if (unlink(general2)) { + debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno)); + exit(EXIT_FAILURE); + } longline_destroy(&line); if (ourl) { @@ -180,8 +183,10 @@ void topsites(void) exit(EXIT_FAILURE); } - unlink(general2); - unlink(general3); + if (unlink(general3)) { + debuga(_("Cannot delete %s - %s\n"),general3,strerror(errno)); + exit(EXIT_FAILURE); + } if((fp_ou=fopen(report,"w"))==NULL) { debuga(_("(topsites) Cannot open log file %s\n"),report); diff --git a/topuser.c b/topuser.c index b2a3e43..01a32d7 100644 --- a/topuser.c +++ b/topuser.c @@ -168,7 +168,10 @@ void topuser(void) exit(EXIT_FAILURE); } - unlink(top2); + if (unlink(top2)) { + debuga(_("Cannot delete %s - %s\n"),top2,strerror(errno)); + exit(EXIT_FAILURE); + } snprintf(top3,sizeof(top3),"%s/index.html",outdirname); if((fp_top3=fopen(top3,"w"))==NULL) { @@ -303,7 +306,10 @@ void topuser(void) fprintf(fp_top3,"\"T\"",uinfo->filename,uinfo->filename,ImageFile,_("date/time report")); } else { sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename); - unlink(val1); + if (unlink(val1)) { + debuga(_("Cannot delete %s - %s\n"),val1,strerror(errno)); + exit(EXIT_FAILURE); + } } if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { if((ReportType & REPORT_TYPE_USERS_SITES) == 0) @@ -355,7 +361,10 @@ void topuser(void) topcount++; } fclose(fp_top1); - unlink(top1); + if (unlink(top1)) { + debuga(_("Cannot delete %s - %s\n"),top1,strerror(errno)); + exit(EXIT_FAILURE); + } longline_destroy(&line); if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { diff --git a/useragent.c b/useragent.c index 144d236..76c6123 100644 --- a/useragent.c +++ b/useragent.c @@ -145,7 +145,10 @@ void useragent(void) exit(EXIT_FAILURE); } - unlink(tmp3); + if (unlink(tmp3)) { + debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); if((fp_ht=fopen(hfile,"w"))==NULL) { @@ -222,7 +225,10 @@ void useragent(void) exit(EXIT_FAILURE); } - unlink(tmp2); + if (unlink(tmp2)) { + debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno)); + exit(EXIT_FAILURE); + } if((fp_ou=fopen(tmp2,"w"))==NULL) { debuga(_("(useragent) Cannot open file %s\n"),tmp2); @@ -267,7 +273,10 @@ void useragent(void) exit(EXIT_FAILURE); } - unlink(tmp3); + if (unlink(tmp3)) { + debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); cstatus=system(csort); @@ -282,7 +291,10 @@ void useragent(void) exit(EXIT_FAILURE); } - unlink(tmp2); + if (unlink(tmp2)) { + debuga(_("Cannot delete %s - %s\n"),tmp2,strerror(errno)); + exit(EXIT_FAILURE); + } fputs("

\n",fp_ht); @@ -312,7 +324,10 @@ void useragent(void) if (fclose(fp_ht)==EOF) debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); - unlink(tmp3); + if (unlink(tmp3)) { + debuga(_("Cannot delete %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } return; }