Skip to content

Commit dee3412

Browse files
committed
Added optional --with-zlib support for compressed log files parsing.
Automatically detect and handle compressed files, falling back to standard file handling if zlib is not available. Enable with: ./configure --enable-utf8 --enable-geoip=mmdb --with-openssl --with-getline --with-zlib Example: ``` Before: zcat access.log.gz | goaccess - --log-format=COMBINED -o report.html After: goaccess access.log access.log.gz --log-format=COMBINED -o report.html ``` Closes #981
1 parent 5e5e7d2 commit dee3412

File tree

7 files changed

+417
-45
lines changed

7 files changed

+417
-45
lines changed

Makefile.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ goaccess_SOURCES = \
156156
src/csv.h \
157157
src/error.c \
158158
src/error.h \
159+
src/fileio.c \
160+
src/fileio.h \
159161
src/gdashboard.c \
160162
src/gdashboard.h \
161163
src/gdns.c \
@@ -247,6 +249,10 @@ if WITH_RDYNAMIC
247249
AM_LDFLAGS = -rdynamic
248250
endif
249251

252+
if WITH_ZLIB
253+
AM_LDFLAGS = -lz
254+
endif
255+
250256
AM_CFLAGS += -Wall -Wextra -Wnested-externs -Wformat=2 -g
251257
AM_CFLAGS += -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
252258
AM_CFLAGS += -Wwrite-strings -Wshadow -Wpointer-arith -Wsign-compare

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ if test "$openssl" = 'yes'; then
7676
fi
7777
AM_CONDITIONAL([WITH_SSL], [test "x$with_openssl" = "xyes"])
7878

79+
# Build with zlib
80+
AC_ARG_WITH([zlib],
81+
[AS_HELP_STRING([--with-zlib], [Build with zlib support for reading gzipped logs. Default is disabled])],
82+
[zlib="$withval"],
83+
[zlib="no"])
84+
85+
if test "$zlib" = 'yes'; then
86+
AC_CHECK_LIB([z], [gzopen], [], [AC_MSG_ERROR([zlib library missing])])
87+
AC_CHECK_HEADERS([zlib.h], [], [AC_MSG_ERROR([zlib header missing])])
88+
AC_DEFINE([HAVE_ZLIB], 1, [Build with zlib support])
89+
fi
90+
AM_CONDITIONAL([WITH_ZLIB], [test "x$zlib" = "xyes"])
91+
7992
# GeoIP
8093
AC_ARG_ENABLE([geoip],[AS_HELP_STRING([--enable-geoip],[Enable GeoIP country lookup. Supported types: mmdb, legacy. Default is disabled])],[geoip="$enableval"],[geoip=no])
8194

@@ -277,6 +290,7 @@ Your build configuration:
277290
Geolocation : $geolocation
278291
Storage method : $storage
279292
TLS/SSL : $openssl
293+
zlib support : $zlib
280294
Bugs : $PACKAGE_BUGREPORT
281295

282296
EOF

