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 * Open a fifolog file or partition for reading or writing.
48 * Return value is NULL for success or a error description string to
49 * be augmented by errno if non-zero.
51 * The second function is just an error-handling wrapper around the
52 * first which, does the actual work.
56 fifolog_int_open_i(struct fifolog_file *f, const char *fname, int mode)
62 f->fd = open(fname, mode ? O_RDWR : O_RDONLY);
64 return ("Cannot open");
66 /* Determine initial record size guesstimate */
67 i = ioctl(f->fd, DIOCGSECTORSIZE, &f->recsize);
68 if (i != 0 && errno != ENOTTY)
69 return ("ioctl(DIOCGSECTORSIZE) failed");
72 i = fstat(f->fd, &st);
74 if (!S_ISREG(st.st_mode))
75 return ("Neither disk nor regular file");
77 f->logsize = st.st_size;
78 } else if (f->recsize < 64) {
79 return ("Disk device sectorsize smaller than 64");
81 i = ioctl(f->fd, DIOCGMEDIASIZE, &f->logsize);
82 if (i < 0 && errno != ENOTTY)
83 return ("ioctl(DIOCGMEDIASIZE) failed");
86 /* Allocate a record buffer */
87 f->recbuf = malloc(f->recsize);
88 if (f->recbuf == NULL)
89 return ("Cannot malloc");
91 /* Read and validate the label sector */
92 i = pread(f->fd, f->recbuf, f->recsize, 0);
93 if (i < 0 || i < (int)f->recsize)
94 return ("Read error, first sector");
97 if (memcmp(f->recbuf, FIFOLOG_FMT_MAGIC, strlen(FIFOLOG_FMT_MAGIC) + 1))
98 return ("Wrong or missing magic string");
100 u = be32dec(f->recbuf + FIFOLOG_OFF_BS);
102 return ("Wrong record size in header (<64)");
104 if ((off_t)u >= f->logsize)
105 return ("Record size in header bigger than fifolog");
109 /* Reallocate the buffer to correct size if necessary */
110 if (u != f->recsize) {
114 f->recbuf = malloc(f->recsize);
115 if (f->recbuf == NULL)
116 return ("Cannot malloc");
119 /* Calculate number of records in fifolog */
122 return ("less than 10 records in fifolog");
124 f->logsize--; /* the label record */
126 /* Initialize zlib handling */
128 f->zs = calloc(sizeof *f->zs, 1);
130 return ("cannot malloc");
136 fifolog_int_open(struct fifolog_file **ff, const char *fname, int mode)
138 struct fifolog_file fs, *f;
143 memset(f, 0, sizeof *f);
145 retval = fifolog_int_open_i(f, fname, mode);
147 if (retval == NULL) {
148 *ff = malloc(sizeof *f);
150 memcpy(*ff, f, sizeof *f);
151 (*ff)->magic = FIFOLOG_FILE_MAGIC;
155 fifolog_int_close(&f);
161 fifolog_int_close(struct fifolog_file **ff)
163 struct fifolog_file *f;
174 if (f->recbuf != NULL)
179 fifolog_int_file_assert(const struct fifolog_file *ff)
182 CHECK_OBJ_NOTNULL(ff, FIFOLOG_FILE_MAGIC);
184 assert(ff->recbuf != NULL);
191 * Return zero on success
195 fifolog_int_read(const struct fifolog_file *ff, off_t recno)
199 fifolog_int_file_assert(ff);
200 if (recno >= ff->logsize)
202 recno++; /* label sector */
203 i = pread(ff->fd, ff->recbuf, ff->recsize, recno * ff->recsize);
206 if (i != (int)ff->recsize)
212 * Find the last written record in the fifolog.
214 * Return is error string or NULL on success
218 fifolog_int_findend(const struct fifolog_file *ff, off_t *last)
224 fifolog_int_file_assert(ff);
227 e = fifolog_int_read(ff, o);
229 return("Read error, first record");
231 seq0 = be32dec(ff->recbuf);
233 /* If the first records sequence is zero, the fifolog is empty */
239 /* Do a binary search for a discontinuity in the sequence numbers */
242 e = fifolog_int_read(ff, o + s);
244 return ("Read error while searching");
245 seq = be32dec(ff->recbuf);
246 if (seq == seq0 + s) {
251 assert(o < ff->logsize);