From: Frederic Marchal Date: Sat, 13 Jun 2015 18:30:39 +0000 (+0200) Subject: Restore support for bzip2 files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a1e4e370333e352a6941a06d3fe5b06facf13496;p=thirdparty%2Fsarg.git Restore support for bzip2 files Sarg can read log compressed with bzip2 again. It doesn't use an external program to do that. It relies on the bzlib library if it is found. --- diff --git a/configure.ac b/configure.ac index 1edb8a4..8c76b70 100644 --- a/configure.ac +++ b/configure.ac @@ -189,6 +189,30 @@ AS_IF([test "x$with_zlib" != "xno" ], zlib_status="disabled" ]) +# Build with bzlib +AC_ARG_WITH([bzlib], + AS_HELP_STRING([--with-bzlib],[Compile with support to decompress bz2 files]), + [],[with_bzlib=check]) +AS_IF([test "x$with_bzlib" != "xno" ], +[ + AC_CHECK_HEADERS(bzlib.h) + AS_IF([test "x$ac_cv_header_bzlib_h" = "xyes"], + [ + AC_CHECK_LIB([bz2],[BZ2_bzReadOpen], + [ + LIBS="-lbz2 ${LIBS}" + HAVE_BZLIB_LIB="yes" + ],[ + HAVE_BZLIB_LIB="" + ]) + AS_IF([test "x$HAVE_BZLIB_LIB" != "xyes"],[AC_MSG_ERROR([bzlib was not found])]) + ],[ + bzlib_status="not found" + ]) +],[ + bzlib_status="disabled" +]) + dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST AC_STRUCT_TM @@ -441,3 +465,9 @@ AS_IF([test "x$zlib_status" = "xdisabled"],[ ],[test "x$zlib_status" = "xnot found"],[ AC_MSG_NOTICE([zlib.h was not found so it won't be possible to process gzipped files]) ]) + +AS_IF([test "x$bzlib_status" = "xdisabled"],[ + AC_MSG_NOTICE([Not building with bzlib as requested on the configuration command line]) +],[test "x$bzlib_status" = "xnot found"],[ + AC_MSG_NOTICE([bzlib.h was not found so it won't be possible to process bzipped files]) +]) diff --git a/decomp.c b/decomp.c index 28be907..5689d76 100644 --- a/decomp.c +++ b/decomp.c @@ -146,9 +146,9 @@ struct BzlibInternalFile BZFILE *BzFile; //! \c True if end of file is reached. bool Eof; - //! Copy of the original file handle. - int fd; -} + //! Original file in case a rewind is necessary. + FILE *File; +}; /*! * Read from bzip file. @@ -163,9 +163,14 @@ static int Bzip_Read(void *Data,void *Buffer,int Size) { struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data; int nread; + int bzerr=BZ_OK; - nread=BZ2_bzread(BData->BzFile,Buffer,Size); - if (nread==0) BData->Eof=true; + if (BData->Eof) return(0); + nread=BZ2_bzRead(&bzerr,BData->BzFile,Buffer,Size); + if (bzerr==BZ_STREAM_END) + BData->Eof=true; + else if (bzerr!=BZ_OK) + return(0); return(nread); } @@ -190,21 +195,17 @@ static int Bzip_Eof(void *Data) static void Bzip_Rewind(void *Data) { struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data; - int fd; + int bzerr=BZ_OK; - 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"); + BZ2_bzReadClose(&bzerr,BData->BzFile); + rewind(BData->File); + BData->BzFile=BZ2_bzReadOpen(&bzerr,BData->File,0,0,NULL,0); if (!BData->BzFile) { debuga(__FILE__,__LINE__,_("Cannot rewind bzip file\n")); exit(EXIT_FAILURE); } + BData->Eof=false; } /*! @@ -217,9 +218,10 @@ static void Bzip_Rewind(void *Data) static int Bzip_Close(void *Data) { struct BzlibInternalFile *BData=(struct BzlibInternalFile *)Data; + int bzerr=BZ_OK; - BZ2_bzclose(BData->BzFile); - close(BData->fd); + BZ2_bzReadClose(&bzerr,BData->BzFile); + fclose(BData->File); free(BData); return(0); } @@ -233,6 +235,7 @@ static FileObject *Bzip_Open(int fd) { FileObject *File; struct BzlibInternalFile *BData; + int bzerr=BZ_OK; FileObject_SetLastOpenError(NULL); File=calloc(1,sizeof(*File)); @@ -248,19 +251,19 @@ static FileObject *Bzip_Open(int fd) FileObject_SetLastOpenError(_("Not enough memory")); return(NULL); } - BData->fd=dup(fd); - if (BData->fd==-1) + BData->File=fdopen(fd,"rb"); + if (BData->File==NULL) { free(BData); free(File); FileObject_SetLastOpenError(_("Error duplicating file descriptor")); return(NULL); } - File=Data=BData; - BData->BzFile=BZ2_bzdopen(fd,"rb"); + File->Data=BData; + BData->BzFile=BZ2_bzReadOpen(&bzerr,BData->File,0,0,NULL,0); if (!BData->BzFile) { - close(BData->fd); + fclose(BData->File); free(BData); free(File); FileObject_SetLastOpenError(_("Error opening bzip file")); diff --git a/longline.c b/longline.c index e0c18f7..2e4761d 100644 --- a/longline.c +++ b/longline.c @@ -98,7 +98,7 @@ char *longline_read(FileObject *fp_in,longline line) { int i; char *newbuf; - size_t nread; + int nread; if (line==NULL || line->buffer==NULL) return(NULL); @@ -109,7 +109,7 @@ char *longline_read(FileObject *fp_in,longline line) break; } nread=(FileObject_Eof(fp_in)!=0) ? 0 : FileObject_Read(fp_in,line->buffer,line->size); - if (nread==0) return(NULL); + if (nread<=0) return(NULL); line->length=nread; line->end=0; } @@ -144,7 +144,7 @@ char *longline_read(FileObject *fp_in,longline line) line->buffer=newbuf; } nread=(FileObject_Eof(fp_in)!=0) ? 0 : FileObject_Read(fp_in,line->buffer+line->length,line->size-line->length); - if (nread==0) { + if (nread<=0) { if (line->end<=line->start) return(NULL); if (line->end>=line->size) { line->end=line->size; diff --git a/readlog.c b/readlog.c index 52caeeb..406f52b 100644 --- a/readlog.c +++ b/readlog.c @@ -368,7 +368,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) // pre-read the file only if we have to show stats if (ShowReadStatistics && ShowReadPercent && fp_in->Rewind) { - size_t nread,i; + int nread,i; bool skipcr=false; char tmp4[MAXLEN];