/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2013
+ * 1998, 2015
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
int PerUserLimitsNumber=0;
//! Log user's who downloaded more than the limit.
struct PerUserLimitStruct PerUserLimits[MAX_USER_LIMITS];
+//! How to create a per user file.
+enum PerUserFileCreationEnum PerUserFileCreation=PUFC_Always;
extern struct globalstatstruct globstat;
void htmlrel(void)
{
- FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2;
+ FileObject *fp_in;
+ FileObject *fp_ip;
+ FILE *fp_ou;
+ FILE *fp_ip2;
long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0;
long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0;
userscan uscan;
if (snprintf(tmp2,sizeof(tmp2),"%s/sargtmp.int_unsort",tmp)>=sizeof(tmp2)) {
- debuga(_("File name too long: %s/sargtmp.int_unsort\n"),tmp);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/sargtmp.int_unsort\n",tmp);
exit(EXIT_FAILURE);
}
if (snprintf(tmp3,sizeof(tmp3),"%s/sargtmp.int_log",tmp)>=sizeof(tmp3)) {
- debuga(_("File name too long: %s/sargtmp.int_log\n"),tmp);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/sargtmp.int_log\n",tmp);
exit(EXIT_FAILURE);
}
sort_labels(&sort_field,&sort_order);
- for (i=0 ; i<PerUserLimitsNumber ; i++) {
- if (access(PerUserLimits[i].File,R_OK)==0 && unlink(PerUserLimits[i].File)==-1) {
- debuga(_("Cannot delete per_user_limit file \"%s\": %s"),PerUserLimits[i].File,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
+ switch (PerUserFileCreation)
+ {
+ case PUFC_Always:
+ for (i=0 ; i<PerUserLimitsNumber ; i++) {
+ FILE *fp_usr=fopen(PerUserLimits[i].File,"wt");
+ if (fp_usr==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot create empty per_user_limit file \"%s\": %s\n"),PerUserLimits[i].File,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (fclose(fp_usr)==EOF) {
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),PerUserLimits[i].File,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ }
+ break;
+
+ case PUFC_AsRequired:
+ for (i=0 ; i<PerUserLimitsNumber ; i++) {
+ if (access(PerUserLimits[i].File,R_OK)==0 && unlink(PerUserLimits[i].File)==-1) {
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),PerUserLimits[i].File,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ }
+ break;
}
uscan=userinfo_startscan();
if (uscan == NULL) {
- debuga(_("Cannot enumerate the user list\n"));
+ debuga(__FILE__,__LINE__,_("Cannot enumerate the user list\n"));
exit(EXIT_FAILURE);
}
while ( (uinfo = userinfo_advancescan(uscan)) != NULL ) {
if (snprintf(warea,sizeof(warea),"%s/%s",outdirname,uinfo->filename)>=sizeof(warea)) {
- debuga(_("Destination directory too long: %s/%s\n"),outdirname,uinfo->filename);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/%s\n",outdirname,uinfo->filename);
exit(EXIT_FAILURE);
}
if (!uinfo->topuser) {
}
if (access(warea, R_OK) != 0) {
- if (mkdir(warea,0755)) {
- debuga(_("Cannot create directory %s - %s\n"),warea,strerror(errno));
+ if (PortableMkDir(warea,0755)) {
+ debuga(__FILE__,__LINE__,_("Cannot create directory \"%s\": %s\n"),warea,strerror(errno));
exit(EXIT_FAILURE);
}
}
tmpsort(uinfo);
if (snprintf(arqin,sizeof(arqin),"%s/htmlrel.txt",tmp)>=sizeof(arqin)) {
- debuga(_("Input file name too long: %s/htmlrel.txt\n"),tmp);
+ debuga(__FILE__,__LINE__,_("Input file name too long: %s/htmlrel.txt\n"),tmp);
exit(EXIT_FAILURE);
}
- if ((fp_in = fopen(arqin, "r")) == 0){
+ if ((fp_in = FileObject_Open(arqin)) == 0){
if (uinfo->no_report) continue;
- debuga(_("(html3) Cannot open file %s: %s\n"),arqin,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqin,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if (snprintf(arqou,sizeof(arqou),"%s/%s/%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqou)) {
- debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname,uinfo->filename,uinfo->filename);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/%s/%s.html\n",outdirname,uinfo->filename,uinfo->filename);
exit(EXIT_FAILURE);
}
if (snprintf(duser,sizeof(duser),"%s/denied_%s.html",outdirname,uinfo->filename)>=sizeof(duser)) {
- debuga(_("File name too long: %s/denied_%s.html\n"),outdirname,uinfo->filename);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/denied_%s.html\n",outdirname,uinfo->filename);
exit(EXIT_FAILURE);
}
if(access(duser, R_OK) != 0)
have_denied_report=true;
if ((line=longline_create())==NULL) {
- debuga(_("Not enough memory to read file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
while((buf=longline_read(fp_in,line))!=NULL) {
getword_start(&gwarea,buf);
if (getword_atoll(<emp,&gwarea,'\t')<0) {
- debuga(_("There is a broken number of access in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
tnacc+=ltemp;
if (getword_atoll(<emp,&gwarea,'\t')<0) {
- debuga(_("There is a broken downloaded size in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid downloaded size in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
tnbytes+=ltemp;
if (getword_ptr(NULL,NULL,&gwarea,'\t')<0) {
- debuga(_("There is a broken url in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_skip(MAXLEN,&gwarea,'\t')<0) {
- debuga(_("There is a broken access code in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid access code in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_atoll(<emp,&gwarea,'\t')<0) {
- debuga(_("There is a broken elapsed time in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
tnelap+=ltemp;
if (getword_atoll(<emp,&gwarea,'\t')<0) {
- debuga(_("There is a broken in-cache volume in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
tnincache+=ltemp;
if (getword_atoll(<emp,&gwarea,'\n')<0) {
- debuga(_("There is a broken out-cache volume in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
tnoucache+=ltemp;
}
- rewind(fp_in);
+ FileObject_Rewind(fp_in);
if ((fp_ou = fopen(arqou, "w")) == 0){
- debuga(_("(html5) Cannot open file %s: %s\n"),arqou,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqou,strerror(errno));
exit(EXIT_FAILURE);
}
fputs("</tr></thead>\n",fp_ou);
if(debug) {
- debuga(_("Making report: %s\n"),uinfo->id);
+ debuga(__FILE__,__LINE__,_("Making report %s\n"),uinfo->id);
}
count=0;
arqip[0]='\0';
while((buf=longline_read(fp_in,line))!=NULL) {
getword_start(&gwarea,buf);
if (getword_atoll(&twork,&gwarea,'\t')<0) {
- debuga(_("There is a broken number of access in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid number of accesses in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_atoll(&nnbytes,&gwarea,'\t')<0) {
- debuga(_("There is a broken number of bytes in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid number of bytes in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
- debuga(_("There is a broken url in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword(tmsg,sizeof(tmsg),&gwarea,'\t')<0) {
- debuga(_("There is a broken access code in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid access code in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_atoll(&nnelap,&gwarea,'\t')<0) {
- debuga(_("There is a broken elapsed time in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_atoll(&incache,&gwarea,'\t')<0) {
- debuga(_("There is a broken in cache column in file %s\n"),arqin);
+ debuga(__FILE__,__LINE__,_("Invalid in-cache size in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
if (getword_atoll(&oucache,&gwarea,'\n')<0) {
- debuga(_("There is a broken out of cache column in file %s (%d)\n"),arqin,__LINE__);
+ debuga(__FILE__,__LINE__,_("Invalid out-of-cache size in file \"%s\"\n"),arqin);
exit(EXIT_FAILURE);
}
} else if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) {
snprintf(warea,sizeof(warea),"%s/%s/tt.html",outdirname,uinfo->filename);
if (unlink(warea)!=0) {
- debuga(_("Cannot delete \"%s\": %s\n"),warea,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),warea,strerror(errno));
}
}
if(iprel) {
if (snprintf(arqip,sizeof(arqip),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(arqip)) {
- debuga(_("File name too long: %s/%s.ip\n"),tmp,uinfo->filename);
+ debuga(__FILE__,__LINE__,_("Path too long: "));
+ debuga_more("%s/%s.ip\n",tmp,uinfo->filename);
exit(EXIT_FAILURE);
}
- if ((fp_ip = fopen(arqip, "r")) == 0){
- debuga(_("(html6) Cannot open file %s: %s\n"),arqip,strerror(errno));
+ if ((fp_ip = FileObject_Open(arqip)) == 0){
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqip,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if ((fp_ip2 = MY_FOPEN(tmp2, "a")) == 0){
- debuga(_("(html7) Cannot open file %s: %s\n"),tmp2,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
if ((line1=longline_create())==NULL) {
- debuga(_("Not enough memory to read file %s\n"),arqip);
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arqip);
exit(EXIT_FAILURE);
}
while((buf=longline_read(fp_ip,line1))!=NULL) {
getword_start(&gwarea,buf);
if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid user IP in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_ptr(buf,&user_url,&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken url in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (strncmp(user_url,url,strlen(url))!=0) continue;
if (getword_skip(15,&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken day in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid day in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_skip(15,&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken time in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_atoll(&userbytes,&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken size in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid size in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_atoll(&userelap,&gwarea,'\0')<0) {
- debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
fprintf(fp_ip2,"%s\t%"PRIu64"\t%"PRIu64"\n",user_ip,(uint64_t)userbytes,(uint64_t)userelap);
}
longline_destroy(&line1);
- fclose(fp_ip);
if (fclose(fp_ip2)==EOF) {
- debuga(_("Write error in %s: %s\n"),tmp2,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (FileObject_Close(fp_ip)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqip,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
if (snprintf(csort,sizeof(csort),"sort -n -t \"\t\" -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2)>=sizeof(csort)) {
- debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
+ debuga(__FILE__,__LINE__,_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),tmp2,tmp3);
exit(EXIT_FAILURE);
}
cstatus=system(csort);
if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
- debuga(_("sort command: %s\n"),csort);
+ debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
+ debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
- if ((fp_ip = MY_FOPEN(tmp3, "r")) == 0) {
- debuga(_("(html8) Cannot open file %s: %s\n"),tmp3,strerror(errno));
+ if ((fp_ip = FileObject_Open(tmp3)) == 0) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if (unlink(tmp2)) {
- debuga(_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
olduserip[0]='\0';
if ((line1=longline_create())==NULL) {
- debuga(_("Not enough memory to read file %s\n"),arqip);
+ debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),arqip);
exit(EXIT_FAILURE);
}
while((buf=longline_read(fp_ip,line1))!=NULL) {
getword_start(&gwarea,buf);
if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid user IP in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_atoll(&userbytes,&gwarea,'\t')<0) {
- debuga(_("Maybe you have a broken size in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid size in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if (getword_atoll(&userelap,&gwarea,'\0')<0) {
- debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3);
+ debuga(__FILE__,__LINE__,_("Invalid elapsed time in file \"%s\"\n"),tmp3);
exit(EXIT_FAILURE);
}
if(strcmp(user_ip,olduserip) != 0) {
unelap+=userelap;
}
- fclose(fp_ip);
+ if (FileObject_Close(fp_ip)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
longline_destroy(&line1);
if (unlink(tmp3)) {
- debuga(_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),tmp3,strerror(errno));
exit(EXIT_FAILURE);
}
unelap=0;
}
- fclose(fp_in);
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqin,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
longline_destroy(&line);
if (iprel && arqip[0]) {
if (!KeepTempLog && unlink(arqip)) {
- debuga(_("Cannot delete \"%s\": %s\n"),arqip,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),arqip,strerror(errno));
exit(EXIT_FAILURE);
}
}
if (!KeepTempLog && unlink(arqin)) {
- debuga(_("Cannot delete \"%s\": %s\n"),arqin,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),arqin,strerror(errno));
exit(EXIT_FAILURE);
}
if (limit>PerUserLimits[i].Limit && (user_limit[maskid] & mask)==0) {
FILE *fp_usr;
- if((fp_usr = fopen(PerUserLimits[i].File, "a")) == 0) {
- debuga(_("(html10) Cannot open per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno));
+ if((fp_usr = fopen(PerUserLimits[i].File, "at")) == 0) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),PerUserLimits[i].File,strerror(errno));
exit(EXIT_FAILURE);
}
- if (PerUserLimits[i].UserId)
- fprintf(fp_usr,"%s\n",uinfo->label);
- else
- fprintf(fp_usr,"%s\n",uinfo->ip);
+ switch (PerUserLimits[i].Output)
+ {
+ case PUOE_UserId:
+ fprintf(fp_usr,"%s\n",uinfo->label);
+ break;
+ case PUOE_UserIp:
+ fprintf(fp_usr,"%s\n",uinfo->ip);
+ break;
+ }
if (fclose(fp_usr)==EOF) {
- debuga(_("Write error in per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),PerUserLimits[i].File,strerror(errno));
exit(EXIT_FAILURE);
}
user_limit[maskid]|=mask;
if(debug)
- debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,
+ debuga(__FILE__,__LINE__,_("Limit exceeded for user %s (%d MB). Added to file \"%s\"\n"),uinfo->label,
PerUserLimits[i].Limit,PerUserLimits[i].File);
}
}
fputs("</tfoot>",fp_ou);
fputs("</table></div>\n",fp_ou);
- if (write_html_trailer(fp_ou)<0)
- debuga(_("Write error in file %s\n"),arqou);
+ write_html_trailer(fp_ou);
if (fclose(fp_ou)==EOF) {
- debuga(_("Write error in %s: %s\n"),arqou,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),arqou,strerror(errno));
exit(EXIT_FAILURE);
}