1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
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. *
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. *
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 *======================================================================*/
23 #include "journal_gz.h"
25 #include "rename_journal.h"
36 #include <sys/types.h>
38 #if HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
50 long long nbytes_written
;
53 static void destructor(struct journal
* this_journal
)
57 this_journal
->vtbl
->close(this_journal
);
59 ppriv
= (struct priv
*)this_journal
->priv
;
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
;
72 time_t epoch
= 0; /* Crashed files may include data from the past. */
74 /* If this journal is already open, return an error. */
85 if ( 0 == stat(ppriv
->path
, &stbuf
) ) {
86 epoch
= stbuf
.st_ctime
;
87 if (stbuf
.st_size
> 0) rename_journal(ppriv
->path
, &epoch
);
99 ppriv
->fp
= gzopen(ppriv
->path
, mode
);
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",
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",
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
);
135 ppriv
->ot
= time(NULL
);
136 ppriv
->nbytes_written
= 0;
141 static int xclose(struct journal
* this_journal
)
143 struct priv
* ppriv
= (struct priv
*)this_journal
->priv
;
150 if ( gzclose(ppriv
->fp
) )
156 rename_journal(ppriv
->path
, &ppriv
->ot
);
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
);
172 ppriv
->nbytes_written
+= 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
)
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
= {
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
));
210 LOG_ER("Malloc failed attempting to allocate %d bytes.\n",
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",
224 this_journal
->vtbl
= &vtbl
;
225 this_journal
->priv
= ppriv
;
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 */
241 #endif /* if HAVE_LIBZ */