return NULL;
}
+static void
+addFreeSpace(FDB *db, off_t offset, size_t length) {
+ if ( db->listcur >= db->listlen ) {
+ db->listlen *= 2;
+ db->space = (FDBFreeSpace*) trealloc( db->space, db->listlen * sizeof(FDBFreeSpace) );
+ }
+
+ db->space[ db->listcur ].offset=offset;
+ db->space[ db->listcur ].length=length;
+
+ db->listcur++;
+}
+
static int
cmpFS(const void* a, const void* b) {
if ( ((FDBFreeSpace*)a)->offset == ((FDBFreeSpace*)b)->offset )
tlog(TL_CRIT,"FDBOpen: free space read failed: %s", strerror(errno));
return FDB_ERROR;
}
+ FDBVacuumFreeSpace(db);
} else {
db->listlen = 8;
db->space = (FDBFreeSpace*)tmalloc( db->listlen*sizeof(FDBFreeSpace) );
header.lenfreespace = 0;
header.isopened = 1;
- if ( lseek(db->fd, 0, SEEK_SET)!=0 || write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ) {
+ if ( lseek(db->fd, 0, SEEK_SET)!=0 ||
+ write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ||
+ fsync(db->fd) ) {
close(db->fd);
if ( db->space ) tfree( db->space );
tlog(TL_CRIT,"FDBOpen: can't modify header: %s", strerror(errno));
FDBFreeSpace *ptr;
FDBVacuumFreeSpace(db);
+
header.lenfreespace = sizeof(FDBFreeSpace)*db->listcur;
ptr = findFreeSpace( db, header.lenfreespace );
tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
} else {
if ( (header.freespace = lseek(db->fd, 0, SEEK_END)) < 0 )
- tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
+ tlog(TL_CRIT|TL_EXIT,"FDBClose: lseek failed: %s", strerror(errno));
+ header.lenfreespace += sizeof(FDBFreeSpace);
+ addFreeSpace(db, header.freespace, header.lenfreespace);
}
if ( write(db->fd, db->space, header.lenfreespace) != header.lenfreespace )
header.isopened=0;
- if ( lseek(db->fd,0,SEEK_SET)!=0 || write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) )
+ if ( lseek(db->fd,0,SEEK_SET)!=0 ||
+ write(db->fd, &header, sizeof(FDBHeader)) != sizeof(FDBHeader) ||
+ fsync(db->fd))
tlog(TL_CRIT|TL_EXIT,"FDBClose: header write failed: %s", strerror(errno));
}
return FDB_OK;
}
+
int
FDBDelete(FDB *db, off_t offset, size_t length) {
if ( db->readonly )
if ( readLen(db, offset, &length) != FDB_OK )
return FDB_ERROR;
- if ( db->listcur >= db->listlen ) {
- db->listlen *= 2;
- db->space = (FDBFreeSpace*) trealloc( db->space, db->listlen * sizeof(FDBFreeSpace) );
- }
-
- db->space[ db->listcur ].offset=offset;
- db->space[ db->listcur ].length=length;
-
- db->listcur++;
+ addFreeSpace(db, offset, length);
return FDB_OK;
}
}
if ( write(db->fd, record, record->length) != record->length )
- tlog(TL_CRIT|TL_EXIT,"FDBPut: write lseek failed: %s", strerror(errno));
+ tlog(TL_CRIT|TL_EXIT,"FDBPut: write failed: %s", strerror(errno));
return FDB_OK;