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 rename_journal(ppriv
->path
, &epoch
);
98 ppriv
->fp
= gzopen(ppriv
->path
, mode
);
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",
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",
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
);
149 ppriv
->ot
= time(NULL
);
150 ppriv
->nbytes_written
= 0;
155 static int xclose(struct journal
* this_journal
)
157 struct priv
* ppriv
= (struct priv
*)this_journal
->priv
;
164 if ( gzclose(ppriv
->fp
) )
170 rename_journal(ppriv
->path
, &ppriv
->ot
);
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
);
186 ppriv
->nbytes_written
+= 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
)
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
= {
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
));
224 LOG_ER("Malloc failed attempting to allocate %d bytes.\n",
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",
238 this_journal
->vtbl
= &vtbl
;
239 this_journal
->priv
= ppriv
;
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 */
255 #endif /* if HAVE_LIBZ */