]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Cleanup writing pool
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Dec 2017 13:08:34 +0000 (13:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Dec 2017 13:08:34 +0000 (13:08 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/stringpool.c

index 494e2f762f6bb3e01dce04753c38ab98bdad58d5..928ed8569f4c65634baf2b7de326a0a3f060eec7 100644 (file)
@@ -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)
index 259cd00173620a1436931fb9112e8e0838b9e56d..52c1012f88a054cfa7d8c3df2a9a93b25683bc87 100644 (file)
@@ -23,6 +23,7 @@
 #include <unistd.h>
 
 #include <loc/libloc.h>
+#include <loc/format.h>
 #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;
 }