redirector.c auth.c download.c grepday.c ip2name_exec.c \
dansguardian_log.c dansguardian_report.c realtime.c btree_cache.c \
usertab.c userinfo.c longline.c url.c fnmatch.c stringbuffer.c \
- filelist.c readlog.c alias.c \
+ filelist.c readlog.c alias.c fileobject.c \
readlog_squid.c readlog_sarg.c readlog_extlog.c readlog_common.c
all: sarg
readlog_squid.o: include/readlog.h
stringbuffer.o: include/stringbuffer.h
userinfo.o: include/stringbuffer.h include/alias.h
+fileobject.o: include/fileobject.h
OBJS = $(SRCS:.c=.o)
void authfail_report(void)
{
- FILE *fp_in = NULL, *fp_ou = NULL;
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
char *buf;
char *url;
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
- if((fp_in=MY_FOPEN(authfail_sort,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),authfail_sort,strerror(errno));
+ if((fp_in=FileObject_Open(authfail_sort))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),authfail_sort,FileObject_GetLastOpenError());
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
output_html_link(fp_ou,url,100);
fputs("</td></th>\n",fp_ou);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),authfail_sort,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),authfail_sort,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
glob_status="disabled"
fi
+# Build with zlib
+AC_ARG_WITH([zlib],
+AS_HELP_STRING([--with-zlib],[Compile with support to decompress gz files]),
+[],[with_zlib=check])
+if ( test "x$with_zlib" != "xno" ) ; then
+ AC_CHECK_HEADERS(zlib.h)
+ if ( test "x$ac_cv_header_zlib_h" = "xyes" ) ; then
+ AC_CHECK_LIB(z, gzopen,LIBS="-lz ${LIBS}"; HAVE_ZLIB_LIB="yes", HAVE_ZLIB_LIB="")
+ if ( test "x$HAVE_ZLIB_LIB" != "xyes" ) ; then
+ AC_MSG_ERROR([zlib was not found])
+ fi
+ else
+ zlib_status="not found"
+ fi
+else
+ zlib_status="disabled"
+fi
+
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_STRUCT_TM
if ( test "x$glob_status" = "xdisabled" ) ; then
AC_MSG_NOTICE([Not building with file globbing as requested on the configuration command line])
fi
+
+if ( test "x$zlib_status" = "xdisabled" ) ; then
+ AC_MSG_NOTICE([Not building with zlib as requested on the configuration command line])
+elif ( test "x$zlib_status" = "xnot found" ) ; then
+ AC_MSG_NOTICE([zlib.h was not found so it won't be possible to process gzipped files])
+fi
void convlog(const char *arq, char df, int dfrom, int duntil)
{
- FILE *fp_in;
+ FileObject *fp_in;
char *buf;
char data[30];
char dia[11];
if(arq[0] == '\0')
arq="/var/log/squid/access.log";
- if((fp_in=MY_FOPEN(arq,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,strerror(errno));
+ if((fp_in=FileObject_Open(arq))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
}
longline_destroy(&line);
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
}
void data_file(char *tmp)
{
- FILE *fp_in, *fp_ou=NULL;
+ FileObject *fp_in;
+ FILE *fp_ou=NULL;
char *buf;
char accdia[11], acchora[9], accip[MAXLEN], *accurl;
exit(EXIT_FAILURE);
}
- if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
+ if((fp_in=FileObject_Open(tmp3))==NULL){
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
strcpy(oldacchora,acchora);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
#include "include/conf.h"
#include "include/defs.h"
+#ifdef HAVE_ZLIB_H
+#include "zlib.h"
+#endif
+#ifdef HAVE_BZLIB_H
+#include "bzlib.h"
+#endif
+
+#ifdef HAVE_ZLIB_H
+/*!
+ * Read from gzip file.
+ *
+ * \param Data The file object.
+ * \param Buffer The boffer to store the data read.
+ * \param Size How many bytes to read.
+ *
+ * \return The number of bytes read.
+ */
+static int Gzip_Read(void *Data,void *Buffer,int Size)
+{
+ return(gzread((gzFile)Data,Buffer,Size));
+}
+
+/*!
+ * Check if end of file is reached.
+ *
+ * \param Data The file object.
+ *
+ * \return \c True if end of file is reached.
+ */
+static int Gzip_Eof(void *Data)
+{
+ return(gzeof((gzFile)Data));
+}
+
+/*!
+ * Return to the beginnig of the file.
+ *
+ * \param Data The file object.
+ */
+static void Gzip_Rewind(void *Data)
+{
+ gzrewind((gzFile)Data);
+}
+
+/*!
+ * Close the file.
+ *
+ * \param Data File to close.
+ *
+ * \return 0 on success or -1 on error.
+ */
+static int Gzip_Close(void *Data)
+{
+ int RetCode=-1;
+ int Status;
+
+ Status=gzclose((gzFile)Data);
+ switch (Status)
+ {
+ case Z_OK:
+ RetCode=0;
+ break;
+ case Z_STREAM_ERROR:
+ FileObject_SetLastCloseError(_("Invalid gzip file"));
+ break;
+ case Z_ERRNO:
+ FileObject_SetLastCloseError(_("File operation error"));
+ break;
+ case Z_MEM_ERROR:
+ FileObject_SetLastCloseError(_("Not enough memory"));
+ break;
+ case Z_BUF_ERROR:
+ FileObject_SetLastCloseError(_("Truncated gzip stream"));
+ break;
+ default:
+ FileObject_SetLastCloseError(_("Unknown error returned by zlib"));
+ break;
+ }
+ return(RetCode);
+}
+
+/*!
+ * Open a file object to read from a gzip file.
+ *
+ * \return The object to pass to other function in this module.
+ */
+static FileObject *Gzip_Open(int fd)
+{
+ FileObject *File;
+
+ FileObject_SetLastOpenError(NULL);
+ File=calloc(1,sizeof(*File));
+ if (!File)
+ {
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ File->Data=gzdopen(fd,"rb");
+ if (!File->Data)
+ {
+ free(File);
+ FileObject_SetLastOpenError(_("Error opening gzip file"));
+ return(NULL);
+ }
+ File->Read=Gzip_Read;
+ File->Eof=Gzip_Eof;
+ File->Rewind=Gzip_Rewind;
+ File->Close=Gzip_Close;
+ return(File);
+}
+#endif
+
+#ifdef HAVE_BZLIB_H
+
+struct BzlibInternalFile
+{
+ //! Bzlib object.
+ BZFILE *BzFile;
+ //! \c True if end of file is reached.
+ bool Eof;
+ //! Copy of the original file handle.
+ int fd;
+}
+
+/*!
+ * Read from bzip file.
+ *
+ * \param Data The file object.
+ * \param Buffer The boffer to store the data read.
+ * \param Size How many bytes to read.
+ *
+ * \return The number of bytes read.
+ */
+static int Bzip_Read(void *Data,void *Buffer,int Size)
+{
+ struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data;
+ int nread;
+
+ nread=BZ2_bzread(BData->BzFile,Buffer,Size);
+ if (nread==0) BData->Eof=true;
+ return(nread);
+}
+
+/*!
+ * Check if end of file is reached.
+ *
+ * \param Data The file object.
+ *
+ * \return \c True if end of file is reached.
+ */
+static int Bzip_Eof(void *Data)
+{
+ struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data;
+ return(BData->Eof);
+}
+
+/*!
+ * Return to the beginnig of the file.
+ *
+ * \param Data The file object.
+ */
+static void Bzip_Rewind(void *Data)
+{
+ struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data;
+ int fd;
+
+ BZ2_bzclose(BData->BzFile);
+ fd=dup(BData->fd);
+ if (fd==-1)
+ {
+ debuga(__FILE__,__LINE__,_("Cannot rewind bzip file\n"));
+ exit(EXIT_FAILURE);
+ }
+ BData->BzFile=BZ2_bzdopen(fd,"rb");
+ if (!BData->BzFile)
+ {
+ debuga(__FILE__,__LINE__,_("Cannot rewind bzip file\n"));
+ exit(EXIT_FAILURE);
+ }
+}
+
+/*!
+ * Close the file.
+ *
+ * \param Data File to close.
+ *
+ * \return 0 on success or -1 on error.
+ */
+static int Bzip_Close(void *Data)
+{
+ struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data;
+
+ BZ2_bzclose(BData->BzFile);
+ close(BData->fd);
+ free(BData);
+ return(0);
+}
+
+/*!
+ * Open a file object to read from a bzip file.
+ *
+ * \return The object to pass to other function in this module.
+ */
+static FileObject *Bzip_Open(int fd)
+{
+ FileObject *File;
+ struct BzlibInternalFile *BData;
+
+ FileObject_SetLastOpenError(NULL);
+ File=calloc(1,sizeof(*File));
+ if (!File)
+ {
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ BData=calloc(1,sizeof(*BData));
+ if (!BData)
+ {
+ free(File);
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ BData->fd=dup(fd);
+ if (BData->fd==-1)
+ {
+ free(BData);
+ free(File);
+ FileObject_SetLastOpenError(_("Error duplicating file descriptor"));
+ return(NULL);
+ }
+ File=Data=BData;
+ BData->BzFile=BZ2_bzdopen(fd,"rb");
+ if (!BData->BzFile)
+ {
+ close(BData->fd);
+ free(BData);
+ free(File);
+ FileObject_SetLastOpenError(_("Error opening bzip file"));
+ return(NULL);
+ }
+ File->Read=Bzip_Read;
+ File->Eof=Bzip_Eof;
+ File->Rewind=Bzip_Rewind;
+ File->Close=Bzip_Close;
+ return(File);
+}
+#endif
+
/*!
Open the log file. If it is compressed, uncompress it through a pipe.
\date 2010-05-10 - F Marchal\n
The function doesn't use a temporary file any more and read the compressed file through a pipe.
*/
-FILE *decomp(const char *arq, bool *pipe)
+FileObject *decomp(const char *arq)
{
- FILE *fi;
- char cmd[1024];
- int arqlen;
-
- arqlen=strlen(arq);
- if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) {
- /*
- TRANSLATORS: The last %s is the command used to uncompress
- the log file such as zcat ou bzip2.
- */
- debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
- if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
- debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
- exit(EXIT_FAILURE);
- }
- *pipe=true;
- fi=popen(cmd,"r");
- }
- else if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) {
- debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"bzcat");
- if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) {
- debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
- exit(EXIT_FAILURE);
- }
- *pipe=true;
- fi=popen(cmd,"r");
- }
- else if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) {
- debuga(__FILE__,__LINE__,_("Decompressing log file \"%s\" with %s\n"),arq,"zcat");
- if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) {
- debuga(__FILE__,__LINE__,_("decompression command too long for log file %s\n"),arq);
- exit(EXIT_FAILURE);
- }
- *pipe=true;
- fi=popen(cmd,"r");
- }
- else {
- *pipe=false;
- fi=MY_FOPEN(arq,"r");
+ int fd;
+ FileObject *fi;
+ unsigned char buf[3];
+ ssize_t nread;
+
+ // guess file type
+ fd=open(arq,O_RDONLY | O_LARGEFILE);
+ if (fd==-1) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ nread=read(fd,buf,sizeof(buf));
+ if (nread==-1) {
+ debuga(__FILE__,__LINE__,_("Error while reading \"%s\" to guess its type: %s\n"),arq,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (nread<3) {
+ debuga(__FILE__,__LINE__,_("File \"%s\" is too small to guess its type\n"),arq);
+ exit(EXIT_FAILURE);
+ }
+ if (lseek(fd,0,SEEK_SET)==-1) {
+ debuga(__FILE__,__LINE__,_("Cannot return to the beginning of file \"%s\": %s"),arq,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (buf[0]==0x1F && buf[1]==0x8B && buf[2]==0x08)//gzip file
+ {
+#ifdef HAVE_ZLIB_H
+ fi=Gzip_Open(fd);
+#else
+ debuga(__FILE__,__LINE__,_("Sarg was not compiled with gzip support to read file \"%s\"\n"),arq);
+ exit(EXIT_FAILURE);
+#endif
+ }
+ else if (buf[0]==0x42 && buf[1]==0x5A && buf[2]==0x68)//bzip2 file
+ {
+#ifdef HAVE_BZLIB_H
+ fi=Bzip_Open(fd);
+#else
+ debuga(__FILE__,__LINE__,_("Sarg was not compiled with bzip support to read file \"%s\"\n"),arq);
+ exit(EXIT_FAILURE);
+#endif
+ }
+ else //normal file
+ {
+ fi=FileObject_FdOpen(fd);
}
return(fi);
}
*/
void gen_denied_report(void)
{
- FILE *fp_in = NULL, *fp_ou = NULL;
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
char *buf;
char *url;
sprintf(report,"%s/denied.html",outdirname);
- if((fp_in=MY_FOPEN(denied_sort,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_sort,strerror(errno));
+ if((fp_in=FileObject_Open(denied_sort))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),denied_sort,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
output_html_link(fp_ou,url,100);
fputs("</td></tr>\n",fp_ou);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),denied_sort,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),denied_sort,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
*/
void download_report(void)
{
- FILE *fp_in = NULL, *fp_ou = NULL;
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
char *buf;
char *url;
// produce the report.
snprintf(report,sizeof(report),"%s/download.html",outdirname);
- if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report_in,strerror(errno));
+ if((fp_in=FileObject_Open(report_in))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report_in,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
output_html_link(fp_ou,url,100);
fputs("</td></tr>\n",fp_ou);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),report_in,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),report_in,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
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;
+ FileObject *fp_in;
+ FILE *fp_top1, *fp_top2, *fp_top3;
long long int ttnbytes=0, ttnacc=0, tnacc=0;
long long int tnbytes=0, ttnelap=0, tnelap=0;
long long int nacc, nbytes, elap;
const struct userinfostruct *uinfo;
snprintf(wger,sizeof(wger),"%s/sarg-general",dirname);
- if((fp_in=fopen(wger,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,strerror(errno));
+ if((fp_in=FileObject_Open(wger))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
tnacc+=item.nacc;
tnelap+=item.nelap;
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
--- /dev/null
+/*
+ * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
+ * 1998, 2015
+ *
+ * SARG donations:
+ * please look at http://sarg.sourceforge.net/donations.php
+ * Support:
+ * http://sourceforge.net/projects/sarg/forums/forum/363374
+ * ---------------------------------------------------------------------
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ *
+ */
+/*!\file
+\brief Encapsulate a file object
+
+The file can be a standard file of the C library or a gzip file or
+a bzip file.
+*/
+
+#include "include/conf.h"
+#include "include/stringbuffer.h"
+#include "include/fileobject.h"
+
+//! Message describing the last open error.
+static char LastOpenErrorString[2048]="";
+//! Message describing the last close error.
+static char LastCloseErrorString[2048]="";
+
+/*!
+ * Read a file using the standard C api.
+ *
+ * \param Data The file object.
+ * \param Buffer The boffer to store the data read.
+ * \param Size How many bytes to read.
+ *
+ * \return The number of bytes read.
+ */
+static int Standard_Read(void *Data,void *Buffer,int Size)
+{
+ return(fread(Buffer,1,Size,(FILE *)Data));
+}
+
+/*!
+ * Check if end of file is reached.
+ *
+ * \param Data The file object.
+ *
+ * \return \c True if end of file is reached.
+ */
+static int Standard_Eof(void *Data)
+{
+ return(feof((FILE *)Data));
+}
+
+/*!
+ * Return to the beginnig of the file.
+ *
+ * \param Data The file object.
+ */
+static void Standard_Rewind(void *Data)
+{
+ rewind((FILE *)Data);
+}
+
+/*!
+ * Close a file using the standard C api.
+ *
+ * \param Data File to close.
+ *
+ * \return EOF on error.
+ */
+static int Standard_Close(void *Data)
+{
+ int RetCode=0;
+
+ if (fclose((FILE *)Data)==EOF)
+ {
+ FileObject_SetLastCloseError(strerror(errno));
+ RetCode=-1;
+ }
+ return(RetCode);
+}
+
+/*!
+ * Open a file for reading using the standard C api.
+ *
+ * \param FileName The file to open.
+ *
+ * \return The object to pass to other function in this module.
+ */
+FileObject *FileObject_Open(const char *FileName)
+{
+ FileObject *File;
+
+ LastOpenErrorString[0]='\0';
+ File=malloc(sizeof(*File));
+ if (!File)
+ {
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ File->Data=MY_FOPEN(FileName,"r");
+ if (!File->Data)
+ {
+ free(File);
+ FileObject_SetLastOpenError(strerror(errno));
+ return(NULL);
+ }
+ File->Read=Standard_Read;
+ File->Eof=Standard_Eof;
+ File->Rewind=Standard_Rewind;
+ File->Close=Standard_Close;
+ return(File);
+}
+
+/*!
+ * Open a file for reading using the standard C api.
+ *
+ * \param FileName The file to open.
+ *
+ * \return The object to pass to other function in this module.
+ */
+FileObject *FileObject_FdOpen(int fd)
+{
+ FileObject *File;
+
+ LastOpenErrorString[0]='\0';
+ File=malloc(sizeof(*File));
+ if (!File)
+ {
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ File->Data=fdopen(fd,"r");
+ if (!File->Data)
+ {
+ free(File);
+ FileObject_SetLastOpenError(strerror(errno));
+ return(NULL);
+ }
+ File->Read=Standard_Read;
+ File->Eof=Standard_Eof;
+ File->Rewind=Standard_Rewind;
+ File->Close=Standard_Close;
+ return(File);
+}
+
+/*!
+ * Read the content of the file using the function identified
+ * by the file object.
+ *
+ * \param File The file object to read.
+ * \param Buffer The buffer to write the data into.
+ * \param Size The maximum number of bytes to read.
+ *
+ * \return The number of bytes read or -1.
+ */
+int FileObject_Read(FileObject *File,void *Buffer,int Size)
+{
+ return(File->Read(File->Data,Buffer,Size));
+}
+
+/*!
+ * Check if the end of file is reached.
+ *
+ * \param File The file object.
+ *
+ * \return \c True if end of file is reached.
+ */
+int FileObject_Eof(FileObject *File)
+{
+ return(File->Eof(File->Data));
+}
+
+/*!
+ * Return to the beginning of the file.
+ *
+ * \param File The file object.
+ */
+void FileObject_Rewind(FileObject *File)
+{
+ File->Rewind(File->Data);
+}
+
+/*!
+ * Close the file opened. The memory is freed. The object
+ * cannot be reused after this function returns.
+ *
+ * \param File The file object to close.
+ *
+ * \return Zero on success or -1 on failure.
+ */
+int FileObject_Close(FileObject *File)
+{
+ LastCloseErrorString[0]='\0';
+ int RetCode=File->Close(File->Data);
+ free(File);
+ return(RetCode);
+}
+
+/*!
+ * Set the message returned by the last open error.
+ *
+ * \param Message The message explaining what error occurred
+ * when the file was opened.
+ */
+void FileObject_SetLastOpenError(const char *Message)
+{
+ if (Message)
+ {
+ strncpy(LastOpenErrorString,Message,sizeof(LastOpenErrorString)-1);
+ LastOpenErrorString[sizeof(LastOpenErrorString)-1]='\0';
+ }
+ else
+ LastOpenErrorString[0]='\0';
+}
+
+/*!
+ * Get a message explaining the last open error.
+ *
+ * \return The string to display to the user.
+ */
+const char *FileObject_GetLastOpenError(void)
+{
+ return(LastOpenErrorString);
+}
+
+/*!
+ * Set the message returned by the last close error.
+ *
+ * \param Message The message explaining what error occurred
+ * when the file was closed.
+ */
+void FileObject_SetLastCloseError(const char *Message)
+{
+ if (Message)
+ {
+ strncpy(LastCloseErrorString,Message,sizeof(LastCloseErrorString)-1);
+ LastCloseErrorString[sizeof(LastCloseErrorString)-1]='\0';
+ }
+ else
+ LastCloseErrorString[0]='\0';
+}
+
+/*!
+ * Get a message explaining the last close error.
+ *
+ * \return The string to display to the user.
+ */
+const char *FileObject_GetLastCloseError(void)
+{
+ return(LastCloseErrorString);
+}
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;
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(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqin,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqin,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
tnoucache+=ltemp;
}
- rewind(fp_in);
+ FileObject_Rewind(fp_in);
if ((fp_ou = fopen(arqou, "w")) == 0){
debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arqou,strerror(errno));
exit(EXIT_FAILURE);
}
- if ((fp_ip = fopen(arqip, "r")) == 0){
- debuga(__FILE__,__LINE__,_("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);
}
debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),tmp2,strerror(errno));
exit(EXIT_FAILURE);
}
- if (fclose(fp_ip)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqip,strerror(errno));
+ if (FileObject_Close(fp_ip)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqip,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
- if ((fp_ip = MY_FOPEN(tmp3, "r")) == 0) {
- debuga(__FILE__,__LINE__,_("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);
}
unelap+=userelap;
}
- if (fclose(fp_ip)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+ if (FileObject_Close(fp_ip)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line1);
unelap=0;
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqin,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arqin,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
#define __attribute__(a)
#endif
+#include "include/fileobject.h"
+
//! \brief Constants to compare the log level to display messages
enum DebugLogLevel
{
void data_file(char *tmp);
// decomp.c
-FILE *decomp(const char *arq, bool *pipe);
+FileObject *decomp(const char *arq);
// denied.c
void denied_open(void);
// longline.c
__attribute__((warn_unused_result)) /*@null@*//*@only@*/longline longline_create(void);
void longline_reset(longline line);
-/*@null@*/char *longline_read(FILE *fp_in,/*@null@*/longline line);
+/*@null@*/char *longline_read(FileObject *fp_in,/*@null@*/longline line);
void longline_destroy(/*@out@*//*@only@*//*@null@*/longline *line_ptr);
// index.c
--- /dev/null
+#ifndef FILEOBJECT_H
+#define FILEOBJECT_H
+
+typedef struct FileObjectStruct
+{
+ void *Data;
+ int (*Read)(void *Data,void *Buffer,int Size);
+ int (*Eof)(void *Data);
+ void (*Rewind)(void *Data);
+ int (*Close)(void *Data);
+} FileObject;
+
+FileObject *FileObject_Open(const char *FileName);
+FileObject *FileObject_FdOpen(int fd);
+int FileObject_Read(FileObject *File,void *Buffer,int Size);
+int FileObject_Eof(FileObject *File);
+void FileObject_Rewind(FileObject *File);
+int FileObject_Close(FileObject *File);
+
+void FileObject_SetLastOpenError(const char *Message);
+const char *FileObject_GetLastOpenError(void);
+void FileObject_SetLastCloseError(const char *Message);
+const char *FileObject_GetLastCloseError(void);
+
+#endif // FILEOBJECT_H
}
}
-char *longline_read(FILE *fp_in,longline line)
+char *longline_read(FileObject *fp_in,longline line)
{
int i;
char *newbuf;
line->end=i;
break;
}
- nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer,1,line->size,fp_in);
+ nread=(FileObject_Eof(fp_in)!=0) ? 0 : FileObject_Read(fp_in,line->buffer,line->size);
if (nread==0) return(NULL);
line->length=nread;
line->end=0;
}
line->buffer=newbuf;
}
- nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in);
+ nread=(FileObject_Eof(fp_in)!=0) ? 0 : FileObject_Read(fp_in,line->buffer+line->length,line->size-line->length);
if (nread==0) {
if (line->end<=line->start) return(NULL);
if (line->end>=line->size) {
//! The latest date in time format.
static struct tm LatestDateTime;
+/*!
+ * Read from standard input.
+ *
+ * \param Data The file object.
+ * \param Buffer The boffer to store the data read.
+ * \param Size How many bytes to read.
+ *
+ * \return The number of bytes read.
+ */
+static int Stdin_Read(void *Data,void *Buffer,int Size)
+{
+ return(fread(Buffer,1,Size,(FILE *)Data));
+}
+
+/*!
+ * Check if end of file is reached.
+ *
+ * \param Data The file object.
+ *
+ * \return \c True if end of file is reached.
+ */
+static int Stdin_Eof(void *Data)
+{
+ return(feof((FILE *)Data));
+}
+
+/*!
+ * Mimic a close of standard input but do nothing
+ *
+ * \param Data File to close.
+ *
+ * \return EOF on error.
+ */
+static int Stdin_Close(void *Data)
+{
+ return(0);
+}
+
+/*!
+ * Open a file object to read from standard input.
+ *
+ * \return The object to pass to other function in this module.
+ */
+static FileObject *Stdin_Open(void)
+{
+ FileObject *File;
+
+ FileObject_SetLastOpenError(NULL);
+ File=calloc(1,sizeof(*File));
+ if (!File)
+ {
+ FileObject_SetLastOpenError(_("Not enough memory"));
+ return(NULL);
+ }
+ File->Data=stdin;
+ File->Read=Stdin_Read;
+ File->Eof=Stdin_Eof;
+ File->Rewind=NULL;
+ File->Close=Stdin_Close;
+ return(File);
+}
+
/*!
* Initialize the memory structure needed by LogLine_Parse() to parse
* a log line.
int maxopenfiles=MAX_OPEN_USER_FILES;
unsigned long int recs1=0UL;
unsigned long int recs2=0UL;
- FILE *fp_in=NULL;
- bool from_pipe;
- bool from_stdin;
+ FileObject *fp_in=NULL;
bool download_flag=false;
bool id_is_ip;
enum ReadLogReturnCodeEnum log_entry_status;
LogFormats[x]->NewFile(arq);
if (arq[0]=='-' && arq[1]=='\0') {
+ fp_in=Stdin_Open();
if(debug)
debuga(__FILE__,__LINE__,_("Reading access log file: from stdin\n"));
- fp_in=stdin;
- from_stdin=true;
} else {
if (Filter->DateRange[0]!='\0') {
if (stat(arq,&logstat)!=0) {
}
}
}
- fp_in=decomp(arq,&from_pipe);
- if(fp_in==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open input log file \"%s\": %s\n"),arq,strerror(errno));
+ fp_in=decomp(arq);
+ if (fp_in==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open input log file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
- if(debug) debuga(__FILE__,__LINE__,_("Reading access log file: %s\n"),arq);
- from_stdin=false;
+ if (debug) debuga(__FILE__,__LINE__,_("Reading access log file: %s\n"),arq);
}
download_flag=false;
recs2=0UL;
// pre-read the file only if we have to show stats
- if (ShowReadStatistics && ShowReadPercent && !from_stdin && !from_pipe) {
+ if (ShowReadStatistics && ShowReadPercent && fp_in->Rewind) {
size_t nread,i;
bool skipcr=false;
char tmp4[MAXLEN];
- while ((nread=fread(tmp4,1,sizeof(tmp4),fp_in))>0) {
+ while ((nread=FileObject_Read(fp_in,tmp4,sizeof(tmp4)))>0) {
for (i=0 ; i<nread ; i++)
if (skipcr) {
if (tmp4[i]!='\n' && tmp4[i]!='\r') {
}
}
}
- rewind(fp_in);
+ FileObject_Rewind(fp_in);
printf(_("SARG: Records in file: %lu, reading: %3.2f%%"),recs1,(float) 0);
putchar('\r');
fflush( stdout ) ;
}
longline_destroy(&line);
- if (!from_stdin) {
- if (from_pipe)
- pclose(fp_in);
- else {
- fclose(fp_in);
- if (ShowReadStatistics) {
- if (ShowReadPercent)
- printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs2, (float) 100 );
- else
- printf(_("SARG: Records in file: %lu\n"),recs2);
- }
- }
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
+ exit(EXIT_FAILURE);
+ }
+ if (ShowReadStatistics) {
+ if (ShowReadPercent)
+ printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs2, (float) 100 );
+ else
+ printf(_("SARG: Records in file: %lu\n"),recs2);
}
}
void realtime(void)
{
- FILE *fp;
+ FileObject *fp;
char file_name[2048];
char *buf;
longline line;
debuga(__FILE__,__LINE__,_("No log file to read the last %d lines from\n"),realtime_access_log_lines);
exit(EXIT_FAILURE);
}
- fp = fopen(file_name, "r");
+ fp = FileObject_Open(file_name);
if (!fp) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),file_name,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),file_name,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
if (NextIndex>StoreSize) StoreSize=NextIndex;
if (NextIndex>realtime_access_log_lines) NextIndex=0;
}
- if (fclose(fp)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),file_name,strerror(errno));
+ if (FileObject_Close(fp)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),file_name,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
static void read_log(const char *wentp, FILE *fp_ou,int dfrom,int duntil)
{
- FILE *fp_in = NULL;
+ FileObject *fp_in = NULL;
char *buf;
int i;
longline line;
exit(EXIT_FAILURE);
}
- if ((fp_in=fopen(wentp,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wentp,strerror(errno));
+ if ((fp_in=FileObject_Open(wentp))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wentp,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
while ((buf=longline_read(fp_in,line)) != NULL) {
parse_log(fp_ou,buf);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wentp,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wentp,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
void redirector_report(void)
{
- FILE *fp_in = NULL, *fp_ou = NULL;
+ FileObject *fp_in = NULL;
+ FILE *fp_ou = NULL;
char *buf;
char *url;
snprintf(report,sizeof(report),"%s/redirector.html",outdirname);
- if((fp_in=fopen(redirector_sorted,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),redirector_sorted,strerror(errno));
+ if((fp_in=FileObject_Open(redirector_sorted))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),redirector_sorted,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
output_html_link(fp_ou,url,100);
fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),redirector_sorted,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),redirector_sorted,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
void gerarel(void)
{
- FILE *fp_in;
+ FileObject *fp_in;
FILE *fp_gen;
FILE *fp_tmp=NULL;
debuga_more("%s/%s.user_log\n",tmp,uinfo->filename);
exit(EXIT_FAILURE);
}
- if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,strerror(errno));
+ if((fp_in=FileObject_Open(tmp3))==NULL){
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),tmp3,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
user=uinfo->filename;
strcpy(oldaccdia,accdia);
strcpy(oldacchora,acchora);
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),tmp3,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
void siteuser(void)
{
- FILE *fp_in, *fp_ou;
+ FileObject *fp_in;
+ FILE *fp_ou;
char *buf;
char *ourl;
exit(EXIT_FAILURE);
}
- if((fp_in=fopen(general2,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,strerror(errno));
+ if((fp_in=FileObject_Open(general2))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,FileObject_GetLastOpenError());
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
}
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
*/
void splitlog(const char *arq, char df, int dfrom, int duntil, int convert, const char *splitprefix)
{
- FILE *fp_in;
+ FileObject *fp_in;
FILE *fp_ou=NULL;
char *buf;
char data[30];
if(arq[0] == '\0')
arq="/var/log/squid/access.log";
- if((fp_in=MY_FOPEN(arq,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,strerror(errno));
+ if((fp_in=FileObject_Open(arq))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),arq,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
}
longline_destroy(&line);
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
if (autosplit && fp_ou) {
void topsites(void)
{
- FILE *fp_in, *fp_ou;
+ FileObject *fp_in;
+ FILE *fp_ou;
char *buf;
char *url;
exit(EXIT_FAILURE);
}
- if((fp_in=fopen(general2,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,strerror(errno));
+ if((fp_in=FileObject_Open(general2))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),general2,FileObject_GetLastOpenError());
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
ttntime+=item.nelap;
#endif
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),general2,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
- if((fp_in=fopen(sites,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),sites,strerror(errno));
+ if((fp_in=FileObject_Open(sites))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),sites,FileObject_GetLastOpenError());
debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
exit(EXIT_FAILURE);
}
if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%d\"",nusers);
fprintf(fp_ou,">%s</td></tr>\n",fixnum(nusers,1));
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),sites,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),sites,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
void topuser(void)
{
- FILE *fp_in = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL;
+ FileObject *fp_in = NULL;
+ FileObject *fp_top1 = NULL;
+ FILE *fp_top2 = NULL, *fp_top3 = NULL;
long long int ttnbytes=0, ttnacc=0, tnacc=0;
long long int tnbytes=0, ttnelap=0, tnelap=0;
long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0;
debuga(__FILE__,__LINE__,_("Creating top users report...\n"));
ntopuser = 0;
snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname);
- if((fp_in=fopen(wger,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,strerror(errno));
+ if((fp_in=FileObject_Open(wger))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),wger,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
tnincache+=item.incache;
tnoucache+=item.oucache;
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wger,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);
exit(EXIT_FAILURE);
}
- if((fp_top1=fopen(top1,"r"))==NULL) {
- debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top1,strerror(errno));
+ if((fp_top1=FileObject_Open(top1))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),top1,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
topcount++;
}
- if (fclose(fp_top1)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top1,strerror(errno));
+ if (FileObject_Close(fp_top1)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),top1,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
if (!KeepTempLog && unlink(top1)) {
*/
void read_hostalias(const char *Filename)
{
- FILE *fi;
+ FileObject *fi;
longline line;
char *buf;
if (debug) debuga(__FILE__,__LINE__,_("Reading host alias file \"%s\"\n"),Filename);
- fi=fopen(Filename,"rt");
+ fi=FileObject_Open(Filename);
if (!fi) {
- debuga(__FILE__,__LINE__,_("Cannot read host name alias file \"%s\": %s\n"),Filename,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot read host name alias file \"%s\": %s\n"),Filename,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
}
longline_destroy(&line);
- if (fclose(fi)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),Filename,strerror(errno));
+ if (FileObject_Close(fi)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),Filename,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
*/
void read_useralias(const char *Filename)
{
- FILE *fi;
+ FileObject *fi;
longline line;
char *buf;
exit(EXIT_FAILURE);
}
- fi=fopen(Filename,"rt");
+ fi=FileObject_Open(Filename);
if (!fi) {
- debuga(__FILE__,__LINE__,_("Cannot read user name alias file \"%s\": %s\n"),Filename,strerror(errno));
+ debuga(__FILE__,__LINE__,_("Cannot read user name alias file \"%s\": %s\n"),Filename,FileObject_GetLastOpenError());
exit(EXIT_FAILURE);
}
}
longline_destroy(&line);
- if (fclose(fi)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),Filename,strerror(errno));
+ if (FileObject_Close(fi)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),Filename,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media)
{
- FILE *fp_in;
+ FileObject *fp_in;
char *buf;
char wdir[MAXLEN];
char user[MAX_USER_LEN];
debuga_more("%s%s/sarg-general",dirname,name);
exit(EXIT_FAILURE);
}
- if ((fp_in = fopen(wdir, "r")) == 0) {
+ if ((fp_in = FileObject_Open(wdir)) == NULL) {
if (snprintf(wdir,sizeof(wdir),"%s%s/general",dirname,name)>=sizeof(wdir)) {
debuga(__FILE__,__LINE__,_("Buffer too small to store "));
debuga_more("%s%s/general",dirname,name);
exit(EXIT_FAILURE);
}
- if ((fp_in = fopen(wdir, "r")) == 0) {
+ if ((fp_in = FileObject_Open(wdir)) == NULL) {
return;
}
}
}
break;
}
- if (fclose(fp_in)==EOF) {
- debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,strerror(errno));
+ if (FileObject_Close(fp_in)) {
+ debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),wdir,FileObject_GetLastCloseError());
exit(EXIT_FAILURE);
}
longline_destroy(&line);