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);
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,
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],
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;
}
}
}
+ if (!decompressor) {
+ return true;
+ }
+
*decompressor = decompressor_from_type(header->compression_type);
if (!*decompressor) {
*errmsg = format(
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,
// 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;
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;
}
#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);
//
// 1: Introduced in ccache 3.8.
-static const char RESULT_MAGIC[4] = "cCrS";
+const char RESULT_MAGIC[4] = "cCrS";
enum {
FILE_MARKER = 0,
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;
}
#include "conf.h"
+extern const char RESULT_MAGIC[4];
#define RESULT_VERSION 1
struct result_files;