]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Refactor common_header_initialize_*
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 3 Jul 2019 12:36:01 +0000 (14:36 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 3 Jul 2019 12:36:01 +0000 (14:36 +0200)
In preparation for adding cache compression statistics and cache
recompression functionality.

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

index d272e4c5d4f1e7cce667ef5256ef350bbf0db2ed..69fde2188860211955627314e99d3c2ad435b805 100644 (file)
 bool
 common_header_initialize_for_writing(
        struct common_header *header,
+       FILE *output,
        const char magic[4],
        uint8_t version,
+       uint8_t compression_type,
+       int8_t compression_level,
        uint64_t content_size,
        XXH64_state_t *checksum,
        struct compressor **compressor,
-       struct compr_state **compr_state,
-       FILE *output)
+       struct compr_state **compr_state)
 {
-       enum compression_type compr_type = compression_type_from_config();
-       int8_t compr_level = compression_level_from_config();
-
        memcpy(header->magic, magic, 4);
        header->version = version;
-       header->compression_type = compr_type;
-       header->compression_level = compr_level;
+       header->compression_type = compression_type;
+       header->compression_level = compression_level;
        header->content_size = content_size;
 
        XXH64_reset(checksum, 0);
@@ -68,8 +67,8 @@ common_header_initialize_for_writing(
 bool common_header_initialize_for_reading(
        struct common_header *header,
        FILE *input,
-       const char magic[4],
-       uint8_t accepted_version,
+       const char expected_magic[4],
+       uint8_t expected_version,
        struct decompressor **decompressor,
        struct decompr_state **decompr_state,
        XXH64_state_t *checksum,
@@ -87,7 +86,7 @@ bool common_header_initialize_for_reading(
        header->compression_level = header_bytes[6];
        header->content_size = UINT64_FROM_BYTES(header_bytes + 7);
 
-       if (memcmp(header->magic, magic, sizeof(header->magic)) != 0) {
+       if (memcmp(header->magic, expected_magic, sizeof(header->magic)) != 0) {
                *errmsg = format(
                        "Bad magic value 0x%x%x%x%x",
                        header->magic[0],
@@ -97,11 +96,11 @@ bool common_header_initialize_for_reading(
                return false;
        }
 
-       if (header->version != accepted_version) {
+       if (header->version != expected_version) {
                *errmsg = format(
                        "Unknown version (actual %u, expected %u)",
                        header->version,
-                       accepted_version);
+                       expected_version);
                return false;
        }
 
@@ -120,6 +119,10 @@ bool common_header_initialize_for_reading(
                }
        }
 
+       if (!decompressor) {
+               return true;
+       }
+
        *decompressor = decompressor_from_type(header->compression_type);
        if (!*decompressor) {
                *errmsg = format(
index 598d8ec1602d289e78d2cf3fc13655d5f2ecf71f..b2daa50997fc81af735d8387855d72928d86949e 100644 (file)
@@ -14,21 +14,48 @@ struct common_header {
        uint64_t content_size;
 };
 
+// Initialize a common_header and write the header data to a file.
+//
+// header:            Header to initialize.
+// output:            Open file to write to.
+// magic:             File format magic bytes.
+// version:           File format version.
+// compression_type:  Compression type to use.
+// compression_level: Compression level to use.
+// content_size:      Content size.
+// compressor:        Compressor created from compression_type.
+// compressor_state:  State for the compressor.
+// checksum:          Checksum state that will be updated with the written
+//                    bytes.
 bool common_header_initialize_for_writing(
        struct common_header *header,
+       FILE *output,
        const char magic[4],
        uint8_t version,
+       uint8_t compression_type,
+       int8_t compression_level,
        uint64_t content_size,
        XXH64_state_t *checksum,
        struct compressor **compressor,
-       struct compr_state **compr_state,
-       FILE *output);
+       struct compr_state **compr_state);
 
+// Initialize a common_header by reading header data from a file.
+//
+// header:             Header to initialize.
+// input:              Open file to read from.
+// expected_magic:     Expected file format magic bytes.
+// expected_version:   Expected file format version.
+// decompressor:       Decompressor created from the compression type field in
+//                     the header. Pass NULL to not create a decompressor.
+// decompressor_state: State for the decompressor. Should be NULL if
+//                     decompressor is NULL.
+// checksum:           Checksum state that will be updated with the read bytes.
+//                     Pass NULL if decompressor is NULL.
 bool common_header_initialize_for_reading(
        struct common_header *header,
        FILE *input,
-       const char magic[4],
-       uint8_t accepted_version,
+       const char expected_magic[4],
+       uint8_t expected_version,
        struct decompressor **decompressor,
        struct decompr_state **decompr_state,
        XXH64_state_t *checksum,
index 302a43711481bf832cf4a9a26a05611559e41291..d43dea632d49bcce9e03152c8eafa1089269349d 100644 (file)
@@ -98,7 +98,7 @@
 // 1: Introduced in ccache 3.0. (Files are always compressed with gzip.)
 // 2: Introduced in ccache 3.8.
 
-static const char MANIFEST_MAGIC[4] = "cCmF";
+const char MANIFEST_MAGIC[4] = "cCmF";
 static const uint32_t MAX_MANIFEST_ENTRIES = 100;
 static const uint32_t MAX_MANIFEST_FILE_INFO_ENTRIES = 10000;
 
@@ -395,13 +395,15 @@ write_manifest(FILE *f, const struct manifest *mf)
        struct compr_state *compr_state;
        if (!common_header_initialize_for_writing(
                    &header,
+                   f,
                    MANIFEST_MAGIC,
                    MANIFEST_VERSION,
+                   compression_type_from_config(),
+                   compression_level_from_config(),
                    content_size,
                    checksum,
                    &compressor,
-                   &compr_state,
-                   f)) {
+                   &compr_state)) {
                goto out;
        }
 
index 1c78cf01a51128ca65c987ec7a19dcf3e1b3377d..c0e283d05f530f86b742cceb223fd89a2683b844 100644 (file)
@@ -5,6 +5,7 @@
 #include "hashutil.h"
 #include "hashtable.h"
 
+extern const char MANIFEST_MAGIC[4];
 #define MANIFEST_VERSION 2
 
 struct digest *manifest_get(struct conf *conf, const char *manifest_path);
index e1fd01a9dade21802da087060f837588d937cf2b..5fb8eea1b5a3ea61b9b5de111a3274b466ee73f6 100644 (file)
@@ -77,7 +77,7 @@
 //
 // 1: Introduced in ccache 3.8.
 
-static const char RESULT_MAGIC[4] = "cCrS";
+const char RESULT_MAGIC[4] = "cCrS";
 
 enum {
        FILE_MARKER = 0,
@@ -426,13 +426,15 @@ bool result_put(const char *path, struct result_files *list)
        struct compr_state *compr_state;
        if (!common_header_initialize_for_writing(
                    &header,
+                   f,
                    RESULT_MAGIC,
                    RESULT_VERSION,
+                   compression_type_from_config(),
+                   compression_level_from_config(),
                    content_size,
                    checksum,
                    &compressor,
-                   &compr_state,
-                   f)) {
+                   &compr_state)) {
                goto out;
        }
 
index be7638e471eebe9f09beae8002dcb2e8115f663b..6860467ee5232485fa12aa9800f2940695759804 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "conf.h"
 
+extern const char RESULT_MAGIC[4];
 #define RESULT_VERSION 1
 
 struct result_files;