From: Joel Rosdahl Date: Sat, 29 Jun 2019 18:37:08 +0000 (+0200) Subject: Refactor functions for reading/writing common header X-Git-Tag: v4.0~927 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=206c85c1f39fbeb948af0556938cd51e9b79fdb4;p=thirdparty%2Fccache.git Refactor functions for reading/writing common header The functions now work on bytes instead of FILEs, allowing for the caller to optionally peek at read/written bytes. --- diff --git a/src/common_header.c b/src/common_header.c index ebfb18398..50e998978 100644 --- a/src/common_header.c +++ b/src/common_header.c @@ -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( diff --git a/src/common_header.h b/src/common_header.h index def2ee209..f25e3a1b5 100644 --- a/src/common_header.h +++ b/src/common_header.h @@ -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 diff --git a/src/manifest.c b/src/manifest.c index 6dddba5b0..171745ce5 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -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; } diff --git a/src/result.c b/src/result.c index adc5639ee..8ad039faa 100644 --- a/src/result.c +++ b/src/result.c @@ -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; }