Finishing the total time counters
[lwes-journaller.git] / src / journal_file.c
blob5bee2869defd633fd547e09d322a06f911464b0d
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_file.h"
25 #include "rename_journal.h"
26 #include "log.h"
27 #include "opt.h"
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.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 struct priv {
43 char* path;
44 FILE* fp;
45 time_t ot;
46 long long nbytes_written;
49 static void destructor(struct journal* this_journal)
51 struct priv* ppriv;
53 this_journal->vtbl->close(this_journal);
55 ppriv = (struct priv*)this_journal->priv;
57 free(ppriv->path);
58 free(ppriv);
60 this_journal->vtbl = 0;
61 this_journal->priv = 0;
64 static int xopen(struct journal* this_journal, int flags)
66 struct priv* ppriv = (struct priv*)this_journal->priv;
67 const char* mode;
68 struct stat buf;
69 time_t epoch = 0; /* Crashed files may include data from the past. */
71 /* If this journal is already open, return an error. */
72 if ( ppriv->fp )
74 return -1;
77 switch ( flags )
79 case O_RDONLY:
80 mode = "rb";
81 break;
83 case O_WRONLY:
84 mode = "wb";
85 if ( 0 == stat(ppriv->path, &buf) )
87 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 = fopen(ppriv->path, mode);
101 if ( ! ppriv->fp )
103 return -1;
106 #if HAVE_SYS_STATVFS_H
107 if ( flags == O_WRONLY )
109 struct statvfs stfsbuf;
111 if ( -1 == statvfs(ppriv->path, &stfsbuf) )
113 LOG_WARN("Unable to determine free space available for %s.\n",
114 ppriv->path);
116 else
118 /* Check free space. */
119 /* tmpfs give f_bsize==0 */
120 long bsize = (stfsbuf.f_bsize!=0) ? stfsbuf.f_bsize : 4096 ;
121 long lsz = ppriv->nbytes_written / bsize ;
123 if ( lsz > (stfsbuf.f_bavail / 2.) )
125 LOG_WARN("Low on disk space for new log %s.\n", ppriv->path);
126 LOG_WARN("Available space is %d blocks of %d bytes each.\n", stfsbuf.f_bavail, bsize);
127 LOG_WARN("Last log file contained %lld bytes.\n", ppriv->nbytes_written);
131 #endif
133 ppriv->ot = time(NULL);
134 ppriv->nbytes_written = 0;
136 return 0;
139 static int xclose(struct journal* this_journal)
141 struct priv* ppriv = (struct priv*)this_journal->priv;
143 if ( ! ppriv->fp )
145 return -1;
148 if ( fclose(ppriv->fp) )
150 ppriv->fp = 0;
151 return -1;
154 rename_journal(ppriv->path, &ppriv->ot);
155 ppriv->fp = 0;
156 return 0;
159 static int xread(struct journal* this_journal, void* ptr, size_t size)
161 size_t ret = fread(ptr, size, 1, ((struct priv*)this_journal->priv)->fp);
162 return (int)ret * size;
165 static int xwrite(struct journal* this_journal, void* ptr, size_t size)
167 struct priv* ppriv = (struct priv*)this_journal->priv;
169 size_t ret = fwrite(ptr, size, 1, ((struct priv*)this_journal->priv)->fp);
170 ppriv->nbytes_written += ret * size;
171 return (int)ret * size;
174 int journal_file_ctor(struct journal* this_journal, const char* path)
176 static struct journal_vtbl vtbl = {
177 destructor,
178 xopen, xclose,
179 xread, xwrite
182 struct priv* ppriv;
184 this_journal->vtbl = 0;
185 this_journal->priv = 0;
187 ppriv = (struct priv*)malloc(sizeof(struct priv));
188 if ( 0 == ppriv )
190 LOG_ER("Failed to allocate %d bytes for journal data.\n",
191 sizeof(*ppriv));
192 return -1;
194 memset(ppriv, 0, sizeof(*ppriv));
196 if ( 0 == (ppriv->path = strdup(path)) )
198 free(ppriv);
199 return -1;
202 this_journal->vtbl = &vtbl;
203 this_journal->priv = ppriv;
205 return 0;