Finishing the total time counters
[lwes-journaller.git] / src / journal_gz.c
blobaf47ce3eedd34553e14647a3d3ab74a1d058544e
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 const 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 epoch = stbuf.st_ctime;
87 if (stbuf.st_size > 0) rename_journal(ppriv->path, &epoch);
89 break;
91 case O_RDWR:
92 mode = "w+b";
93 break;
95 default:
96 return -1;
99 ppriv->fp = gzopen(ppriv->path, mode);
101 if ( ! ppriv->fp )
102 return -1;
104 #if HAVE_SYS_STATVFS_H
105 if ( flags == O_WRONLY )
107 struct statvfs stfsbuf;
109 if ( -1 == statvfs(ppriv->path, &stfsbuf) )
111 LOG_WARN("Unable to determine free space available for %s.\n",
112 ppriv->path);
114 else
116 /* Check free space. */
118 /* tmpfs give f_bsize==0 */
119 long bsize = (stfsbuf.f_bsize!=0) ? stfsbuf.f_bsize : 4096 ;
120 long lsz = ppriv->nbytes_written / bsize ;
122 if ( lsz > (abs(stfsbuf.f_bavail) / 2.) )
124 LOG_WARN("Low on disk space for new gz log %s.\n",
125 ppriv->path);
126 LOG_WARN("Available space is %d blocks of %d bytes each.\n",
127 stfsbuf.f_bavail, bsize);
128 LOG_WARN("Last log file contained %lld bytes.\n",
129 ppriv->nbytes_written);
133 #endif
135 ppriv->ot = time(NULL);
136 ppriv->nbytes_written = 0;
138 return 0;
141 static int xclose(struct journal* this_journal)
143 struct priv* ppriv = (struct priv*)this_journal->priv;
145 if ( ! ppriv->fp )
147 return -1;
150 if ( gzclose(ppriv->fp) )
152 ppriv->fp = 0;
153 return -1;
156 rename_journal(ppriv->path, &ppriv->ot);
157 ppriv->fp = 0;
158 return 0;
161 static int xread(struct journal* this_journal, void* ptr, size_t size)
163 return gzread(((struct priv*)this_journal->priv)->fp, ptr, size);
166 static int xwrite(struct journal* this_journal, void* ptr, size_t size)
168 struct priv* ppriv = (struct priv*)this_journal->priv;
170 int ret = gzwrite(ppriv->fp, ptr, size);
171 if ( ret > 0 )
172 ppriv->nbytes_written += ret;
173 return ret;
176 static int tailmatch(const char* str, const char* tail)
178 size_t strsz = strlen(str);
179 size_t tailsz = strlen(tail);
181 if ( tailsz > strsz )
182 return 0;
184 return strcmp(str + (strsz - tailsz), tail) == 0;
187 int journal_gz_ctor(struct journal* this_journal, const char* path)
189 static struct journal_vtbl vtbl = {
190 destructor,
191 xopen, xclose,
192 xread, xwrite
195 struct priv* ppriv;
197 this_journal->vtbl = 0;
198 this_journal->priv = 0;
200 if ( ! tailmatch(path, JOURNAL_GZ_EXT) )
202 LOG_WARN("Compressed journal file (\"%s\") doesn't end with "
203 "expected extension (\"%s\").\n",
204 path, JOURNAL_GZ_EXT);
207 ppriv = (struct priv*)malloc(sizeof(struct priv));
208 if ( 0 == ppriv )
210 LOG_ER("Malloc failed attempting to allocate %d bytes.\n",
211 sizeof(*ppriv));
212 return -1;
214 memset(ppriv, 0, sizeof(*ppriv));
216 if ( 0 == (ppriv->path = strdup(path)) )
218 LOG_ER("The strdup() function failed attempting to dup \"%s\".\n",
219 path);
220 free(ppriv);
221 return -1;
224 this_journal->vtbl = &vtbl;
225 this_journal->priv = ppriv;
227 return 0;
230 #else /* if HAVE_LIBZ */
232 int journal_gz_ctor(struct journal* this_journal, const char* path)
234 this_journal->vtbl = 0;
235 this_journal->priv = 0;
236 (void)path; /* appease -Wall -Werror */
238 return -1;
241 #endif /* if HAVE_LIBZ */