]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Add function to align to page boundaries
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Dec 2017 13:16:54 +0000 (13:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 12 Dec 2017 13:16:54 +0000 (13:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/stringpool.c

index d4f27e02368b95198fc8f83d0ac5910c91f7a623..a263d0270dba16d422ea28e85d52fa49f800847f 100644 (file)
@@ -346,6 +346,12 @@ static void loc_database_make_magic(struct loc_database* db, struct loc_database
        magic->version = htons(LOC_DATABASE_VERSION);
 }
 
        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);
 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);
 
        }
        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;
 
 
        // 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)
        // Write all ASes
        r = loc_database_write_as_section(db, &header, &offset, f);
        if (r)
index 52c1012f88a054cfa7d8c3df2a9a93b25683bc87..27388cc12551bfd4231a095092b1beb09730fec2 100644 (file)
@@ -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);
 
 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);
 }
 }