src/fileio.c

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/**
2+
* fileio.c -- gFile I/O abstraction layer for goaccess
3+
* This provides a unified interface for reading both regular and gzipped files
4+
* ______ ___
5+
* / ____/___ / | _____________ __________
6+
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
7+
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
8+
* \____/\____/_/ |_\___/\___/\___/____/____/
9+
*
10+
* The MIT License (MIT)
11+
* Copyright (c) 2009-2025 Gerardo Orellana <hello @ goaccess.io>
12+
*
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be included in all
21+
* copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
* SOFTWARE.
30+
*/
31+
32+
#include <stdio.h>
33+
#include <stdlib.h>
34+
#include <string.h>
35+
#include <errno.h>
36+
37+
#include "fileio.h"
38+
39+
#ifdef HAVE_ZLIB
40+
/* Check if a file is gzipped by examining its magic bytes */
41+
static int
42+
is_gzipped_file (const char *filename) {
43+
FILE *fp;
44+
unsigned char magic[2];
45+
int result = 0;
46+
47+
if ((fp = fopen (filename, "rb")) == NULL)
48+
return 0;
49+
50+
if (fread (magic, 1, 2, fp) == 2) {
51+
/* gzip magic number is 0x1f 0x8b */
52+
if (magic[0] == 0x1f && magic[1] == 0x8b)
53+
result = 1;
54+
}
55+
56+
fclose (fp);
57+
return result;
58+
}
59+
#endif
60+
61+
/* Open a file for reading, automatically detecting gzip compression */
62+
GFileHandle *
63+
gfile_open (const char *filename, const char *mode) {
64+
GFileHandle *fh;
65+
66+
if ((fh = calloc (1, sizeof (GFileHandle))) == NULL)
67+
return NULL;
68+
69+
#ifdef HAVE_ZLIB
70+
/* Check if file is gzipped */
71+
if (is_gzipped_file (filename)) {
72+
fh->is_gzipped = 1;
73+
fh->gzfp = gzopen (filename, mode);
74+
if (fh->gzfp == NULL) {
75+
free (fh);
76+
return NULL;
77+
}
78+
fh->fp = NULL;
79+
return fh;
80+
}
81+
#endif
82+
83+
/* Open as regular file */
84+
#ifdef HAVE_ZLIB
85+
fh->is_gzipped = 0;
86+
fh->gzfp = NULL;
87+
#endif
88+
fh->fp = fopen (filename, mode);
89+
if (fh->fp == NULL) {
90+
free (fh);
91+
return NULL;
92+
}
93+
94+
return fh;
95+
}
96+
97+
/* Close a file handle */
98+
void
99+
gfile_close (GFileHandle *fh) {
100+
if (!fh)
101+
return;
102+
103+
#ifdef HAVE_ZLIB
104+
if (fh->is_gzipped && fh->gzfp) {
105+
gzclose (fh->gzfp);
106+
} else
107+
#endif
108+
if (fh->fp) {
109+
fclose (fh->fp);
110+
}
111+
112+
free (fh);
113+
}
114+
115+
/* Read a line from the file */
116+
char *
117+
gfile_gets (char *buf, int size, GFileHandle *fh) {
118+
if (!fh || !buf || size <= 0)
119+
return NULL;
120+
121+
#ifdef HAVE_ZLIB
122+
if (fh->is_gzipped && fh->gzfp) {
123+
return gzgets (fh->gzfp, buf, size);
124+
}
125+
#endif
126+
127+
if (fh->fp)
128+
return fgets (buf, size, fh->fp);
129+
130+
return NULL;
131+
}
132+
133+
/* Check if end of file is reached */
134+
int
135+
gfile_eof (GFileHandle *fh) {
136+
if (!fh)
137+
return 1;
138+
139+
#ifdef HAVE_ZLIB
140+
if (fh->is_gzipped && fh->gzfp) {
141+
return gzeof (fh->gzfp);
142+
}
143+
#endif
144+
145+
if (fh->fp)
146+
return feof (fh->fp);
147+
148+
return 1;
149+
}
150+
151+
/* Read data from file */
152+
int
153+
gfile_read (void *buf, size_t size, size_t count, GFileHandle *fh) {
154+
if (!fh || !buf)
155+
return 0;
156+
157+
#ifdef HAVE_ZLIB
158+
if (fh->is_gzipped && fh->gzfp) {
159+
int bytes_to_read = size * count;
160+
int bytes_read = gzread (fh->gzfp, buf, bytes_to_read);
161+
if (bytes_read < 0)
162+
return 0;
163+
return bytes_read / size;
164+
}
165+
#endif
166+
167+
if (fh->fp)
168+
return fread (buf, size, count, fh->fp);
169+
170+
return 0;
171+
}
172+
173+
/* Seek to a position in the file */
174+
int
175+
gfile_seek (GFileHandle *fh, long offset, int whence) {
176+
if (!fh)
177+
return -1;
178+
179+
#ifdef HAVE_ZLIB
180+
if (fh->is_gzipped && fh->gzfp) {
181+
/* gzseek returns the current offset, or -1 on error */
182+
z_off_t result = gzseek (fh->gzfp, offset, whence);
183+
return (result == -1) ? -1 : 0;
184+
}
185+
#endif
186+
187+
if (fh->fp)
188+
return fseek (fh->fp, offset, whence);
189+
190+
return -1;
191+
}
192+
193+
/* Get current position in file */
194+
long
195+
gfile_tell (GFileHandle *fh) {
196+
if (!fh)
197+
return -1;
198+
199+
#ifdef HAVE_ZLIB
200+
if (fh->is_gzipped && fh->gzfp) {
201+
return (long) gztell (fh->gzfp);
202+
}
203+
#endif
204+
205+
if (fh->fp)
206+
return ftell (fh->fp);
207+
208+
return -1;
209+
}
210+
211+
/* Check for file errors */
212+
int
213+
gfile_error (GFileHandle *fh) {
214+
if (!fh)
215+
return 1;
216+
217+
#ifdef HAVE_ZLIB
218+
if (fh->is_gzipped && fh->gzfp) {
219+
int errnum;
220+
gzerror (fh->gzfp, &errnum);
221+
return errnum != Z_OK && errnum != Z_STREAM_END;
222+
}
223+
#endif
224+
225+
if (fh->fp)
226+
return ferror (fh->fp);
227+
228+
return 1;
229+
}

src/fileio.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* ______ ___
3+
* / ____/___ / | _____________ __________
4+
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
5+
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
6+
* \____/\____/_/ |_\___/\___/\___/____/____/
7+
*
8+
* The MIT License (MIT)
9+
* Copyright (c) 2009-2025 Gerardo Orellana <hello @ goaccess.io>
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in all
19+
* copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27+
* SOFTWARE.
28+
*/
29+
30+
#ifndef FILEIO_H_INCLUDED
31+
#define FILEIO_H_INCLUDED
32+
33+
#include <stdio.h>
34+
35+
#ifdef HAVE_CONFIG_H
36+
#include <config.h>
37+
#endif
38+
39+
#ifdef HAVE_ZLIB
40+
#include <zlib.h>
41+
#endif
42+
43+
/* File handle abstraction */
44+
typedef struct GFileHandle_ {
45+
#ifdef HAVE_ZLIB
46+
gzFile gzfp;
47+
int is_gzipped;
48+
#endif
49+
FILE *fp;
50+
} GFileHandle;
51+
52+
/* Function prototypes */
53+
GFileHandle *gfile_open (const char *filename, const char *mode);
54+
void gfile_close (GFileHandle * fh);
55+
char *gfile_gets (char *buf, int size, GFileHandle * fh);
56+
int gfile_eof (GFileHandle * fh);
57+
int gfile_read (void *buf, size_t size, size_t count, GFileHandle * fh);
58+
int gfile_seek (GFileHandle * fh, long offset, int whence);
59+
long gfile_tell (GFileHandle * fh);
60+
int gfile_error (GFileHandle * fh);
61+
62+
#endif /* FILEIO_H_INCLUDED */

0 commit comments

Comments
 (0)