]> git.ipfire.org Git - location/debian/libloc.git/blobdiff - src/writer.c
Update upstream source from tag 'upstream/0.9.15'
[location/debian/libloc.git] / src / writer.c
index 4420972975b7df36e79244bf464db3c9f07e80b4..51e9a8ec2a60957037daba637cf41f0c65428ba5 100644 (file)
@@ -91,7 +91,7 @@ LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer,
                FILE* fkey1, FILE* fkey2) {
        struct loc_writer* w = calloc(1, sizeof(*w));
        if (!w)
-               return -ENOMEM;
+               return 1;
 
        w->ctx = loc_ref(ctx);
        w->refcount = 1;
@@ -172,7 +172,8 @@ static void loc_writer_free(struct loc_writer* writer) {
                loc_network_tree_unref(writer->networks);
 
        // Unref the string pool
-       loc_stringpool_unref(writer->pool);
+       if (writer->pool)
+               loc_stringpool_unref(writer->pool);
 
        loc_unref(writer->ctx);
        free(writer);
@@ -537,6 +538,8 @@ static int loc_database_write_countries(struct loc_writer* writer,
 static int loc_writer_create_signature(struct loc_writer* writer,
                struct loc_database_header_v1* header, FILE* f, EVP_PKEY* private_key,
                char* signature, size_t* length) {
+       size_t bytes_read = 0;
+
        DEBUG(writer->ctx, "Creating signature...\n");
 
        // Read file from the beginning
@@ -554,7 +557,12 @@ static int loc_writer_create_signature(struct loc_writer* writer,
 
        // Read magic
        struct loc_database_magic magic;
-       fread(&magic, 1, sizeof(magic), f);
+       bytes_read = fread(&magic, 1, sizeof(magic), f);
+       if (bytes_read < sizeof(magic)) {
+               ERROR(writer->ctx, "Could not read header: %m\n");
+               r = 1;
+               goto END;
+       }
 
        hexdump(writer->ctx, &magic, sizeof(magic));
 
@@ -580,11 +588,11 @@ static int loc_writer_create_signature(struct loc_writer* writer,
        // Walk through the file in chunks of 64kB
        char buffer[64 * 1024];
        while (!feof(f)) {
-               size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
+               bytes_read = fread(buffer, 1, sizeof(buffer), f);
 
                if (ferror(f)) {
-                       ERROR(writer->ctx, "Error reading from file: %s\n", strerror(errno));
-                       r = errno;
+                       ERROR(writer->ctx, "Error reading from file: %m\n");
+                       r = 1;
                        goto END;
                }
 
@@ -745,5 +753,8 @@ LOC_EXPORT int loc_writer_write(struct loc_writer* writer, FILE* f, enum loc_dat
 
        fwrite(&header, 1, sizeof(header), f);
 
+       // Flush everything
+       fflush(f);
+
        return r;
 }