]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Refactor code for getting manifest/result content size
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 15 Jul 2019 20:26:41 +0000 (22:26 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 15 Jul 2019 20:40:47 +0000 (22:40 +0200)
src/common_header.c
src/common_header.h
src/compress.c
src/manifest.c
src/manifest.h
src/result.c
src/result.h

index f91ffbac17b6db670e65d16148f5f8870dfdbe0b..1619908e779136c4ec05b01501ba9cd3c78e1292 100644 (file)
@@ -146,7 +146,9 @@ bool common_header_initialize_for_reading(
        return true;
 }
 
-size_t common_header_size(const struct common_header *header, bool *is_compressed)
+size_t
+common_header_content_size(
+       const struct common_header *header, bool *is_compressed)
 {
        *is_compressed = header->compression_type != COMPR_TYPE_NONE;
        return header->content_size;
index 76179ded1689ac98aff86e1b28a8fa5ba6439f45..4b473ad76bf8e84d25df26316bc9b936cbf5ae55 100644 (file)
@@ -61,7 +61,8 @@ bool common_header_initialize_for_reading(
        XXH64_state_t *checksum,
        char **errmsg);
 
-size_t common_header_size(const struct common_header *header, bool *is_compressed);
+size_t common_header_content_size(
+       const struct common_header *header, bool *is_compressed);
 
 void common_header_dump(const struct common_header *header, FILE *f);
 
index 99685f10b1adddd694005ff6ecdbba55bcec0076..7187b9df2aec1011018d9b35395055095656540f 100644 (file)
@@ -1,5 +1,4 @@
-// Copyright (C) 2002-2006 Andrew Tridgell
-// Copyright (C) 2009-2018 Joel Rosdahl
+// Copyright (C) 2019 Joel Rosdahl
 //
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
 
 #include "ccache.h"
 
+#include "common_header.h"
 #include "manifest.h"
 #include "result.h"
 
+static size_t
+content_size(
+       const char *path, const char *magic, uint8_t version, bool *is_compressed)
+{
+       char *errmsg;
+       size_t size = 0;
+       FILE *f = fopen(path, "rb");
+       if (!f) {
+               cc_log("Failed to open %s for reading: %s", path, strerror(errno));
+               goto error;
+       }
+       struct common_header header;
+       if (!common_header_initialize_for_reading(
+                   &header,
+                   f,
+                   magic,
+                   version,
+                   NULL,
+                   NULL,
+                   NULL,
+                   &errmsg)) {
+               cc_log("Error: %s", errmsg);
+               goto error;
+       }
+       size = common_header_content_size(&header, is_compressed);
+
+error:
+       if (f) {
+               fclose(f);
+       }
+       return size;
+}
+
 static unsigned num_files;
 static unsigned comp_files;
 
@@ -50,16 +83,20 @@ measure_fn(const char *fname, struct stat *st)
 
        size_t uncompressed_size;
        bool is_compressed;
-       if (str_endswith(p, ".manifest")) {
-               uncompressed_size = manifest_size(fname, &is_compressed);
-       } else if (str_endswith(p, ".result")) {
-               uncompressed_size = result_size(fname, &is_compressed);
+       const char *file_ext = get_extension(p);
+       if (str_eq(file_ext, ".manifest")) {
+               uncompressed_size =
+                       content_size(fname, MANIFEST_MAGIC, MANIFEST_VERSION, &is_compressed);
+       } else if (str_eq(file_ext, ".result")) {
+               uncompressed_size =
+                       content_size(fname, RESULT_MAGIC, RESULT_VERSION, &is_compressed);
        } else {
                uncompressed_size = 0;
                is_compressed = false;
        }
 
-       // Ignore unknown files in the cache, including any files from older versions.
+       // Ignore unknown files in the cache, including any files from older
+       // versions.
        if (uncompressed_size > 0) {
                cache_size += st->st_size;
                num_files++;
index c1e5062f5a9156d92bcb880cde75b2410fa5ca0e..d43dea632d49bcce9e03152c8eafa1089269349d 100644 (file)
@@ -782,37 +782,6 @@ out:
        return ret;
 }
 
-size_t
-manifest_size(const char *path, bool *is_compressed)
-{
-       char *errmsg;
-       size_t size = 0;
-       FILE *f = fopen(path, "rb");
-       if (!f) {
-               cc_log("Failed to open %s for reading: %s", path, strerror(errno));
-               goto error;
-       }
-       struct common_header header;
-       if (!common_header_initialize_for_reading(
-               &header,
-               f,
-               MANIFEST_MAGIC,
-               MANIFEST_VERSION,
-               NULL,
-               NULL,
-               NULL,
-               &errmsg)) {
-               cc_log("Error: %s", errmsg);
-               goto error;
-       }
-       size = common_header_size(&header, is_compressed);
-error:
-       if (f) {
-               fclose(f);
-       }
-       return size;
-}
-
 bool
 manifest_dump(const char *manifest_path, FILE *stream)
 {
index eca033948158a1d8e9cc2d6d97dc437fa4e7baf4..c0e283d05f530f86b742cceb223fd89a2683b844 100644 (file)
@@ -11,7 +11,6 @@ extern const char MANIFEST_MAGIC[4];
 struct digest *manifest_get(struct conf *conf, const char *manifest_path);
 bool manifest_put(const char *manifest_path, struct digest *result_digest,
                   struct hashtable *included_files);
-size_t manifest_size(const char *manifest_path, bool *is_compressed);
 bool manifest_dump(const char *manifest_path, FILE *stream);
 
 #endif
index 978edd4887e1bbcf91a56cc973cf3b6d7fb6ab19..ec1a25df46acad83917e99f431c4de2bef6bf5c5 100644 (file)
@@ -724,38 +724,6 @@ out:
        return ret;
 }
 
-
-size_t
-result_size(const char *path, bool *is_compressed)
-{
-       size_t size = 0;
-       char *errmsg;
-       FILE *f = fopen(path, "rb");
-       if (!f) {
-               cc_log("Failed to open %s for reading: %s", path, strerror(errno));
-               goto error;
-       }
-       struct common_header header;
-       if (!common_header_initialize_for_reading(
-               &header,
-               f,
-               RESULT_MAGIC,
-               RESULT_VERSION,
-               NULL,
-               NULL,
-               NULL,
-               &errmsg)) {
-               cc_log("Error: %s", errmsg);
-               goto error;
-       }
-       size = common_header_size(&header, is_compressed);
-error:
-       if (f) {
-               fclose(f);
-       }
-       return size;
-}
-
 bool
 result_dump(const char *path, FILE *stream)
 {
index ca9a08924b67e3e253f145917529c9939701cdf3..71f8ed512581bf645df4ffd52e37103790c32e8c 100644 (file)
@@ -16,7 +16,6 @@ void result_files_free(struct result_files *c);
 
 bool result_get(const char *path, struct result_files *list);
 bool result_put(const char *path, struct result_files *list);
-size_t result_size(const char *path, bool *is_compressed);
 bool result_dump(const char *path, FILE *stream);
 
 #endif