2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
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.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
45 findFreeSpace(FDB *db, size_t length) {
46 FDBFreeSpace *ptr = db->space;
48 while(ptr && ptr - db->space < db->listcur) {
49 if ( ptr->length >= length )
58 addFreeSpace(FDB *db, off_t offset, size_t length) {
59 if ( db->listcur >= db->listlen ) {
61 db->space = (FDBFreeSpace*) trealloc( db->space, db->listlen * sizeof(FDBFreeSpace) );
64 db->space[ db->listcur ].offset=offset;
65 db->space[ db->listcur ].length=length;
71 cmpFS(const void* a, const void* b) {
72 if ( ((FDBFreeSpace*)a)->offset == ((FDBFreeSpace*)b)->offset )
74 return ( ((FDBFreeSpace*)a)->offset > ((FDBFreeSpace*)b)->offset ) ? 1 : -1;
78 FDBVacuumFreeSpace(FDB *db) {
79 FDBFreeSpace *ptr=db->space+1, *ok=db->space;
81 if ( db->listcur < 2 )
84 qsort( db->space, db->listcur, sizeof(FDBFreeSpace), cmpFS);
87 while( ptr - db->space < db->listcur ) {
88 if ( ok->offset + ok->length == ptr->offset || ptr->length==0 ) {
89 ok->length += ptr->length;
96 /* remove void spaces */
98 while( ptr - db->space < db->listcur ) {
99 if ( ptr->length != 0 ) {
101 memcpy(ok,ptr,sizeof(FDBFreeSpace));
106 db->listcur = ok - db->space;
110 FDBOpen(FDB *db, char *file, int readonly) {
114 memset(db, 0, sizeof(FDB));
118 db->fd = open(file, O_RDONLY);
120 tlog(TL_CRIT,"FDBOpen: open failed: %s",strerror(errno));
123 if ( flock( db->fd, LOCK_SH ) < 0 ) {
124 tlog(TL_CRIT,"FDBOpen: flock failed: %s",strerror(errno));
129 db->fd = open(file, O_CREAT | O_RDWR, 0666);
131 tlog(TL_CRIT,"FDBOpen: open failed: %s",strerror(errno));
134 if ( flock( db->fd, LOCK_EX ) < 0 ) {
135 tlog(TL_CRIT,"FDBOpen: flock failed: %s",strerror(errno));
141 rc = read(db->fd, &header, sizeof(FDBHeader));
144 flock( db->fd, LOCK_UN );
146 tlog(TL_CRIT,"FDBOpen: read failed: %s", strerror(errno));
148 } else if ( rc==0 ) {
149 memset(&header, 0, sizeof(FDBHeader));
150 } else if ( rc != sizeof(FDBHeader) ) {
151 flock( db->fd, LOCK_UN );
153 tlog(TL_CRIT,"FDBOpen: header fault: %d bytes only", rc);
155 } else if ( header.isopened ) {
156 flock( db->fd, LOCK_UN );
158 tlog(TL_CRIT,"FDBOpen: file wasn't closed correctly");
163 if ( !db->readonly ) {
164 if ( header.freespace ) {
165 db->listlen = db->listcur = (header.lenfreespace / sizeof(FDBFreeSpace));
166 db->space = (FDBFreeSpace*)tmalloc( header.lenfreespace + sizeof(FDBFreeSpace) );
168 if ( lseek(db->fd, header.freespace, SEEK_SET)!=header.freespace ||
169 read( db->fd, db->space, header.lenfreespace ) != header.lenfreespace ) {
170 flock( db->fd, LOCK_UN );
172 tlog(TL_CRIT,"FDBOpen: free space read failed: %s", strerror(errno));
175 FDBVacuumFreeSpace(db);
178 db->space = (FDBFreeSpace*)tmalloc( db->listlen*sizeof(FDBFreeSpace) );
181 header.freespace = 0;
182 header.lenfreespace = 0;
185 if ( lseek(db->fd, 0, SEEK_SET)!=0 ||
186 write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ||
188 flock( db->fd, LOCK_UN );
190 if ( db->space ) tfree( db->space );
191 tlog(TL_CRIT,"FDBOpen: can't modify header: %s", strerror(errno));
202 if ( !db->readonly) {
205 memset(&header,0,sizeof(FDBHeader));
210 FDBVacuumFreeSpace(db);
212 header.lenfreespace = sizeof(FDBFreeSpace)*db->listcur;
213 ptr = findFreeSpace( db, header.lenfreespace );
216 header.freespace = ptr->offset;
217 if ( lseek(db->fd, ptr->offset, SEEK_SET) != ptr->offset )
218 tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
220 if ( (header.freespace = lseek(db->fd, 0, SEEK_END)) < 0 )
221 tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
222 header.lenfreespace += sizeof(FDBFreeSpace);
223 addFreeSpace(db, header.freespace, header.lenfreespace);
226 if ( write(db->fd, db->space, header.lenfreespace) != header.lenfreespace )
227 tlog(TL_CRIT|TL_EXIT,"FDBClose: write failed: %s", strerror(errno));
232 if ( lseek(db->fd,0,SEEK_SET)!=0 ||
233 write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ||
235 tlog(TL_CRIT|TL_EXIT,"FDBClose: header write failed: %s", strerror(errno));
238 flock( db->fd, LOCK_UN );
248 readLen(FDB *db, off_t offset, size_t *size) {
249 if ( lseek(db->fd,offset,SEEK_SET)!=offset)
252 if ( read(db->fd,size,sizeof(size_t)) != sizeof(size_t) )
260 FDBDelete(FDB *db, off_t offset, size_t length) {
265 if ( readLen(db, offset, &length) != FDB_OK )
268 addFreeSpace(db, offset, length);
275 FDBGet(FDB *db, off_t offset, size_t length, FDBRecord **record) {
280 if ( offset < sizeof(FDBHeader) )
284 if ( readLen(db, offset, &length) != FDB_OK )
287 *record = (FDBRecord*)tmalloc( length );
289 if ( lseek(db->fd,offset,SEEK_SET)!=offset)
292 if ( (rc=read(db->fd,*record,length)) != length ) {
293 (*record)->length = rc;
294 tlog(TL_CRIT,"FDBGet: read (%d bytes) less than needed (%d bytes): %s", rc, length, strerror(errno));
295 return FDB_INCORRECT;
298 if ( (*record)->length != length ) {
299 tlog(TL_ALARM, "FDBGet: wrong length in opts: %d bytes and %d bytes really", length, (*record)->length);
300 if ( (*record)->length > length ) {
301 rc = (*record)->length;
303 return FDBGet(db, offset, rc, record);
312 FDBPut(FDB *db, FDBRecord *record, off_t *offset ) {
318 ptr = findFreeSpace( db, record->length );
320 *offset = ptr->offset;
321 ptr->length -= record->length;
322 if ( ptr->length == 0 ) {
323 if ( (ptr - db->space) + 1 != db->listcur )
324 memmove(ptr, ptr+1, (db->listcur - (ptr - db->space) + 1) * sizeof(FDBFreeSpace));
327 ptr->offset += record->length;
328 if ( lseek(db->fd, *offset, SEEK_SET) != *offset )
329 tlog(TL_CRIT|TL_EXIT,"FDBPut: lseek failed: %s", strerror(errno));
331 if ( (*offset = lseek(db->fd, 0, SEEK_END)) < 0 )
332 tlog(TL_CRIT|TL_EXIT,"FDBPut: lseek failed: %s", strerror(errno));
335 if ( write(db->fd, record, record->length) != record->length )
336 tlog(TL_CRIT|TL_EXIT,"FDBPut: write failed: %s", strerror(errno));