2 * Copyright (c) 2005-2008 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/endian.h>
43 #include "libfifolog_int.h"
46 * Memory handling for zlib
50 fifo_zalloc(voidpf opaque __unused, uInt items, uInt size)
53 return calloc(items,size);
57 fifo_zfree(voidpf opaque __unused, voidpf address)
64 * Open a fifolog file or partition for reading or writing.
66 * Return value is NULL for success or a error description string to
67 * be augmented by errno if non-zero.
69 * The second function is just an error-handling wrapper around the
70 * first which, does the actual work.
74 fifolog_int_open_i(struct fifolog_file *f, const char *fname, int mode)
80 f->fd = open(fname, mode ? O_RDWR : O_RDONLY);
82 return ("Cannot open");
84 /* Determine initial record size guesstimate */
85 i = ioctl(f->fd, DIOCGSECTORSIZE, &f->recsize);
86 if (i != 0 && errno != ENOTTY)
87 return ("ioctl(DIOCGSECTORSIZE) failed");
90 i = fstat(f->fd, &st);
91 if (!S_ISREG(st.st_mode))
92 return ("Neither disk nor regular file");
94 f->logsize = st.st_size;
95 } else if (f->recsize < 64) {
96 return ("Disk device sectorsize smaller than 64");
98 i = ioctl(f->fd, DIOCGMEDIASIZE, &f->logsize);
99 if (i < 0 && errno != ENOTTY)
100 return ("ioctl(DIOCGMEDIASIZE) failed");
103 /* Allocate a record buffer */
104 f->recbuf = malloc(f->recsize);
105 if (f->recbuf == NULL)
106 return ("Cannot malloc");
108 /* Read and validate the label sector */
109 i = pread(f->fd, f->recbuf, f->recsize, 0);
110 if (i < 0 || i < (int)f->recsize)
111 return ("Read error, first sector");
114 if (memcmp(f->recbuf, FIFOLOG_FMT_MAGIC, strlen(FIFOLOG_FMT_MAGIC) + 1))
115 return ("Wrong or missing magic string");
117 u = be32dec(f->recbuf + FIFOLOG_OFF_BS);
119 return ("Wrong record size in header (<64)");
121 if ((off_t)u >= f->logsize)
122 return ("Record size in header bigger than fifolog");
126 /* Reallocate the buffer to correct size if necessary */
127 if (u != f->recsize) {
131 f->recbuf = malloc(f->recsize);
132 if (f->recbuf == NULL)
133 return ("Cannot malloc");
136 /* Calculate number of records in fifolog */
139 return ("less than 10 records in fifolog");
141 f->logsize--; /* the label record */
143 /* Initialize zlib handling */
145 f->zs = calloc(sizeof *f->zs, 1);
147 return ("cannot malloc");
148 f->zs->zalloc = fifo_zalloc;
149 f->zs->zfree = fifo_zfree;
155 fifolog_int_open(struct fifolog_file **ff, const char *fname, int mode)
157 struct fifolog_file fs, *f;
162 memset(f, 0, sizeof *f);
164 retval = fifolog_int_open_i(f, fname, mode);
166 if (retval == NULL) {
167 *ff = malloc(sizeof *f);
169 memcpy(*ff, f, sizeof *f);
170 (*ff)->magic = FIFOLOG_FILE_MAGIC;
174 fifolog_int_close(&f);
180 fifolog_int_close(struct fifolog_file **ff)
182 struct fifolog_file *f;
193 if (f->recbuf != NULL)
198 fifolog_int_file_assert(const struct fifolog_file *ff)
201 CHECK_OBJ_NOTNULL(ff, FIFOLOG_FILE_MAGIC);
203 assert(ff->recbuf != NULL);
210 * Return zero on success
214 fifolog_int_read(const struct fifolog_file *ff, off_t recno)
218 fifolog_int_file_assert(ff);
219 if (recno >= ff->logsize)
221 recno++; /* label sector */
222 i = pread(ff->fd, ff->recbuf, ff->recsize, recno * ff->recsize);
225 if (i != (int)ff->recsize)
231 * Find the last written record in the fifolog.
233 * Return is error string or NULL on success
237 fifolog_int_findend(const struct fifolog_file *ff, off_t *last)
243 fifolog_int_file_assert(ff);
246 e = fifolog_int_read(ff, o);
248 return("Read error, first record");
250 seq0 = be32dec(ff->recbuf);
252 /* If the first records sequence is zero, the fifolog is empty */
258 /* Do a binary search for a discontinuity in the sequence numbers */
261 e = fifolog_int_read(ff, o + s);
263 return ("Read error while searching");
264 seq = be32dec(ff->recbuf);
265 if (seq == seq0 + s) {
270 assert(o < ff->logsize);