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_file.h"
25 #include "rename_journal.h"
36 #include <sys/types.h>
38 #if HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
46 long long nbytes_written
;
49 static void destructor(struct journal
* this_journal
)
53 this_journal
->vtbl
->close(this_journal
);
55 ppriv
= (struct priv
*)this_journal
->priv
;
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
;
69 time_t epoch
= 0; /* Crashed files may include data from the past. */
71 /* If this journal is already open, return an error. */
85 if ( 0 == stat(ppriv
->path
, &buf
) )
87 rename_journal(ppriv
->path
, &epoch
);
99 ppriv
->fp
= fopen(ppriv
->path
, mode
);
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",
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
);
133 ppriv
->ot
= time(NULL
);
134 ppriv
->nbytes_written
= 0;
139 static int xclose(struct journal
* this_journal
)
141 struct priv
* ppriv
= (struct priv
*)this_journal
->priv
;
148 if ( fclose(ppriv
->fp
) )
154 rename_journal(ppriv
->path
, &ppriv
->ot
);
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
= {
184 this_journal
->vtbl
= 0;
185 this_journal
->priv
= 0;
187 ppriv
= (struct priv
*)malloc(sizeof(struct priv
));
190 LOG_ER("Failed to allocate %d bytes for journal data.\n",
194 memset(ppriv
, 0, sizeof(*ppriv
));
196 if ( 0 == (ppriv
->path
= strdup(path
)) )
202 this_journal
->vtbl
= &vtbl
;
203 this_journal
->priv
= ppriv
;