]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Restore support for bzip2 files
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Sat, 13 Jun 2015 18:30:39 +0000 (20:30 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Sat, 13 Jun 2015 18:30:39 +0000 (20:30 +0200)
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.

configure.ac
decomp.c
longline.c
readlog.c

index 1edb8a44d0f7abaaebfbd87f5b838c53d49c72cb..8c76b700410cc93920e919c3805a7d93cd33b130 100644 (file)
@@ -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])
+])
index 28be9076a2e06b8d442d276779e9625d0334ec65..5689d769177d2e66b229925dc504ac8c75ce2938 100644 (file)
--- 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"));
index e0c18f78c4cec8a423e8610ee0c1832758d4b4f3..2e4761d919a982c5ab7c84ce6f9d826b778c0557 100644 (file)
@@ -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;
index 52caeeb42646d457dfc560f7234ff72bacc1aefb..406f52bbca30d0cd4ae2e97507c63c192441ad72 100644 (file)
--- 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];