From: Michael Tremer Date: Tue, 12 Dec 2017 13:16:54 +0000 (+0000) Subject: database: Add function to align to page boundaries X-Git-Tag: 0.9.0~172 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=8f5b676af42606975d5473c17bd8474527566b16;hp=78ace4ed4d7cbc6eddd8f59554905adf75644b4e;ds=sidebyside database: Add function to align to page boundaries Signed-off-by: Michael Tremer --- diff --git a/src/database.c b/src/database.c index d4f27e0..a263d02 100644 --- a/src/database.c +++ b/src/database.c @@ -346,6 +346,12 @@ static void loc_database_make_magic(struct loc_database* db, struct loc_database magic->version = htons(LOC_DATABASE_VERSION); } +static void loc_database_align_page_boundary(off_t* offset, FILE* f) { + // Move to next page boundary + while (*offset % LOC_DATABASE_PAGE_SIZE > 0) + *offset += fwrite("", 1, 1, f); +} + 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); @@ -412,15 +418,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); + loc_database_align_page_boundary(&offset, f); // Write pool r = loc_database_write_pool(db, &header, &offset, f); if (r) return r; + loc_database_align_page_boundary(&offset, f); + // Write all ASes r = loc_database_write_as_section(db, &header, &offset, f); if (r) diff --git a/src/stringpool.c b/src/stringpool.c index 52c1012..27388cc 100644 --- a/src/stringpool.c +++ b/src/stringpool.c @@ -252,11 +252,5 @@ 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); - 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; + return fwrite(pool->data, sizeof(*pool->data), size, f); }