From c34e76f14a91161908d61e03b4a8e6734a8051ec Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 12 Dec 2017 13:08:34 +0000 Subject: [PATCH] database: Cleanup writing pool Signed-off-by: Michael Tremer --- src/database.c | 37 ++++++++++++++++++++++++------------- src/stringpool.c | 9 ++++++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/database.c b/src/database.c index 494e2f7..928ed85 100644 --- a/src/database.c +++ b/src/database.c @@ -346,6 +346,21 @@ static void loc_database_make_magic(struct loc_database* db, struct loc_database magic->version = htons(LOC_DATABASE_VERSION); } +static int loc_database_write_pool(struct loc_database* db, struct loc_database_header_v0* header, off_t* offset, FILE* f) { + // Save the offset of the pool section + DEBUG(db->ctx, "Pool starts at %jd bytes\n", *offset); + header->pool_offset = htonl(*offset); + + // Write the pool + size_t pool_length = loc_stringpool_write(db->pool, f); + *offset += pool_length; + + DEBUG(db->ctx, "Pool has a length of %zu bytes\n", pool_length); + header->pool_length = htonl(pool_length); + + return 0; +} + LOC_EXPORT int loc_database_write(struct loc_database* db, FILE* f) { struct loc_database_magic magic; loc_database_make_magic(db, &magic); @@ -374,6 +389,15 @@ LOC_EXPORT int loc_database_write(struct loc_database* db, FILE* f) { } offset += sizeof(header); + // Move to next page boundary + while (offset % LOC_DATABASE_PAGE_SIZE > 0) + offset += fwrite("", 1, 1, f); + + // Write pool + r = loc_database_write_pool(db, &header, &offset, f); + if (r) + return r; + // Write all ASes header.as_offset = htonl(offset); @@ -387,19 +411,6 @@ LOC_EXPORT int loc_database_write(struct loc_database* db, FILE* f) { } header.as_length = htonl(db->as_count * sizeof(dbas)); - // Move to next page boundary - while (offset % LOC_DATABASE_PAGE_SIZE > 0) - offset += fwrite("", 1, 1, f); - - // Save the offset of the pool section - DEBUG(db->ctx, "Pool starts at %jd bytes\n", offset); - header.pool_offset = htonl(offset); - - // Size of the pool - size_t pool_length = loc_stringpool_write(db->pool, f); - DEBUG(db->ctx, "Pool has a length of %zu bytes\n", pool_length); - header.pool_length = htonl(pool_length); - // Write the header r = fseek(f, sizeof(magic), SEEK_SET); if (r) diff --git a/src/stringpool.c b/src/stringpool.c index 259cd00..52c1012 100644 --- a/src/stringpool.c +++ b/src/stringpool.c @@ -23,6 +23,7 @@ #include #include +#include #include "libloc-private.h" #include "stringpool.h" @@ -251,5 +252,11 @@ LOC_EXPORT int loc_stringpool_read(struct loc_stringpool* pool, FILE* f, off_t o LOC_EXPORT size_t loc_stringpool_write(struct loc_stringpool* pool, FILE* f) { size_t size = loc_stringpool_get_size(pool); - return fwrite(pool->data, sizeof(*pool->data), size, f); + size_t bytes_written = fwrite(pool->data, sizeof(*pool->data), size, f); + + // Move to next page boundary + while (bytes_written % LOC_DATABASE_PAGE_SIZE > 0) + bytes_written += fwrite("", 1, 1, f); + + return bytes_written; } -- 2.39.2