diff --git a/src/database.c b/src/database.c index d9e5a3f..a0cb674 100644 --- a/src/database.c +++ b/src/database.c @@ -28,6 +28,7 @@ struct _simdb_t { int fd; /**< database file descriptor */ int flags; /**< database flags and capabilities, see SIMDB_FLAGS_* and SIMDB_CAP_* defines */ + int records; /**< database records count */ char path[PATH_MAX]; /**< path to database file */ }; @@ -128,6 +129,7 @@ simdb_open(const char *path, int mode, int *error) { db->fd = fd; db->flags = flags; + db->records = (st.st_size / SIMDB_REC_LEN) - 1; strncpy(db->path, path, sizeof(db->path)); @@ -221,6 +223,9 @@ simdb_write(simdb_t *db, int start, int records, simdb_urec_t *data) { if (records <= 0) return 0; + if ((start + records - 1) > db->records) + db->records = (start + records - 1); + return records; } @@ -252,15 +257,8 @@ simdb_record_del(simdb_t *db, int num) { int simdb_records_count(simdb_t * const db) { - struct stat st; - off_t size = 0; - - memset(&st, 0x0, sizeof(struct stat)); - - fstat(db->fd, &st); - size = st.st_size; - - return size / SIMDB_REC_LEN; + assert(db != NULL); + return db->records; } inline static float diff --git a/tests/io.c b/tests/io.c index ee64a8b..165d496 100644 --- a/tests/io.c +++ b/tests/io.c @@ -39,9 +39,15 @@ int main() { db = simdb_open(path, mode, &ret); assert(db != NULL); + ret = simdb_records_count(db); + assert(ret == 0); + ret = simdb_write(db, 1, 2, rec); assert(ret == 2); /* success */ + ret = simdb_records_count(db); + assert(ret == 2); + ret = simdb_read(db, 1, 0, &data); assert(ret == SIMDB_ERR_USAGE);