From 8f70336c47a53ced3d5a9a340c83df057acb7d79 Mon Sep 17 00:00:00 2001 From: teodor Date: Mon, 4 Oct 2004 19:59:37 +0000 Subject: [PATCH 1/1] cleanup code, remove possible freespace leak on close --- flatdb.c | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/flatdb.c b/flatdb.c index d633744..b591041 100644 --- a/flatdb.c +++ b/flatdb.c @@ -51,6 +51,19 @@ findFreeSpace(FDB *db, size_t length) { 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 ) @@ -131,6 +144,7 @@ FDBOpen(FDB *db, char *file, int readonly) { 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) ); @@ -140,7 +154,9 @@ FDBOpen(FDB *db, char *file, int readonly) { 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)); @@ -163,6 +179,7 @@ FDBClose(FDB *db) { FDBFreeSpace *ptr; FDBVacuumFreeSpace(db); + header.lenfreespace = sizeof(FDBFreeSpace)*db->listcur; ptr = findFreeSpace( db, header.lenfreespace ); @@ -172,7 +189,9 @@ FDBClose(FDB *db) { 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 ) @@ -181,7 +200,9 @@ FDBClose(FDB *db) { 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)); } @@ -204,6 +225,7 @@ readLen(FDB *db, off_t offset, size_t *size) { return FDB_OK; } + int FDBDelete(FDB *db, off_t offset, size_t length) { if ( db->readonly ) @@ -213,15 +235,7 @@ FDBDelete(FDB *db, off_t offset, size_t length) { 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; } @@ -286,7 +300,7 @@ FDBPut(FDB *db, FDBRecord *record, off_t *offset ) { } 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; -- 2.46.1