]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Refactor functions for reading/writing common header
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 29 Jun 2019 18:37:08 +0000 (20:37 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 1 Jul 2019 19:30:02 +0000 (21:30 +0200)
The functions now work on bytes instead of FILEs, allowing for the
caller to optionally peek at read/written bytes.

src/common_header.c
src/common_header.h
src/manifest.c
src/result.c

index ebfb183980f880a6de08ef3e7bd4effe61f451e8..50e998978655b6043e0a09f4794cbb6a76fd72a1 100644 (file)
@@ -19,7 +19,7 @@
 #include "compression.h"
 #include "common_header.h"
 
-void common_header_init_from_config(
+void common_header_from_config(
        struct common_header *header,
        const char magic[4],
        uint8_t RESULT_VERSION,
@@ -35,29 +35,24 @@ void common_header_init_from_config(
        header->content_size = content_size;
 }
 
-bool common_header_init_from_file(struct common_header *header, FILE *f)
+void
+common_header_from_bytes(struct common_header *header, uint8_t *buffer)
 {
-       char buffer[COMMON_HEADER_SIZE];
-       if (fread(buffer, 1, sizeof(buffer), f) != sizeof(buffer)) {
-               return false;
-       }
        memcpy(header->magic, buffer, 4);
        header->version = buffer[4];
        header->compression_type = buffer[5];
        header->compression_level = buffer[6];
        header->content_size = UINT64_FROM_BYTES(buffer + 7);
-       return true;
 }
 
-bool common_header_write_to_file(const struct common_header *header, FILE *f)
+void
+common_header_to_bytes(const struct common_header *header, uint8_t *buffer)
 {
-       char buffer[COMMON_HEADER_SIZE];
        memcpy(buffer, header->magic, 4);
        buffer[4] = header->version;
        buffer[5] = header->compression_type;
        buffer[6] = header->compression_level;
        BYTES_FROM_UINT64(buffer + 7, header->content_size);
-       return fwrite(buffer, 1, sizeof(buffer), f) == sizeof(buffer);
 }
 
 bool common_header_verify(
index def2ee20904f1c94757f45527792007c719387ca..f25e3a1b5ca63d0369ca5dd3fe683819c1ba4ee0 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef COMMON_HEADER_H
+#define COMMON_HEADER_H
+
 #define COMMON_HEADER_SIZE 15
 
 struct common_header {
@@ -8,13 +11,16 @@ struct common_header {
        uint64_t content_size;
 };
 
-void common_header_init_from_config(
+void common_header_from_config(
        struct common_header *header,
        const char magic[4],
        uint8_t RESULT_VERSION,
        uint64_t content_size);
-bool common_header_init_from_file(struct common_header *header, FILE *f);
-bool common_header_write_to_file(const struct common_header *header, FILE *f);
+void common_header_from_bytes(struct common_header *header, uint8_t *bytes);
+void common_header_to_bytes(
+       const struct common_header *header, uint8_t *bytes);
 bool common_header_verify(
        const struct common_header *header, int fd, const char *name, char **errmsg);
 void common_header_dump(const struct common_header *header, FILE *f);
+
+#endif
index 6dddba5b099b25a14cb734a502d4424babb6ab2d..171745ce5695c5048120cad004b56956a70e52aa 100644 (file)
@@ -252,11 +252,14 @@ read_manifest(const char *path, char **errmsg)
                goto out;
        }
 
-       if (!common_header_init_from_file(&mf->header, f)) {
+       uint8_t header_bytes[COMMON_HEADER_SIZE];
+       if (fread(header_bytes, sizeof(header_bytes), 1, f) != 1) {
                *errmsg = format("Failed to read header from %s", path);
                goto out;
        }
 
+       common_header_from_bytes(&mf->header, header_bytes);
+
        if (memcmp(mf->header.magic, MAGIC, sizeof(MAGIC)) != 0) {
                *errmsg = format(
                        "Result file has bad magic value 0x%x%x%x%x",
@@ -393,9 +396,10 @@ write_manifest(FILE *f, const struct manifest *mf)
        }
 
        struct common_header header;
-       common_header_init_from_config(
-               &header, MAGIC, MANIFEST_VERSION, content_size);
-       if (!common_header_write_to_file(&header, f)) {
+       common_header_from_config(&header, MAGIC, MANIFEST_VERSION, content_size);
+       uint8_t header_bytes[COMMON_HEADER_SIZE];
+       common_header_to_bytes(&header, header_bytes);
+       if (fwrite(header_bytes, sizeof(header_bytes), 1, f) != 1) {
                goto error;
        }
 
index adc5639ee271de2b164b0d587ddccd2dd694f4fa..8ad039faac82ace5f7ff49022e097cadd770d790 100644 (file)
@@ -163,12 +163,15 @@ read_result(
                goto out;
        }
 
-       struct common_header header;
-       if (!common_header_init_from_file(&header, f)) {
+       uint8_t header_bytes[COMMON_HEADER_SIZE];
+       if (fread(header_bytes, sizeof(header_bytes), 1, f) != 1) {
                *errmsg = format("Failed to read header from %s", path);
                goto out;
        }
 
+       struct common_header header;
+       common_header_from_bytes(&header, header_bytes);
+
        if (memcmp(header.magic, MAGIC, sizeof(MAGIC)) != 0) {
                *errmsg = format(
                        "Result file has bad magic value 0x%x%x%x%x",
@@ -404,8 +407,11 @@ bool result_put(const char *path, struct result_files *list)
        }
 
        struct common_header header;
-       common_header_init_from_config(&header, MAGIC, RESULT_VERSION, content_size);
-       if (!common_header_write_to_file(&header, f)) {
+       common_header_from_config(&header, MAGIC, RESULT_VERSION, content_size);
+
+       uint8_t header_bytes[COMMON_HEADER_SIZE];
+       common_header_to_bytes(&header, header_bytes);
+       if (fwrite(header_bytes, sizeof(header_bytes), 1, f) != 1) {
                cc_log("Failed to write result file header to %s", tmp_file);
                goto out;
        }