2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2008 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/endian.h>
45 #include "libfifolog_int.h"
48 * Open a fifolog file or partition for reading or writing.
50 * Return value is NULL for success or a error description string to
51 * be augmented by errno if non-zero.
53 * The second function is just an error-handling wrapper around the
54 * first which, does the actual work.
58 fifolog_int_open_i(struct fifolog_file *f, const char *fname, int mode)
64 f->fd = open(fname, mode ? O_RDWR : O_RDONLY);
66 return ("Cannot open");
68 /* Determine initial record size guesstimate */
69 i = ioctl(f->fd, DIOCGSECTORSIZE, &f->recsize);
70 if (i != 0 && errno != ENOTTY)
71 return ("ioctl(DIOCGSECTORSIZE) failed");
74 i = fstat(f->fd, &st);
76 if (!S_ISREG(st.st_mode))
77 return ("Neither disk nor regular file");
79 f->logsize = st.st_size;
80 } else if (f->recsize < 64) {
81 return ("Disk device sectorsize smaller than 64");
83 i = ioctl(f->fd, DIOCGMEDIASIZE, &f->logsize);
84 if (i < 0 && errno != ENOTTY)
85 return ("ioctl(DIOCGMEDIASIZE) failed");
88 /* Allocate a record buffer */
89 f->recbuf = malloc(f->recsize);
90 if (f->recbuf == NULL)
91 return ("Cannot malloc");
93 /* Read and validate the label sector */
94 i = pread(f->fd, f->recbuf, f->recsize, 0);
95 if (i < 0 || i < (int)f->recsize)
96 return ("Read error, first sector");
99 if (memcmp(f->recbuf, FIFOLOG_FMT_MAGIC, strlen(FIFOLOG_FMT_MAGIC) + 1))
100 return ("Wrong or missing magic string");
102 u = be32dec(f->recbuf + FIFOLOG_OFF_BS);
104 return ("Wrong record size in header (<64)");
106 if ((off_t)u >= f->logsize)
107 return ("Record size in header bigger than fifolog");
111 /* Reallocate the buffer to correct size if necessary */
112 if (u != f->recsize) {
116 f->recbuf = malloc(f->recsize);
117 if (f->recbuf == NULL)
118 return ("Cannot malloc");
121 /* Calculate number of records in fifolog */
124 return ("less than 10 records in fifolog");
126 f->logsize--; /* the label record */
128 /* Initialize zlib handling */
130 f->zs = calloc(1, sizeof(*f->zs));
132 return ("cannot malloc");
138 fifolog_int_open(struct fifolog_file **ff, const char *fname, int mode)
140 struct fifolog_file fs, *f;
145 memset(f, 0, sizeof *f);
147 retval = fifolog_int_open_i(f, fname, mode);
149 if (retval == NULL) {
150 *ff = malloc(sizeof *f);
152 memcpy(*ff, f, sizeof *f);
153 (*ff)->magic = FIFOLOG_FILE_MAGIC;
157 fifolog_int_close(&f);
163 fifolog_int_close(struct fifolog_file **ff)
165 struct fifolog_file *f;
176 if (f->recbuf != NULL)
181 fifolog_int_file_assert(const struct fifolog_file *ff)
184 CHECK_OBJ_NOTNULL(ff, FIFOLOG_FILE_MAGIC);
186 assert(ff->recbuf != NULL);
193 * Return zero on success
197 fifolog_int_read(const struct fifolog_file *ff, off_t recno)
201 fifolog_int_file_assert(ff);
202 if (recno >= ff->logsize)
204 recno++; /* label sector */
205 i = pread(ff->fd, ff->recbuf, ff->recsize, recno * ff->recsize);
208 if (i != (int)ff->recsize)
214 * Find the last written record in the fifolog.
216 * Return is error string or NULL on success
220 fifolog_int_findend(const struct fifolog_file *ff, off_t *last)
226 fifolog_int_file_assert(ff);
229 e = fifolog_int_read(ff, o);
231 return("Read error, first record");
233 seq0 = be32dec(ff->recbuf);
235 /* If the first records sequence is zero, the fifolog is empty */
241 /* Do a binary search for a discontinuity in the sequence numbers */
244 e = fifolog_int_read(ff, o + s);
246 return ("Read error while searching");
247 seq = be32dec(ff->recbuf);
248 if (seq == seq0 + s) {
253 assert(o < ff->logsize);