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.
42 findFreeSpace(FDB *db, size_t length) {
43 FDBFreeSpace *ptr = db->space;
45 while(ptr && ptr - db->space < db->listcur) {
46 if ( ptr->length >= length )
55 cmpFS(const void* a, const void* b) {
56 if ( ((FDBFreeSpace*)a)->offset == ((FDBFreeSpace*)b)->offset )
58 return ( ((FDBFreeSpace*)a)->offset > ((FDBFreeSpace*)b)->offset ) ? 1 : -1;
62 FDBVacuumFreeSpace(FDB *db) {
63 FDBFreeSpace *ptr=db->space+1, *ok=db->space;
64 if ( db->listcur < 2 )
67 qsort( db->space, db->listcur, sizeof(FDBFreeSpace), cmpFS);
69 while( ptr - db->space < db->listcur ) {
70 tassert( ok->offset + ok->length <= ptr->offset );
71 if ( ok->offset + ok->length == ptr->offset || ptr->length==0 ) {
72 ok->length += ptr->length;
76 memcpy(ok, ptr, sizeof(FDBFreeSpace));
81 db->listcur = ok - db->space + 1;
85 FDBOpen(FDB *db, char *file, int readonly) {
89 memset(db, 0, sizeof(FDB));
93 db->fd = open(file, O_RDONLY | O_SHLOCK);
95 db->fd = open(file, O_CREAT | O_RDWR | O_EXLOCK, 0666);
99 memset(db, 0, sizeof(FDB));
100 tlog(TL_CRIT,"FDBOpen: open failed: %s", strerror(errno));
104 rc = read(db->fd, &header, sizeof(FDBHeader));
108 tlog(TL_CRIT,"FDBOpen: read failed: %s", strerror(errno));
110 } else if ( rc==0 ) {
111 memset(&header, 0, sizeof(FDBHeader));
112 } else if ( rc != sizeof(FDBHeader) ) {
114 tlog(TL_CRIT,"FDBOpen: header fault: %d bytes only", rc);
116 } else if ( header.isopened ) {
118 tlog(TL_CRIT,"FDBOpen: file wasn't closed correctly");
123 if ( !db->readonly ) {
124 if ( header.freespace ) {
125 db->listlen = db->listcur = (header.lenfreespace / sizeof(FDBFreeSpace));
126 db->space = (FDBFreeSpace*)tmalloc( header.lenfreespace + sizeof(FDBFreeSpace) );
128 if ( lseek(db->fd, header.freespace, SEEK_SET)!=header.freespace ||
129 read( db->fd, db->space, header.lenfreespace ) != header.lenfreespace ) {
131 tlog(TL_CRIT,"FDBOpen: free space read failed: %s", strerror(errno));
136 db->space = (FDBFreeSpace*)tmalloc( db->listlen*sizeof(FDBFreeSpace) );
139 header.freespace = 0;
140 header.lenfreespace = 0;
143 if ( lseek(db->fd, 0, SEEK_SET)!=0 || write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ) {
145 if ( db->space ) tfree( db->space );
146 tlog(TL_CRIT,"FDBOpen: can't modify header: %s", strerror(errno));
157 if ( !db->readonly) {
160 memset(&header,0,sizeof(FDBHeader));
165 FDBVacuumFreeSpace(db);
166 header.lenfreespace = sizeof(FDBFreeSpace)*db->listcur;
167 ptr = findFreeSpace( db, header.lenfreespace );
170 header.freespace = ptr->offset;
171 if ( lseek(db->fd, ptr->offset, SEEK_SET) != ptr->offset )
172 tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
174 if ( (header.freespace = lseek(db->fd, 0, SEEK_END)) < 0 )
175 tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
178 if ( write(db->fd, db->space, header.lenfreespace) != header.lenfreespace )
179 tlog(TL_CRIT|TL_EXIT,"FDBClose: write failed: %s", strerror(errno));
184 if ( lseek(db->fd,0,SEEK_SET)!=0 || write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) )
185 tlog(TL_CRIT|TL_EXIT,"FDBClose: header write failed: %s", strerror(errno));
197 readLen(FDB *db, off_t offset, size_t *size) {
198 if ( lseek(db->fd,offset,SEEK_SET)!=offset)
201 if ( read(db->fd,size,sizeof(size_t)) != sizeof(size_t) )
208 FDBDelete(FDB *db, off_t offset, size_t length) {
213 if ( readLen(db, offset, &length) != FDB_OK )
216 if ( db->listcur >= db->listlen ) {
218 db->space = (FDBFreeSpace*) trealloc( db->space, db->listlen * sizeof(FDBFreeSpace) );
221 db->space[ db->listcur ].offset=offset;
222 db->space[ db->listcur ].length=length;
231 FDBGet(FDB *db, off_t offset, size_t length, FDBRecord **record) {
240 if ( readLen(db, offset, &length) != FDB_OK )
243 *record = (FDBRecord*)tmalloc( length );
245 if ( lseek(db->fd,offset,SEEK_SET)!=offset)
248 if ( (rc=read(db->fd,*record,length)) != length ) {
249 (*record)->length = rc;
250 tlog(TL_CRIT,"FDBGet: read (%d bytes) less than needed (%d bytes): %s", rc, length, strerror(errno));
251 return FDB_INCORRECT;
254 if ( (*record)->length != length ) {
255 tlog(TL_ALARM, "FDBGet: wrong length in opts: %d bytes and %d bytes really", length, (*record)->length);
256 if ( (*record)->length > length ) {
257 rc = (*record)->length;
259 return FDBGet(db, offset, rc, record);
268 FDBPut(FDB *db, FDBRecord *record, off_t *offset ) {
274 ptr = findFreeSpace( db, record->length );
276 *offset = ptr->offset;
277 ptr->offset += record->length;
278 ptr->length -= record->length;
279 if ( ptr->length == 0 )
280 FDBVacuumFreeSpace(db);
281 if ( lseek(db->fd, *offset, SEEK_SET) != *offset )
282 tlog(TL_CRIT|TL_EXIT,"FDBPut: lseek failed: %s", strerror(errno));
284 if ( (*offset = lseek(db->fd, 0, SEEK_END)) < 0 )
285 tlog(TL_CRIT|TL_EXIT,"FDBPut: lseek failed: %s", strerror(errno));
288 if ( write(db->fd, record, record->length) != record->length )
289 tlog(TL_CRIT|TL_EXIT,"FDBPut: write lseek failed: %s", strerror(errno));