Must use (void) instead of () for function declarations.
[lwes-journaller.git] / src / journal_gz.c
blob6299a500cde39fb1c9c1d8f0fab1718e2ebb6dda
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
20 #include "config.h"
22 #include "journal.h"
23 #include "journal_gz.h"
25 #include "rename_journal.h"
26 #include "log.h"
27 #include "opt.h"
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
38 #if HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
40 #endif
42 #if HAVE_LIBZ
44 #include <zlib.h>
46 struct priv {
47 char* path;
48 gzFile fp;
49 time_t ot;
50 long long nbytes_written;
53 static void destructor(struct journal* this_journal)
55 struct priv* ppriv;
57 this_journal->vtbl->close(this_journal);
59 ppriv = (struct priv*)this_journal->priv;
60 free(ppriv->path);
61 free(ppriv);
63 this_journal->vtbl = 0;
64 this_journal->priv = 0;
67 static int xopen(struct journal* this_journal, int flags)
69 struct priv* ppriv = (struct priv*)this_journal->priv;
70 char* mode;
71 struct stat stbuf;
72 time_t epoch = 0; /* Crashed files may include data from the past. */
74 /* If this journal is already open, return an error. */
75 if ( ppriv->fp )
76 return -1;
78 switch ( flags ) {
79 case O_RDONLY:
80 mode = "rb";
81 break;
83 case O_WRONLY:
84 mode = "wb";
85 if ( 0 == stat(ppriv->path, &stbuf) ) {
86 rename_journal(ppriv->path, &epoch);
88 break;
90 case O_RDWR:
91 mode = "w+b";
92 break;
94 default:
95 return -1;
98 ppriv->fp = gzopen(ppriv->path, mode);
100 if ( ! ppriv->fp )
101 return -1;
103 #if HAVE_SYS_STATVFS_H
104 if ( flags == O_WRONLY )
106 struct statvfs stfsbuf;
108 if ( -1 == statvfs(ppriv->path, &stfsbuf) )
110 LOG_WARN("Unable to determine free space available for %s.\n",
111 ppriv->path);
113 else
115 /* Check free space. */
117 /* tmpfs give f_bsize==0 */
118 long bsize = (stfsbuf.f_bsize!=0) ? stfsbuf.f_bsize : 4096 ;
119 long lsz = ppriv->nbytes_written / bsize ;
121 if ( ! arg_sink_ram )
122 { /* -sink-ram has different boundary conditions */
123 if ( lsz > (abs(stfsbuf.f_bavail) / 2.) )
125 LOG_WARN("Low on disk space for new gz log %s.\n",
126 ppriv->path);
127 LOG_WARN("Available space is %d blocks of %d bytes each.\n",
128 stfsbuf.f_bavail, bsize);
129 LOG_WARN("Last log file contained %lld bytes.\n",
130 ppriv->nbytes_written);
133 else /* test boundaries of /sink/ram */
135 if ( lsz > stfsbuf.f_bavail / 2. )
137 long long avail_bytes = stfsbuf.f_bavail * 4096 ;
138 LOG_WARN("Low on %s space for new gz log %s.\n",
139 arg_sink_ram, ppriv->path);
140 LOG_WARN("Available space is %d bytes.\n", avail_bytes);
141 LOG_WARN("Last log file contained %lld bytes.\n",
142 ppriv->nbytes_written);
147 #endif
149 ppriv->ot = time(NULL);
150 ppriv->nbytes_written = 0;
152 return 0;
155 static int xclose(struct journal* this_journal)
157 struct priv* ppriv = (struct priv*)this_journal->priv;
159 if ( ! ppriv->fp )
161 return -1;
164 if ( gzclose(ppriv->fp) )
166 ppriv->fp = 0;
167 return -1;
170 rename_journal(ppriv->path, &ppriv->ot);
171 ppriv->fp = 0;
172 return 0;
175 static int xread(struct journal* this_journal, void* ptr, size_t size)
177 return gzread(((struct priv*)this_journal->priv)->fp, ptr, size);
180 static int xwrite(struct journal* this_journal, void* ptr, size_t size)
182 struct priv* ppriv = (struct priv*)this_journal->priv;
184 int ret = gzwrite(ppriv->fp, ptr, size);
185 if ( ret > 0 )
186 ppriv->nbytes_written += ret;
187 return ret;
190 static int tailmatch(const char* str, const char* tail)
192 size_t strsz = strlen(str);
193 size_t tailsz = strlen(tail);
195 if ( tailsz > strsz )
196 return 0;
198 return strcmp(str + (strsz - tailsz), tail) == 0;
201 int journal_gz_ctor(struct journal* this_journal, const char* path)
203 static struct journal_vtbl vtbl = {
204 destructor,
205 xopen, xclose,
206 xread, xwrite
209 struct priv* ppriv;
211 this_journal->vtbl = 0;
212 this_journal->priv = 0;
214 if ( ! tailmatch(path, JOURNAL_GZ_EXT) )
216 LOG_WARN("Compressed journal file (\"%s\") doesn't end with "
217 "expected extension (\"%s\").\n",
218 path, JOURNAL_GZ_EXT);
221 ppriv = (struct priv*)malloc(sizeof(struct priv));
222 if ( 0 == ppriv )
224 LOG_ER("Malloc failed attempting to allocate %d bytes.\n",
225 sizeof(*ppriv));
226 return -1;
228 memset(ppriv, 0, sizeof(*ppriv));
230 if ( 0 == (ppriv->path = strdup(path)) )
232 LOG_ER("The strdup() function failed attempting to dup \"%s\".\n",
233 path);
234 free(ppriv);
235 return -1;
238 this_journal->vtbl = &vtbl;
239 this_journal->priv = ppriv;
241 return 0;
244 #else /* if HAVE_LIBZ */
246 int journal_gz_ctor(struct journal* this_journal, const char* path)
248 this_journal->vtbl = 0;
249 this_journal->priv = 0;
250 (void)path; /* appease -Wall -Werror */
252 return -1;
255 #endif /* if HAVE_LIBZ */